@indra211/exios 2.0.1 → 3.1.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.
- package/README.md +182 -0
- package/dist/index.cjs +640 -16
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +216 -10
- package/dist/index.js +641 -16
- package/dist/index.node.cjs +640 -16
- package/dist/index.node.js +641 -16
- package/package.json +8 -3
package/dist/index.d.cts
CHANGED
|
@@ -34,5 +34,24 @@ declare namespace exios {
|
|
|
34
34
|
type ResponseType = Pkg.ResponseType;
|
|
35
35
|
type RetryConfig = Pkg.RetryConfig;
|
|
36
36
|
type Transformer = Pkg.Transformer;
|
|
37
|
+
type CacheConfig = Pkg.CacheConfig;
|
|
38
|
+
type CacheEntry = Pkg.CacheEntry;
|
|
39
|
+
type CacheProvider = Pkg.CacheProvider;
|
|
40
|
+
type DedupeConfig = Pkg.DedupeConfig;
|
|
41
|
+
type ExiosCache = Pkg.ExiosCache;
|
|
42
|
+
type MemoryCache = Pkg.MemoryCache;
|
|
43
|
+
type CanceledEvent = Pkg.CanceledEvent;
|
|
44
|
+
type CircuitEvent = Pkg.CircuitEvent;
|
|
45
|
+
type ErrorEvent = Pkg.ErrorEvent;
|
|
46
|
+
type ErrorMeta = Pkg.ErrorMeta;
|
|
47
|
+
type ExiosEventListener<K extends Pkg.ExiosEventName> = Pkg.ExiosEventListener<K>;
|
|
48
|
+
type ExiosEventMap = Pkg.ExiosEventMap;
|
|
49
|
+
type ExiosEventName = Pkg.ExiosEventName;
|
|
50
|
+
type RequestEvent = Pkg.RequestEvent;
|
|
51
|
+
type RequestEventBase = Pkg.RequestEventBase;
|
|
52
|
+
type ResponseEvent = Pkg.ResponseEvent;
|
|
53
|
+
type ResponseMeta = Pkg.ResponseMeta;
|
|
54
|
+
type RetryEvent = Pkg.RetryEvent;
|
|
55
|
+
type TimeoutEvent = Pkg.TimeoutEvent;
|
|
37
56
|
}
|
|
38
57
|
export = exios;
|
package/dist/index.d.ts
CHANGED
|
@@ -15,10 +15,9 @@ declare class ExiosError<T = any, D = any> extends Error {
|
|
|
15
15
|
response?: ExiosResponse<T, D>;
|
|
16
16
|
status?: number;
|
|
17
17
|
readonly isExiosError: true;
|
|
18
|
-
meta:
|
|
19
|
-
attempts: number;
|
|
20
|
-
};
|
|
18
|
+
meta: ErrorMeta;
|
|
21
19
|
constructor(message: string, code?: string, config?: InternalConfig<D>, request?: any, response?: ExiosResponse<T, D>);
|
|
20
|
+
/** Log-safe: credentials, sensitive headers (Authorization, Cookie, API keys) and the body are redacted. */
|
|
22
21
|
toJSON(): Record<string, unknown>;
|
|
23
22
|
static from<T = any, D = any>(err: unknown, code: string | undefined, config?: InternalConfig<D>, request?: any, response?: ExiosResponse<T, D>): ExiosError<T, D>;
|
|
24
23
|
}
|
|
@@ -191,6 +190,65 @@ interface ExiosRequestConfig<D = any> {
|
|
|
191
190
|
redirect?: RequestRedirect;
|
|
192
191
|
retry?: false | Partial<RetryConfig>;
|
|
193
192
|
breaker?: false | Partial<BreakerConfig>;
|
|
193
|
+
/** Id of this logical request (retries share it). Generated when not set. */
|
|
194
|
+
requestId?: string;
|
|
195
|
+
/** Id that ties this request to a wider operation; often set on an instance. */
|
|
196
|
+
correlationId?: string;
|
|
197
|
+
/**
|
|
198
|
+
* Send `correlationId` (or `requestId` when there is none) in this header, e.g. "x-correlation-id".
|
|
199
|
+
* Off by default: an extra header can trigger a CORS preflight. A header the caller already set wins.
|
|
200
|
+
*/
|
|
201
|
+
correlationIdHeader?: string;
|
|
202
|
+
/** Free-form data that travels with the request: readable in interceptors, events and adapters. Never sent. */
|
|
203
|
+
metadata?: Record<string, unknown>;
|
|
204
|
+
/**
|
|
205
|
+
* Time limit (ms) for the whole logical request: every attempt, retry and backoff sleep.
|
|
206
|
+
* `timeout` still limits each attempt. 0 / undefined = no limit. For `responseType: 'stream'` it ends
|
|
207
|
+
* when the headers arrive.
|
|
208
|
+
*/
|
|
209
|
+
totalTimeout?: number;
|
|
210
|
+
/** Share one in-flight request between identical concurrent requests (GET/HEAD by default). Off by default. */
|
|
211
|
+
dedupe?: boolean | DedupeConfig;
|
|
212
|
+
/** Cache successful responses (GET/HEAD by default). Off by default. */
|
|
213
|
+
cache?: boolean | Partial<CacheConfig>;
|
|
214
|
+
}
|
|
215
|
+
interface DedupeConfig {
|
|
216
|
+
/** Methods that may be deduplicated. Default: ["get", "head"]. */
|
|
217
|
+
methods?: Lowercase<Method>[];
|
|
218
|
+
/** Custom key; return undefined to not deduplicate this request. Default: method + URL + params + headers. */
|
|
219
|
+
key?: (config: InternalConfig) => string | undefined;
|
|
220
|
+
}
|
|
221
|
+
/** What a cache provider stores for one response. */
|
|
222
|
+
interface CacheEntry {
|
|
223
|
+
status: number;
|
|
224
|
+
statusText: string;
|
|
225
|
+
headers: Record<string, string | string[]>;
|
|
226
|
+
data: unknown;
|
|
227
|
+
/** `Date.now()` when stored. */
|
|
228
|
+
storedAt: number;
|
|
229
|
+
/** `Date.now()` after which the entry is stale and is not used. */
|
|
230
|
+
expiresAt: number;
|
|
231
|
+
}
|
|
232
|
+
/** Storage for cached responses. Methods may be sync or async. */
|
|
233
|
+
interface CacheProvider {
|
|
234
|
+
get(key: string): CacheEntry | undefined | Promise<CacheEntry | undefined>;
|
|
235
|
+
set(key: string, entry: CacheEntry): void | Promise<void>;
|
|
236
|
+
delete(key: string): void | Promise<void>;
|
|
237
|
+
clear(): void | Promise<void>;
|
|
238
|
+
/** Needed only for `instance.cache.invalidate()`. */
|
|
239
|
+
keys?(): Iterable<string> | Promise<Iterable<string>>;
|
|
240
|
+
}
|
|
241
|
+
interface CacheConfig {
|
|
242
|
+
/** Time to live in ms. Default 60000. */
|
|
243
|
+
ttl: number;
|
|
244
|
+
/** Methods whose responses may be cached. Default: ["get", "head"]. */
|
|
245
|
+
methods: Lowercase<Method>[];
|
|
246
|
+
/** Custom key; return undefined to not cache this request. Default: method + URL + params + headers. */
|
|
247
|
+
key?: (config: InternalConfig) => string | undefined;
|
|
248
|
+
/** Where entries live. Default: the instance's in-memory LRU cache (100 entries). */
|
|
249
|
+
provider?: CacheProvider;
|
|
250
|
+
/** Honour Cache-Control (`no-store`, `no-cache`, `max-age`) on the request and the response. Default true. */
|
|
251
|
+
respectCacheControl: boolean;
|
|
194
252
|
}
|
|
195
253
|
/**
|
|
196
254
|
* Type of `instance.defaults`: like a request config, but every header bucket, `retry` and `breaker`
|
|
@@ -204,6 +262,8 @@ interface ExiosDefaults<D = any> extends Omit<ExiosRequestConfig<D>, "headers" |
|
|
|
204
262
|
interface InternalConfig<D = any> extends Omit<ExiosRequestConfig<D>, "headers" | "retry" | "breaker"> {
|
|
205
263
|
method: Lowercase<Method>;
|
|
206
264
|
headers: Record<string, string>;
|
|
265
|
+
/** Always set by the time a request reaches an adapter. */
|
|
266
|
+
requestId?: string;
|
|
207
267
|
retry: false | RetryConfig;
|
|
208
268
|
breaker: false | BreakerConfig;
|
|
209
269
|
}
|
|
@@ -215,10 +275,104 @@ interface ExiosResponse<T = any, D = any> {
|
|
|
215
275
|
config: InternalConfig<D>;
|
|
216
276
|
/** XMLHttpRequest (xhr), ClientRequest (http) or Request (fetch). */
|
|
217
277
|
request: any;
|
|
218
|
-
meta:
|
|
219
|
-
|
|
220
|
-
|
|
278
|
+
meta: ResponseMeta;
|
|
279
|
+
}
|
|
280
|
+
/** Diagnostics attached to a response. Fields other than `attempts` are set by exios ≥ 3. */
|
|
281
|
+
interface ResponseMeta {
|
|
282
|
+
/** Attempts made, retries included. */
|
|
283
|
+
attempts: number;
|
|
284
|
+
requestId?: string;
|
|
285
|
+
correlationId?: string;
|
|
286
|
+
/** Whole logical request, retries and backoff included (ms). */
|
|
287
|
+
durationMs?: number;
|
|
288
|
+
/** The final attempt only (ms). */
|
|
289
|
+
responseTimeMs?: number;
|
|
290
|
+
/** Served from the cache (no network request was made). */
|
|
291
|
+
cached?: boolean;
|
|
292
|
+
/** Shared with an identical request already in flight. */
|
|
293
|
+
deduped?: boolean;
|
|
294
|
+
}
|
|
295
|
+
/** Diagnostics attached to an ExiosError. Fields other than `attempts` are set by exios ≥ 3. */
|
|
296
|
+
interface ErrorMeta {
|
|
297
|
+
attempts: number;
|
|
298
|
+
requestId?: string;
|
|
299
|
+
correlationId?: string;
|
|
300
|
+
method?: Lowercase<Method>;
|
|
301
|
+
/** baseURL + url, without the serialized `params`. */
|
|
302
|
+
url?: string;
|
|
303
|
+
durationMs?: number;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Fields every request lifecycle event carries. */
|
|
307
|
+
interface RequestEventBase {
|
|
308
|
+
requestId: string;
|
|
309
|
+
correlationId?: string;
|
|
310
|
+
method: Lowercase<Method>;
|
|
311
|
+
/** baseURL + url, without the serialized `params`. */
|
|
312
|
+
url: string;
|
|
313
|
+
config: InternalConfig;
|
|
314
|
+
/** `config.metadata`, for convenience. */
|
|
315
|
+
metadata?: Record<string, unknown>;
|
|
316
|
+
/** `Date.now()` when the event was emitted. */
|
|
317
|
+
timestamp: number;
|
|
221
318
|
}
|
|
319
|
+
type RequestEvent = RequestEventBase;
|
|
320
|
+
interface ResponseEvent extends RequestEventBase {
|
|
321
|
+
response: ExiosResponse;
|
|
322
|
+
status: number;
|
|
323
|
+
attempts: number;
|
|
324
|
+
/** Whole logical request, retries and backoff included. */
|
|
325
|
+
durationMs: number;
|
|
326
|
+
/** The final attempt only. */
|
|
327
|
+
responseTimeMs: number;
|
|
328
|
+
}
|
|
329
|
+
interface ErrorEvent extends RequestEventBase {
|
|
330
|
+
/** Usually an ExiosError; a value thrown by user code (e.g. `retry.shouldRetry`) is passed as is. */
|
|
331
|
+
error: unknown;
|
|
332
|
+
code?: string;
|
|
333
|
+
status?: number;
|
|
334
|
+
attempts: number;
|
|
335
|
+
durationMs: number;
|
|
336
|
+
}
|
|
337
|
+
interface RetryEvent extends RequestEventBase {
|
|
338
|
+
/** The attempt that failed (1 = the first try). */
|
|
339
|
+
attempt: number;
|
|
340
|
+
delayMs: number;
|
|
341
|
+
error?: ExiosError;
|
|
342
|
+
status?: number;
|
|
343
|
+
}
|
|
344
|
+
interface TimeoutEvent extends RequestEventBase {
|
|
345
|
+
/** "attempt": one attempt hit `timeout` (it may be retried). "total": `totalTimeout` ended the request. */
|
|
346
|
+
kind: "attempt" | "total";
|
|
347
|
+
attempt: number;
|
|
348
|
+
timeoutMs: number;
|
|
349
|
+
error: ExiosError;
|
|
350
|
+
}
|
|
351
|
+
interface CanceledEvent extends RequestEventBase {
|
|
352
|
+
error: ExiosError;
|
|
353
|
+
attempts: number;
|
|
354
|
+
durationMs: number;
|
|
355
|
+
}
|
|
356
|
+
interface CircuitEvent {
|
|
357
|
+
/** Breaker key (by default the request origin). */
|
|
358
|
+
key: string;
|
|
359
|
+
from: BreakerState;
|
|
360
|
+
to: BreakerState;
|
|
361
|
+
timestamp: number;
|
|
362
|
+
}
|
|
363
|
+
interface ExiosEventMap {
|
|
364
|
+
request: RequestEvent;
|
|
365
|
+
response: ResponseEvent;
|
|
366
|
+
error: ErrorEvent;
|
|
367
|
+
retry: RetryEvent;
|
|
368
|
+
timeout: TimeoutEvent;
|
|
369
|
+
canceled: CanceledEvent;
|
|
370
|
+
circuitOpen: CircuitEvent;
|
|
371
|
+
circuitHalfOpen: CircuitEvent;
|
|
372
|
+
circuitClose: CircuitEvent;
|
|
373
|
+
}
|
|
374
|
+
type ExiosEventName = keyof ExiosEventMap;
|
|
375
|
+
type ExiosEventListener<K extends ExiosEventName> = (event: ExiosEventMap[K]) => void;
|
|
222
376
|
|
|
223
377
|
interface InterceptorOptions {
|
|
224
378
|
synchronous?: boolean;
|
|
@@ -251,8 +405,30 @@ declare class Exios {
|
|
|
251
405
|
reset(key?: string): void;
|
|
252
406
|
};
|
|
253
407
|
private readonly registry;
|
|
408
|
+
private readonly events;
|
|
409
|
+
private readonly memoryCache;
|
|
410
|
+
private readonly inflight;
|
|
411
|
+
/** The instance cache: `defaults.cache.provider`, or the built-in in-memory cache. */
|
|
412
|
+
cache: ExiosCache;
|
|
254
413
|
constructor(defaults: ExiosDefaults);
|
|
255
414
|
request<T = any, R = ExiosResponse<T>, D = any>(configOrUrl: string | Cfg<D>, config?: Cfg<D>): Promise<R>;
|
|
415
|
+
/** Subscribe to a lifecycle event; returns a function that unsubscribes. */
|
|
416
|
+
on<K extends ExiosEventName>(name: K, listener: ExiosEventListener<K>): () => void;
|
|
417
|
+
/** Like `on`, but the listener runs at most once. */
|
|
418
|
+
once<K extends ExiosEventName>(name: K, listener: ExiosEventListener<K>): () => void;
|
|
419
|
+
/** Remove one listener, or every listener of `name` when `listener` is omitted. */
|
|
420
|
+
off<K extends ExiosEventName>(name: K, listener?: ExiosEventListener<K>): void;
|
|
421
|
+
/**
|
|
422
|
+
* After request interceptors: ids → cache read → dedupe → network → cache write.
|
|
423
|
+
* Cache hits and deduplicated followers make no network request and emit no lifecycle events.
|
|
424
|
+
*/
|
|
425
|
+
private dispatch;
|
|
426
|
+
/** A response made without a network request of its own (cache hit, deduplicated follower). */
|
|
427
|
+
private localResponse;
|
|
428
|
+
/** Store a successful response; provider failures are reported, never thrown. */
|
|
429
|
+
private store;
|
|
430
|
+
/** totalTimeout, timing, error context and lifecycle events around dispatchRequest. */
|
|
431
|
+
private network;
|
|
256
432
|
/** Runs every request interceptor in this tick with promise-chain semantics, then dispatches. */
|
|
257
433
|
private runSync;
|
|
258
434
|
getUri(config?: ExiosRequestConfig): string;
|
|
@@ -269,8 +445,22 @@ declare class Exios {
|
|
|
269
445
|
putForm<T = any, R = ExiosResponse<T>, D = any>(url: string, data?: D, config?: Cfg<D>): Promise<R>;
|
|
270
446
|
patchForm<T = any, R = ExiosResponse<T>, D = any>(url: string, data?: D, config?: Cfg<D>): Promise<R>;
|
|
271
447
|
}
|
|
272
|
-
declare const METHODS: readonly ["request", "getUri", "get", "delete", "head", "options", "post", "put", "patch", "postForm", "putForm", "patchForm"];
|
|
273
|
-
|
|
448
|
+
declare const METHODS: readonly ["request", "on", "once", "off", "getUri", "get", "delete", "head", "options", "post", "put", "patch", "postForm", "putForm", "patchForm"];
|
|
449
|
+
/** `instance.cache`: manage the instance's cache provider. */
|
|
450
|
+
interface ExiosCache {
|
|
451
|
+
/** Remove every entry. */
|
|
452
|
+
clear(): Promise<void>;
|
|
453
|
+
/** Remove one entry by key. */
|
|
454
|
+
delete(key: string): Promise<void>;
|
|
455
|
+
/** The default cache key a request config would get (undefined if it is not cacheable). */
|
|
456
|
+
key(config?: ExiosRequestConfig): string | undefined;
|
|
457
|
+
/**
|
|
458
|
+
* Remove entries: a URL (resolved against baseURL; the URL itself, its query variants and sub-paths),
|
|
459
|
+
* a RegExp tested against keys, or a predicate. Resolves with the number removed.
|
|
460
|
+
*/
|
|
461
|
+
invalidate(match: string | RegExp | ((key: string) => boolean)): Promise<number>;
|
|
462
|
+
}
|
|
463
|
+
interface ExiosStatic extends Pick<Exios, (typeof METHODS)[number] | "defaults" | "interceptors" | "breaker" | "cache"> {
|
|
274
464
|
<T = any, R = ExiosResponse<T>, D = any>(config: ExiosRequestConfig<D>): Promise<R>;
|
|
275
465
|
<T = any, R = ExiosResponse<T>, D = any>(url: string, config?: ExiosRequestConfig<D>): Promise<R>;
|
|
276
466
|
create(config?: ExiosRequestConfig): ExiosStatic;
|
|
@@ -279,6 +469,21 @@ type ExiosInstance = ExiosStatic;
|
|
|
279
469
|
|
|
280
470
|
declare function mergeConfig(a: ExiosRequestConfig, b?: ExiosRequestConfig): ExiosRequestConfig;
|
|
281
471
|
|
|
472
|
+
/** In-memory LRU cache: the default provider. Expired entries are dropped when read. */
|
|
473
|
+
declare class MemoryCache implements CacheProvider {
|
|
474
|
+
private readonly map;
|
|
475
|
+
readonly maxEntries: number;
|
|
476
|
+
constructor(options?: {
|
|
477
|
+
maxEntries?: number;
|
|
478
|
+
});
|
|
479
|
+
get size(): number;
|
|
480
|
+
get(key: string): CacheEntry | undefined;
|
|
481
|
+
set(key: string, entry: CacheEntry): void;
|
|
482
|
+
delete(key: string): void;
|
|
483
|
+
clear(): void;
|
|
484
|
+
keys(): Iterable<string>;
|
|
485
|
+
}
|
|
486
|
+
|
|
282
487
|
declare function toFormData(obj: object, fd?: FormData): FormData;
|
|
283
488
|
|
|
284
489
|
declare const statics: {
|
|
@@ -290,10 +495,11 @@ declare const statics: {
|
|
|
290
495
|
isCancel: typeof isCancel;
|
|
291
496
|
mergeConfig: typeof mergeConfig;
|
|
292
497
|
toFormData: typeof toFormData;
|
|
498
|
+
MemoryCache: typeof MemoryCache;
|
|
293
499
|
};
|
|
294
500
|
type ExiosDefault = ExiosStatic & typeof statics;
|
|
295
501
|
declare const exios: ExiosDefault;
|
|
296
502
|
declare const create: (config?: ExiosRequestConfig) => ExiosStatic;
|
|
297
503
|
|
|
298
|
-
export { CancelToken, CanceledError, CircuitOpenError, ExiosError, InterceptorManager, create, exios as default, isCancel, isExiosError, mergeConfig, toFormData };
|
|
299
|
-
export type { AdapterConfig, AdapterFn, AdapterName, BreakerConfig, BreakerState, CancelTokenSource, Canceler, ExiosDefault, ExiosDefaults, ExiosInstance, ExiosRequestConfig, ExiosResponse, ExiosStatic, HeaderValue, HeadersDefaults, InterceptorHandler, InterceptorOptions, InternalConfig, Method, Outcome, ProgressEvent, ProxyConfig, RawHeaders, RequestBreakerOverride, ResponseType, RetryConfig, Transformer };
|
|
504
|
+
export { CancelToken, CanceledError, CircuitOpenError, ExiosError, InterceptorManager, MemoryCache, create, exios as default, isCancel, isExiosError, mergeConfig, toFormData };
|
|
505
|
+
export type { AdapterConfig, AdapterFn, AdapterName, BreakerConfig, BreakerState, CacheConfig, CacheEntry, CacheProvider, CancelTokenSource, CanceledEvent, Canceler, CircuitEvent, DedupeConfig, ErrorEvent, ErrorMeta, ExiosCache, ExiosDefault, ExiosDefaults, ExiosEventListener, ExiosEventMap, ExiosEventName, ExiosInstance, ExiosRequestConfig, ExiosResponse, ExiosStatic, HeaderValue, HeadersDefaults, InterceptorHandler, InterceptorOptions, InternalConfig, Method, Outcome, ProgressEvent, ProxyConfig, RawHeaders, RequestBreakerOverride, RequestEvent, RequestEventBase, ResponseEvent, ResponseMeta, ResponseType, RetryConfig, RetryEvent, TimeoutEvent, Transformer };
|