fast-npm-meta 0.0.1 → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Anthony Fu <https://github.com/antfu>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const ofetch = require('ofetch');
4
-
5
3
  const defaultOptions = {
6
4
  /**
7
5
  * API endpoint for fetching package versions
@@ -12,17 +10,11 @@ const defaultOptions = {
12
10
  };
13
11
  async function getLatestVersionBatch(packages, options = {}) {
14
12
  const {
15
- apiEndpoint = defaultOptions.apiEndpoint
13
+ apiEndpoint = defaultOptions.apiEndpoint,
14
+ fetch: fetchApi = fetch
16
15
  } = options;
17
- const data = await ofetch.$fetch(
18
- packages.join("+"),
19
- {
20
- baseURL: apiEndpoint,
21
- query: {
22
- force: options.force ? true : void 0
23
- }
24
- }
25
- );
16
+ const query = options.force ? "?force=true" : "";
17
+ const data = await fetchApi(new URL(packages.join("+") + query, apiEndpoint)).then((r) => r.json());
26
18
  if (!Array.isArray(data))
27
19
  return [data];
28
20
  return data;
@@ -33,17 +25,11 @@ async function getLatestVersion(name, options = {}) {
33
25
  }
34
26
  async function getVersionsBatch(packages, options = {}) {
35
27
  const {
36
- apiEndpoint = defaultOptions.apiEndpoint
28
+ apiEndpoint = defaultOptions.apiEndpoint,
29
+ fetch: fetchApi = fetch
37
30
  } = options;
38
- const data = await ofetch.$fetch(
39
- `/versions/${packages.join("+")}`,
40
- {
41
- baseURL: apiEndpoint,
42
- query: {
43
- force: options.force ? true : void 0
44
- }
45
- }
46
- );
31
+ const query = options.force ? "?force=true" : "";
32
+ const data = await fetchApi(new URL(`/versions/${packages.join("+")}${query}`, apiEndpoint)).then((r) => r.json());
47
33
  if (!Array.isArray(data))
48
34
  return [data];
49
35
  return data;
@@ -53,8 +39,22 @@ async function getVersions(name, options = {}) {
53
39
  return data;
54
40
  }
55
41
 
42
+ const NPM_REGISTRY = "https://registry.npmjs.org/";
43
+ function pickRegistry(scope, npmConfigs, defaultRegistry = NPM_REGISTRY) {
44
+ let registry = scope ? npmConfigs[`${scope.replace(/^@?/, "@")}:registry`] : void 0;
45
+ if (!registry && typeof npmConfigs.scope === "string") {
46
+ registry = npmConfigs[`${npmConfigs.scope.replace(/^@?/, "@")}:registry`];
47
+ }
48
+ if (!registry) {
49
+ registry = npmConfigs.registry || defaultRegistry;
50
+ }
51
+ return registry;
52
+ }
53
+
54
+ exports.NPM_REGISTRY = NPM_REGISTRY;
56
55
  exports.defaultOptions = defaultOptions;
57
56
  exports.getLatestVersion = getLatestVersion;
58
57
  exports.getLatestVersionBatch = getLatestVersionBatch;
59
58
  exports.getVersions = getVersions;
60
59
  exports.getVersionsBatch = getVersionsBatch;
60
+ exports.pickRegistry = pickRegistry;
package/dist/index.d.cts CHANGED
@@ -16,6 +16,10 @@ interface ResolvedPackageVersion {
16
16
  interface ApiOptions {
17
17
  force?: boolean;
18
18
  apiEndpoint?: string;
19
+ /**
20
+ * Fetch function
21
+ */
22
+ fetch?: typeof fetch;
19
23
  }
