fast-npm-meta 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -25,45 +25,77 @@ interface PackageVersionsInfo extends Pick<PackageManifest, 'name' | 'distTags'
25
25
  interface PackageVersionsInfoWithMetadata extends PackageManifest {
26
26
  specifier: string;
27
27
  }
28
- interface ResolvedPackageVersion extends Partial<PackageVersionMeta> {
28
+ interface PackageError {
29
+ name: string;
30
+ error: string;
31
+ }
32
+ type MaybeError<T> = T | PackageError;
33
+ interface ResolvedPackageVersion {
29
34
  name: string;
30
35
  version: string | null;
31
36
  specifier: string;
32
37
  publishedAt: string | null;
33
38
  lastSynced: number;
34
39
  }
40
+ interface ResolvedPackageVersionWithMetadata extends ResolvedPackageVersion, PackageVersionMeta {
41
+ }
35
42
 
36
- interface FetchOptions {
43
+ interface FetchOptions<Throw extends boolean = true> {
44
+ /**
45
+ * API endpoint for fetching package versions
46
+ *
47
+ * @default 'https://npm.antfu.dev/'
48
+ */
37
49
  apiEndpoint?: string;
38
50
  /**
39
51
  * Fetch function
52
+ *
53
+ * @default [globalThis.fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
40
54
  */
41
55
  fetch?: typeof fetch;
56
+ /**
57
+ * Should throw error or return error object
58
+ *
59
+ * @default true
60
+ */
61
+ throw?: Throw;
42
62
  }
43
- interface GetVersionsOptions<WithMetadata extends boolean> extends FetchOptions {
63
+ interface GetVersionsOptions<Metadata extends boolean = false, Throw extends boolean = true> extends FetchOptions<Throw> {
44
64
  /**
45
65
  * By pass cache and get the latest data
66
+ *
67
+ * @default false
46
68
  */
47
69
  force?: boolean;
48
70
  /**
49
71
  * Include all versions that are newer than the specified version
72
+ *
73
+ * @default false
50
74
  */
51
75
  loose?: boolean;
52
76
  /**
53
77
  * Includes metadata, this will change the return type
78
+ *
79
+ * @default false
54
80
  */
55
- metadata?: WithMetadata;
81
+ metadata?: Metadata;
56
82
  }
57
- interface GetLatestVersionOptions extends FetchOptions {
83
+ interface GetLatestVersionOptions<Metadata extends boolean = false, Throw extends boolean = true> extends FetchOptions<Throw> {
58
84
  /**
59
85
  * By pass cache and get the latest data
86
+ *
87
+ * @default false
60
88
  */
61
89
  force?: boolean;
62
90
  /**
63
91
  * Includes metadata
92
+ *
93
+ * @default false
64
94
  */
65
- metadata?: boolean;
95
+ metadata?: Metadata;
66
96
  }
97
+ type InferGetVersionsResult<Metadata, Throw> = Metadata extends true ? Throw extends true ? PackageVersionsInfoWithMetadata : MaybeError<PackageVersionsInfoWithMetadata> : Throw extends true ? PackageVersionsInfo : MaybeError<PackageVersionsInfo>;
98
+ type InferGetLatestVersionResult<Metadata, Throw> = Metadata extends true ? Throw extends true ? ResolvedPackageVersionWithMetadata : MaybeError<ResolvedPackageVersionWithMetadata> : Throw extends true ? ResolvedPackageVersion : MaybeError<ResolvedPackageVersion>;
67
99
  declare const defaultOptions: {
68
100
  /**
69
101
  * API endpoint for fetching package versions
@@ -72,12 +104,10 @@ declare const defaultOptions: {
72
104
  */
73
105
  apiEndpoint: string;
74
106
  };
75
- declare function getLatestVersionBatch(packages: string[], options?: GetLatestVersionOptions): Promise<ResolvedPackageVersion[]>;
76
- declare function getLatestVersion(name: string, options?: GetLatestVersionOptions): Promise<ResolvedPackageVersion>;
77
- declare function getVersionsBatch(packages: string[], options?: GetVersionsOptions<false>): Promise<PackageVersionsInfo[]>;
78
- declare function getVersionsBatch(packages: string[], options: GetVersionsOptions<true>): Promise<PackageVersionsInfoWithMetadata[]>;
79
- declare function getVersions(name: string, options?: GetVersionsOptions<false>): Promise<PackageVersionsInfo>;
80
- declare function getVersions(name: string, options: GetVersionsOptions<true>): Promise<PackageVersionsInfoWithMetadata>;
107
+ declare function getLatestVersionBatch<Metadata extends boolean = false, Throw extends boolean = true>(packages: string[], options?: GetLatestVersionOptions<Metadata, Throw>): Promise<InferGetLatestVersionResult<Metadata, Throw>[]>;
108
+ declare function getLatestVersion<Metadata extends boolean = false, Throw extends boolean = true>(name: string, options?: GetLatestVersionOptions<Metadata, Throw>): Promise<InferGetLatestVersionResult<Metadata, Throw>>;
109
+ declare function getVersionsBatch<Metadata extends boolean = false, Throw extends boolean = true>(packages: string[], options?: GetVersionsOptions<Metadata, Throw>): Promise<InferGetVersionsResult<Metadata, Throw>[]>;
110
+ declare function getVersions<Metadata extends boolean = false, Throw extends boolean = true>(name: string, options?: GetVersionsOptions<Metadata, Throw>): Promise<InferGetVersionsResult<Metadata, Throw>>;
81
111
 
82
112
  declare const NPM_REGISTRY = "https://registry.npmjs.org/";
83
113
  /**
@@ -89,4 +119,4 @@ declare const NPM_REGISTRY = "https://registry.npmjs.org/";
89
119
  */
90
120
  declare function pickRegistry(scope: string | null | undefined, npmConfigs: Record<string, unknown>, defaultRegistry?: string): string;
91
121
 
92
- export { type FetchOptions, type GetLatestVersionOptions, type GetVersionsOptions, NPM_REGISTRY, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch, pickRegistry };
122
+ export { type FetchOptions, type GetLatestVersionOptions, type GetVersionsOptions, type InferGetLatestVersionResult, type InferGetVersionsResult, NPM_REGISTRY, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch, pickRegistry };
package/dist/index.mjs CHANGED
@@ -9,18 +9,19 @@ const defaultOptions = {
9
9
  async function getLatestVersionBatch(packages, options = {}) {
10
10
  const {
11
11
  apiEndpoint = defaultOptions.apiEndpoint,
12
- fetch: fetchApi = fetch
12
+ fetch: fetchApi = fetch,
13
+ throw: throwError = true
13
14
  } = options;
14
15
  let query = [
15
16
  options.force ? "force=true" : "",
16
- options.metadata ? "metadata=true" : ""
17
+ options.metadata ? "metadata=true" : "",
18
+ throwError ? "" : "throw=false"
17
19
  ].filter(Boolean).join("&");
18
20
  if (query)
19
21
  query = `?${query}`;
20
22
  const data = await fetchApi(new URL(packages.join("+") + query, apiEndpoint)).then((r) => r.json());
21
- if (!Array.isArray(data))
22
- return [data];
23
- return data;
23
+ const list = toArray(data);
24
+ return throwError ? throwErrorObject(list) : list;
24
25
  }
25
26
  async function getLatestVersion(name, options = {}) {
26
27
  const [data] = await getLatestVersionBatch([name], options);
@@ -29,24 +30,37 @@ async function getLatestVersion(name, options = {}) {
29
30
  async function getVersionsBatch(packages, options = {}) {
30
31
  const {
31
32
  apiEndpoint = defaultOptions.apiEndpoint,
32
- fetch: fetchApi = fetch
33
+ fetch: fetchApi = fetch,
34
+ throw: throwError = true
33
35
  } = options;
34
36
  let query = [
35
37
  options.force ? "force=true" : "",
36
38
  options.loose ? "loose=true" : "",
37
- options.metadata ? "metadata=true" : ""
39
+ options.metadata ? "metadata=true" : "",
40
+ throwError ? "" : "throw=false"
38
41
  ].filter(Boolean).join("&");
39
42
  if (query)
40
43
  query = `?${query}`;
41
44
  const data = await fetchApi(new URL(`/versions/${packages.join("+")}${query}`, apiEndpoint)).then((r) => r.json());
42
- if (!Array.isArray(data))
43
- return [data];
44
- return data;
45
+ const list = toArray(data);
46
+ return throwError ? throwErrorObject(list) : list;
45
47
  }
46
48
  async function getVersions(name, options = {}) {
47
49
  const [data] = await getVersionsBatch([name], options);
48
50
  return data;
49
51
  }
52
+ function throwErrorObject(data) {
53
+ for (const item of toArray(data)) {
54
+ if (item && "error" in item)
55
+ throw new Error(item.message || item.error);
56
+ }
57
+ return data;
58
+ }
59
+ function toArray(data) {
60
+ if (Array.isArray(data))
61
+ return data;
62
+ return [data];
63
+ }
50
64
 
51
65
  const NPM_REGISTRY = "https://registry.npmjs.org/";
52
66
  function pickRegistry(scope, npmConfigs, defaultRegistry = NPM_REGISTRY) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fast-npm-meta",
3
3
  "type": "module",
4
- "version": "0.3.1",
4
+ "version": "0.4.1",
5
5
  "description": "Get npm package metadata",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -15,23 +15,11 @@
15
15
  "keywords": [],
16
16
  "sideEffects": false,
17
17
  "exports": {
18
- ".": {
19
- "types": "./dist/index.d.ts",
20
- "import": "./dist/index.mjs",
21
- "require": "./dist/index.cjs"
22
- }
18
+ ".": "./dist/index.mjs"
23
19
  },
24
20
  "main": "./dist/index.mjs",
25
21
  "module": "./dist/index.mjs",
26
- "types": "./dist/index.d.ts",
27
- "typesVersions": {
28
- "*": {
29
- "*": [
30
- "./dist/*",
31
- "./dist/index.d.ts"
32
- ]
33
- }
34
- },
22
+ "types": "./dist/index.d.mts",
35
23
  "files": [
36
24
  "dist"
37
25
  ],
package/dist/index.cjs DELETED
@@ -1,71 +0,0 @@
1
- 'use strict';
2
-
3
- const defaultOptions = {
4
- /**
5
- * API endpoint for fetching package versions
6
- *
7
- * @default 'https://npm.antfu.dev/'
8
- */
9
- apiEndpoint: "https://npm.antfu.dev/"
10
- };
11
- async function getLatestVersionBatch(packages, options = {}) {
12
- const {
13
- apiEndpoint = defaultOptions.apiEndpoint,
14
- fetch: fetchApi = fetch
15
- } = options;
16
- let query = [
17
- options.force ? "force=true" : "",
18
- options.metadata ? "metadata=true" : ""
19
- ].filter(Boolean).join("&");
20
- if (query)
21
- query = `?${query}`;
22
- const data = await fetchApi(new URL(packages.join("+") + query, apiEndpoint)).then((r) => r.json());
23
- if (!Array.isArray(data))
24
- return [data];
25
- return data;
26
- }
27
- async function getLatestVersion(name, options = {}) {
28
- const [data] = await getLatestVersionBatch([name], options);
29
- return data;
30
- }
31
- async function getVersionsBatch(packages, options = {}) {
32
- const {
33
- apiEndpoint = defaultOptions.apiEndpoint,
34
- fetch: fetchApi = fetch
35
- } = options;
36
- let query = [
37
- options.force ? "force=true" : "",
38
- options.loose ? "loose=true" : "",
39
- options.metadata ? "metadata=true" : ""
40
- ].filter(Boolean).join("&");
41
- if (query)
42
- query = `?${query}`;
43
- const data = await fetchApi(new URL(`/versions/${packages.join("+")}${query}`, apiEndpoint)).then((r) => r.json());
44
- if (!Array.isArray(data))
45
- return [data];
46
- return data;
47
- }
48
- async function getVersions(name, options = {}) {
49
- const [data] = await getVersionsBatch([name], options);
50
- return data;
51
- }
52
-
53
- const NPM_REGISTRY = "https://registry.npmjs.org/";
54
- function pickRegistry(scope, npmConfigs, defaultRegistry = NPM_REGISTRY) {
55
- let registry = scope ? npmConfigs[`${scope.replace(/^@?/, "@")}:registry`] : void 0;
56
- if (!registry && typeof npmConfigs.scope === "string") {
57
- registry = npmConfigs[`${npmConfigs.scope.replace(/^@?/, "@")}:registry`];
58
- }
59
- if (!registry) {
60
- registry = npmConfigs.registry || defaultRegistry;
61
- }
62
- return registry;
63
- }
64
-
65
- exports.NPM_REGISTRY = NPM_REGISTRY;
66
- exports.defaultOptions = defaultOptions;
67
- exports.getLatestVersion = getLatestVersion;
68
- exports.getLatestVersionBatch = getLatestVersionBatch;
69
- exports.getVersions = getVersions;
70
- exports.getVersionsBatch = getVersionsBatch;
71
- exports.pickRegistry = pickRegistry;
package/dist/index.d.cts DELETED
@@ -1,92 +0,0 @@
1
- interface PackageManifest {
2
- name: string;
3
- distTags: Record<string, string> & {
4
- latest: string;
5
- };
6
- versionsMeta: Record<string, PackageVersionMeta>;
7
- timeCreated: string;
8
- timeModified: string;
9
- lastSynced: number;
10
- }
11
- type Engines = Partial<Record<string, string>>;
12
- interface PackageVersionMeta {
13
- time?: string;
14
- engines?: Engines;
15
- deprecated?: string;
16
- }
17
- interface PackageVersionsInfo extends Pick<PackageManifest, 'name' | 'distTags' | 'lastSynced'> {
18
- versions: string[];
19
- specifier: string;
20
- time: {
21
- created: string;
22
- modified: string;
23
- } & Record<string, string>;
24
- }
25
- interface PackageVersionsInfoWithMetadata extends PackageManifest {
26
- specifier: string;
27
- }
28
- interface ResolvedPackageVersion extends Partial<PackageVersionMeta> {
29
- name: string;
30
- version: string | null;
31
- specifier: string;
32
- publishedAt: string | null;
33
- lastSynced: number;
34
- }
35
-
36
- interface FetchOptions {
37
- apiEndpoint?: string;
38
- /**
39
- * Fetch function
40
- */
41
- fetch?: typeof fetch;
42
- }
43
- interface GetVersionsOptions<WithMetadata extends boolean> extends FetchOptions {
44
- /**
45
- * By pass cache and get the latest data
46
- */
47
- force?: boolean;
48
- /**
49
- * Include all versions that are newer than the specified version
50
- */
51
- loose?: boolean;
52
- /**
53
- * Includes metadata, this will change the return type
54
- */
55
- metadata?: WithMetadata;
56
- }
57
- interface GetLatestVersionOptions extends FetchOptions {
58
- /**
59
- * By pass cache and get the latest data
60
- */
61
- force?: boolean;
62
- /**
63
- * Includes metadata
64
- */
65
- metadata?: boolean;
66
- }
67
- declare const defaultOptions: {
68
- /**
69
- * API endpoint for fetching package versions
70
- *
71
- * @default 'https://npm.antfu.dev/'
72
- */
73
- apiEndpoint: string;
74
- };
75
- declare function getLatestVersionBatch(packages: string[], options?: GetLatestVersionOptions): Promise<ResolvedPackageVersion[]>;
76
- declare function getLatestVersion(name: string, options?: GetLatestVersionOptions): Promise<ResolvedPackageVersion>;
77
- declare function getVersionsBatch(packages: string[], options?: GetVersionsOptions<false>): Promise<PackageVersionsInfo[]>;
78
- declare function getVersionsBatch(packages: string[], options: GetVersionsOptions<true>): Promise<PackageVersionsInfoWithMetadata[]>;
79
- declare function getVersions(name: string, options?: GetVersionsOptions<false>): Promise<PackageVersionsInfo>;
80
- declare function getVersions(name: string, options: GetVersionsOptions<true>): Promise<PackageVersionsInfoWithMetadata>;
81
-
82
- declare const NPM_REGISTRY = "https://registry.npmjs.org/";
83
- /**
84
- * Lightweight replacement of `npm-registry-fetch` function `pickRegistry`'
85
- *
86
- * @param scope - scope of package, get from 'npm-package-arg'
87
- * @param npmConfigs - npm configs, read from `.npmrc` file
88
- * @param defaultRegistry - default registry, default to 'https://registry.npmjs.org/'
89
- */
90
- declare function pickRegistry(scope: string | null | undefined, npmConfigs: Record<string, unknown>, defaultRegistry?: string): string;
91
-
92
- export { type FetchOptions, type GetLatestVersionOptions, type GetVersionsOptions, NPM_REGISTRY, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch, pickRegistry };
package/dist/index.d.ts DELETED
@@ -1,92 +0,0 @@
1
- interface PackageManifest {
2
- name: string;
3
- distTags: Record<string, string> & {
4
- latest: string;
5
- };
6
- versionsMeta: Record<string, PackageVersionMeta>;
7
- timeCreated: string;
8
- timeModified: string;
9
- lastSynced: number;
10
- }
11
- type Engines = Partial<Record<string, string>>;
12
- interface PackageVersionMeta {
13
- time?: string;
14
- engines?: Engines;
15
- deprecated?: string;
16
- }
17
- interface PackageVersionsInfo extends Pick<PackageManifest, 'name' | 'distTags' | 'lastSynced'> {
18
- versions: string[];
19
- specifier: string;
20
- time: {
21
- created: string;
22
- modified: string;
23
- } & Record<string, string>;
24
- }
25
- interface PackageVersionsInfoWithMetadata extends PackageManifest {
26
- specifier: string;
27
- }
28
- interface ResolvedPackageVersion extends Partial<PackageVersionMeta> {
29
- name: string;
30
- version: string | null;
31
- specifier: string;
32
- publishedAt: string | null;
33
- lastSynced: number;
34
- }
35
-
36
- interface FetchOptions {
37
- apiEndpoint?: string;
38
- /**
39
- * Fetch function
40
- */
41
- fetch?: typeof fetch;
42
- }
43
- interface GetVersionsOptions<WithMetadata extends boolean> extends FetchOptions {
44
- /**
45
- * By pass cache and get the latest data
46
- */
47
- force?: boolean;
48
- /**
49
- * Include all versions that are newer than the specified version
50
- */
51
- loose?: boolean;
52
- /**
53
- * Includes metadata, this will change the return type
54
- */
55
- metadata?: WithMetadata;
56
- }
57
- interface GetLatestVersionOptions extends FetchOptions {
58
- /**
59
- * By pass cache and get the latest data
60
- */
61
- force?: boolean;
62
- /**
63
- * Includes metadata
64
- */
65
- metadata?: boolean;
66
- }
67
- declare const defaultOptions: {
68
- /**
69
- * API endpoint for fetching package versions
70
- *
71
- * @default 'https://npm.antfu.dev/'
72
- */
73
- apiEndpoint: string;
74
- };
75
- declare function getLatestVersionBatch(packages: string[], options?: GetLatestVersionOptions): Promise<ResolvedPackageVersion[]>;
76
- declare function getLatestVersion(name: string, options?: GetLatestVersionOptions): Promise<ResolvedPackageVersion>;
77
- declare function getVersionsBatch(packages: string[], options?: GetVersionsOptions<false>): Promise<PackageVersionsInfo[]>;
78
- declare function getVersionsBatch(packages: string[], options: GetVersionsOptions<true>): Promise<PackageVersionsInfoWithMetadata[]>;
79
- declare function getVersions(name: string, options?: GetVersionsOptions<false>): Promise<PackageVersionsInfo>;
80
- declare function getVersions(name: string, options: GetVersionsOptions<true>): Promise<PackageVersionsInfoWithMetadata>;
81
-
82
- declare const NPM_REGISTRY = "https://registry.npmjs.org/";
83
- /**
84
- * Lightweight replacement of `npm-registry-fetch` function `pickRegistry`'
85
- *
86
- * @param scope - scope of package, get from 'npm-package-arg'
87
- * @param npmConfigs - npm configs, read from `.npmrc` file
88
- * @param defaultRegistry - default registry, default to 'https://registry.npmjs.org/'
89
- */
90
- declare function pickRegistry(scope: string | null | undefined, npmConfigs: Record<string, unknown>, defaultRegistry?: string): string;
91
-
92
- export { type FetchOptions, type GetLatestVersionOptions, type GetVersionsOptions, NPM_REGISTRY, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch, pickRegistry };