fast-npm-meta 0.3.1 → 0.4.0

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,6 +25,11 @@ interface PackageVersionsInfo extends Pick<PackageManifest, 'name' | 'distTags'
25
25
  interface PackageVersionsInfoWithMetadata extends PackageManifest {
26
26
  specifier: string;
27
27
  }
28
+ interface PackageError {
29
+ name: string;
30
+ error: string;
31
+ }
32
+ type MaybeError<T> = T | PackageError;
28
33
  interface ResolvedPackageVersion extends Partial<PackageVersionMeta> {
29
34
  name: string;
30
35
  version: string | null;
@@ -39,6 +44,12 @@ interface FetchOptions {
39
44
  * Fetch function
40
45
  */
41
46
  fetch?: typeof fetch;
47
+ /**
48
+ * Should throw error or return error object
49
+ *
50
+ * @default false
51
+ */
52
+ throw?: boolean;
42
53
  }
43
54
  interface GetVersionsOptions<WithMetadata extends boolean> extends FetchOptions {
44
55
  /**
@@ -72,12 +83,12 @@ declare const defaultOptions: {
72
83
  */
73
84
  apiEndpoint: string;
74
85
  };
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>;
86
+ declare function getLatestVersionBatch(packages: string[], options?: GetLatestVersionOptions): Promise<MaybeError<ResolvedPackageVersion>[]>;
87
+ declare function getLatestVersion(name: string, options?: GetLatestVersionOptions): Promise<MaybeError<ResolvedPackageVersion>>;
88
+ declare function getVersionsBatch(packages: string[], options?: GetVersionsOptions<false>): Promise<MaybeError<PackageVersionsInfo>[]>;
89
+ declare function getVersionsBatch(packages: string[], options: GetVersionsOptions<true>): Promise<MaybeError<PackageVersionsInfoWithMetadata>[]>;
90
+ declare function getVersions(name: string, options?: GetVersionsOptions<false>): Promise<MaybeError<PackageVersionsInfo>>;
91
+ declare function getVersions(name: string, options: GetVersionsOptions<true>): Promise<MaybeError<PackageVersionsInfoWithMetadata>>;
81
92
 
82
93
  declare const NPM_REGISTRY = "https://registry.npmjs.org/";
83
94
  /**
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 = false
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 = false
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.0",
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 };