npm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package npm is the library behind the npmcli command line: the HTTP client, request shaping, and the typed data models for the npm package registry.

Two public APIs: the registry at registry.npmjs.org for package metadata and the downloads API at api.npmjs.org for download statistics. Both are open and require no key.

Index

Constants

View Source
const DefaultUserAgent = "npmcli/0.1.0 (github.com/tamnd/npm-cli)"

DefaultUserAgent identifies the client to npm APIs.

View Source
const Host = "registry.npmjs.org"

Host is the primary registry hostname.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when the registry returns a 404 for a package.

Functions

This section is empty.

Types

type Client

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

Client talks to the npm registry and downloads APIs.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client with the given config.

func (*Client) Deps

func (c *Client) Deps(ctx context.Context, name string) ([]Dependency, error)

Deps fetches the dependencies for the latest version of a package.

func (*Client) Downloads

func (c *Client) Downloads(ctx context.Context, name, period string) (*Downloads, error)

Downloads fetches download counts for a package over a period. period can be "last-week", "last-month", "last-year", or a date range "YYYY-MM-DD:YYYY-MM-DD".

func (*Client) Info

func (c *Client) Info(ctx context.Context, name string) (*Package, error)

Info fetches full package metadata for the latest version.

func (*Client) Search

func (c *Client) Search(ctx context.Context, query string, limit int) ([]SearchResult, error)

Search searches the npm registry.

func (*Client) Version

func (c *Client) Version(ctx context.Context, name, version string) (*PackageVersion, error)

Version fetches metadata for a specific version of a package.

type Config

type Config struct {
	RegistryURL  string
	DownloadsURL string
	UserAgent    string
	Rate         time.Duration
	Timeout      time.Duration
	Retries      int
}

Config holds constructor parameters for both endpoints.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

type Dependency

type Dependency struct {
	Name       string `json:"name"`
	Constraint string `json:"constraint"`
	Kind       string `json:"kind"` // "dep" or "devDep"
}

Dependency is a flat record for one dependency entry.

type Domain

type Domain struct{}

Domain is the npm registry driver.

func (Domain) Classify

func (Domain) Classify(input string) (uriType, id string, err error)

Classify turns any accepted input into the canonical (type, id), so `ant resolve` and `ant url` touch no network.

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for help and version output.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate returns the live npmjs.com URL for a (type, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type Downloads

type Downloads struct {
	Package   string `json:"package"`
	Downloads int    `json:"downloads"`
	Start     string `json:"start"`
	End       string `json:"end"`
}

Downloads is the record returned by the downloads command.

type Package

type Package struct {
	Name        string   `json:"name" kit:"id"`
	Description string   `json:"description"`
	Version     string   `json:"version"`
	Keywords    []string `json:"keywords"`
	Homepage    string   `json:"homepage"`
	License     string   `json:"license"`
	Author      string   `json:"author"`
	Repository  string   `json:"repository"`
	Created     string   `json:"created"`
	Modified    string   `json:"modified"`
	Readme      string   `json:"readme,omitempty"`
	URL         string   `json:"url"`
}

Package is the record returned by the info command.

type PackageVersion

type PackageVersion struct {
	Name            string            `json:"name"`
	Version         string            `json:"version"`
	Description     string            `json:"description"`
	Keywords        []string          `json:"keywords"`
	Homepage        string            `json:"homepage"`
	License         string            `json:"license"`
	Author          string            `json:"author"`
	Dependencies    map[string]string `json:"dependencies"`
	DevDependencies map[string]string `json:"dev_dependencies"`
	URL             string            `json:"url"`
}

PackageVersion is the record returned by the version command.

type SearchResult

type SearchResult struct {
	Rank        int      `json:"rank"`
	Name        string   `json:"name"`
	Version     string   `json:"version"`
	Description string   `json:"description"`
	Keywords    []string `json:"keywords"`
	ScoreFinal  float64  `json:"score_final"`
	URL         string   `json:"url"`
}

SearchResult is one hit from the npm search API.

Jump to

Keyboard shortcuts

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