pkgsite

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package pkgsite provides a small client for the pkg.go.dev v1beta HTTP API.

The API is stateless and GET-only. See https://go.dev/blog/pkgsite-api for background. This client mirrors the surface of the reference client that ships with golang.org/x/pkgsite (cmd/internal/pkgsite-cli/client), but is an independent, dependency-free reimplementation.

Index

Constants

View Source
const DefaultServer = "https://pkg.go.dev"

DefaultServer is the public pkg.go.dev API server.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code       int         `json:"code"` // HTTP status code
	Message    string      `json:"message"`
	Fixes      []string    `json:"fixes,omitempty"`      // suggestions for how to fix
	Candidates []Candidate `json:"candidates,omitempty"` // for ambiguous paths
}

APIError is a structured error returned by the API for a non-200 response. It implements error.

func (*APIError) Error

func (e *APIError) Error() string

type Candidate

type Candidate struct {
	ModulePath  string `json:"modulePath"`
	PackagePath string `json:"packagePath"`
}

Candidate is a possible resolution for an ambiguous package path.

type Client

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

Client fetches data from the pkg.go.dev v1beta API.

func New

func New(server string, opts ...Option) (*Client, error)

New creates a Client that talks to the given server (e.g. DefaultServer).

func (*Client) GetImportedBy

func (c *Client) GetImportedBy(ctx context.Context, path, version string, opts ImportedByOptions) (*PackageImportedBy, error)

GetImportedBy fetches the packages that import the given package (reverse dependencies).

func (*Client) GetModule

func (c *Client) GetModule(ctx context.Context, path, version string, opts ModuleOptions) (*Module, error)

GetModule fetches information about a module at the given path and version. An empty version means the latest version.

func (*Client) GetPackage

func (c *Client) GetPackage(ctx context.Context, path, version string, opts PackageOptions) (*Package, error)

GetPackage fetches information about a package at the given path and version. An empty version means the latest version.

func (*Client) GetPackages

func (c *Client) GetPackages(ctx context.Context, modulePath, version string, opts PaginationOptions) (*PaginatedResponse[ModulePackageResponse], error)

GetPackages fetches the packages contained in the given module path and version.

func (*Client) GetSymbols

func (c *Client) GetSymbols(ctx context.Context, path, version string, opts SymbolsOptions) (*PaginatedResponse[Symbol], error)

GetSymbols fetches the exported symbols of a package.

func (*Client) GetVersions

GetVersions fetches the list of versions for the given module path.

func (*Client) GetVulns

func (c *Client) GetVulns(ctx context.Context, path, version string, opts PaginationOptions) (*PaginatedResponse[Vulnerability], error)

GetVulns fetches known vulnerabilities for the given module path and version.

func (*Client) Search

func (c *Client) Search(ctx context.Context, query string, opts SearchOptions) (*PaginatedResponse[SearchResult], error)

Search queries pkg.go.dev for packages matching query.

type ImportedByOptions

type ImportedByOptions struct {
	Module string
	PaginationOptions
}

ImportedByOptions are options for GetImportedBy.

type License

type License struct {
	Types    []string `json:"types"`
	FilePath string   `json:"filePath"`
	Contents string   `json:"contents,omitempty"`
}

License is license information in API responses.

type Module

type Module struct {
	Path    string `json:"path"`
	Version string `json:"version"`
	// CommitTime is the time the version was created, as reported by the
	// module proxy's .info endpoint.
	CommitTime        time.Time `json:"commitTime"`
	IsLatest          bool      `json:"isLatest"`
	IsRedistributable bool      `json:"isRedistributable"`
	IsStandardLibrary bool      `json:"isStandardLibrary"`
	HasGoMod          bool      `json:"hasGoMod"`
	RepoURL           string    `json:"repoUrl"`
	GoModContents     string    `json:"goModContents,omitempty"`
	Readme            *Readme   `json:"readme,omitempty"`
	Licenses          []License `json:"licenses,omitempty"`
}

Module is the response for /v1beta/module/{modulePath}.

type ModuleOptions

type ModuleOptions struct {
	Readme   bool // include the README contents
	Licenses bool // include license information
}

ModuleOptions are options for GetModule.

type ModulePackageResponse

type ModulePackageResponse struct {
	Path     string `json:"path"`
	Name     string `json:"name"`
	Synopsis string `json:"synopsis"`
}

ModulePackageResponse is a single package listed for a module.

type Option

type Option func(*Client)

Option configures a Client.

func WithCache added in v0.2.0

func WithCache(ttl time.Duration, maxEntries int, maxBytes int64) Option

WithCache caches successful responses for ttl, keyed by request URL, evicting least-recently-used entries to stay within maxEntries and maxBytes (each a non-positive value meaning unbounded on that axis). Because version-pinned data is immutable and "latest" lookups tolerate a short staleness window, this safely avoids refetching during an assistant's multi-step exploration. A non-positive ttl, or both bounds non-positive, disables caching.

