-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
84 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from "zod"; | ||
|
||
export const fetchData = async <T extends z.ZodTypeAny>( | ||
url: string, | ||
schema: T, | ||
): Promise<z.TypeOf<T>> => { | ||
const response = await fetch(url); | ||
const json = await response.json(); | ||
return schema.parse(json) as z.infer<T>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<typeof registryMetadataSchema>; | ||
|
||
/** | ||
`getRegistryMetadata` returns the metadata describing the registry itself. | ||
@param registry - URL of the registry (default: npm registry) | ||
*/ | ||
export const getRegistryMetadata = async (registry = npmRegistryUrl): Promise<RegistryMetadata> => | ||
fetchData(registry, registryMetadataSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; |