npm

package
v0.0.0-...-8b8482c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

README

npm

This package allows to get some information about a npm package from the npm registry.

Installation

go get github.com/listendev/pkg/npm

Usage

npm.GetPackageList
pkg, err := npm.GetPackageList(context.Background(), "react")
if err != nil {
    panic(err)
}

fmt.Println(pkg.Name)
fmt.Println(pkg.Versions["0.0.1"].Dist.Shasum)
npm.GetPackageVersion
pkg, err := npm.GetPackageVersion(context.Background(), "react", "0.0.1")
if err != nil {
    panic(err)
}

fmt.Println(pkg.Name)
fmt.Println(pkg.Dist.Shasum)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPackageNotFound        = errors.New("package not found")
	ErrVersionNotFound        = errors.New("version not found")
	ErrLatestVersionNotFound  = errors.New("latest version not found")
	ErrCouldNotDecodeResponse = errors.New("could not decode registry response")
	ErrCouldNotDoRequest      = errors.New("could not start request to the registry")
	ErrCouldNotCreateRequest  = errors.New("could not create request to the registry")
)

Functions

func SplitName

func SplitName(packagename string) (string, string)

SplitName returns the organization and the name part of an NPM package.

Notice it assumes the package name is a valid one, thus it doesn't perform any validation.

Types

type Dist

type Dist struct {
	Shasum       string `json:"shasum"`
	TarballURL   string `json:"tarball"`
	NumFiles     int    `json:"fileCount"`
	UnpackedSize int    `json:"unpackedSize"`
}

type DistTags

type DistTags struct {
	Latest string `json:"latest"`
	Next   string `json:"next"`
}

type MockRegistryClient

type MockRegistryClient struct {
	// contains filtered or unexported fields
}

func NewMockRegistryClient

func NewMockRegistryClient(listFilename, versionFilename string) (*MockRegistryClient, error)

func (*MockRegistryClient) GetPackageLatestVersion

func (r *MockRegistryClient) GetPackageLatestVersion(_ context.Context, name string) (*PackageVersion, error)

func (*MockRegistryClient) GetPackageList

func (r *MockRegistryClient) GetPackageList(_ context.Context, name string) (*PackageList, error)

func (*MockRegistryClient) GetPackageVersion

func (r *MockRegistryClient) GetPackageVersion(_ context.Context, name, _ string) (*PackageVersion, error)

type NoOpRegistryClient

type NoOpRegistryClient struct{}

func (*NoOpRegistryClient) GetPackageLatestVersion

func (c *NoOpRegistryClient) GetPackageLatestVersion(_ context.Context, _ string) (*PackageVersion, error)

func (*NoOpRegistryClient) GetPackageList

func (c *NoOpRegistryClient) GetPackageList(_ context.Context, _ string) (*PackageList, error)

func (*NoOpRegistryClient) GetPackageVersion

func (c *NoOpRegistryClient) GetPackageVersion(_ context.Context, _, _ string) (*PackageVersion, error)

type PackageJSON

type PackageJSON struct {
	Name                 string            `json:"name"`
	Scripts              map[string]string `json:"scripts"`
	Dependencies         map[string]string `json:"dependencies"`
	DevDependencies      map[string]string `json:"devDependencies"`
	PeerDependencies     map[string]string `json:"peerDependencies"`
	BundleDependencies   []string          `json:"bundleDependencies"`
	OptionalDependencies map[string]string `json:"optionalDependencies"`
}

func NewPackageJSONFromDir

func NewPackageJSONFromDir(dir string) (*PackageJSON, error)

func NewPackageJSONFromReader

func NewPackageJSONFromReader(reader io.Reader) (*PackageJSON, error)

type PackageList

type PackageList struct {
	Name     string                    `json:"name"`
	Versions map[string]PackageVersion `json:"versions"`
	DistTags DistTags                  `json:"dist-tags"`
	Time     map[string]time.Time      `json:"time"`
}

func (*PackageList) LatestVersionTime

func (l *PackageList) LatestVersionTime() (*time.Time, error)

func (*PackageList) MaintainersByVersion

func (l *PackageList) MaintainersByVersion(version string) (PackageMaintainers, error)

type PackageMaintainer

type PackageMaintainer struct {
	Name string `json:"name"`
	Mail string `json:"email"`
}

type PackageMaintainers

type PackageMaintainers []PackageMaintainer

func (PackageMaintainers) Emails

func (pm PackageMaintainers) Emails() []string

type PackageVersion

type PackageVersion struct {
	Name            string             `json:"name"`
	Description     string             `json:"description"`
	Version         string             `json:"version"`
	Dist            Dist               `json:"dist"`
	Maintainers     PackageMaintainers `json:"maintainers"`
	Scripts         map[string]string  `json:"scripts"`
	Dependencies    map[string]string  `json:"dependencies"`
	DevDependencies map[string]string  `json:"devDependencies"`
}

PackageVersion represents the NPM registry response for the route <package_name>/<version>.

type Registry

type Registry interface {
	GetPackageList(ctx context.Context, name string) (*PackageList, error)
	GetPackageVersion(ctx context.Context, name, version string) (*PackageVersion, error)
	GetPackageLatestVersion(ctx context.Context, name string) (*PackageVersion, error)
}

func NewNoOpRegistryClient

func NewNoOpRegistryClient() Registry

func NewRegistryClient

func NewRegistryClient(config RegistryClientConfig) (Registry, error)

type RegistryClient

type RegistryClient struct {
	// contains filtered or unexported fields
}

func (*RegistryClient) GetPackageLatestVersion

func (c *RegistryClient) GetPackageLatestVersion(parent context.Context, name string) (*PackageVersion, error)

func (*RegistryClient) GetPackageList

func (c *RegistryClient) GetPackageList(parent context.Context, name string) (*PackageList, error)

func (*RegistryClient) GetPackageVersion

func (c *RegistryClient) GetPackageVersion(parent context.Context, name, version string) (*PackageVersion, error)

type RegistryClientConfig

type RegistryClientConfig struct {
	Timeout   time.Duration
	BaseURL   string
	UserAgent string
}

type ServiceError

type ServiceError struct {
	StatusCode int
	Message    string
}

func (*ServiceError) Error

func (e *ServiceError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL