npm-registry-sdk
Version:
A fully type-safe client library for the npm registry API.
136 lines • 4.78 kB
JavaScript
import { endpoints } from "./endpoints.js";
import { RegistryError } from "./error.js";
import { buildSearchQueryWithQualifiers } from "./qualifiers.js";
import { request } from "./utils/request.js";
const DEFAULT_REGISTRY_URL = 'https://registry.npmjs.org';
const DEFAULT_REGISTRY_API_URL = 'https://api.npmjs.org';
export default class NpmRegistry {
registry;
api;
request;
constructor({ api, registry, ...request } = {}) {
this.registry = registry || DEFAULT_REGISTRY_URL;
this.api = api || DEFAULT_REGISTRY_API_URL;
this.request = request;
}
/**
* Get the registry metadata
* @returns The registry metadata
* @see https://github.com/npm/registry/blob/main/docs/REGISTRY-API.md#get
*/
getRegistryMetadata() {
return request(this.registry, {
endpoint: endpoints.getRegistryMetadata(),
...this.request,
});
}
/**
* Get the public signing keys used by the registry.
* @returns The registry keys
* @see https://docs.npmjs.com/about-registry-signatures
*/
async getRegistryKeys() {
return request(this.registry, {
endpoint: endpoints.getRegistryKeys(),
...this.request,
});
}
/**
* Get the download statistics for the registry
* @param period The download period (e.g., 'last-day', 'last-week', 'last-month', 'last-year', 'yyyy-mm-dd:yyyy-mm-dd')
* @param packageName The package name (optional)
* @returns The download statistics
* @see https://github.com/npm/registry/blob/master/docs/download-counts.md#point-values
*/
async getRegistryDownloads(period, packageName) {
return request(this.api, {
endpoint: endpoints.getRegistryDownloads(period, packageName),
...this.request,
});
}
/**
* Get package information from the registry
* @param packageName The package name
* @returns The package information
* @see https://github.com/npm/registry/blob/main/docs/REGISTRY-API.md#getpackage
*/
async getPackage(packageName) {
return request(this.registry, {
endpoint: endpoints.getPackage(packageName),
...this.request,
});
}
/**
* Get package metadata for a specific version
* @param packageName The package name
* @param version The package version
* @returns Package metadata
* @see https://github.com/npm/registry/blob/main/docs/REGISTRY-API.md#getpackageversion
*/
async getPackageVersion(packageName, version) {
return request(this.registry, {
endpoint: endpoints.getPackageVersion(packageName, version),
...this.request,
});
}
/**
* Get the latest version of a package
* @param packageName The package name
* @returns The latest version metadata
*/
async getLatestVersion(packageName) {
return request(this.registry, {
endpoint: endpoints.getLatestVersion(packageName),
...this.request,
});
}
/**
* Search for packages in the registry
* @param query The search query
* @param options The search options
* @returns The search results
* @see https://github.com/npm/registry/blob/main/docs/REGISTRY-API.md#get-v1search
*/
async search(query, options) {
const { qualifiers, ...searchOptions } = options || {};
const queryWithQualifiers = qualifiers
? buildSearchQueryWithQualifiers(query, qualifiers)
: query;
return request(this.registry, {
endpoint: endpoints.search(),
params: {
text: queryWithQualifiers,
...searchOptions,
},
...this.request,
});
}
/**
* Get the dist-tags for a package
* @param packageName The package name
* @returns The dist-tags
*/
async getDistTags(packageName) {
return request(this.registry, {
endpoint: endpoints.getDistTags(packageName),
...this.request,
});
}
/**
* Download a package tarball
* @param packageName The package name
* @param version The package version
* @returns The tarball as an ArrayBuffer
*/
async downloadTarball(packageName, version) {
const tarballUrl = this.registry + endpoints.downloadTarball(packageName, version);
const response = await fetch(tarballUrl, {
...this.request,
});
if (!response.ok) {
throw new RegistryError(`Failed to download tarball: ${response.statusText}`, 'DOWNLOAD_ERROR');
}
return response.arrayBuffer();
}
}
//# sourceMappingURL=registry.js.map