20
24
  declare const defaultOptions: {
21
25
  /**
@@ -30,4 +34,14 @@ declare function getLatestVersion(name: string, options?: ApiOptions): Promise<R
30
34
  declare function getVersionsBatch(packages: string[], options?: ApiOptions): Promise<PackageManifest[]>;
31
35
  declare function getVersions(name: string, options?: ApiOptions): Promise<PackageManifest>;
32
36
 
33
- export { type ApiOptions, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch };
37
+ declare const NPM_REGISTRY = "https://registry.npmjs.org/";
38
+ /**
39
+ * Lightweight replacement of `npm-registry-fetch` function `pickRegistry`'
40
+ *
41
+ * @param scope - scope of package, get from 'npm-package-arg'
42
+ * @param npmConfigs - npm configs, read from `.npmrc` file
43
+ * @param defaultRegistry - default registry, default to 'https://registry.npmjs.org/'
44
+ */
45
+ declare function pickRegistry(scope: string | null | undefined, npmConfigs: Record<string, unknown>, defaultRegistry?: string): string;
46
+
47
+ export { type ApiOptions, NPM_REGISTRY, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch, pickRegistry };
package/dist/index.d.mts CHANGED
@@ -16,6 +16,10 @@ interface ResolvedPackageVersion {
16
16
  interface ApiOptions {
17
17
  force?: boolean;
18
18
  apiEndpoint?: string;
19
+ /**
20
+ * Fetch function
21
+ */
22
+ fetch?: typeof fetch;
19
23
  }
20
24
  declare const defaultOptions: {
21
25
  /**
@@ -30,4 +34,14 @@ declare function getLatestVersion(name: string, options?: ApiOptions): Promise<R
30
34
  declare function getVersionsBatch(packages: string[], options?: ApiOptions): Promise<PackageManifest[]>;
31
35
  declare function getVersions(name: string, options?: ApiOptions): Promise<PackageManifest>;
32
36
 
33
- export { type ApiOptions, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch };
37
+ declare const NPM_REGISTRY = "https://registry.npmjs.org/";
38
+ /**
39
+ * Lightweight replacement of `npm-registry-fetch` function `pickRegistry`'
40
+ *
41
+ * @param scope - scope of package, get from 'npm-package-arg'
42
+ * @param npmConfigs - npm configs, read from `.npmrc` file
43
+ * @param defaultRegistry - default registry, default to 'https://registry.npmjs.org/'
44
+ */
45
+ declare function pickRegistry(scope: string | null | undefined, npmConfigs: Record<string, unknown>, defaultRegistry?: string): string;
46
+
47
+ export { type ApiOptions, NPM_REGISTRY, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch, pickRegistry };
package/dist/index.d.ts CHANGED
@@ -16,6 +16,10 @@ interface ResolvedPackageVersion {
16
16
  interface ApiOptions {
17
17
  force?: boolean;
18
18
  apiEndpoint?: string;
19
+ /**
20
+ * Fetch function
21
+ */
22
+ fetch?: typeof fetch;
19
23
  }
20
24
  declare const defaultOptions: {
21
25
  /**
@@ -30,4 +34,14 @@ declare function getLatestVersion(name: string, options?: ApiOptions): Promise<R
30
34
  declare function getVersionsBatch(packages: string[], options?: ApiOptions): Promise<PackageManifest[]>;
31
35
  declare function getVersions(name: string, options?: ApiOptions): Promise<PackageManifest>;
32
36
 
33
- export { type ApiOptions, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch };
37
+ declare const NPM_REGISTRY = "https://registry.npmjs.org/";
38
+ /**
39
+ * Lightweight replacement of `npm-registry-fetch` function `pickRegistry`'
40
+ *
41
+ * @param scope - scope of package, get from 'npm-package-arg'
42
+ * @param npmConfigs - npm configs, read from `.npmrc` file
43
+ * @param defaultRegistry - default registry, default to 'https://registry.npmjs.org/'
44
+ */
45
+ declare function pickRegistry(scope: string | null | undefined, npmConfigs: Record<string, unknown>, defaultRegistry?: string): string;
46
+
47
+ export { type ApiOptions, NPM_REGISTRY, type PackageManifest, type ResolvedPackageVersion, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch, pickRegistry };
package/dist/index.mjs CHANGED
@@ -1,5 +1,3 @@
1
- import { $fetch } from 'ofetch';
2
-
3
1
  const defaultOptions = {
4
2
  /**
5
3
  * API endpoint for fetching package versions
@@ -10,17 +8,11 @@ const defaultOptions = {
10
8
  };
11
9
  async function getLatestVersionBatch(packages, options = {}) {
12
10
  const {
13
- apiEndpoint = defaultOptions.apiEndpoint
11
+ apiEndpoint = defaultOptions.apiEndpoint,
12
+ fetch: fetchApi = fetch
14
13
  } = options;
15
- const data = await $fetch(
16
- packages.join("+"),
17
- {
18
- baseURL: apiEndpoint,
19
- query: {
20
- force: options.force ? true : void 0
21
- }
22
- }
23
- );
14
+ const query = options.force ? "?force=true" : "";
15
+ const data = await fetchApi(new URL(packages.join("+") + query, apiEndpoint)).then((r) => r.json());
24
16
  if (!Array.isArray(data))
25
17
  return [data];
26
18
  return data;
@@ -31,17 +23,11 @@ async function getLatestVersion(name, options = {}) {
31
23
  }
32
24
  async function getVersionsBatch(packages, options = {}) {
33
25
  const {
34
- apiEndpoint = defaultOptions.apiEndpoint
26
+ apiEndpoint = defaultOptions.apiEndpoint,
27
+ fetch: fetchApi = fetch
35
28
  } = options;
36
- const data = await $fetch(
37
- `/versions/${packages.join("+")}`,
38
- {
39
- baseURL: apiEndpoint,
40
- query: {
41
- force: options.force ? true : void 0
42
- }
43
- }
44
- );
29
+ const query = options.force ? "?force=true" : "";
30
+ const data = await fetchApi(new URL(`/versions/${packages.join("+")}${query}`, apiEndpoint)).then((r) => r.json());
45
31
  if (!Array.isArray(data))
46
32
  return [data];
47
33
  return data;
@@ -51,4 +37,16 @@ async function getVersions(name, options = {}) {
51
37
  return data;
52
38
  }
53
39
 
54
- export { defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch };
40
+ const NPM_REGISTRY = "https://registry.npmjs.org/";
41
+ function pickRegistry(scope, npmConfigs, defaultRegistry = NPM_REGISTRY) {
42
+ let registry = scope ? npmConfigs[`${scope.replace(/^@?/, "@")}:registry`] : void 0;
43
+ if (!registry && typeof npmConfigs.scope === "string") {
44
+ registry = npmConfigs[`${npmConfigs.scope.replace(/^@?/, "@")}:registry`];
45
+ }
46
+ if (!registry) {
47
+ registry = npmConfigs.registry || defaultRegistry;
48
+ }
49
+ return registry;
50
+ }
51
+
52
+ export { NPM_REGISTRY, defaultOptions, getLatestVersion, getLatestVersionBatch, getVersions, getVersionsBatch, pickRegistry };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fast-npm-meta",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.1.1",
5
5
  "description": "Get npm package metadata",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -37,10 +37,6 @@
37
37
  ],
38
38
  "scripts": {
39
39
  "build": "unbuild",
40
- "dev": "unbuild --stub",
41
- "prepublishOnly": "nr build"
42
- },
43
- "dependencies": {
44
- "ofetch": "^1.3.4"
40
+ "dev": "unbuild --stub"
45
41
  }
46
- }
42
+ }