func WithHTTPClient

func WithHTTPClient(h *http.Client) Option

WithHTTPClient sets the HTTP client used for requests. The default client has a 30s timeout.

func WithRetry added in v0.2.0

func WithRetry(maxRetries int, baseDelay time.Duration) Option

WithRetry retries transient failures (network errors and HTTP 429/5xx) up to maxRetries additional times, with exponential backoff starting at baseDelay. A maxRetries of 0 disables retries. A baseDelay <= 0 keeps the default.

func WithUserAgent

func WithUserAgent(ua string) Option

WithUserAgent sets the User-Agent header sent with each request.

type Package

type Package struct {
	ModulePath        string    `json:"modulePath"`
	Version           string    `json:"version"`
	IsLatest          bool      `json:"isLatest"`
	IsStandardLibrary bool      `json:"isStandardLibrary"`
	GOOS              string    `json:"goos"`
	GOARCH            string    `json:"goarch"`
	Docs              string    `json:"docs,omitempty"`
	Imports           []string  `json:"imports,omitempty"`
	Licenses          []License `json:"licenses,omitempty"`
	PackageInfo
}

Package is the response for /v1beta/package/{packagePath}.

type PackageImportedBy

type PackageImportedBy struct {
	ModulePath string                    `json:"modulePath"`
	Version    string                    `json:"version"`
	ImportedBy PaginatedResponse[string] `json:"importedBy"`
}

PackageImportedBy is the response for /v1beta/imported-by/{packagePath}.

type PackageInfo

type PackageInfo struct {
	Path     string `json:"path"`
	Name     string `json:"name"`
	Synopsis string `json:"synopsis"`
	// IsRedistributable reports whether the license allows distribution.
	IsRedistributable bool `json:"isRedistributable"`
}

PackageInfo holds the fields common to a package across responses.

type PackageOptions

type PackageOptions struct {
	Module   string // disambiguate the module path for an ambiguous package path
	Doc      string // render docs in this format: "text", "md", or "html"
	Examples bool   // include examples (requires Doc)
	Imports  bool   // list imported packages
	Licenses bool   // include license information
	GOOS     string // target GOOS
	GOARCH   string // target GOARCH
}

PackageOptions are options for GetPackage.

type PackageSymbols

type PackageSymbols struct {
	ModulePath string                    `json:"modulePath"`
	Version    string                    `json:"version"`
	Symbols    PaginatedResponse[Symbol] `json:"symbols"`
}

PackageSymbols is the response for /v1beta/symbols/{packagePath}.

type PackagesResponse

type PackagesResponse struct {
	ModulePath        string                         `json:"modulePath"`
	Version           string                         `json:"version"`
	IsStandardLibrary bool                           `json:"isStandardLibrary"`
	Packages          PaginatedResponse[PackageInfo] `json:"packages"`
}

PackagesResponse is the response for /v1beta/packages/{modulePath}.

type PaginatedResponse

type PaginatedResponse[T any] struct {
	Items         []T    `json:"items"`
	Total         int    `json:"total"`
	NextPageToken string `json:"nextPageToken,omitempty"`
}

PaginatedResponse is a generic paginated response. A non-empty NextPageToken indicates more results are available; pass it back as the token option to fetch the next page.

type PaginationOptions

type PaginationOptions struct {
	Limit int    // maximum items per page; 0 means the server default
	Token string // page token from a previous response's NextPageToken
}

PaginationOptions are common options for paginated endpoints.

type Readme

type Readme struct {
	Filepath string `json:"filepath"`
	Contents string `json:"contents"`
}

Readme is a README file.

type SearchOptions

type SearchOptions struct {
	Symbol string // restrict results to packages exporting this symbol
	PaginationOptions
}

SearchOptions are options for Search.

type SearchResult

type SearchResult struct {
	PackagePath string `json:"packagePath"`
	ModulePath  string `json:"modulePath"`
	Version     string `json:"version"`
	Synopsis    string `json:"synopsis"`
}

SearchResult is a single result from /v1beta/search.

type Symbol

type Symbol struct {
	Name     string `json:"name"`
	Kind     string `json:"kind"`
	Synopsis string `json:"synopsis"`
	Parent   string `json:"parent,omitempty"`
}

Symbol is an exported symbol in a package.

type SymbolsOptions

type SymbolsOptions struct {
	Module string
	GOOS   string
	GOARCH string
	PaginationOptions
}

SymbolsOptions are options for GetSymbols.

type VersionResponse

type VersionResponse struct {
	Version string `json:"version"`
}

VersionResponse is a single version from /v1beta/versions/{modulePath}.

type Vulnerability

type Vulnerability struct {
	ID           string `json:"id"`
	Summary      string `json:"summary"`
	Details      string `json:"details"`
	FixedVersion string `json:"fixedVersion"`
}

Vulnerability is a vulnerability report for a module path and version.

Jump to

Keyboard shortcuts

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