kinetex 0.0.2 → 1.0.0-rc.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/LICENSE +16 -4
- package/README.md +2434 -701
- package/dist/browser/kinetex.esm.js +86 -0
- package/dist/browser/kinetex.js +19056 -1999
- package/dist/browser/kinetex.min.js +85 -8
- package/dist/cjs/aws-sigv4.js +903 -0
- package/dist/cjs/cache.js +1099 -0
- package/dist/cjs/circuit-breaker.js +362 -0
- package/dist/cjs/client.js +2890 -0
- package/dist/cjs/cookie-parser.js +2182 -0
- package/dist/cjs/cookie-store.js +783 -0
- package/dist/cjs/cookiejar.js +175 -0
- package/dist/cjs/core.js +1267 -0
- package/dist/cjs/dedup.js +267 -0
- package/dist/cjs/digest.js +552 -0
- package/dist/cjs/graphql.js +1086 -0
- package/dist/cjs/headers.js +2264 -0
- package/dist/cjs/interceptors.js +1115 -0
- package/dist/cjs/lifecycle.js +886 -0
- package/dist/cjs/logging.js +887 -0
- package/dist/cjs/mod.js +98 -0
- package/dist/cjs/package.json +1 -3
- package/dist/cjs/pagination.js +535 -0
- package/dist/cjs/progress.js +694 -0
- package/dist/cjs/response.js +1212 -0
- package/dist/cjs/socks5.js +681 -0
- package/dist/cjs/sse.js +827 -0
- package/dist/cjs/types.js +217 -0
- package/dist/cjs/url.js +1269 -0
- package/dist/cjs/utils.js +901 -0
- package/dist/cjs/worker.js +56 -0
- package/dist/cjs/ws.js +1099 -0
- package/dist/esm/aws-sigv4.js +904 -0
- package/dist/esm/aws-sigv4.js.map +1 -0
- package/dist/esm/cache.js +1100 -0
- package/dist/esm/cache.js.map +1 -0
- package/dist/esm/circuit-breaker.js +363 -0
- package/dist/esm/circuit-breaker.js.map +1 -0
- package/dist/esm/client.js +2891 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/cookie-parser.js +2183 -0
- package/dist/esm/cookie-parser.js.map +1 -0
- package/dist/esm/cookie-store.js +784 -0
- package/dist/esm/cookie-store.js.map +1 -0
- package/dist/esm/cookiejar.js +176 -0
- package/dist/esm/cookiejar.js.map +1 -0
- package/dist/esm/core.js +1268 -0
- package/dist/esm/core.js.map +1 -0
- package/dist/esm/dedup.js +268 -0
- package/dist/esm/dedup.js.map +1 -0
- package/dist/esm/digest.js +553 -0
- package/dist/esm/digest.js.map +1 -0
- package/dist/esm/graphql.js +1087 -0
- package/dist/esm/graphql.js.map +1 -0
- package/dist/esm/headers.js +2265 -0
- package/dist/esm/headers.js.map +1 -0
- package/dist/esm/interceptors.js +1116 -0
- package/dist/esm/interceptors.js.map +1 -0
- package/dist/esm/lifecycle.js +887 -0
- package/dist/esm/lifecycle.js.map +1 -0
- package/dist/esm/logging.js +888 -0
- package/dist/esm/logging.js.map +1 -0
- package/dist/esm/mod.js +99 -0
- package/dist/esm/mod.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/pagination.js +536 -0
- package/dist/esm/pagination.js.map +1 -0
- package/dist/esm/progress.js +695 -0
- package/dist/esm/progress.js.map +1 -0
- package/dist/esm/response.js +1213 -0
- package/dist/esm/response.js.map +1 -0
- package/dist/esm/socks5.js +682 -0
- package/dist/esm/socks5.js.map +1 -0
- package/dist/esm/sse.js +828 -0
- package/dist/esm/sse.js.map +1 -0
- package/dist/esm/types.js +218 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/url.js +1270 -0
- package/dist/esm/url.js.map +1 -0
- package/dist/esm/utils.js +902 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/worker.js +57 -0
- package/dist/esm/worker.js.map +1 -0
- package/dist/esm/ws.js +1100 -0
- package/dist/esm/ws.js.map +1 -0
- package/dist/types/aws-sigv4.d.ts +382 -0
- package/dist/types/aws-sigv4.d.ts.map +1 -0
- package/dist/types/cache.d.ts +397 -0
- package/dist/types/cache.d.ts.map +1 -0
- package/dist/types/circuit-breaker.d.ts +241 -0
- package/dist/types/circuit-breaker.d.ts.map +1 -0
- package/dist/types/client.d.ts +616 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/cookie-parser.d.ts +203 -0
- package/dist/types/cookie-parser.d.ts.map +1 -0
- package/dist/types/cookie-store.d.ts +316 -0
- package/dist/types/cookie-store.d.ts.map +1 -0
- package/dist/types/cookiejar.d.ts +165 -0
- package/dist/types/cookiejar.d.ts.map +1 -0
- package/dist/types/core.d.ts +297 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/types/dedup.d.ts +164 -0
- package/dist/types/dedup.d.ts.map +1 -0
- package/dist/types/digest.d.ts +136 -0
- package/dist/types/digest.d.ts.map +1 -0
- package/dist/types/graphql.d.ts +530 -0
- package/dist/types/graphql.d.ts.map +1 -0
- package/dist/types/headers.d.ts +1119 -0
- package/dist/types/headers.d.ts.map +1 -0
- package/dist/types/interceptors.d.ts +681 -0
- package/dist/types/interceptors.d.ts.map +1 -0
- package/dist/types/lifecycle.d.ts +553 -0
- package/dist/types/lifecycle.d.ts.map +1 -0
- package/dist/types/logging.d.ts +518 -0
- package/dist/types/logging.d.ts.map +1 -0
- package/dist/types/mod.d.ts +92 -0
- package/dist/types/mod.d.ts.map +1 -0
- package/dist/types/pagination.d.ts +401 -0
- package/dist/types/pagination.d.ts.map +1 -0
- package/dist/types/progress.d.ts +344 -0
- package/dist/types/progress.d.ts.map +1 -0
- package/dist/types/response.d.ts +484 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/socks5.d.ts +218 -0
- package/dist/types/socks5.d.ts.map +1 -0
- package/dist/types/sse.d.ts +399 -0
- package/dist/types/sse.d.ts.map +1 -0
- package/dist/types/types.d.ts +967 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/url.d.ts +530 -0
- package/dist/types/url.d.ts.map +1 -0
- package/dist/types/utils.d.ts +300 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/worker.d.ts +55 -0
- package/dist/types/worker.d.ts.map +1 -0
- package/dist/types/ws.d.ts +480 -0
- package/dist/types/ws.d.ts.map +1 -0
- package/package.json +194 -127
- package/CHANGELOG.md +0 -50
- package/SECURITY.md +0 -31
- package/dist/browser/kinetex.cjs.js +0 -9
- package/dist/browser/kinetex.esm.min.js +0 -9
- package/dist/browser/kinetex.js.map +0 -7
- package/dist/cjs/auth/index.cjs +0 -333
- package/dist/cjs/auth/index.d.cts +0 -41
- package/dist/cjs/auth/index.d.ts.map +0 -1
- package/dist/cjs/auth/index.js.map +0 -1
- package/dist/cjs/core/chain.cjs +0 -127
- package/dist/cjs/core/chain.d.cts +0 -44
- package/dist/cjs/core/chain.d.ts.map +0 -1
- package/dist/cjs/core/chain.js.map +0 -1
- package/dist/cjs/core/transport.cjs +0 -544
- package/dist/cjs/core/transport.d.cts +0 -11
- package/dist/cjs/core/transport.d.ts.map +0 -1
- package/dist/cjs/core/transport.js.map +0 -1
- package/dist/cjs/core/types.cjs +0 -54
- package/dist/cjs/core/types.d.cts +0 -322
- package/dist/cjs/core/types.d.ts.map +0 -1
- package/dist/cjs/core/types.js.map +0 -1
- package/dist/cjs/index.cjs +0 -272
- package/dist/cjs/index.d.cts +0 -12
- package/dist/cjs/index.d.ts.map +0 -1
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/middleware/pipeline.cjs +0 -451
- package/dist/cjs/middleware/pipeline.d.cts +0 -32
- package/dist/cjs/middleware/pipeline.d.ts.map +0 -1
- package/dist/cjs/middleware/pipeline.js.map +0 -1
- package/dist/cjs/plugins/cookies.cjs +0 -192
- package/dist/cjs/plugins/cookies.d.cts +0 -57
- package/dist/cjs/plugins/cookies.d.ts.map +0 -1
- package/dist/cjs/plugins/cookies.js.map +0 -1
- package/dist/cjs/plugins/extras.cjs +0 -316
- package/dist/cjs/plugins/extras.d.cts +0 -87
- package/dist/cjs/plugins/extras.d.ts.map +0 -1
- package/dist/cjs/plugins/extras.js.map +0 -1
- package/dist/cjs/plugins/index.cjs +0 -18
- package/dist/cjs/plugins/index.d.cts +0 -7
- package/dist/cjs/plugins/index.d.ts.map +0 -1
- package/dist/cjs/plugins/index.js.map +0 -1
- package/dist/cjs/plugins/proxy.cjs +0 -161
- package/dist/cjs/plugins/proxy.d.cts +0 -25
- package/dist/cjs/plugins/proxy.d.ts.map +0 -1
- package/dist/cjs/plugins/proxy.js.map +0 -1
- package/dist/cjs/utils/index.cjs +0 -12
- package/dist/cjs/utils/index.d.cts +0 -6
- package/dist/cjs/utils/index.d.ts.map +0 -1
- package/dist/cjs/utils/index.js.map +0 -1
- package/dist/esm/auth/index.js +0 -324
- package/dist/esm/auth/index.js.map +0 -1
- package/dist/esm/core/chain.js +0 -123
- package/dist/esm/core/chain.js.map +0 -1
- package/dist/esm/core/transport.js +0 -536
- package/dist/esm/core/transport.js.map +0 -1
- package/dist/esm/core/types.js +0 -46
- package/dist/esm/core/types.js.map +0 -1
- package/dist/esm/index.js +0 -241
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/middleware/pipeline.js +0 -438
- package/dist/esm/middleware/pipeline.js.map +0 -1
- package/dist/esm/plugins/cookies.js +0 -187
- package/dist/esm/plugins/cookies.js.map +0 -1
- package/dist/esm/plugins/extras.js +0 -306
- package/dist/esm/plugins/extras.js.map +0 -1
- package/dist/esm/plugins/index.js +0 -4
- package/dist/esm/plugins/index.js.map +0 -1
- package/dist/esm/plugins/proxy.js +0 -124
- package/dist/esm/plugins/proxy.js.map +0 -1
- package/dist/esm/utils/index.js +0 -6
- package/dist/esm/utils/index.js.map +0 -1
- package/dist/types/auth/index.d.ts +0 -41
- package/dist/types/auth/index.d.ts.map +0 -1
- package/dist/types/core/chain.d.ts +0 -44
- package/dist/types/core/chain.d.ts.map +0 -1
- package/dist/types/core/transport.d.ts +0 -11
- package/dist/types/core/transport.d.ts.map +0 -1
- package/dist/types/core/types.d.ts +0 -322
- package/dist/types/core/types.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/middleware/pipeline.d.ts +0 -32
- package/dist/types/middleware/pipeline.d.ts.map +0 -1
- package/dist/types/plugins/cookies.d.ts +0 -57
- package/dist/types/plugins/cookies.d.ts.map +0 -1
- package/dist/types/plugins/extras.d.ts +0 -87
- package/dist/types/plugins/extras.d.ts.map +0 -1
- package/dist/types/plugins/index.d.ts +0 -7
- package/dist/types/plugins/index.d.ts.map +0 -1
- package/dist/types/plugins/proxy.d.ts +0 -25
- package/dist/types/plugins/proxy.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts +0 -6
- package/dist/types/utils/index.d.ts.map +0 -1
|
@@ -0,0 +1,967 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All shared types, generics, and interfaces for kinetex.
|
|
3
|
+
*/
|
|
4
|
+
/** Supported runtime identifiers. */
|
|
5
|
+
export type Runtime = "node" | "deno" | "bun" | "browser" | "cloudflare-workers" | "edge" | "unknown";
|
|
6
|
+
/** HTTP method strings (upper-cased). */
|
|
7
|
+
export type HTTPMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" | "TRACE" | "CONNECT";
|
|
8
|
+
/**
|
|
9
|
+
* HTTP protocol versions supported by the library.
|
|
10
|
+
*
|
|
11
|
+
* When set in {@link KinetexRequest.httpVersion}, the client attempts to use the specified version.
|
|
12
|
+
* Falls back to the next available version if the requested version is unavailable:
|
|
13
|
+
* - `HTTP/2` requires ALPN negotiation or h2 upgrade support
|
|
14
|
+
* - `HTTP/3` requires HTTP/3-capable transport (experimental)
|
|
15
|
+
*
|
|
16
|
+
* In most runtimes (Deno, Bun, Browser), HTTP/2 is negotiated automatically over TLS.
|
|
17
|
+
* On Node.js, use {@link createTransport} with `preferHTTP2: true` for HTTP/2 support.
|
|
18
|
+
*/
|
|
19
|
+
export type HTTPVersion = "HTTP/1.0" | "HTTP/1.1" | "HTTP/2" | "HTTP/3";
|
|
20
|
+
/**
|
|
21
|
+
* Branded type utility — creates a nominal subtype.
|
|
22
|
+
*
|
|
23
|
+
* Use to prevent accidental mixing of semantically different values
|
|
24
|
+
* that share the same underlying type.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* type UserId = Brand<string, "UserId">;
|
|
29
|
+
* type OrderId = Brand<number, "OrderId">;
|
|
30
|
+
*
|
|
31
|
+
* function getUser(id: UserId) { /* ... */ }
|
|
32
|
+
* getUser("abc" as UserId); // OK
|
|
33
|
+
* getUser(123 as unknown as UserId); // Error
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export type Brand<T, B extends string> = T & {
|
|
37
|
+
/** Brand discriminator — prevents accidental mixing of semantically different values that share the same underlying type. */
|
|
38
|
+
readonly __brand: B;
|
|
39
|
+
};
|
|
40
|
+
/** Branded type for request identifiers — prevents mixing up with other strings. */
|
|
41
|
+
export type RequestId = Brand<string, "RequestId">;
|
|
42
|
+
/**
|
|
43
|
+
* Create a RequestId from a string.
|
|
44
|
+
* @param id - The string to wrap.
|
|
45
|
+
* @returns The branded RequestId.
|
|
46
|
+
*/
|
|
47
|
+
export declare function toRequestId(id: string): RequestId;
|
|
48
|
+
/** A plain record of header name → value(s). */
|
|
49
|
+
export type HeadersInit = Record<string, string | string[]>;
|
|
50
|
+
/** Serializable query parameter value types. */
|
|
51
|
+
export type QueryValue = string | number | boolean | null | undefined;
|
|
52
|
+
/** Query parameter map — supports arrays for repeated keys. */
|
|
53
|
+
export type QueryParams = Record<string, QueryValue | QueryValue[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Request body types accepted by kinetex.
|
|
56
|
+
* Compatible with the Fetch API's BodyInit.
|
|
57
|
+
*
|
|
58
|
+
* Note: `null` is a valid body value indicating no body should be sent.
|
|
59
|
+
* This is different from `undefined` which means "use default". All request
|
|
60
|
+
* builders handle `null` correctly by omitting the body from the fetch call.
|
|
61
|
+
*/
|
|
62
|
+
export type BodyInit = string | Uint8Array | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | URLSearchParams | FormData | Blob | null;
|
|
63
|
+
/**
|
|
64
|
+
* Fully-resolved request object passed through the pipeline.
|
|
65
|
+
*
|
|
66
|
+
* @typeParam M - HTTP method
|
|
67
|
+
*/
|
|
68
|
+
export interface KinetexRequest<M extends HTTPMethod = HTTPMethod> {
|
|
69
|
+
/** Fully-qualified URL string. */
|
|
70
|
+
readonly url: string;
|
|
71
|
+
/** HTTP method. */
|
|
72
|
+
readonly method: M;
|
|
73
|
+
/** Normalized request headers (lowercase keys). */
|
|
74
|
+
readonly headers: Record<string, string>;
|
|
75
|
+
/** Serialized request body (null for bodyless methods). */
|
|
76
|
+
readonly body: BodyInit | null;
|
|
77
|
+
/** AbortSignal for cancellation. */
|
|
78
|
+
readonly signal: AbortSignal | null;
|
|
79
|
+
/** Arbitrary per-request metadata — freely readable/writable by hooks. */
|
|
80
|
+
readonly meta: Record<string, unknown>;
|
|
81
|
+
/** Preferred HTTP version. Falls back if unavailable. */
|
|
82
|
+
readonly httpVersion?: HTTPVersion;
|
|
83
|
+
/**
|
|
84
|
+
* Redirect mode passed to fetch.
|
|
85
|
+
* - `"follow"` (default) — automatically follow redirects.
|
|
86
|
+
* - `"manual"` — return the redirect response as-is (used internally for cookie-jar redirect following).
|
|
87
|
+
* - `"error"` — throw on any redirect.
|
|
88
|
+
*/
|
|
89
|
+
readonly redirect?: "follow" | "manual" | "error";
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Mutable builder state — accumulates options before `.send()`.
|
|
93
|
+
*
|
|
94
|
+
* ⚠️ This is an internal type used by the request builder. Do not use directly.
|
|
95
|
+
* It is exported for advanced use cases (e.g., middleware that needs to inspect
|
|
96
|
+
* or modify the builder state), but most users should use the public API.
|
|
97
|
+
*/
|
|
98
|
+
export interface RequestState {
|
|
99
|
+
/** Target URL. */
|
|
100
|
+
url: string;
|
|
101
|
+
/** HTTP method. */
|
|
102
|
+
method: HTTPMethod;
|
|
103
|
+
/** Request headers (lowercase keys). */
|
|
104
|
+
headers: Record<string, string>;
|
|
105
|
+
/** Query parameters. */
|
|
106
|
+
query: QueryParams;
|
|
107
|
+
/** Serialized request body (null for bodyless methods). */
|
|
108
|
+
body: BodyInit | null;
|
|
109
|
+
/** AbortSignal for cancellation. */
|
|
110
|
+
signal: AbortSignal | null;
|
|
111
|
+
/** Arbitrary per-request metadata. */
|
|
112
|
+
meta: Record<string, unknown>;
|
|
113
|
+
/** Preferred HTTP version. */
|
|
114
|
+
httpVersion?: HTTPVersion;
|
|
115
|
+
/** Request timeout in ms. */
|
|
116
|
+
timeout?: number;
|
|
117
|
+
/** Number of retry attempts. */
|
|
118
|
+
retries?: number;
|
|
119
|
+
/** Delay between retries in ms. */
|
|
120
|
+
retryDelay?: number;
|
|
121
|
+
/** HTTP status codes that trigger a retry. */
|
|
122
|
+
retryStatuses?: number[];
|
|
123
|
+
/** Whether to follow redirects. */
|
|
124
|
+
followRedirects?: boolean;
|
|
125
|
+
/** Maximum number of redirects. */
|
|
126
|
+
maxRedirects?: number;
|
|
127
|
+
/** Authentication configuration. */
|
|
128
|
+
auth?: AuthConfig;
|
|
129
|
+
/** Proxy configuration. */
|
|
130
|
+
proxy?: ProxyConfig;
|
|
131
|
+
/** Per-request cache configuration. */
|
|
132
|
+
cache?: CacheRequestConfig;
|
|
133
|
+
/** Maximum response body size in bytes. */
|
|
134
|
+
sizeLimit?: number;
|
|
135
|
+
/** Upload progress callback. */
|
|
136
|
+
onUploadProgress?: ProgressCallback;
|
|
137
|
+
/** Download progress callback. */
|
|
138
|
+
onDownloadProgress?: ProgressCallback;
|
|
139
|
+
/** Tags for grouping/categorization. */
|
|
140
|
+
tags?: string[];
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Parsed, typed HTTP response.
|
|
144
|
+
*
|
|
145
|
+
* @typeParam T - Parsed body type
|
|
146
|
+
*/
|
|
147
|
+
export interface KinetexResponse<T = unknown> {
|
|
148
|
+
/** HTTP status code. */
|
|
149
|
+
readonly status: number;
|
|
150
|
+
/** HTTP status text. */
|
|
151
|
+
readonly statusText: string;
|
|
152
|
+
/** Normalized response headers (lowercase keys). */
|
|
153
|
+
readonly headers: Record<string, string>;
|
|
154
|
+
/** Parsed response body. */
|
|
155
|
+
readonly data: T;
|
|
156
|
+
/** Raw response body bytes (before parsing). */
|
|
157
|
+
readonly rawBody: Uint8Array | null;
|
|
158
|
+
/** Final URL after redirects. */
|
|
159
|
+
readonly url: string;
|
|
160
|
+
/** Whether this response was served from cache. */
|
|
161
|
+
readonly cached: boolean;
|
|
162
|
+
/** Whether the request was redirected. */
|
|
163
|
+
readonly redirected: boolean;
|
|
164
|
+
/** HTTP version used. */
|
|
165
|
+
readonly httpVersion: HTTPVersion;
|
|
166
|
+
/** Total request duration in ms. */
|
|
167
|
+
readonly durationMs: number;
|
|
168
|
+
/** The originating request. */
|
|
169
|
+
readonly request: KinetexRequest;
|
|
170
|
+
/** Attempt number (1 = first try, 2+ = retries). */
|
|
171
|
+
readonly attempt: number;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Base error codes used by kinetex.
|
|
175
|
+
*
|
|
176
|
+
* - ENETWORK: Server/endpoint unreachable (not proxy-related)
|
|
177
|
+
* - EPROXY: Proxy configuration or proxy connection error
|
|
178
|
+
* - ETIMEOUT: Request/connection timeout
|
|
179
|
+
* - EABORT: Request cancelled by caller
|
|
180
|
+
* - EHTTPSTATUS: Server returned 4xx/5xx status
|
|
181
|
+
* - ESIZELIMIT: Response body exceeded size limit
|
|
182
|
+
* - EPARSE: Failed to parse response body
|
|
183
|
+
* - EVALIDATION: Invalid request configuration
|
|
184
|
+
* - EAUTH: Authentication failed
|
|
185
|
+
* - EREDIRECT: Redirect error (too many, invalid location, etc.)
|
|
186
|
+
* - EUNKNOWN: Unknown/unexpected error
|
|
187
|
+
*/
|
|
188
|
+
export type KinetexErrorCode = "ENETWORK" | "ETIMEOUT" | "EABORT" | "EHTTPSTATUS" | "ESIZELIMIT" | "EPARSE" | "EVALIDATION" | "EAUTH" | "EPROXY" | "EREDIRECT" | "EUNKNOWN";
|
|
189
|
+
/**
|
|
190
|
+
* Validate a string is a known `KinetexErrorCode`.
|
|
191
|
+
* @param code - The error code string to validate.
|
|
192
|
+
* @returns The validated code if valid, otherwise `undefined`.
|
|
193
|
+
*/
|
|
194
|
+
export declare function validateErrorCode(code: unknown): KinetexErrorCode | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* Base kinetex error.
|
|
197
|
+
*
|
|
198
|
+
* Note: The `code` parameter accepts any string at runtime. The `KinetexErrorCode`
|
|
199
|
+
* type is a TypeScript hint for valid codes, but no runtime validation is performed.
|
|
200
|
+
* This is intentional for flexibility. For stricter validation, use `validateErrorCode()`.
|
|
201
|
+
*/
|
|
202
|
+
export declare class KinetexError extends Error {
|
|
203
|
+
/** Machine-readable error code. */
|
|
204
|
+
readonly code: KinetexErrorCode;
|
|
205
|
+
/** Original request, if available. */
|
|
206
|
+
readonly request?: KinetexRequest;
|
|
207
|
+
/** Response, if available (for HTTP-level errors). */
|
|
208
|
+
readonly response?: KinetexResponse<unknown>;
|
|
209
|
+
/** Underlying cause. */
|
|
210
|
+
readonly cause?: unknown;
|
|
211
|
+
/**
|
|
212
|
+
* @param message - Human-readable error description.
|
|
213
|
+
* @param code - Machine-readable error code.
|
|
214
|
+
* @param options - Additional context (request, response, cause).
|
|
215
|
+
*/
|
|
216
|
+
constructor(message: string, code: KinetexErrorCode, options?: {
|
|
217
|
+
request?: KinetexRequest;
|
|
218
|
+
response?: KinetexResponse<unknown>;
|
|
219
|
+
cause?: unknown;
|
|
220
|
+
});
|
|
221
|
+
/** True if the error is a network/transport failure (not proxy-related). */
|
|
222
|
+
get isNetwork(): boolean;
|
|
223
|
+
/** True if the error is a proxy-related failure. */
|
|
224
|
+
get isProxy(): boolean;
|
|
225
|
+
/** True if the error is a timeout. */
|
|
226
|
+
get isTimeout(): boolean;
|
|
227
|
+
/** True if the request was aborted by the caller. */
|
|
228
|
+
get isAbort(): boolean;
|
|
229
|
+
/** True if the server returned a 4xx or 5xx status. */
|
|
230
|
+
get isHTTPError(): boolean;
|
|
231
|
+
/** HTTP status code shorthand (null for non-HTTP errors). */
|
|
232
|
+
get status(): number | null;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Thrown when the response body exceeds the configured size limit.
|
|
236
|
+
* @param bytesRead - Number of bytes read before exceeding the limit.
|
|
237
|
+
* @param limit - Configured size limit in bytes.
|
|
238
|
+
* @param request - The request that triggered the error.
|
|
239
|
+
*/
|
|
240
|
+
export declare class SizeLimitError extends KinetexError {
|
|
241
|
+
/** Number of bytes read before exceeding the limit. */
|
|
242
|
+
readonly bytesRead: number;
|
|
243
|
+
/** Configured size limit in bytes. */
|
|
244
|
+
readonly limit: number;
|
|
245
|
+
constructor(
|
|
246
|
+
/** Number of bytes read before exceeding the limit. */
|
|
247
|
+
bytesRead: number,
|
|
248
|
+
/** Configured size limit in bytes. */
|
|
249
|
+
limit: number, request?: KinetexRequest);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Thrown when the server returns a 4xx or 5xx status.
|
|
253
|
+
* @param response - The HTTP response that caused the error.
|
|
254
|
+
* @param request - The request that produced the response.
|
|
255
|
+
*/
|
|
256
|
+
export declare class HTTPStatusError extends KinetexError {
|
|
257
|
+
constructor(response: KinetexResponse<unknown>, request: KinetexRequest);
|
|
258
|
+
/** True if the status is a 4xx client error. */
|
|
259
|
+
get isClientError(): boolean;
|
|
260
|
+
/** True if the status is a 5xx server error. */
|
|
261
|
+
get isServerError(): boolean;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Thrown when a request is aborted.
|
|
265
|
+
* @param request - The request that was aborted, if available.
|
|
266
|
+
*/
|
|
267
|
+
export declare class AbortError extends KinetexError {
|
|
268
|
+
constructor(request?: KinetexRequest);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Thrown when a network error occurs.
|
|
272
|
+
* @param message - Human-readable error description.
|
|
273
|
+
* @param request - The request that triggered the error, if available.
|
|
274
|
+
*/
|
|
275
|
+
export declare class NetworkError extends KinetexError {
|
|
276
|
+
constructor(message: string, request?: KinetexRequest);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Thrown when a validation error occurs.
|
|
280
|
+
* @param message - Description of the validation failure.
|
|
281
|
+
* @param request - The invalid request, if available.
|
|
282
|
+
*/
|
|
283
|
+
export declare class ValidationError extends KinetexError {
|
|
284
|
+
constructor(message: string, request?: KinetexRequest);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Thrown when an authentication error occurs.
|
|
288
|
+
* @param message - Description of the authentication failure.
|
|
289
|
+
* @param request - The request that failed authentication, if available.
|
|
290
|
+
*/
|
|
291
|
+
export declare class AuthError extends KinetexError {
|
|
292
|
+
constructor(message: string, request?: KinetexRequest);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Thrown when a proxy error occurs.
|
|
296
|
+
* @param message - Description of the proxy failure.
|
|
297
|
+
* @param request - The request that triggered the proxy error, if available.
|
|
298
|
+
*/
|
|
299
|
+
export declare class ProxyError extends KinetexError {
|
|
300
|
+
constructor(message: string, request?: KinetexRequest);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Thrown when a redirect error occurs.
|
|
304
|
+
* @param message - Description of the redirect failure.
|
|
305
|
+
* @param request - The request that encountered the redirect error, if available.
|
|
306
|
+
*/
|
|
307
|
+
export declare class RedirectError extends KinetexError {
|
|
308
|
+
constructor(message: string, request?: KinetexRequest);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Thrown when a request times out.
|
|
312
|
+
* @param timeoutMs - The timeout duration in milliseconds.
|
|
313
|
+
* @param request - The request that timed out, if available.
|
|
314
|
+
*/
|
|
315
|
+
export declare class TimeoutError extends KinetexError {
|
|
316
|
+
/** The timeout duration in milliseconds. */
|
|
317
|
+
readonly timeoutMs: number;
|
|
318
|
+
constructor(
|
|
319
|
+
/** The timeout duration in milliseconds. */
|
|
320
|
+
timeoutMs: number, request?: KinetexRequest);
|
|
321
|
+
}
|
|
322
|
+
/** Authentication configuration. */
|
|
323
|
+
export type AuthConfig = {
|
|
324
|
+
/** Bearer authentication. */
|
|
325
|
+
type: "bearer";
|
|
326
|
+
/** Token value or a factory function that returns one. */
|
|
327
|
+
token: string | (() => string | Promise<string>);
|
|
328
|
+
} | {
|
|
329
|
+
/** HTTP Basic authentication. */
|
|
330
|
+
type: "basic";
|
|
331
|
+
/** Username for basic auth. */
|
|
332
|
+
username: string;
|
|
333
|
+
/** Password for basic auth. */
|
|
334
|
+
password: string;
|
|
335
|
+
} | {
|
|
336
|
+
/** API key sent via a custom header. */
|
|
337
|
+
type: "apikey";
|
|
338
|
+
/** HTTP header name carrying the key. */
|
|
339
|
+
header: string;
|
|
340
|
+
/** API key value or a factory function that returns one. */
|
|
341
|
+
key: string | (() => string | Promise<string>);
|
|
342
|
+
} | {
|
|
343
|
+
/** HTTP Digest authentication. */
|
|
344
|
+
type: "digest";
|
|
345
|
+
/** Username for digest auth. */
|
|
346
|
+
username: string;
|
|
347
|
+
/** Password for digest auth. */
|
|
348
|
+
password: string;
|
|
349
|
+
} | {
|
|
350
|
+
/** Custom authentication handler. */
|
|
351
|
+
type: "custom";
|
|
352
|
+
/**
|
|
353
|
+
* Custom authentication transform.
|
|
354
|
+
* @returns Modified request, or void to leave request unchanged.
|
|
355
|
+
*/
|
|
356
|
+
apply: (req: KinetexRequest) => KinetexRequest | Promise<KinetexRequest> | void;
|
|
357
|
+
};
|
|
358
|
+
/** Proxy configuration. */
|
|
359
|
+
export interface ProxyConfig {
|
|
360
|
+
/** Proxy URL: http://, https://, socks5://, socks5h:// */
|
|
361
|
+
url: string;
|
|
362
|
+
/** Username for proxy authentication. */
|
|
363
|
+
username?: string;
|
|
364
|
+
/** Password for proxy authentication. */
|
|
365
|
+
password?: string;
|
|
366
|
+
}
|
|
367
|
+
/** Retry policy configuration. */
|
|
368
|
+
export interface RetryConfig {
|
|
369
|
+
/** Maximum number of retry attempts. Default: 3 */
|
|
370
|
+
maxRetries: number;
|
|
371
|
+
/** Base delay in ms (exponential back-off base). Default: 300 */
|
|
372
|
+
baseDelayMs: number;
|
|
373
|
+
/** Maximum delay cap in ms. Default: 30_000 */
|
|
374
|
+
maxDelayMs: number;
|
|
375
|
+
/** Jitter factor 0–1. Default: 0.3 */
|
|
376
|
+
jitter: number;
|
|
377
|
+
/** HTTP status codes that trigger a retry. Default: [408,429,500,502,503,504] */
|
|
378
|
+
statuses: number[];
|
|
379
|
+
/** Retry on network errors (ENETWORK). Default: true */
|
|
380
|
+
onNetworkError: boolean;
|
|
381
|
+
/**
|
|
382
|
+
* Retry on timeout errors (ETIMEOUT). Default: false
|
|
383
|
+
*
|
|
384
|
+
* Timeouts indicate the server is genuinely slow for this request.
|
|
385
|
+
* Retrying will usually just time out again and burn the full
|
|
386
|
+
* `timeout × maxRetries` budget. Set to `true` only if your server
|
|
387
|
+
* has known transient latency spikes you want to ride out.
|
|
388
|
+
*/
|
|
389
|
+
onTimeout: boolean;
|
|
390
|
+
/** HTTP methods eligible for retry. Default: ["GET","HEAD","PUT","DELETE","OPTIONS","TRACE"] */
|
|
391
|
+
methods: HTTPMethod[];
|
|
392
|
+
/**
|
|
393
|
+
* Custom predicate — overrides statuses + onNetworkError when provided.
|
|
394
|
+
* @param context - Retry context with request, response, error, and attempt info.
|
|
395
|
+
*/
|
|
396
|
+
shouldRetry?: (context: RetryContext) => boolean | Promise<boolean>;
|
|
397
|
+
/** Called before each retry with the upcoming delay. */
|
|
398
|
+
onRetry?: (context: RetryContext, delayMs: number) => void | Promise<void>;
|
|
399
|
+
}
|
|
400
|
+
/** Context passed to retry callbacks. */
|
|
401
|
+
export interface RetryContext {
|
|
402
|
+
/** The original request being retried. */
|
|
403
|
+
request: KinetexRequest;
|
|
404
|
+
/** The response from the failed attempt (null if no response). */
|
|
405
|
+
response: KinetexResponse<unknown> | null;
|
|
406
|
+
/** The error that triggered the retry. */
|
|
407
|
+
error: unknown;
|
|
408
|
+
/** Current attempt number (1 = first try). */
|
|
409
|
+
attempt: number;
|
|
410
|
+
/** Maximum number of retry attempts configured. */
|
|
411
|
+
maxRetries: number;
|
|
412
|
+
}
|
|
413
|
+
/** Per-request cache configuration. */
|
|
414
|
+
export interface CacheRequestConfig {
|
|
415
|
+
/** If false, bypass cache entirely. Default: true */
|
|
416
|
+
enabled?: boolean;
|
|
417
|
+
/** Override TTL in ms for this specific request. */
|
|
418
|
+
ttlMs?: number;
|
|
419
|
+
/** Cache tags for targeted invalidation. */
|
|
420
|
+
tags?: string[];
|
|
421
|
+
/** If true, force a fresh fetch and update the cache. */
|
|
422
|
+
forceRefresh?: boolean;
|
|
423
|
+
}
|
|
424
|
+
/** Progress event snapshot. */
|
|
425
|
+
export interface ProgressEvent {
|
|
426
|
+
/** Bytes transferred so far. */
|
|
427
|
+
loaded: number;
|
|
428
|
+
/** Total bytes (null if unknown). */
|
|
429
|
+
total: number | null;
|
|
430
|
+
/** Completion percentage 0–100, null if total unknown. */
|
|
431
|
+
percent: number | null;
|
|
432
|
+
/** Transfer rate in bytes/sec (EMA-smoothed). */
|
|
433
|
+
rate: number;
|
|
434
|
+
/** ETA in ms (null if unavailable). */
|
|
435
|
+
eta: number | null;
|
|
436
|
+
/** Elapsed ms since transfer started. */
|
|
437
|
+
elapsed: number;
|
|
438
|
+
/** Whether the transfer is complete. */
|
|
439
|
+
done: boolean;
|
|
440
|
+
}
|
|
441
|
+
/** Progress callback. */
|
|
442
|
+
export type ProgressCallback = (event: ProgressEvent) => void;
|
|
443
|
+
/**
|
|
444
|
+
* An interceptor that can modify, replace, or short-circuit a request.
|
|
445
|
+
*
|
|
446
|
+
* Return values:
|
|
447
|
+
* - `void` / `undefined` — pass through (possibly mutated ctx.request)
|
|
448
|
+
* - `KinetexRequest` — replace the request
|
|
449
|
+
* - `KinetexResponse` — short-circuit; skip the actual fetch
|
|
450
|
+
* - throw — abort the pipeline with that error
|
|
451
|
+
*/
|
|
452
|
+
export type RequestInterceptor = (ctx: InterceptorContext) => void | KinetexRequest | KinetexResponse<unknown> | Promise<void | KinetexRequest | KinetexResponse<unknown>>;
|
|
453
|
+
/**
|
|
454
|
+
* An interceptor that can modify or replace a response, or trigger a retry.
|
|
455
|
+
*
|
|
456
|
+
* Return values:
|
|
457
|
+
* - `void` / `undefined` — pass through
|
|
458
|
+
* - `KinetexResponse` — replace the response
|
|
459
|
+
* - `KinetexRequest` — retry (re-run from dispatch)
|
|
460
|
+
* - throw — convert to error pipeline
|
|
461
|
+
*/
|
|
462
|
+
export type ResponseInterceptor = (ctx: InterceptorContext) => void | KinetexResponse<unknown> | KinetexRequest | Promise<void | KinetexResponse<unknown> | KinetexRequest>;
|
|
463
|
+
/**
|
|
464
|
+
* An interceptor that can recover from errors.
|
|
465
|
+
*
|
|
466
|
+
* Return values:
|
|
467
|
+
* - `void` — rethrow the error
|
|
468
|
+
* - `KinetexResponse` — recover with a synthetic response
|
|
469
|
+
* - throw — replace with a different error
|
|
470
|
+
*/
|
|
471
|
+
export type ErrorInterceptor = (ctx: InterceptorContext) => void | KinetexResponse<unknown> | Promise<void | KinetexResponse<unknown>>;
|
|
472
|
+
/** Context threaded through the interceptor pipeline. */
|
|
473
|
+
export interface InterceptorContext {
|
|
474
|
+
/** Mutable request — modify in place or return a new one. */
|
|
475
|
+
request: KinetexRequest;
|
|
476
|
+
/** Set once the response is received. */
|
|
477
|
+
response: KinetexResponse<unknown> | null;
|
|
478
|
+
/** Set when an error occurs. */
|
|
479
|
+
error: unknown | null;
|
|
480
|
+
/** Monotonic start time (ms). */
|
|
481
|
+
startedAt: number;
|
|
482
|
+
/** Current attempt number. */
|
|
483
|
+
attempt: number;
|
|
484
|
+
/** Whether the pipeline has been aborted. */
|
|
485
|
+
aborted: boolean;
|
|
486
|
+
/** Arbitrary pipeline-scoped storage. */
|
|
487
|
+
store: Map<symbol | string, unknown>;
|
|
488
|
+
}
|
|
489
|
+
/** All lifecycle hook registrations. */
|
|
490
|
+
export interface LifecycleHooks {
|
|
491
|
+
/** Before the request is sent. */
|
|
492
|
+
onBeforeRequest?: Array<(req: KinetexRequest, ctx: HookContext) => KinetexRequest | void | Promise<KinetexRequest | void>>;
|
|
493
|
+
/** After the request is sent (before response is processed). */
|
|
494
|
+
onAfterRequest?: Array<(req: KinetexRequest, ctx: HookContext) => void | Promise<void>>;
|
|
495
|
+
/** Before the response is returned to the caller. */
|
|
496
|
+
onBeforeResponse?: Array<(res: KinetexResponse<unknown>, ctx: HookContext) => KinetexResponse<unknown> | void | Promise<KinetexResponse<unknown> | void>>;
|
|
497
|
+
/** After the response is returned. */
|
|
498
|
+
onAfterResponse?: Array<(res: KinetexResponse<unknown>, ctx: HookContext) => void | Promise<void>>;
|
|
499
|
+
/** On error (may return a recovery response). */
|
|
500
|
+
onError?: Array<(err: unknown, ctx: HookContext) => KinetexResponse<unknown> | void | Promise<KinetexResponse<unknown> | void>>;
|
|
501
|
+
/** Before a retry attempt. */
|
|
502
|
+
onRetry?: Array<(ctx: RetryContext) => void | Promise<void>>;
|
|
503
|
+
/** On upload progress. */
|
|
504
|
+
onUploadProgress?: Array<ProgressCallback>;
|
|
505
|
+
/** On download progress. */
|
|
506
|
+
onDownloadProgress?: Array<ProgressCallback>;
|
|
507
|
+
}
|
|
508
|
+
/** Context available to all lifecycle hooks. */
|
|
509
|
+
export interface HookContext {
|
|
510
|
+
/** The request being processed. */
|
|
511
|
+
request: KinetexRequest;
|
|
512
|
+
/** The response received (null before dispatch or on error). */
|
|
513
|
+
response: KinetexResponse<unknown> | null;
|
|
514
|
+
/** The error that occurred (null on success). */
|
|
515
|
+
error: unknown | null;
|
|
516
|
+
/** Monotonic start time in ms. */
|
|
517
|
+
startedAt: number;
|
|
518
|
+
/** Current attempt number (1 = first try). */
|
|
519
|
+
attempt: number;
|
|
520
|
+
/** Arbitrary hook-scoped metadata. */
|
|
521
|
+
meta: Record<string, unknown>;
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Global configuration for a `Kinetex` client instance.
|
|
525
|
+
*/
|
|
526
|
+
export interface KinetexConfig {
|
|
527
|
+
/**
|
|
528
|
+
* Base URL prepended to all relative request URLs.
|
|
529
|
+
* @example "https://api.example.com/v1"
|
|
530
|
+
*/
|
|
531
|
+
baseURL?: string;
|
|
532
|
+
/** Default headers sent with every request. */
|
|
533
|
+
headers?: HeadersInit;
|
|
534
|
+
/** Default query parameters appended to every request URL. */
|
|
535
|
+
params?: QueryParams;
|
|
536
|
+
/** Default request timeout in ms. 0 = no timeout. Default: 30_000 */
|
|
537
|
+
timeout?: number;
|
|
538
|
+
/** Default retry policy. */
|
|
539
|
+
retry?: Partial<RetryConfig>;
|
|
540
|
+
/** Default authentication. */
|
|
541
|
+
auth?: AuthConfig;
|
|
542
|
+
/** Proxy configuration. */
|
|
543
|
+
proxy?: ProxyConfig;
|
|
544
|
+
/** HTTP cache configuration. */
|
|
545
|
+
cache?: import("./cache.ts").CacheConfig;
|
|
546
|
+
/** Logging configuration. */
|
|
547
|
+
logger?: import("./logging.ts").LoggerConfig | false;
|
|
548
|
+
/** Whether to throw on HTTP error status codes (4xx/5xx). Default: true */
|
|
549
|
+
throwOnError?: boolean;
|
|
550
|
+
/** Maximum response body size in bytes. 0 = no limit. Default: 0 */
|
|
551
|
+
maxResponseSize?: number;
|
|
552
|
+
/** Maximum request body size in bytes. 0 = no limit. Default: 0 */
|
|
553
|
+
maxRequestSize?: number;
|
|
554
|
+
/** Follow redirects. Default: true */
|
|
555
|
+
followRedirects?: boolean;
|
|
556
|
+
/** Maximum number of redirects to follow. Default: 10 */
|
|
557
|
+
maxRedirects?: number;
|
|
558
|
+
/** Enforce HTTPS-only requests. Rejects HTTP URLs. Default: false */
|
|
559
|
+
httpsOnly?: boolean;
|
|
560
|
+
/** Preferred HTTP version. Default: "HTTP/2" */
|
|
561
|
+
httpVersion?: HTTPVersion;
|
|
562
|
+
/** Custom fetch implementation (useful for testing). */
|
|
563
|
+
fetch?: typeof globalThis.fetch;
|
|
564
|
+
/** Interceptors registered at construction time. */
|
|
565
|
+
interceptors?: {
|
|
566
|
+
/** Request interceptors run before the request is sent. */
|
|
567
|
+
request?: RequestInterceptor[];
|
|
568
|
+
/** Response interceptors run after the response is received. */
|
|
569
|
+
response?: ResponseInterceptor[];
|
|
570
|
+
/** Error interceptors run when an error occurs in the pipeline. */
|
|
571
|
+
error?: ErrorInterceptor[];
|
|
572
|
+
};
|
|
573
|
+
/** Lifecycle hooks registered at construction time. */
|
|
574
|
+
hooks?: LifecycleHooks;
|
|
575
|
+
/**
|
|
576
|
+
* Cookie jar instance. Pass `true` to auto-create one,
|
|
577
|
+
* or a pre-existing instance.
|
|
578
|
+
*/
|
|
579
|
+
cookieJar?: import("./cookiejar.ts").CookieJar | boolean;
|
|
580
|
+
/** HAR recording. Pass `true` to enable with defaults. */
|
|
581
|
+
har?: boolean;
|
|
582
|
+
/** Global response transform applied after parsing. */
|
|
583
|
+
transformResponse?: <T>(data: unknown, response: KinetexResponse<unknown>) => T;
|
|
584
|
+
/** Global request transform applied before sending. */
|
|
585
|
+
transformRequest?: (req: KinetexRequest) => KinetexRequest | Promise<KinetexRequest>;
|
|
586
|
+
/**
|
|
587
|
+
* Enforce strict header validation.
|
|
588
|
+
*
|
|
589
|
+
* When `true`, requests with invalid header names or values throw a
|
|
590
|
+
* `KinetexError` with code `"EVALIDATION"` instead of silently dropping
|
|
591
|
+
* the offending header. Recommended for production services where silent
|
|
592
|
+
* data loss is unacceptable.
|
|
593
|
+
*
|
|
594
|
+
* Default: `false` — emit `console.warn` and drop.
|
|
595
|
+
*
|
|
596
|
+
* @see Architecture Risk C: Silent Header Dropping
|
|
597
|
+
*/
|
|
598
|
+
strictHeaders?: boolean;
|
|
599
|
+
/**
|
|
600
|
+
* Built-in token-bucket rate limiter applied to all outgoing requests.
|
|
601
|
+
*
|
|
602
|
+
* Registered automatically as the highest-priority request interceptor
|
|
603
|
+
* so it runs before auth, cache, and dedup.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```ts
|
|
607
|
+
* const client = kinetex({
|
|
608
|
+
* rateLimit: { limit: 100, windowMs: 60_000 }, // 100 req/min
|
|
609
|
+
* });
|
|
610
|
+
* ```
|
|
611
|
+
*
|
|
612
|
+
* @see Enterprise Hardening #6: Centralized Rate Limit Enforcer
|
|
613
|
+
*/
|
|
614
|
+
rateLimit?: {
|
|
615
|
+
/** Maximum requests per window. Default: 60 */
|
|
616
|
+
limit?: number;
|
|
617
|
+
/** Window size in ms. Default: 60_000 (1 minute) */
|
|
618
|
+
windowMs?: number;
|
|
619
|
+
/** If `true`, queue excess requests; if `false`, reject immediately. Default: true */
|
|
620
|
+
queue?: boolean;
|
|
621
|
+
/** Max number of queued requests before rejecting. Default: 100 */
|
|
622
|
+
maxQueue?: number;
|
|
623
|
+
};
|
|
624
|
+
/**
|
|
625
|
+
* Default WebSocket client configuration inherited by `client.ws()`.
|
|
626
|
+
* These values are merged with per-call options.
|
|
627
|
+
*/
|
|
628
|
+
ws?: {
|
|
629
|
+
/** High-water mark in bytes for backpressure. Default: 65536 */
|
|
630
|
+
highWaterMark?: number;
|
|
631
|
+
/** Low-water mark in bytes for backpressure release. Default: 16384 */
|
|
632
|
+
lowWaterMark?: number;
|
|
633
|
+
/** Max outbound messages per second (0 = unlimited). Default: 0 */
|
|
634
|
+
maxSendRate?: number;
|
|
635
|
+
/** Automatically re-join subscribed rooms after reconnect. Default: true */
|
|
636
|
+
keepRooms?: boolean;
|
|
637
|
+
};
|
|
638
|
+
/**
|
|
639
|
+
* Custom function to compute the circuit-breaker key for a request.
|
|
640
|
+
*
|
|
641
|
+
* Default behavior: key = request origin (`https://api.example.com`).
|
|
642
|
+
* Override to scope breakers per-method, per-route, or per-tenant.
|
|
643
|
+
*
|
|
644
|
+
* @example Per-method isolation:
|
|
645
|
+
* ```ts
|
|
646
|
+
* const client = kinetex({
|
|
647
|
+
* circuitBreakerKeyFn: (req) =>
|
|
648
|
+
* `${new URL(req.url).origin}:${req.method}`,
|
|
649
|
+
* });
|
|
650
|
+
* ```
|
|
651
|
+
*
|
|
652
|
+
* @see Enterprise Hardening #2: Circuit Breaker Per-Method
|
|
653
|
+
*/
|
|
654
|
+
circuitBreakerKeyFn?: (req: KinetexRequest) => string;
|
|
655
|
+
/**
|
|
656
|
+
* AWS SigV4 request signing applied automatically to every outgoing request.
|
|
657
|
+
*
|
|
658
|
+
* When set, a `SigV4Signer` is registered as a request interceptor that
|
|
659
|
+
* signs headers (Authorization, x-amz-date, x-amz-content-sha256) before
|
|
660
|
+
* the request is sent. Compatible with all AWS services and API Gateway.
|
|
661
|
+
*
|
|
662
|
+
* @example
|
|
663
|
+
* ```ts
|
|
664
|
+
* const client = kinetex({
|
|
665
|
+
* baseURL: "https://execute-api.us-east-1.amazonaws.com",
|
|
666
|
+
* awsSigning: {
|
|
667
|
+
* credentials: { accessKeyId: "...", secretAccessKey: "..." },
|
|
668
|
+
* region: "us-east-1",
|
|
669
|
+
* service: "execute-api",
|
|
670
|
+
* },
|
|
671
|
+
* });
|
|
672
|
+
* ```
|
|
673
|
+
*
|
|
674
|
+
* @see Enterprise Hardening #3: Optional Request Signing
|
|
675
|
+
*/
|
|
676
|
+
awsSigning?: import("./aws-sigv4.ts").SigningConfig;
|
|
677
|
+
/**
|
|
678
|
+
* Pipeline trace callback — called at the start and end of each pipeline
|
|
679
|
+
* stage for every request.
|
|
680
|
+
*
|
|
681
|
+
* Use this for detailed observability without adding interceptors:
|
|
682
|
+
* structured logging, distributed tracing, or performance profiling.
|
|
683
|
+
*
|
|
684
|
+
* The callback is synchronous and must not throw.
|
|
685
|
+
*
|
|
686
|
+
* @example
|
|
687
|
+
* ```ts
|
|
688
|
+
* const client = kinetex({
|
|
689
|
+
* onPipelineTrace: (step) => {
|
|
690
|
+
* tracer.addEvent(step.stage, { elapsedMs: step.elapsedMs });
|
|
691
|
+
* },
|
|
692
|
+
* });
|
|
693
|
+
* ```
|
|
694
|
+
*
|
|
695
|
+
* @see Architecture Risk A: Unclear Responsibility Separation
|
|
696
|
+
*/
|
|
697
|
+
onPipelineTrace?: (step: PipelineStep) => void;
|
|
698
|
+
/**
|
|
699
|
+
* Called when a background stale-while-revalidate (SWR) fetch fails.
|
|
700
|
+
*
|
|
701
|
+
* Without this callback, SWR revalidation errors are silently swallowed and
|
|
702
|
+
* stale data continues to be served. Use this hook to monitor revalidation
|
|
703
|
+
* health, increment error counters, or trigger alerts.
|
|
704
|
+
*
|
|
705
|
+
* @param error - The error that caused the revalidation to fail.
|
|
706
|
+
* @param req - The request that was being revalidated.
|
|
707
|
+
*
|
|
708
|
+
* @example
|
|
709
|
+
* ```ts
|
|
710
|
+
* const client = kinetex({
|
|
711
|
+
* cache: { ... },
|
|
712
|
+
* onSWRError: (err, req) => {
|
|
713
|
+
* metrics.increment("cache.swr_error");
|
|
714
|
+
* logger.warn("SWR revalidation failed", { url: req.url, error: err });
|
|
715
|
+
* },
|
|
716
|
+
* });
|
|
717
|
+
* ```
|
|
718
|
+
*/
|
|
719
|
+
onSWRError?: (error: unknown, req: KinetexRequest) => void;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Names of the distinct processing stages in the kinetex request pipeline.
|
|
723
|
+
*
|
|
724
|
+
* **Execution order:**
|
|
725
|
+
* 1. `request_interceptors` — all registered request interceptors run in priority order
|
|
726
|
+
* 2. `lifecycle_before` — `onBeforeRequest` lifecycle hooks
|
|
727
|
+
* 3. `auth` — authentication header injection (bearer, basic, api-key, custom)
|
|
728
|
+
* 4. `cache_lookup` — cache read (short-circuits if HIT)
|
|
729
|
+
* 5. `transport_send` — actual HTTP send (fetch / HTTP2 / SOCKS5)
|
|
730
|
+
* 6. `response_decompression` — content-encoding decompression (gzip, deflate, br)
|
|
731
|
+
* 7. `response_parse` — body parsing (JSON, text, binary)
|
|
732
|
+
* 8. `cache_store` — cache write for cacheable responses
|
|
733
|
+
* 9. `response_interceptors` — all registered response interceptors
|
|
734
|
+
* 10. `lifecycle_after` — `onAfterResponse` lifecycle hooks
|
|
735
|
+
* 11. `retry` — retry decision + delay (only on failure)
|
|
736
|
+
* 12. `error_interceptors` — all registered error interceptors (only on error)
|
|
737
|
+
*/
|
|
738
|
+
export type PipelineStageName = "request_interceptors" | "lifecycle_before" | "auth" | "cache_lookup" | "transport_send" | "response_decompression" | "response_parse" | "cache_store" | "response_interceptors" | "lifecycle_after" | "retry" | "error_interceptors";
|
|
739
|
+
/**
|
|
740
|
+
* A single pipeline trace event emitted via `KinetexConfig.onPipelineTrace`.
|
|
741
|
+
*
|
|
742
|
+
* @example
|
|
743
|
+
* ```ts
|
|
744
|
+
* const client = kinetex({
|
|
745
|
+
* onPipelineTrace: (step) => {
|
|
746
|
+
* console.log(`[${step.stage}] ${step.requestId} +${step.elapsedMs}ms`);
|
|
747
|
+
* },
|
|
748
|
+
* });
|
|
749
|
+
* ```
|
|
750
|
+
*/
|
|
751
|
+
export interface PipelineStep {
|
|
752
|
+
/** Unique identifier for this request (propagated across retries). */
|
|
753
|
+
requestId: RequestId;
|
|
754
|
+
/** The pipeline stage that just completed (or errored). */
|
|
755
|
+
stage: PipelineStageName;
|
|
756
|
+
/** Elapsed ms since the request started (not since the previous step). */
|
|
757
|
+
elapsedMs: number;
|
|
758
|
+
/** Current retry attempt number (1 = first attempt). */
|
|
759
|
+
attempt: number;
|
|
760
|
+
/** `"start"` when entering a stage, `"end"` when leaving. */
|
|
761
|
+
event: "start" | "end";
|
|
762
|
+
/**
|
|
763
|
+
* Set when this step produced or consumed a cache result.
|
|
764
|
+
* `"hit"` = served from cache, `"miss"` = not in cache, `"store"` = written to cache.
|
|
765
|
+
*/
|
|
766
|
+
cacheStatus?: "hit" | "miss" | "store";
|
|
767
|
+
/** Set when the stage ended with an error. */
|
|
768
|
+
error?: unknown;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Options passed to individual request methods — override instance defaults.
|
|
772
|
+
*
|
|
773
|
+
* @typeParam T - Expected parsed response body type.
|
|
774
|
+
*/
|
|
775
|
+
export interface SendOptions<T = unknown> {
|
|
776
|
+
/** Override base URL for this request. */
|
|
777
|
+
baseURL?: string;
|
|
778
|
+
/** Additional / override headers. */
|
|
779
|
+
headers?: HeadersInit;
|
|
780
|
+
/** Query parameters (merged with instance defaults). */
|
|
781
|
+
params?: QueryParams;
|
|
782
|
+
/** Request body. */
|
|
783
|
+
body?: BodyInit;
|
|
784
|
+
/** Override timeout in ms. */
|
|
785
|
+
timeout?: number;
|
|
786
|
+
/** Override retry config. */
|
|
787
|
+
retry?: Partial<RetryConfig> | false;
|
|
788
|
+
/** Override auth. */
|
|
789
|
+
auth?: AuthConfig | false;
|
|
790
|
+
/** Override proxy. */
|
|
791
|
+
proxy?: ProxyConfig | false;
|
|
792
|
+
/** Maximum request body size in bytes. 0 = no limit. */
|
|
793
|
+
maxRequestSize?: number;
|
|
794
|
+
/** AbortSignal. */
|
|
795
|
+
signal?: AbortSignal;
|
|
796
|
+
/** Per-request cache config. */
|
|
797
|
+
cache?: CacheRequestConfig | false;
|
|
798
|
+
/** Response size limit in bytes. */
|
|
799
|
+
maxResponseSize?: number;
|
|
800
|
+
/** Whether to throw on HTTP 4xx/5xx. */
|
|
801
|
+
throwOnError?: boolean;
|
|
802
|
+
/** Whether to follow redirects. */
|
|
803
|
+
followRedirects?: boolean;
|
|
804
|
+
/** Max redirects. */
|
|
805
|
+
maxRedirects?: number;
|
|
806
|
+
/** Preferred HTTP version. */
|
|
807
|
+
httpVersion?: HTTPVersion;
|
|
808
|
+
/** Upload progress callback. */
|
|
809
|
+
onUploadProgress?: ProgressCallback;
|
|
810
|
+
/** Download progress callback. */
|
|
811
|
+
onDownloadProgress?: ProgressCallback;
|
|
812
|
+
/** Cache tags for invalidation. */
|
|
813
|
+
tags?: string[];
|
|
814
|
+
/** Response body parser. Defaults to JSON for application/json, text otherwise. */
|
|
815
|
+
parseResponse?: (raw: Uint8Array, headers: Record<string, string>, url: string) => T | Promise<T>;
|
|
816
|
+
/**
|
|
817
|
+
* Called when the default body parser silently falls back from JSON to raw
|
|
818
|
+
* text due to a parse error.
|
|
819
|
+
*
|
|
820
|
+
* This makes implicit type-conversion failures visible without requiring a
|
|
821
|
+
* custom `parseResponse`. Useful for monitoring and debugging content-type
|
|
822
|
+
* mismatches in production.
|
|
823
|
+
*
|
|
824
|
+
* @param raw - The raw response body bytes.
|
|
825
|
+
* @param error - The parse error that caused the fallback.
|
|
826
|
+
*
|
|
827
|
+
* @example
|
|
828
|
+
* ```ts
|
|
829
|
+
* const res = await client.get<User>("/profile", {
|
|
830
|
+
* parseFailure: (raw, err) => {
|
|
831
|
+
* logger.warn("JSON parse failed, got text instead", { error: err.message });
|
|
832
|
+
* },
|
|
833
|
+
* });
|
|
834
|
+
* ```
|
|
835
|
+
*/
|
|
836
|
+
parseFailure?: (raw: Uint8Array, error: Error) => void;
|
|
837
|
+
/**
|
|
838
|
+
* Callback invoked on successful response.
|
|
839
|
+
*
|
|
840
|
+
* Alternative to the promise return pattern. If both `onSuccess` and the
|
|
841
|
+
* promise are used, the callback is invoked first, then the promise resolves.
|
|
842
|
+
*
|
|
843
|
+
* @example
|
|
844
|
+
* ```ts
|
|
845
|
+
* client.get("/data", {
|
|
846
|
+
* onSuccess: (res) => console.log("Got:", res.data),
|
|
847
|
+
* onError: (err) => console.error("Failed:", err.message),
|
|
848
|
+
* });
|
|
849
|
+
* ```
|
|
850
|
+
*/
|
|
851
|
+
onSuccess?: (res: KinetexResponse<T>) => void;
|
|
852
|
+
/**
|
|
853
|
+
* Callback invoked on request/response error.
|
|
854
|
+
*
|
|
855
|
+
* Called for both network errors and HTTP error responses (4xx/5xx when
|
|
856
|
+
* `throwOnError` is true). Use this for callback-style error handling
|
|
857
|
+
* without try/catch.
|
|
858
|
+
*/
|
|
859
|
+
onError?: (err: KinetexError) => void;
|
|
860
|
+
/** Arbitrary metadata attached to the request. */
|
|
861
|
+
meta?: Record<string, unknown>;
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* A single HAR log entry conforming to the HTTP Archive specification.
|
|
865
|
+
*/
|
|
866
|
+
export interface HAREntry {
|
|
867
|
+
/** ISO 8601 timestamp of the request start. */
|
|
868
|
+
startedDateTime: string;
|
|
869
|
+
/** Total elapsed time in ms. */
|
|
870
|
+
time: number;
|
|
871
|
+
/** Serialized request data. */
|
|
872
|
+
request: {
|
|
873
|
+
/** HTTP method. */
|
|
874
|
+
method: string;
|
|
875
|
+
/** Request URL. */
|
|
876
|
+
url: string;
|
|
877
|
+
/** HTTP version string (e.g. "HTTP/1.1", "HTTP/2"). */
|
|
878
|
+
httpVersion: string;
|
|
879
|
+
/** Request headers as name/value pairs. */
|
|
880
|
+
headers: Array<{
|
|
881
|
+
/** Header name. */
|
|
882
|
+
name: string;
|
|
883
|
+
/** Header value. */
|
|
884
|
+
value: string;
|
|
885
|
+
}>;
|
|
886
|
+
/** Query parameters as name/value pairs. */
|
|
887
|
+
queryString: Array<{
|
|
888
|
+
/** Parameter name. */
|
|
889
|
+
name: string;
|
|
890
|
+
/** Parameter value. */
|
|
891
|
+
value: string;
|
|
892
|
+
}>;
|
|
893
|
+
/** Request body size in bytes (-1 if unknown). */
|
|
894
|
+
bodySize: number;
|
|
895
|
+
/** Posted data, if applicable. */
|
|
896
|
+
postData?: {
|
|
897
|
+
/** MIME type of the posted data. */
|
|
898
|
+
mimeType: string;
|
|
899
|
+
/** Plain-text body of the posted data. */
|
|
900
|
+
text: string;
|
|
901
|
+
};
|
|
902
|
+
};
|
|
903
|
+
/** Serialized response data. */
|
|
904
|
+
response: {
|
|
905
|
+
/** HTTP status code. */
|
|
906
|
+
status: number;
|
|
907
|
+
/** HTTP status text. */
|
|
908
|
+
statusText: string;
|
|
909
|
+
/** HTTP version string. */
|
|
910
|
+
httpVersion: string;
|
|
911
|
+
/** Response headers as name/value pairs. */
|
|
912
|
+
headers: Array<{
|
|
913
|
+
/** Header name. */
|
|
914
|
+
name: string;
|
|
915
|
+
/** Header value. */
|
|
916
|
+
value: string;
|
|
917
|
+
}>;
|
|
918
|
+
/** Response content metadata. */
|
|
919
|
+
content: {
|
|
920
|
+
/** Response body size in bytes. */
|
|
921
|
+
size: number;
|
|
922
|
+
/** MIME type of the response. */
|
|
923
|
+
mimeType: string;
|
|
924
|
+
/** Response body text (only included for text responses). */
|
|
925
|
+
text?: string;
|
|
926
|
+
};
|
|
927
|
+
/** Redirect target URL, if applicable. */
|
|
928
|
+
redirectURL: string;
|
|
929
|
+
/** Response body size in bytes (-1 if unknown). */
|
|
930
|
+
bodySize: number;
|
|
931
|
+
};
|
|
932
|
+
/** Timing breakdown in ms. */
|
|
933
|
+
timings: {
|
|
934
|
+
/** Time spent sending the request in ms. */
|
|
935
|
+
send: number;
|
|
936
|
+
/** Time spent waiting for the response in ms. */
|
|
937
|
+
wait: number;
|
|
938
|
+
/** Time spent receiving the response in ms. */
|
|
939
|
+
receive: number;
|
|
940
|
+
};
|
|
941
|
+
/** Cache state before and after the request. */
|
|
942
|
+
cache: {
|
|
943
|
+
/** State of the cache before the request (always null in HAR 1.2). */
|
|
944
|
+
beforeRequest?: null;
|
|
945
|
+
/** State of the cache after the request (always null in HAR 1.2). */
|
|
946
|
+
afterRequest?: null;
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* Full HAR log object conforming to the HTTP Archive specification.
|
|
951
|
+
*/
|
|
952
|
+
export interface HARLog {
|
|
953
|
+
/** HAR spec version (e.g. "1.2"). */
|
|
954
|
+
version: string;
|
|
955
|
+
/** Information about the creator of this HAR log. */
|
|
956
|
+
creator: {
|
|
957
|
+
/** Name of the library/tool that created the log. */
|
|
958
|
+
name: string;
|
|
959
|
+
/** Version of the creating library/tool. */
|
|
960
|
+
version: string;
|
|
961
|
+
/** Optional comment about the creator. */
|
|
962
|
+
comment?: string;
|
|
963
|
+
};
|
|
964
|
+
/** Ordered list of request/response entries. */
|
|
965
|
+
entries: HAREntry[];
|
|
966
|
+
}
|
|
967
|
+
//# sourceMappingURL=types.d.ts.map
|