blossom-client-sdk 5.1.0 → 5.2.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.
@@ -66,11 +66,13 @@ export async function getBlobUrls(uri, opts) {
66
66
  }
67
67
  /** Resolves a blossom URI to a downloaded blob by trying servers from hints sequentially */
68
68
  export async function resolveBlob(uri, opts) {
69
+ opts?.signal?.throwIfAborted();
69
70
  const parsed = parseInput(uri);
70
71
  const collector = createServerCollector(buildBlobPath(parsed.sha256, parsed.ext));
71
72
  /** Try GET on each URL, return the first successful response */
72
73
  const tryUrls = async (urls) => {
73
74
  for (const url of urls) {
75
+ opts?.signal?.throwIfAborted();
74
76
  try {
75
77
  const res = await fetchWithTimeout(new URL(url), {
76
78
  signal: opts?.signal,
@@ -80,6 +82,7 @@ export async function resolveBlob(uri, opts) {
80
82
  return res;
81
83
  }
82
84
  catch {
85
+ opts?.signal?.throwIfAborted();
83
86
  // server unreachable or error — try next
84
87
  continue;
85
88
  }
@@ -96,7 +99,9 @@ export async function resolveBlob(uri, opts) {
96
99
  // 2. resolve and try author hints (deferred — requires relay queries)
97
100
  if (opts?.getServers) {
98
101
  for (const pubkey of parsed.authors) {
102
+ opts?.signal?.throwIfAborted();
99
103
  const authorServers = await opts.getServers(pubkey);
104
+ opts?.signal?.throwIfAborted();
100
105
  if (authorServers) {
101
106
  const urls = collector.add(authorServers);
102
107
  if (urls.length > 0) {
@@ -1,11 +1,13 @@
1
1
  import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from "../auth.js";
2
2
  import HTTPError from "../error.js";
3
3
  import { fetchWithTimeout, getBlobSha256, getBlobSize, getBlobType } from "../helpers/index.js";
4
- import { mergeHeaders } from "../helpers/headers.js";
4
+ import { handleChallenges } from "../internal/challenges.js";
5
+ import { toWebBytes } from "../internal/bytes.js";
5
6
  /** Upload a blob to a server, handles payment and auth */
6
7
  export async function uploadBlob(server, blob, opts) {
7
8
  const url = new URL("/upload", server);
8
9
  const sha256 = await getBlobSha256(blob);
10
+ const body = blob instanceof Blob ? blob : toWebBytes(blob);
9
11
  const resolveAuth = async (preset = false) => {
10
12
  const reused = opts?.authEvents
11
13
  ? await getReusableAuthEvent(opts.authEvents, { server, type: "upload", blob: sha256 })
@@ -47,66 +49,43 @@ export async function uploadBlob(server, blob, opts) {
47
49
  timeout: opts?.timeout,
48
50
  });
49
51
  let upload = undefined;
52
+ if (![401, 402, 404].includes(firstTry.status))
53
+ await HTTPError.handleErrorResponse(firstTry);
50
54
  if (firstTry.status === 404) {
51
55
  // BUD-06 HEAD endpoint is not supported. attempt to upload
52
56
  upload = firstTry = await fetchWithTimeout(url, {
53
- body: blob,
57
+ body,
54
58
  method: "PUT",
59
+ headers,
55
60
  signal: opts?.signal,
56
61
  timeout: opts?.timeout,
57
62
  });
58
63
  }
59
- // handle auth and payment
60
- switch (firstTry.status) {
61
- case 401: {
62
- // throw an error if auth is requested and disabled
64
+ const request = (headers) => fetchWithTimeout(url, {
65
+ method: "PUT",
66
+ body,
67
+ headers,
68
+ signal: opts?.signal,
69
+ timeout: opts?.timeout,
70
+ });
71
+ upload = await handleChallenges(upload ?? (firstTry.ok ? await request(headers) : firstTry), headers, request, {
72
+ signal: opts?.signal,
73
+ auth: async () => {
63
74
  if (opts?.auth === false)
64
75
  throw new Error("Authorization disabled");
65
- // Request authorization event for this upload
66
- const auth = await resolveAuth();
67
- // Try upload with auth
68
- upload = await fetchWithTimeout(url, {
69
- method: "PUT",
70
- body: blob,
71
- headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
72
- signal: opts?.signal,
73
- timeout: opts?.timeout,
74
- });
75
- break;
76
- }
77
- case 402: {
78
- let paymentHeaders;
76
+ return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
77
+ },
78
+ payment: async (headers) => {
79
79
  if (opts?.onPaymentRequired)
80
- paymentHeaders = await opts.onPaymentRequired(server, sha256, blob, firstTry.headers);
81
- else if (opts?.onPayment) {
80
+ return opts.onPaymentRequired(server, sha256, blob, headers);
81
+ if (opts?.onPayment) {
82
82
  const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
83
- const request = await getPaymentRequestFromHeaders(firstTry.headers);
84
- paymentHeaders = { "X-Cashu": encodePaymentToken(await opts.onPayment(server, sha256, blob, request)) };
83
+ const request = await getPaymentRequestFromHeaders(headers);
84
+ return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, sha256, blob, request)) };
85
85
  }
86
- else
87
- throw new Error("Missing payment handler");
88
- // Try upload with payment
89
- upload = await fetchWithTimeout(url, {
90
- method: "PUT",
91
- body: blob,
92
- headers: mergeHeaders(headers, paymentHeaders),
93
- signal: opts?.signal,
94
- timeout: opts?.timeout,
95
- });
96
- break;
97
- }
98
- }
99
- if (firstTry.status >= 500)
100
- throw new Error("Server error");
101
- // check passed, upload
102
- if (!upload)
103
- upload = await fetchWithTimeout(url, {
104
- method: "PUT",
105
- body: blob,
106
- headers: { ...headers },
107
- signal: opts?.signal,
108
- timeout: opts?.timeout,
109
- });
86
+ throw new Error("Missing payment handler");
87
+ },
88
+ });
110
89
  // handle errors
111
90
  await HTTPError.handleErrorResponse(upload);
112
91
  // return blob descriptor
@@ -1,4 +1,5 @@
1
- import { bytesToHex } from "@noble/hashes/utils";
1
+ import { bytesToHex } from "@noble/hashes/utils.js";
2
+ import { toWebBytes } from "../internal/bytes.js";
2
3
  /** Checks if a string is a 64 length hex string */
3
4
  export function isSha256(str) {
4
5
  return str.match(/^[0-9a-f]{64}$/);
@@ -23,7 +24,7 @@ export async function computeBlobSha256(blob) {
23
24
  }
24
25
  else {
25
26
  // nodejs Buffer
26
- buffer = blob;
27
+ buffer = toWebBytes(blob);
27
28
  }
28
29
  let hash;
29
30
  if (typeof crypto !== "undefined" && crypto.subtle) {
@@ -31,7 +32,7 @@ export async function computeBlobSha256(blob) {
31
32
  hash = new Uint8Array(hashBuffer);
32
33
  }
33
34
  else {
34
- const { sha256 } = await import("@noble/hashes/sha2");
35
+ const { sha256 } = await import("@noble/hashes/sha2.js");
35
36
  hash = sha256.create().update(new Uint8Array(buffer)).digest();
36
37
  }
37
38
  return bytesToHex(hash);
@@ -1,4 +1,5 @@
1
1
  export declare class TimeoutError extends Error {
2
+ name: string;
2
3
  }
3
4
  /** Creates and optionally wraps an AbortSignal with a timeout */
4
5
  export declare function wrapSignalWithTimeout(timeout: number, message: string, signal: AbortSignal | undefined): {
@@ -1,13 +1,14 @@
1
1
  export class TimeoutError extends Error {
2
+ name = "TimeoutError";
2
3
  }
3
4
  /** Creates and optionally wraps an AbortSignal with a timeout */
4
5
  export function wrapSignalWithTimeout(timeout, message, signal) {
5
6
  const controller = new AbortController();
6
- if (signal)
7
- signal.addEventListener("abort", (err) => controller.abort(err), { once: true });
8
7
  const t = setTimeout(() => {
9
8
  controller.abort(new TimeoutError(message));
10
9
  }, timeout);
11
10
  const cancel = () => clearTimeout(t);
12
- return { cancel, signal: controller.signal };
11
+ // Native composition preserves existing abort reasons and avoids retaining listeners
12
+ // on a caller's long-lived signal. It also keeps cancellation working after headers arrive.
13
+ return { cancel, signal: signal ? AbortSignal.any([signal, controller.signal]) : controller.signal };
13
14
  }
package/lib/index.d.ts CHANGED
@@ -4,5 +4,6 @@ export * from "./helpers/index.js";
4
4
  export * from "./media.js";
5
5
  export * from "./nostr.js";
6
6
  export * from "./error.js";
7
+ export { default as HTTPError } from "./error.js";
7
8
  export * from "./types.js";
8
9
  export * as Actions from "./actions/index.js";
package/lib/index.js CHANGED
@@ -4,5 +4,6 @@ export * from "./helpers/index.js";
4
4
  export * from "./media.js";
5
5
  export * from "./nostr.js";
6
6
  export * from "./error.js";
7
+ export { default as HTTPError } from "./error.js";
7
8
  export * from "./types.js";
8
9
  export * as Actions from "./actions/index.js";
@@ -0,0 +1,2 @@
1
+ /** Return a Web API-compatible view, copying only shared-memory-backed inputs. */
2
+ export declare function toWebBytes(bytes: Uint8Array): Uint8Array<ArrayBuffer>;
@@ -0,0 +1,6 @@
1
+ /** Return a Web API-compatible view, copying only shared-memory-backed inputs. */
2
+ export function toWebBytes(bytes) {
3
+ return bytes.buffer instanceof ArrayBuffer
4
+ ? new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength)
5
+ : Uint8Array.from(bytes);
6
+ }
@@ -0,0 +1,6 @@
1
+ /** Handle each challenge at most once, including challenges from a preflight response. */
2
+ export declare function handleChallenges(response: Response, headers: HeadersInit, request: (headers: HeadersInit) => Promise<Response>, handlers: {
3
+ auth: () => Promise<HeadersInit>;
4
+ payment: (headers: Headers) => Promise<HeadersInit>;
5
+ signal?: AbortSignal;
6
+ }): Promise<Response>;
@@ -0,0 +1,15 @@
1
+ import { mergeHeaders } from "../helpers/headers.js";
2
+ /** Handle each challenge at most once, including challenges from a preflight response. */
3
+ export async function handleChallenges(response, headers, request, handlers) {
4
+ const handled = new Set();
5
+ while ((response.status === 401 || response.status === 402) && !handled.has(response.status)) {
6
+ handlers.signal?.throwIfAborted();
7
+ handled.add(response.status);
8
+ const nextHeaders = response.status === 401 ? await handlers.auth() : await handlers.payment(response.headers);
9
+ handlers.signal?.throwIfAborted();
10
+ headers = mergeHeaders(headers, nextHeaders);
11
+ await response.body?.cancel();
12
+ response = await request(headers);
13
+ }
14
+ return response;
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blossom-client-sdk",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "Client SDK for talking to blossom servers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -42,8 +42,8 @@
42
42
  "types": "./lib/auth.d.ts"
43
43
  },
44
44
  "./actions": {
45
- "import": "./lib/actions.js",
46
- "types": "./lib/actions.d.ts"
45
+ "import": "./lib/actions/index.js",
46
+ "types": "./lib/actions/index.d.ts"
47
47
  },
48
48
  "./actions/*": {
49
49
  "import": "./lib/actions/*.js",
@@ -51,10 +51,10 @@
51
51
  }
52
52
  },
53
53
  "engines": {
54
- "node": ">=18"
54
+ "node": ">=20.19.0"
55
55
  },
56
56
  "dependencies": {
57
- "@noble/hashes": "^1.8.0"
57
+ "@noble/hashes": "^2.4.0"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "hls.js": "^1.6.16"
@@ -65,26 +65,30 @@
65
65
  }
66
66
  },
67
67
  "devDependencies": {
68
- "@cashu/cashu-ts": "^2.4.3",
69
- "@changesets/cli": "^2.29.3",
70
- "@testing-library/dom": "^10.4.0",
71
- "@types/node": "^22.15.14",
72
- "@vitest/browser": "^3.1.3",
73
- "@vitest/coverage-v8": "^3.1.3",
74
- "hls.js": "^1.6.16",
75
- "nostr-tools": "^2.12.0",
76
- "playwright": "^1.52.0",
77
- "prettier": "^3.5.3",
68
+ "@cashu/cashu-ts": "^2.9.0",
69
+ "@testing-library/dom": "^10.4.2",
70
+ "@types/node": "^22.20.5",
71
+ "@vitest/browser": "^3.2.7",
72
+ "@vitest/coverage-v8": "^3.2.7",
73
+ "hls.js": "^1.7.4",
74
+ "nostr-tools": "^2.26.1",
75
+ "playwright": "^1.65.1",
76
+ "prettier": "^3.10.1",
78
77
  "typedoc": "^0.25.13",
79
78
  "typedoc-plugin-mermaid": "^1.12.0",
80
- "typescript": "^5.8.3",
81
- "vitest": "^3.1.3",
79
+ "typescript": "^5.9.3",
80
+ "vitest": "^3.2.7",
82
81
  "vitest-fetch-mock": "^0.4.5"
83
82
  },
84
83
  "scripts": {
85
84
  "build": "tsc",
86
85
  "prepublish": "npm run build",
87
86
  "test": "vitest run",
87
+ "test:browser": "vitest run --browser --browser.headless",
88
+ "test:node": "pnpm build && node node_modules/vitest/vitest.mjs run && node --test tests/package-exports.test.mjs tests/runtime/*.test.mjs",
89
+ "test:deno": "pnpm build && deno run -A --no-config --no-lock --node-modules-dir=manual node_modules/vitest/vitest.mjs run && deno test -A --no-config --no-lock --no-check --node-modules-dir=manual tests/package-exports.test.mjs tests/runtime/",
90
+ "test:bun": "pnpm build && bun run --bun node_modules/vitest/vitest.mjs run && bun test tests/package-exports.test.mjs tests/runtime/",
91
+ "test:all": "pnpm test:node && pnpm test:deno && pnpm test:bun && pnpm test:browser",
88
92
  "coverage": "vitest run --coverage",
89
93
  "format": "prettier -w .",
90
94
  "docs": "typedoc src/index.ts"
@@ -2,7 +2,7 @@ import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from
2
2
  import { ServerType } from "../types.js";
3
3
  import HTTPError from "../error.js";
4
4
  import { fetchWithTimeout } from "../helpers/fetch.js";
5
- import { mergeHeaders } from "../helpers/headers.js";
5
+ import { handleChallenges } from "../internal/challenges.js";
6
6
  import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
7
7
 
8
8
  export type DeleteOptions<S extends ServerType> = {
@@ -59,50 +59,24 @@ export async function deleteBlob<S extends ServerType>(server: S, hash: string,
59
59
  }
60
60
  }
61
61
 
62
- let res = await fetchWithTimeout(url, {
63
- method: "DELETE",
64
- headers,
62
+ const request = (headers: HeadersInit) =>
63
+ fetchWithTimeout(url, { method: "DELETE", headers, signal: opts?.signal, timeout: opts?.timeout });
64
+ const res = await handleChallenges(await request(headers), headers, request, {
65
65
  signal: opts?.signal,
66
- timeout: opts?.timeout,
67
- });
68
-
69
- // handle auth and payment
70
- switch (res.status) {
71
- case 401: {
72
- // throw an error if authorization is required but disabled
66
+ auth: async () => {
73
67
  if (opts?.auth === false) throw new Error("Authorization disabled");
74
-
75
- // request authorization for this request
76
- const auth = await resolveAuth();
77
-
78
- // Try delete with auth
79
- res = await fetchWithTimeout(url, {
80
- signal: opts?.signal,
81
- method: "DELETE",
82
- headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
83
- timeout: opts?.timeout,
84
- });
85
- break;
86
- }
87
- case 402: {
88
- let paymentHeaders: HeadersInit;
89
- if (opts?.onPaymentRequired) paymentHeaders = await opts.onPaymentRequired(server, hash, res.headers);
90
- else if (opts?.onPayment) {
68
+ return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
69
+ },
70
+ payment: async (headers) => {
71
+ if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, hash, headers);
72
+ if (opts?.onPayment) {
91
73
  const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
92
- const request = await getPaymentRequestFromHeaders(res.headers);
93
- paymentHeaders = { "X-Cashu": encodePaymentToken(await opts.onPayment(server, hash, request)) };
94
- } else throw new Error("Missing payment handler");
95
-
96
- // Try delete with payment
97
- res = await fetchWithTimeout(url, {
98
- signal: opts?.signal,
99
- method: "DELETE",
100
- headers: mergeHeaders(headers, paymentHeaders),
101
- timeout: opts?.timeout,
102
- });
103
- break;
104
- }
105
- }
74
+ const request = await getPaymentRequestFromHeaders(headers);
75
+ return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, hash, request)) };
76
+ }
77
+ throw new Error("Missing payment handler");
78
+ },
79
+ });
106
80
 
107
81
  // handle errors
108
82
  await HTTPError.handleErrorResponse(res);
@@ -3,7 +3,7 @@ import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
3
3
  import HTTPError from "../error.js";
4
4
  import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from "../auth.js";
5
5
  import { fetchWithTimeout } from "../helpers/index.js";
6
- import { mergeHeaders } from "../helpers/headers.js";
6
+ import { handleChallenges } from "../internal/challenges.js";
7
7
 
8
8
  export type DownloadOptions<S extends ServerType> = {
9
9
  /** AbortSignal to cancel the action */
@@ -59,49 +59,24 @@ export async function downloadBlob<S extends ServerType>(server: S, hash: string
59
59
  }
60
60
  }
61
61
 
62
- let download = await fetchWithTimeout(url, {
63
- headers,
62
+ const request = (headers: HeadersInit) =>
63
+ fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
64
+ const download = await handleChallenges(await request(headers), headers, request, {
64
65
  signal: opts?.signal,
65
- timeout: opts?.timeout,
66
- });
67
-
68
- // handle auth and payment
69
- switch (download.status) {
70
- case 401: {
71
- // throw an error if auth is requested and disabled
66
+ auth: async () => {
72
67
  if (opts?.auth === false) throw new Error("Authorization disabled");
73
-
74
- // Request authorization for this request
75
- const auth = await resolveAuth();
76
-
77
- // Try download with auth
78
- download = await fetchWithTimeout(url, {
79
- headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
80
- signal: opts?.signal,
81
- timeout: opts?.timeout,
82
- });
83
-
84
- break;
85
- }
86
- case 402: {
87
- let paymentHeaders: HeadersInit;
88
- if (opts?.onPaymentRequired) paymentHeaders = await opts.onPaymentRequired(server, hash, download.headers);
89
- else if (opts?.onPayment) {
68
+ return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
69
+ },
70
+ payment: async (headers) => {
71
+ if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, hash, headers);
72
+ if (opts?.onPayment) {
90
73
  const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
91
- const request = await getPaymentRequestFromHeaders(download.headers);
92
- paymentHeaders = { "X-Cashu": encodePaymentToken(await opts.onPayment(server, hash, request)) };
93
- } else throw new Error("Missing payment handler");
94
-
95
- // Try download with payment
96
- download = await fetchWithTimeout(url, {
97
- headers: mergeHeaders(headers, paymentHeaders),
98
- signal: opts?.signal,
99
- timeout: opts?.timeout,
100
- });
101
-
102
- break;
103
- }
104
- }
74
+ const request = await getPaymentRequestFromHeaders(headers);
75
+ return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, hash, request)) };
76
+ }
77
+ throw new Error("Missing payment handler");
78
+ },
79
+ });
105
80
 
106
81
  // check download errors
107
82
  await HTTPError.handleErrorResponse(download);
@@ -1,5 +1,6 @@
1
1
  import { ServerType } from "../types.js";
2
2
  import { fetchWithTimeout } from "../helpers/index.js";
3
+ import HTTPError from "../error.js";
3
4
 
4
5
  export type HasBlobOptions = {
5
6
  /** AbortSignal to cancel the action */
@@ -8,12 +9,14 @@ export type HasBlobOptions = {
8
9
  timeout?: number;
9
10
  };
10
11
 
11
- /** Checks if a blob exists on a server */
12
+ /** Returns false for a missing blob (404), true on success, and throws on other HTTP errors. */
12
13
  export async function hasBlob<S extends ServerType>(server: S, hash: string, opts?: HasBlobOptions): Promise<boolean> {
13
14
  const res = await fetchWithTimeout(new URL("/" + hash, server), {
14
15
  method: "HEAD",
15
16
  signal: opts?.signal,
16
17
  timeout: opts?.timeout,
17
18
  });
18
- return res.status !== 404;
19
+ if (res.status === 404) return false;
20
+ await HTTPError.handleErrorResponse(res);
21
+ return true;
19
22
  }
@@ -3,7 +3,7 @@ import { ServerType } from "../types.js";
3
3
  import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
4
4
  import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from "../auth.js";
5
5
  import { fetchWithTimeout } from "../helpers/index.js";
6
- import { mergeHeaders } from "../helpers/headers.js";
6
+ import { handleChallenges } from "../internal/challenges.js";
7
7
 
8
8
  export type ListOptions<S extends ServerType> = {
9
9
  /** AbortSignal to cancel the action */
@@ -68,42 +68,24 @@ export async function listBlobs<S extends ServerType>(
68
68
  }
69
69
  }
70
70
 
71
- let list = await fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
72
-
73
- // handle auth and payments
74
- switch (list.status) {
75
- case 401: {
76
- // throw an error if auth is requested and disabled
71
+ const request = (headers: HeadersInit) =>
72
+ fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
73
+ const list = await handleChallenges(await request(headers), headers, request, {
74
+ signal: opts?.signal,
75
+ auth: async () => {
77
76
  if (opts?.auth === false) throw new Error("Authorization disabled");
78
-
79
- const auth = await resolveAuth();
80
-
81
- // Try list with auth
82
- list = await fetchWithTimeout(url, {
83
- headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
84
- signal: opts?.signal,
85
- timeout: opts?.timeout,
86
- });
87
- break;
88
- }
89
- case 402: {
90
- let paymentHeaders: HeadersInit;
91
- if (opts?.onPaymentRequired) paymentHeaders = await opts.onPaymentRequired(server, list.headers);
92
- else if (opts?.onPayment) {
77
+ return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
78
+ },
79
+ payment: async (headers) => {
80
+ if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, headers);
81
+ if (opts?.onPayment) {
93
82
  const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
94
- const request = await getPaymentRequestFromHeaders(list.headers);
95
- paymentHeaders = { "X-Cashu": encodePaymentToken(await opts.onPayment(server, request)) };
96
- } else throw new Error("Missing payment handler");
97
-
98
- // Try list with payment
99
- list = await fetchWithTimeout(url, {
100
- headers: mergeHeaders(headers, paymentHeaders),
101
- signal: opts?.signal,
102
- timeout: opts?.timeout,
103
- });
104
- break;
105
- }
106
- }
83
+ const request = await getPaymentRequestFromHeaders(headers);
84
+ return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, request)) };
85
+ }
86
+ throw new Error("Missing payment handler");
87
+ },
88
+ });
107
89
 
108
90
  // handle errors
109
91
  await HTTPError.handleErrorResponse(list);