@dunx/http 3.1.3 → 3.2.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.
@@ -1,130 +0,0 @@
1
- // @bun
2
- import {
3
- HttpStatusCode
4
- } from "./chunk-sz4pvqxy.js";
5
-
6
- // src/client/errors.ts
7
- import { AppError } from "@dunx/core";
8
-
9
- class FetchError extends AppError {
10
- status;
11
- statusText;
12
- body;
13
- response;
14
- name = "FetchError";
15
- constructor(status, statusText, body, response) {
16
- super(`HTTP ${status} ${statusText} from ${response.method} ${response.url}`);
17
- this.status = status;
18
- this.statusText = statusText;
19
- this.body = body;
20
- this.response = response;
21
- }
22
- }
23
- Object.defineProperty(FetchError, Symbol.for("dunx.deps"), { value: () => [{ unresolved: "override readonly status: number" }, { unresolved: "readonly statusText: string" }, { unresolved: "readonly body: unknown" }, { unresolved: `readonly response: {
24
- readonly method: string;
25
- readonly url: string;
26
- readonly headers: Headers;
27
- }` }] });
28
-
29
- class FetchTransportError extends AppError {
30
- response;
31
- aborted;
32
- name = "FetchTransportError";
33
- constructor(response, aborted, options) {
34
- super(`${response.method} ${response.url} failed: ${aborted ? "aborted" : "transport error"}`, options);
35
- this.response = response;
36
- this.aborted = aborted;
37
- }
38
- }
39
- Object.defineProperty(FetchTransportError, Symbol.for("dunx.deps"), { value: () => [{ unresolved: "readonly response: { readonly method: string; readonly url: string }" }, { unresolved: "readonly aborted: boolean" }, ErrorOptions] });
40
-
41
- // src/client/json.ts
42
- var safeStringify = (value) => {
43
- const seen = new WeakSet;
44
- return JSON.stringify(value, (_key, entry) => {
45
- if (typeof entry === "object" && entry !== null) {
46
- if (seen.has(entry))
47
- return "[Circular]";
48
- seen.add(entry);
49
- }
50
- return entry;
51
- });
52
- };
53
- var isPlainObject = (value) => {
54
- if (typeof value !== "object" || value === null)
55
- return false;
56
- const proto = Object.getPrototypeOf(value);
57
- return proto === Object.prototype || proto === null;
58
- };
59
- var isJsonBody = (payload) => {
60
- if (payload === null || payload === undefined)
61
- return false;
62
- if (typeof payload !== "object")
63
- return typeof payload !== "string";
64
- return !(payload instanceof FormData || payload instanceof URLSearchParams || payload instanceof Blob || payload instanceof ArrayBuffer || payload instanceof ReadableStream || ArrayBuffer.isView(payload));
65
- };
66
-
67
- // src/client/retry.ts
68
- var uniform = () => {
69
- const buffer = new Uint32Array(1);
70
- crypto.getRandomValues(buffer);
71
- return (buffer[0] ?? 0) / 2 ** 32;
72
- };
73
- var backoffDelay = (attempt, { baseMs, power = 2, jitterMs = 1000, maxMs = 30000 }) => Math.min(baseMs * power ** attempt + uniform() * jitterMs, maxMs);
74
- var retryAfterMs = (headers, now = Date.now()) => {
75
- const header = headers.get("retry-after");
76
- if (header === null)
77
- return;
78
- const seconds = Number(header);
79
- if (Number.isFinite(seconds))
80
- return Math.max(0, seconds * 1000);
81
- const at = Date.parse(header);
82
- return Number.isNaN(at) ? undefined : Math.max(0, at - now);
83
- };
84
- var isRetryableStatus = (status) => status >= HttpStatusCode.INTERNAL_SERVER_ERROR || status === HttpStatusCode.REQUEST_TIMEOUT || status === HttpStatusCode.TOO_MANY_REQUESTS;
85
- var decide = (error, attempt, options) => {
86
- const {
87
- retryDelayMs = 1000,
88
- backoff,
89
- shouldRetryOnStatus = isRetryableStatus,
90
- respectRetryAfter = true
91
- } = options;
92
- const computed = backoffDelay(attempt, { baseMs: retryDelayMs, ...backoff });
93
- if (error instanceof FetchTransportError) {
94
- return { retry: !error.aborted, delayMs: computed };
95
- }
96
- if (error instanceof FetchError) {
97
- if (!shouldRetryOnStatus(error.status))
98
- return { retry: false, delayMs: 0 };
99
- const asked = respectRetryAfter ? retryAfterMs(error.response.headers) : undefined;
100
- const maxMs = backoff?.maxMs ?? 30000;
101
- return {
102
- retry: true,
103
- delayMs: asked === undefined ? computed : Math.min(asked, maxMs)
104
- };
105
- }
106
- return { retry: true, delayMs: computed };
107
- };
108
- var executeWithRetry = async (operation, options = {}) => {
109
- const { maxRetries = 3, onAttempt, onError, onSuccess } = options;
110
- let lastError;
111
- for (let attempt = 0;attempt <= maxRetries; attempt += 1) {
112
- onAttempt?.(attempt + 1, attempt > 0);
113
- try {
114
- const result = await operation();
115
- onSuccess?.(result, attempt + 1);
116
- return result;
117
- } catch (error) {
118
- lastError = error;
119
- const { retry, delayMs } = decide(error, attempt, options);
120
- const willRetry = retry && attempt < maxRetries;
121
- onError?.(error, attempt + 1, willRetry);
122
- if (!willRetry)
123
- throw error;
124
- await Bun.sleep(delayMs);
125
- }
126
- }
127
- throw lastError;
128
- };
129
-
130
- export { FetchError, FetchTransportError, safeStringify, isPlainObject, isJsonBody, backoffDelay, retryAfterMs, isRetryableStatus, executeWithRetry };
@@ -1,22 +0,0 @@
1
- export declare const REQUEST_ID_HEADER = "x-request-id";
2
- /**
3
- * The request id, and the only thing that decides a request has one.
4
- *
5
- * The logging middleware sets the header on a response it returns, and a failure
6
- * is never one - the error mapper builds a fresh `Response` outside the chain. So
7
- * a guard's 401, a validation 400 and every unmatched 404 went out with no id.
8
- *
9
- * Recorded against the request rather than threaded through the mapper, which an
10
- * app writes its own of. {@link stamp} reads back what {@link assign} recorded, so
11
- * a path nothing minted an id for is still answered without a header.
12
- */
13
- export declare class RequestIds {
14
- /**
15
- * Called by `RequestLoggingMiddleware` and by nothing else. Splitting minting
16
- * from recording would let a second caller invent an id the log line does not
17
- * carry.
18
- */
19
- static assign(req: Request): string;
20
- /** The response, with this request's id on it if it was ever given one. */
21
- static stamp(response: Response, req: Request): Response;
22
- }