diff --git a/src/fetch-data.ts b/src/fetch-data.ts new file mode 100644 index 0000000..752300a --- /dev/null +++ b/src/fetch-data.ts @@ -0,0 +1,10 @@ +import { z } from "zod"; + +export const fetchData = async ( + url: string, + schema: T, +): Promise> => { + const response = await fetch(url); + const json = await response.json(); + return schema.parse(json) as z.infer; +}; diff --git a/src/get-registry-metadata.ts b/src/get-registry-metadata.ts new file mode 100644 index 0000000..a60476a --- /dev/null +++ b/src/get-registry-metadata.ts @@ -0,0 +1,56 @@ +import { z } from "zod"; +import { fetchData } from "./fetch-data"; +import { npmRegistryUrl } from "./npm-registry"; + +/** +Zod schema for the registry metadata. +*/ +export const registryMetadataSchema = z + .object({ + /** Database name, usually `registry` */ + db_name: z.string(), + doc_count: z.number(), + doc_del_count: z.number(), + update_seq: z.number(), + purge_seq: z.number(), + compact_running: z.boolean(), + disk_size: z.number(), + data_size: z.number(), + instance_start_time: z.string(), + disk_format_version: z.number(), + committed_update_seq: z.number(), + compacted_seq: z.number(), + uuid: z.string(), + other: z + .object({ + data_size: z.number(), + }) + .passthrough() + .partial(), + sizes: z + .object({ + file: z.number(), + active: z.number(), + external: z.number(), + }) + .passthrough() + .partial(), + }) + .passthrough() + .partial(); + +/** +`RegistryMetadata` describes the metadata describing the registry itself. + +@see {@link https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#registry} +@see {@link https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#get} +*/ +export type RegistryMetadata = z.infer; + +/** +`getRegistryMetadata` returns the metadata describing the registry itself. + +@param registry - URL of the registry (default: npm registry) +*/ +export const getRegistryMetadata = async (registry = npmRegistryUrl): Promise => + fetchData(registry, registryMetadataSchema); diff --git a/src/index.ts b/src/index.ts index fcb7b0e..59cfc8f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,125 +1,11 @@ /** - * This package exports several functions to query - * the {@link https://www.npmjs.com | npm registry} - * (or one of its mirrors) through one of its - * {@link https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md | endpoints}. - * - * @example - * Get the metadata for the npm registry: - * - * ```typescript - * import { getRegistryMetadata } from 'query-registry'; - * - * (async () => { - * const metadata = await getRegistryMetadata(); - * - * // Output: 'registry' - * console.log(metadata.db_name); - * })(); - * ``` - * - * @example - * Get the latest manifest for package `query-registry` from the npm registry: - * - * ```typescript - * import { getPackageManifest } from 'query-registry'; - * - * (async () => { - * const manifest = await getPackageManifest({ name: 'query-registry' }); - * - * // Output: 'query-registry' - * console.log(manifest.name); - * })(); - * ``` - * - * @example - * Get the abbreviated packument for package `query-registry` from the npm registry: - * - * ```typescript - * import { getAbbreviatedPackument } from 'query-registry'; - * - * (async () => { - * const packument = await getAbbreviatedPackument({ name: 'query-registry' }); - * - * // Output: 'query-registry' - * console.log(manifest.name); - * })(); - * ``` - * - * @example - * Get the weekly downloads for package `query-registry` from the npm registry: - * - * ```typescript - * import { getPackageDownloads } from 'query-registry'; - * - * (async () => { - * const downloads = await getPackageDownloads({ name: 'query-registry' }); - * - * // Output: 'query-registry' - * console.log(downloads.package); - * - * // Output: 'number' - * console.log(typeof downloads.downloads); - * })(); - * ``` - * - * @example - * Get the search results for text query `query-registry` from the npm registry: - * - * ```typescript - * import { searchPackages } from 'query-registry'; - * - * (async () => { - * const results = await searchPackages({ query: { text: 'query-registry' } }); - * - * // Output: 'query-registry' - * console.log(results.objects[0].package.name); - * })(); - * ``` - * - * @example - * Enable {@link https://www.npmjs.com/package/debug | debug messages} - * by setting the `DEBUG` environment variable to `query-registry` - * (available only in non production environments): - * - * ```bash - * $ DEBUG="query-registry" - * ``` - * - * @packageDocumentation - */ +TODO: +@packageDocumentation +*/ -export * from "./data/registries"; -export * from "./endpoints/get-abbreviated-packument"; -export * from "./endpoints/get-daily-package-downloads"; -export * from "./endpoints/get-daily-registry-downloads"; -export * from "./endpoints/get-package-downloads"; -export * from "./endpoints/get-package-manifest"; -export * from "./endpoints/get-packument"; -export * from "./endpoints/get-raw-abbreviated-packument"; -export * from "./endpoints/get-raw-package-manifest"; -export * from "./endpoints/get-raw-packument"; -export * from "./endpoints/get-registry-downloads"; -export * from "./endpoints/get-registry-metadata"; -export * from "./endpoints/search-packages"; -export * from "./types/abbreviated-packument"; -export * from "./types/bug-tracker"; -export * from "./types/dist-info"; -export * from "./types/dist-tags"; -export * from "./types/download-period"; -export * from "./types/downloads"; -export * from "./types/git-repository"; -export * from "./types/npm-operational-internal"; -export * from "./types/package-json"; -export * from "./types/package-manifest"; -export * from "./types/packument"; -export * from "./types/person"; -export * from "./types/raw-abbreviated-packument"; -export * from "./types/raw-package-manifest"; -export * from "./types/raw-packument"; -export * from "./types/registry-metadata"; -export * from "./types/repository"; -export * from "./types/search-criteria"; -export * from "./types/search-results"; -export * from "./types/versions-to-timestamps"; -export * from "./utils/errors"; +export { + getRegistryMetadata, + registryMetadataSchema, + type RegistryMetadata, +} from "./get-registry-metadata"; +export { npmRegistryDownloadsApiUrl, npmRegistryUrl } from "./npm-registry"; diff --git a/src/npm-registry.ts b/src/npm-registry.ts new file mode 100644 index 0000000..4316849 --- /dev/null +++ b/src/npm-registry.ts @@ -0,0 +1,9 @@ +/** +Base URL for the {@link https://registry.npmjs.org | npm registry API}. +*/ +export const npmRegistryUrl = "https://registry.npmjs.org"; + +/** +Base URL for the npm registry {@link https://api.npmjs.org | downloads API}. +*/ +export const npmRegistryDownloadsApiUrl = "https://api.npmjs.org";