@npora/request 1.1.0 → 1.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.
- package/README.md +3 -1
- package/dist/index.cjs +79 -6
- package/dist/index.d.cts +39 -1
- package/dist/index.d.ts +39 -1
- package/dist/index.js +79 -6
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -2349,12 +2349,43 @@ function retryPlugin(defaultOptions = {}) {
|
|
|
2349
2349
|
requestContext.error
|
|
2350
2350
|
);
|
|
2351
2351
|
const retryAfter = retryOptions.respectRetryAfter ? parseRetryAfter(requestContext.error) : void 0;
|
|
2352
|
+
const baseDelay = normalizeRetryDelay(
|
|
2353
|
+
retryAfter ?? configuredDelay,
|
|
2354
|
+
retryOptions.maxDelay
|
|
2355
|
+
);
|
|
2356
|
+
const elapsedTime = Math.max(
|
|
2357
|
+
0,
|
|
2358
|
+
Date.now() - requestContext.startTime
|
|
2359
|
+
);
|
|
2360
|
+
const pendingEvent = {
|
|
2361
|
+
attempt: attempt + 1,
|
|
2362
|
+
delay: baseDelay,
|
|
2363
|
+
elapsedTime,
|
|
2364
|
+
error: requestContext.error
|
|
2365
|
+
};
|
|
2366
|
+
const jitteredDelay = retryAfter === void 0 ? await applyJitter(
|
|
2367
|
+
retryOptions.jitter,
|
|
2368
|
+
pendingEvent
|
|
2369
|
+
) : baseDelay;
|
|
2370
|
+
const delay = normalizeRetryDelay(
|
|
2371
|
+
jitteredDelay,
|
|
2372
|
+
retryOptions.maxDelay
|
|
2373
|
+
);
|
|
2374
|
+
const event = {
|
|
2375
|
+
...pendingEvent,
|
|
2376
|
+
delay
|
|
2377
|
+
};
|
|
2378
|
+
if (exceedsElapsedTimeBudget(
|
|
2379
|
+
elapsedTime,
|
|
2380
|
+
delay,
|
|
2381
|
+
retryOptions.maxElapsedTime
|
|
2382
|
+
)) {
|
|
2383
|
+
return void 0;
|
|
2384
|
+
}
|
|
2385
|
+
notifyRetry(retryOptions.onRetry, event);
|
|
2352
2386
|
return {
|
|
2353
2387
|
retry: true,
|
|
2354
|
-
delay
|
|
2355
|
-
retryAfter ?? configuredDelay,
|
|
2356
|
-
retryOptions.maxDelay
|
|
2357
|
-
)
|
|
2388
|
+
delay
|
|
2358
2389
|
};
|
|
2359
2390
|
});
|
|
2360
2391
|
}
|
|
@@ -2368,7 +2399,12 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2368
2399
|
delay: normalizeDelay(defaults.delay),
|
|
2369
2400
|
respectRetryAfter: defaults.respectRetryAfter ?? true,
|
|
2370
2401
|
maxDelay: normalizeMaxDelay(defaults.maxDelay),
|
|
2371
|
-
|
|
2402
|
+
jitter: defaults.jitter ?? false,
|
|
2403
|
+
maxElapsedTime: normalizeMaxElapsedTime(
|
|
2404
|
+
defaults.maxElapsedTime
|
|
2405
|
+
),
|
|
2406
|
+
shouldRetry: defaults.shouldRetry ?? defaultShouldRetry,
|
|
2407
|
+
onRetry: defaults.onRetry
|
|
2372
2408
|
};
|
|
2373
2409
|
}
|
|
2374
2410
|
return {
|
|
@@ -2385,7 +2421,12 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2385
2421
|
maxDelay: normalizeMaxDelay(
|
|
2386
2422
|
retry?.maxDelay ?? defaults.maxDelay
|
|
2387
2423
|
),
|
|
2388
|
-
|
|
2424
|
+
jitter: retry?.jitter ?? defaults.jitter ?? false,
|
|
2425
|
+
maxElapsedTime: normalizeMaxElapsedTime(
|
|
2426
|
+
retry?.maxElapsedTime ?? defaults.maxElapsedTime
|
|
2427
|
+
),
|
|
2428
|
+
shouldRetry: retry?.shouldRetry ?? defaults.shouldRetry ?? defaultShouldRetry,
|
|
2429
|
+
onRetry: retry?.onRetry ?? defaults.onRetry
|
|
2389
2430
|
};
|
|
2390
2431
|
}
|
|
2391
2432
|
function normalizeMethods(methods) {
|
|
@@ -2406,6 +2447,15 @@ function normalizeRetries(retries) {
|
|
|
2406
2447
|
}
|
|
2407
2448
|
return Math.max(0, Math.floor(retries));
|
|
2408
2449
|
}
|
|
2450
|
+
function normalizeMaxElapsedTime(maxElapsedTime) {
|
|
2451
|
+
if (maxElapsedTime === void 0) {
|
|
2452
|
+
return Number.POSITIVE_INFINITY;
|
|
2453
|
+
}
|
|
2454
|
+
if (!Number.isFinite(maxElapsedTime)) {
|
|
2455
|
+
return maxElapsedTime > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
2456
|
+
}
|
|
2457
|
+
return Math.max(0, maxElapsedTime);
|
|
2458
|
+
}
|
|
2409
2459
|
function normalizeDelay(delay) {
|
|
2410
2460
|
if (typeof delay === "number") {
|
|
2411
2461
|
const normalizedDelay = Math.max(0, delay);
|
|
@@ -2422,6 +2472,29 @@ function normalizeRetryDelay(delay, maxDelay) {
|
|
|
2422
2472
|
}
|
|
2423
2473
|
return Math.min(Math.max(0, delay), maxDelay);
|
|
2424
2474
|
}
|
|
2475
|
+
async function applyJitter(jitter, event) {
|
|
2476
|
+
if (jitter === false) {
|
|
2477
|
+
return event.delay;
|
|
2478
|
+
}
|
|
2479
|
+
if (jitter === true) {
|
|
2480
|
+
return Math.random() * event.delay;
|
|
2481
|
+
}
|
|
2482
|
+
return jitter(event);
|
|
2483
|
+
}
|
|
2484
|
+
function exceedsElapsedTimeBudget(elapsedTime, delay, maxElapsedTime) {
|
|
2485
|
+
return elapsedTime >= maxElapsedTime || elapsedTime + delay > maxElapsedTime;
|
|
2486
|
+
}
|
|
2487
|
+
function notifyRetry(onRetry, event) {
|
|
2488
|
+
if (!onRetry) {
|
|
2489
|
+
return;
|
|
2490
|
+
}
|
|
2491
|
+
try {
|
|
2492
|
+
void Promise.resolve(onRetry(event)).catch(ignoreRetryObserverError);
|
|
2493
|
+
} catch {
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
function ignoreRetryObserverError() {
|
|
2497
|
+
}
|
|
2425
2498
|
function canRetryRequest(config, methods) {
|
|
2426
2499
|
const method = config.method ?? "GET";
|
|
2427
2500
|
if (!methods.has(method)) {
|
package/dist/index.d.cts
CHANGED
|
@@ -27,7 +27,45 @@ interface RetryOptions {
|
|
|
27
27
|
* @default 60000
|
|
28
28
|
*/
|
|
29
29
|
maxDelay?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Randomize the configured retry delay.
|
|
32
|
+
*
|
|
33
|
+
* `true` applies full jitter between zero and the configured delay.
|
|
34
|
+
* A function receives the pending retry event and returns the final delay.
|
|
35
|
+
* Server-provided Retry-After delays are not jittered.
|
|
36
|
+
*
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
jitter?: boolean | ((event: RetryEvent) => number | Promise<number>);
|
|
40
|
+
/**
|
|
41
|
+
* Maximum elapsed time across the initial request, retries and planned
|
|
42
|
+
* retry delays.
|
|
43
|
+
*
|
|
44
|
+
* @default unlimited
|
|
45
|
+
*/
|
|
46
|
+
maxElapsedTime?: number;
|
|
30
47
|
shouldRetry?: (error: unknown, attempt: number) => boolean | Promise<boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* Observe a scheduled retry.
|
|
50
|
+
*
|
|
51
|
+
* Callback failures are isolated from the request lifecycle.
|
|
52
|
+
*/
|
|
53
|
+
onRetry?: (event: RetryEvent) => void | Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
interface RetryEvent {
|
|
56
|
+
/**
|
|
57
|
+
* One-based retry number.
|
|
58
|
+
*/
|
|
59
|
+
attempt: number;
|
|
60
|
+
/**
|
|
61
|
+
* Final delay before this retry in milliseconds.
|
|
62
|
+
*/
|
|
63
|
+
delay: number;
|
|
64
|
+
/**
|
|
65
|
+
* Time elapsed since the request started in milliseconds.
|
|
66
|
+
*/
|
|
67
|
+
elapsedTime: number;
|
|
68
|
+
error: unknown;
|
|
31
69
|
}
|
|
32
70
|
interface CacheOptions {
|
|
33
71
|
enabled?: boolean;
|
|
@@ -535,4 +573,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
|
535
573
|
|
|
536
574
|
declare function uploadPlugin(): Plugin;
|
|
537
575
|
|
|
538
|
-
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
|
576
|
+
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -27,7 +27,45 @@ interface RetryOptions {
|
|
|
27
27
|
* @default 60000
|
|
28
28
|
*/
|
|
29
29
|
maxDelay?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Randomize the configured retry delay.
|
|
32
|
+
*
|
|
33
|
+
* `true` applies full jitter between zero and the configured delay.
|
|
34
|
+
* A function receives the pending retry event and returns the final delay.
|
|
35
|
+
* Server-provided Retry-After delays are not jittered.
|
|
36
|
+
*
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
jitter?: boolean | ((event: RetryEvent) => number | Promise<number>);
|
|
40
|
+
/**
|
|
41
|
+
* Maximum elapsed time across the initial request, retries and planned
|
|
42
|
+
* retry delays.
|
|
43
|
+
*
|
|
44
|
+
* @default unlimited
|
|
45
|
+
*/
|
|
46
|
+
maxElapsedTime?: number;
|
|
30
47
|
shouldRetry?: (error: unknown, attempt: number) => boolean | Promise<boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* Observe a scheduled retry.
|
|
50
|
+
*
|
|
51
|
+
* Callback failures are isolated from the request lifecycle.
|
|
52
|
+
*/
|
|
53
|
+
onRetry?: (event: RetryEvent) => void | Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
interface RetryEvent {
|
|
56
|
+
/**
|
|
57
|
+
* One-based retry number.
|
|
58
|
+
*/
|
|
59
|
+
attempt: number;
|
|
60
|
+
/**
|
|
61
|
+
* Final delay before this retry in milliseconds.
|
|
62
|
+
*/
|
|
63
|
+
delay: number;
|
|
64
|
+
/**
|
|
65
|
+
* Time elapsed since the request started in milliseconds.
|
|
66
|
+
*/
|
|
67
|
+
elapsedTime: number;
|
|
68
|
+
error: unknown;
|
|
31
69
|
}
|
|
32
70
|
interface CacheOptions {
|
|
33
71
|
enabled?: boolean;
|
|
@@ -535,4 +573,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
|
535
573
|
|
|
536
574
|
declare function uploadPlugin(): Plugin;
|
|
537
575
|
|
|
538
|
-
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
|
576
|
+
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheOptions, type CachePlugin, type CachePluginOptions, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
package/dist/index.js
CHANGED
|
@@ -2311,12 +2311,43 @@ function retryPlugin(defaultOptions = {}) {
|
|
|
2311
2311
|
requestContext.error
|
|
2312
2312
|
);
|
|
2313
2313
|
const retryAfter = retryOptions.respectRetryAfter ? parseRetryAfter(requestContext.error) : void 0;
|
|
2314
|
+
const baseDelay = normalizeRetryDelay(
|
|
2315
|
+
retryAfter ?? configuredDelay,
|
|
2316
|
+
retryOptions.maxDelay
|
|
2317
|
+
);
|
|
2318
|
+
const elapsedTime = Math.max(
|
|
2319
|
+
0,
|
|
2320
|
+
Date.now() - requestContext.startTime
|
|
2321
|
+
);
|
|
2322
|
+
const pendingEvent = {
|
|
2323
|
+
attempt: attempt + 1,
|
|
2324
|
+
delay: baseDelay,
|
|
2325
|
+
elapsedTime,
|
|
2326
|
+
error: requestContext.error
|
|
2327
|
+
};
|
|
2328
|
+
const jitteredDelay = retryAfter === void 0 ? await applyJitter(
|
|
2329
|
+
retryOptions.jitter,
|
|
2330
|
+
pendingEvent
|
|
2331
|
+
) : baseDelay;
|
|
2332
|
+
const delay = normalizeRetryDelay(
|
|
2333
|
+
jitteredDelay,
|
|
2334
|
+
retryOptions.maxDelay
|
|
2335
|
+
);
|
|
2336
|
+
const event = {
|
|
2337
|
+
...pendingEvent,
|
|
2338
|
+
delay
|
|
2339
|
+
};
|
|
2340
|
+
if (exceedsElapsedTimeBudget(
|
|
2341
|
+
elapsedTime,
|
|
2342
|
+
delay,
|
|
2343
|
+
retryOptions.maxElapsedTime
|
|
2344
|
+
)) {
|
|
2345
|
+
return void 0;
|
|
2346
|
+
}
|
|
2347
|
+
notifyRetry(retryOptions.onRetry, event);
|
|
2314
2348
|
return {
|
|
2315
2349
|
retry: true,
|
|
2316
|
-
delay
|
|
2317
|
-
retryAfter ?? configuredDelay,
|
|
2318
|
-
retryOptions.maxDelay
|
|
2319
|
-
)
|
|
2350
|
+
delay
|
|
2320
2351
|
};
|
|
2321
2352
|
});
|
|
2322
2353
|
}
|
|
@@ -2330,7 +2361,12 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2330
2361
|
delay: normalizeDelay(defaults.delay),
|
|
2331
2362
|
respectRetryAfter: defaults.respectRetryAfter ?? true,
|
|
2332
2363
|
maxDelay: normalizeMaxDelay(defaults.maxDelay),
|
|
2333
|
-
|
|
2364
|
+
jitter: defaults.jitter ?? false,
|
|
2365
|
+
maxElapsedTime: normalizeMaxElapsedTime(
|
|
2366
|
+
defaults.maxElapsedTime
|
|
2367
|
+
),
|
|
2368
|
+
shouldRetry: defaults.shouldRetry ?? defaultShouldRetry,
|
|
2369
|
+
onRetry: defaults.onRetry
|
|
2334
2370
|
};
|
|
2335
2371
|
}
|
|
2336
2372
|
return {
|
|
@@ -2347,7 +2383,12 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2347
2383
|
maxDelay: normalizeMaxDelay(
|
|
2348
2384
|
retry?.maxDelay ?? defaults.maxDelay
|
|
2349
2385
|
),
|
|
2350
|
-
|
|
2386
|
+
jitter: retry?.jitter ?? defaults.jitter ?? false,
|
|
2387
|
+
maxElapsedTime: normalizeMaxElapsedTime(
|
|
2388
|
+
retry?.maxElapsedTime ?? defaults.maxElapsedTime
|
|
2389
|
+
),
|
|
2390
|
+
shouldRetry: retry?.shouldRetry ?? defaults.shouldRetry ?? defaultShouldRetry,
|
|
2391
|
+
onRetry: retry?.onRetry ?? defaults.onRetry
|
|
2351
2392
|
};
|
|
2352
2393
|
}
|
|
2353
2394
|
function normalizeMethods(methods) {
|
|
@@ -2368,6 +2409,15 @@ function normalizeRetries(retries) {
|
|
|
2368
2409
|
}
|
|
2369
2410
|
return Math.max(0, Math.floor(retries));
|
|
2370
2411
|
}
|
|
2412
|
+
function normalizeMaxElapsedTime(maxElapsedTime) {
|
|
2413
|
+
if (maxElapsedTime === void 0) {
|
|
2414
|
+
return Number.POSITIVE_INFINITY;
|
|
2415
|
+
}
|
|
2416
|
+
if (!Number.isFinite(maxElapsedTime)) {
|
|
2417
|
+
return maxElapsedTime > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
2418
|
+
}
|
|
2419
|
+
return Math.max(0, maxElapsedTime);
|
|
2420
|
+
}
|
|
2371
2421
|
function normalizeDelay(delay) {
|
|
2372
2422
|
if (typeof delay === "number") {
|
|
2373
2423
|
const normalizedDelay = Math.max(0, delay);
|
|
@@ -2384,6 +2434,29 @@ function normalizeRetryDelay(delay, maxDelay) {
|
|
|
2384
2434
|
}
|
|
2385
2435
|
return Math.min(Math.max(0, delay), maxDelay);
|
|
2386
2436
|
}
|
|
2437
|
+
async function applyJitter(jitter, event) {
|
|
2438
|
+
if (jitter === false) {
|
|
2439
|
+
return event.delay;
|
|
2440
|
+
}
|
|
2441
|
+
if (jitter === true) {
|
|
2442
|
+
return Math.random() * event.delay;
|
|
2443
|
+
}
|
|
2444
|
+
return jitter(event);
|
|
2445
|
+
}
|
|
2446
|
+
function exceedsElapsedTimeBudget(elapsedTime, delay, maxElapsedTime) {
|
|
2447
|
+
return elapsedTime >= maxElapsedTime || elapsedTime + delay > maxElapsedTime;
|
|
2448
|
+
}
|
|
2449
|
+
function notifyRetry(onRetry, event) {
|
|
2450
|
+
if (!onRetry) {
|
|
2451
|
+
return;
|
|
2452
|
+
}
|
|
2453
|
+
try {
|
|
2454
|
+
void Promise.resolve(onRetry(event)).catch(ignoreRetryObserverError);
|
|
2455
|
+
} catch {
|
|
2456
|
+
}
|
|
2457
|
+
}
|
|
2458
|
+
function ignoreRetryObserverError() {
|
|
2459
|
+
}
|
|
2387
2460
|
function canRetryRequest(config, methods) {
|
|
2388
2461
|
const method = config.method ?? "GET";
|
|
2389
2462
|
if (!methods.has(method)) {
|