@theholocron/http-client 1.0.0 → 1.0.2

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
@@ -20,9 +20,10 @@ interface RestClientConfig {
20
20
  /**
21
21
  * How the token is sent.
22
22
  * - `"bearer"` (default): `Authorization: Bearer <token>`
23
+ * - `"basic"`: `Authorization: Basic <token>`
23
24
  * - `"apikey"`: uses `apiKeyHeader` (e.g. `x-api-key`)
24
25
  */
25
- tokenScheme?: "bearer" | "apikey";
26
+ tokenScheme?: "bearer" | "basic" | "apikey";
26
27
  /** Header name when `tokenScheme` is `"apikey"`. Defaults to `"x-api-key"`. */
27
28
  apiKeyHeader?: string;
28
29
  /**
package/dist/index.mjs CHANGED
@@ -23,6 +23,7 @@ function createRestClient(config) {
23
23
  while (baseUrl.endsWith("/")) baseUrl = baseUrl.slice(0, -1);
24
24
  const staticHeaders = { accept: "application/json" };
25
25
  if (config.tokenScheme === "apikey") staticHeaders[config.apiKeyHeader ?? "x-api-key"] = config.token;
26
+ else if (config.tokenScheme === "basic") staticHeaders["authorization"] = `Basic ${config.token}`;
26
27
  else staticHeaders["authorization"] = `Bearer ${config.token}`;
27
28
  Object.assign(staticHeaders, config.extraHeaders);
28
29
  return {
@@ -0,0 +1,17 @@
1
+ //#region src/testing.d.ts
2
+ interface FetchCall {
3
+ url: string;
4
+ method: string;
5
+ headers: Record<string, string>;
6
+ body: unknown;
7
+ }
8
+ declare function stubFetch(responses: Array<{
9
+ status?: number;
10
+ body?: unknown;
11
+ text?: string;
12
+ }>): {
13
+ fetch: typeof fetch;
14
+ calls: FetchCall[];
15
+ };
16
+ //#endregion
17
+ export { FetchCall, stubFetch };
@@ -0,0 +1,29 @@
1
+ import { vi } from "vitest";
2
+ //#region src/testing.ts
3
+ function stubFetch(responses) {
4
+ const calls = [];
5
+ let i = 0;
6
+ return {
7
+ fetch: vi.fn(async (input, init) => {
8
+ const url = typeof input === "string" ? input : input.toString();
9
+ const body = typeof init?.body === "string" ? JSON.parse(init.body) : init?.body ?? null;
10
+ calls.push({
11
+ url,
12
+ method: (init?.method ?? "GET").toUpperCase(),
13
+ headers: init?.headers ?? {},
14
+ body
15
+ });
16
+ const next = responses[i++] ?? {
17
+ status: 200,
18
+ body: {}
19
+ };
20
+ const status = next.status ?? 200;
21
+ if (status === 204) return new Response(null, { status });
22
+ const text = next.text ?? JSON.stringify(next.body ?? {});
23
+ return new Response(text, { status });
24
+ }),
25
+ calls
26
+ };
27
+ }
28
+ //#endregion
29
+ export { stubFetch };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/http-client",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Shared HTTP primitives for theholocron — REST client factory, auth resolver, and error types",
5
5
  "homepage": "https://github.com/theholocron/clients/tree/main/packages/http-client#readme",
6
6
  "bugs": "https://github.com/theholocron/clients/issues",
@@ -21,12 +21,24 @@
21
21
  "type": "module",
22
22
  "sideEffects": false,
23
23
  "main": "./dist/index.mjs",
24
- "types": "./dist/index.d.mts",
25
24
  "exports": {
26
25
  ".": {
27
26
  "types": "./dist/index.d.mts",
28
27
  "import": "./dist/index.mjs",
29
28
  "default": "./dist/index.mjs"
29
+ },
30
+ "./testing": {
31
+ "types": "./dist/testing.d.mts",
32
+ "import": "./dist/testing.mjs",
33
+ "default": "./dist/testing.mjs"
34
+ }
35
+ },
36
+ "peerDependencies": {
37
+ "vitest": ">=4.0.0"
38
+ },
39
+ "peerDependenciesMeta": {
40
+ "vitest": {
41
+ "optional": true
30
42
  }
31
43
  },
32
44
  "devDependencies": {
@@ -63,5 +75,6 @@
63
75
  "test:watch": "vitest",
64
76
  "test:coverage": "vitest run --coverage",
65
77
  "typecheck": "tsc --noEmit"
66
- }
78
+ },
79
+ "types": "./dist/index.d.mts"
67
80
  }