From 188235f3b48cffdd01f9028ce8e1a930c29b115e Mon Sep 17 00:00:00 2001 From: Edoardo Scibona <12040076+velut@users.noreply.github.com> Date: Sat, 13 Apr 2024 01:37:47 +0200 Subject: [PATCH] feat: add getPackageManifest --- src/get-package-manifest.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/get-package-manifest.ts b/src/get-package-manifest.ts index e8d3d78..06ee687 100644 --- a/src/get-package-manifest.ts +++ b/src/get-package-manifest.ts @@ -1,5 +1,9 @@ +import urlJoin from "url-join"; import { z } from "zod"; import { PackageJson } from "zod-package-json"; +import { assertValidPackageName } from "./assert-valid-package-name"; +import { fetchData } from "./fetch-data"; +import { npmRegistryUrl } from "./npm-registry"; /** `Dist` describes the distribution metadata generated by the registry. @@ -91,3 +95,21 @@ The manifest contains data extracted from `package.json` as well as data generat @see {@link https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#version} */ export type PackageManifest = z.infer; + +/** +`getPackageManifest` returns the manifest describing a specific version of a package (e.g., `foo@1.0.0`). + +@param name - package name +@param versionOrTag - semver version number (e.g., `1.0.0`) or distribution tag (e.g., `latest`) (default: `latest`) +@param registry - URL of the registry (default: npm registry) + +@see {@link PackageManifest} +*/ +export const getPackageManifest = async ( + name: string, + versionOrTag = "latest", + registry = npmRegistryUrl, +): Promise => { + assertValidPackageName(name); + return fetchData(PackageManifest, urlJoin(registry, name, versionOrTag)); +};