@theholocron/npm-client 0.0.0-stage → 1.58.0-alpha.73

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) 2026 Newton Koumantzelis
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/README.md CHANGED
@@ -1,3 +1,25 @@
1
- # Temporary Holding Version
1
+ # `@theholocron/npm-client`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ TypeScript client for the [npm registry](https://github.com/npm/registry/blob/main/docs/REGISTRY-API.md): who a token
4
+ is, and the latest version of a package.
5
+
6
+ ## Install
7
+
8
+ ```bash
9
+ pnpm add @theholocron/npm-client
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ts
15
+ import { createNpmClient } from "@theholocron/npm-client";
16
+
17
+ const npm = createNpmClient({ token: process.env.NPM_TOKEN! });
18
+
19
+ const { username } = await npm.whoami(); // GET /-/whoami
20
+ const latest = await npm.packages.latestVersion("@theholocron/cli"); // GET /@theholocron%2Fcli, dist-tags.latest
21
+ ```
22
+
23
+ `latestVersion()` is `undefined` when the registry does not have the package (a 404). The token is sent as
24
+ `Authorization: Bearer`. npm does not expose a token's creation date, expiry or permissions to the token itself, so the
25
+ client has no call for them.
@@ -0,0 +1,35 @@
1
+ import "@theholocron/http-client";
2
+ import { ErrorSink, Logger } from "@theholocron/observability/core";
3
+ /** Options for `createNpmClient`. */
4
+ interface NpmClientOptions {
5
+ /** An npm access token, sent as a bearer token. */
6
+ token: string;
7
+ /** Override base URL for testing. Defaults to https://registry.npmjs.org. */
8
+ baseUrl?: string;
9
+ /** Override fetch for testing. Defaults to globalThis.fetch. */
10
+ fetch?: typeof fetch;
11
+ /** How long one request may take before it is abandoned, in milliseconds. Defaults to 15,000. */
12
+ timeoutMs?: number;
13
+ /** Structured logger for request/response diagnostics. Defaults to a no-op. */
14
+ logger?: Logger;
15
+ /** Reports transport failures and unexpected 5xx. Defaults to a no-op. */
16
+ errors?: ErrorSink;
17
+ }
18
+ /** Who an npm token belongs to, as `GET /-/whoami` reports it. */
19
+ interface NpmWhoami {
20
+ /** The npm user name the token acts as. */
21
+ username: string;
22
+ }
23
+ /**
24
+ * Creates an npm registry client: `whoami()` and the `packages` group. Two REST clients share the options, because the
25
+ * package document is asked for with its own `Accept` header and `whoami` with the default one.
26
+ */
27
+ export declare function createNpmClient(opts: NpmClientOptions): {
28
+ whoami: () => Promise<NpmWhoami>;
29
+ packages: {
30
+ latestVersion(name: string): Promise<string | undefined>;
31
+ };
32
+ };
33
+ /** The client `createNpmClient` returns. */
34
+ export type NpmClient = ReturnType<typeof createNpmClient>;
35
+ export type { NpmClientOptions, NpmWhoami };
package/dist/index.mjs ADDED
@@ -0,0 +1,70 @@
1
+ import { ProviderApiError, createRestClient } from "@theholocron/http-client";
2
+ // src/packages/packages.ts
3
+ /** A package name as it goes in a path: a scoped `@scope/name` is `@scope%2Fname`, which the registry requires. */
4
+ function packagePath(name) {
5
+ return `/${name.replace("/", "%2F")}`;
6
+ }
7
+ /** The `packages` group of the npm client. */
8
+ function packages(rest) {
9
+ return {
10
+ /**
11
+ * The latest version of a package: `GET /<name>`, read from `dist-tags.latest`. `undefined` when the registry has no
12
+ * such package (404) or the package has no `latest` tag. Any other failure rejects with a `ProviderApiError`.
13
+ *
14
+ * A 404 is also what npm answers for a private package the token cannot read, on purpose, so that a package's
15
+ * existence is not revealed: the two cannot be told apart from here. Every package this repository publishes is
16
+ * public, where a 404 does mean it is not on the registry.
17
+ */
18
+ async latestVersion(name) {
19
+ try {
20
+ return (await rest.request(packagePath(name)))["dist-tags"]?.latest;
21
+ } catch (err) {
22
+ if (err instanceof ProviderApiError && err.status === 404) return void 0;
23
+ throw err;
24
+ }
25
+ } };
26
+ }
27
+ /**
28
+ * A `fetch` that gives up after `timeoutMs`: a registry connection that stays open and says nothing would otherwise make
29
+ * a probe or `doctor tokens` wait forever. The REST client passes no signal of its own, so this one is the only one.
30
+ */
31
+ function withTimeout(base, timeoutMs) {
32
+ return (input, init) => base(input, {
33
+ ...init,
34
+ signal: AbortSignal.timeout(timeoutMs)
35
+ });
36
+ }
37
+ /**
38
+ * Builds a REST client for the npm registry. `accept` replaces the default `application/json`: the package document is
39
+ * asked for in its abbreviated form, which carries the `dist-tags` and none of a package's full history.
40
+ */
41
+ function createNpmRestClient(opts, accept) {
42
+ return createRestClient({
43
+ baseUrl: opts.baseUrl ?? "https://registry.npmjs.org",
44
+ token: opts.token,
45
+ vendor: "npm",
46
+ ...accept ? { extraHeaders: { accept } } : {},
47
+ fetch: withTimeout(opts.fetch ?? globalThis.fetch, opts.timeoutMs ?? 15e3),
48
+ logger: opts.logger,
49
+ errors: opts.errors
50
+ });
51
+ }
52
+ // src/whoami/whoami.ts
53
+ /** Asks the registry who the token is: `GET /-/whoami`. A token the registry refuses rejects with a 401 `ProviderApiError`. */
54
+ function whoami(rest) {
55
+ return () => rest.request("/-/whoami");
56
+ }
57
+ // src/index.ts
58
+ /** The `Accept` header that asks the registry for a package's abbreviated document (its `dist-tags` and versions). */
59
+ const ABBREVIATED = "application/vnd.npm.install-v1+json";
60
+ /**
61
+ * Creates an npm registry client: `whoami()` and the `packages` group. Two REST clients share the options, because the
62
+ * package document is asked for with its own `Accept` header and `whoami` with the default one.
63
+ */
64
+ function createNpmClient(opts) {
65
+ return {
66
+ whoami: whoami(createNpmRestClient(opts)),
67
+ packages: packages(createNpmRestClient(opts, ABBREVIATED))
68
+ };
69
+ }
70
+ export { createNpmClient };
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@theholocron/npm-client",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.58.0-alpha.73",
4
+ "description": "A TypeScript client for the npm registry (whoami and a package's latest version)",
5
+ "keywords": [
6
+ "npm",
7
+ "registry",
8
+ "typescript",
9
+ "http-client",
10
+ "api"
11
+ ],
12
+ "homepage": "https://docs.theholocron.dev/holocron/clients/npm/",
13
+ "bugs": "https://github.com/theholocron/holocron/issues",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/theholocron/holocron.git",
17
+ "directory": "clients/npm-client"
18
+ },
19
+ "license": "MIT",
20
+ "author": "Newton Koumantzelis",
21
+ "sideEffects": false,
22
+ "type": "module",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.mts",
26
+ "import": "./dist/index.mjs",
27
+ "default": "./dist/index.mjs"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "dependencies": {
34
+ "@theholocron/http-client": "^1.58.0-alpha.73",
35
+ "@theholocron/observability": "^0.7.0-alpha.9"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^26",
39
+ "@vitest/coverage-v8": "^5.1.2",
40
+ "@vitest/eslint-plugin": "^1.6.28",
41
+ "eslint": "^10.16.0",
42
+ "eslint-plugin-n": "^18.6.0",
43
+ "globals": "^17.14.0",
44
+ "tsdown": "^0.23.7",
45
+ "typescript": "^6.0.3",
46
+ "vitest": "^5.1.2",
47
+ "@theholocron/eslint-config": "^8.9.0-alpha.24",
48
+ "@theholocron/tsdown-config": "^8.9.0-alpha.24",
49
+ "@theholocron/tsconfig": "^8.9.0-alpha.24",
50
+ "@theholocron/vitest-config": "^8.9.0-alpha.24"
51
+ },
52
+ "engines": {
53
+ "node": ">=22.0.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "scripts": {
59
+ "build": "tsdown && node ../scripts/check-bundle-size.mjs",
60
+ "lint:code": "eslint .",
61
+ "test:unit": "vitest run --coverage",
62
+ "test:watch": "vitest",
63
+ "typecheck": "tsc --noEmit"
64
+ }
6
65
  }