@zayne-labs/callapi 1.10.6 → 1.11.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.
- package/README.md +224 -27
- package/dist/esm/{common-BFea9qeg.js → common-B2rPuIEQ.js} +39 -40
- package/dist/esm/common-B2rPuIEQ.js.map +1 -0
- package/dist/esm/{common-0nTMuN7U.d.ts → common-BMWVqV15.d.ts} +59 -73
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +166 -104
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -1
- package/dist/esm/utils/index.js +1 -1
- package/package.json +6 -6
- package/dist/esm/common-BFea9qeg.js.map +0 -1
package/dist/esm/index.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { HTTPError, ValidationError, createCombinedSignal, createTimeoutSignal, deterministicHashFn, extraOptionDefaults, fallBackRouteSchemaKey, getBody, getCurrentRouteSchemaKeyAndMainInitURL, getFetchImpl, getFullAndNormalizedURL, getHeaders, getMethod, handleConfigValidation, handleSchemaValidation, isArray, isBoolean, isFunction, isHTTPErrorInstance, isObject, isPlainObject, isReadableStream, isString, isValidationErrorInstance, splitBaseConfig, splitConfig, waitFor } from "./common-
|
1
|
+
import { HTTPError, ValidationError, createCombinedSignal, createTimeoutSignal, deterministicHashFn, extraOptionDefaults, fallBackRouteSchemaKey, getBody, getCurrentRouteSchemaKeyAndMainInitURL, getFetchImpl, getFullAndNormalizedURL, getHeaders, getMethod, handleConfigValidation, handleSchemaValidation, isArray, isBoolean, isFunction, isHTTPErrorInstance, isObject, isPlainObject, isReadableStream, isString, isValidationErrorInstance, splitBaseConfig, splitConfig, waitFor } from "./common-B2rPuIEQ.js";
|
2
2
|
|
3
3
|
//#region src/result.ts
|
4
4
|
const getResponseType = (response, parser) => ({
|
@@ -6,8 +6,7 @@ const getResponseType = (response, parser) => ({
|
|
6
6
|
blob: () => response.blob(),
|
7
7
|
formData: () => response.formData(),
|
8
8
|
json: async () => {
|
9
|
-
|
10
|
-
return parser(text);
|
9
|
+
return parser(await response.text());
|
11
10
|
},
|
12
11
|
stream: () => response.body,
|
13
12
|
text: () => response.text()
|
@@ -21,14 +20,14 @@ const textTypes = new Set([
|
|
21
20
|
const JSON_REGEX = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(;.+)?$/i;
|
22
21
|
const detectResponseType = (response) => {
|
23
22
|
const initContentType = response.headers.get("content-type");
|
24
|
-
if (!initContentType) return extraOptionDefaults
|
23
|
+
if (!initContentType) return extraOptionDefaults.responseType;
|
25
24
|
const contentType = initContentType.split(";")[0] ?? "";
|
26
25
|
if (JSON_REGEX.test(contentType)) return "json";
|
27
26
|
if (textTypes.has(contentType) || contentType.startsWith("text/")) return "text";
|
28
27
|
return "blob";
|
29
28
|
};
|
30
29
|
const resolveResponseData = (response, responseType, parser) => {
|
31
|
-
const selectedParser = parser ?? extraOptionDefaults
|
30
|
+
const selectedParser = parser ?? extraOptionDefaults.responseParser;
|
32
31
|
const selectedResponseType = responseType ?? detectResponseType(response);
|
33
32
|
const RESPONSE_TYPE_LOOKUP = getResponseType(response, selectedParser);
|
34
33
|
if (!Object.hasOwn(RESPONSE_TYPE_LOOKUP, selectedResponseType)) throw new Error(`Invalid response type: ${responseType}`);
|
@@ -107,8 +106,8 @@ const getCustomizedErrorResult = (errorResult, customErrorInfo) => {
|
|
107
106
|
|
108
107
|
//#endregion
|
109
108
|
//#region src/hooks.ts
|
110
|
-
const
|
111
|
-
|
109
|
+
const getHookRegistriesAndKeys = () => {
|
110
|
+
const hookRegistries = {
|
112
111
|
onError: /* @__PURE__ */ new Set(),
|
113
112
|
onRequest: /* @__PURE__ */ new Set(),
|
114
113
|
onRequestError: /* @__PURE__ */ new Set(),
|
@@ -121,9 +120,13 @@ const getHookRegistries = () => {
|
|
121
120
|
onSuccess: /* @__PURE__ */ new Set(),
|
122
121
|
onValidationError: /* @__PURE__ */ new Set()
|
123
122
|
};
|
123
|
+
return {
|
124
|
+
hookRegistries,
|
125
|
+
hookRegistryKeys: Object.keys(hookRegistries)
|
126
|
+
};
|
124
127
|
};
|
125
128
|
const composeAllHooks = (hooksArray, hooksExecutionMode) => {
|
126
|
-
const
|
129
|
+
const composedHook = async (ctx) => {
|
127
130
|
switch (hooksExecutionMode) {
|
128
131
|
case "parallel":
|
129
132
|
await Promise.all(hooksArray.map((uniqueHook) => uniqueHook?.(ctx)));
|
@@ -134,9 +137,9 @@ const composeAllHooks = (hooksArray, hooksExecutionMode) => {
|
|
134
137
|
default:
|
135
138
|
}
|
136
139
|
};
|
137
|
-
return
|
140
|
+
return composedHook;
|
138
141
|
};
|
139
|
-
const
|
142
|
+
const executeHooks = async (...hookResultsOrPromise) => {
|
140
143
|
await Promise.all(hookResultsOrPromise);
|
141
144
|
};
|
142
145
|
const executeHooksInCatchBlock = async (hookResultsOrPromise, hookInfo) => {
|
@@ -195,11 +198,11 @@ const toStreamableRequest = async (context) => {
|
|
195
198
|
request,
|
196
199
|
requestInstance
|
197
200
|
};
|
198
|
-
await
|
201
|
+
await executeHooks(options.onRequestStream?.(requestStreamContext));
|
199
202
|
for await (const chunk of body) {
|
200
203
|
transferredBytes += chunk.byteLength;
|
201
204
|
totalBytes = Math.max(totalBytes, transferredBytes);
|
202
|
-
await
|
205
|
+
await executeHooks(options.onRequestStream?.({
|
203
206
|
...requestStreamContext,
|
204
207
|
event: createProgressEvent({
|
205
208
|
chunk,
|
@@ -239,11 +242,11 @@ const toStreamableResponse = async (context) => {
|
|
239
242
|
request,
|
240
243
|
response
|
241
244
|
};
|
242
|
-
await
|
245
|
+
await executeHooks(options.onResponseStream?.(responseStreamContext));
|
243
246
|
for await (const chunk of body) {
|
244
247
|
transferredBytes += chunk.byteLength;
|
245
248
|
totalBytes = Math.max(totalBytes, transferredBytes);
|
246
|
-
await
|
249
|
+
await executeHooks(options.onResponseStream?.({
|
247
250
|
...responseStreamContext,
|
248
251
|
event: createProgressEvent({
|
249
252
|
chunk,
|
@@ -262,19 +265,20 @@ const toStreamableResponse = async (context) => {
|
|
262
265
|
//#region src/dedupe.ts
|
263
266
|
const createDedupeStrategy = async (context) => {
|
264
267
|
const { $GlobalRequestInfoCache: $GlobalRequestInfoCache$1, $LocalRequestInfoCache, baseConfig, config, newFetchController, options: globalOptions, request: globalRequest } = context;
|
265
|
-
const dedupeStrategy = globalOptions.dedupeStrategy ?? extraOptionDefaults
|
268
|
+
const dedupeStrategy = globalOptions.dedupeStrategy ?? globalOptions.dedupe?.strategy ?? extraOptionDefaults.dedupeStrategy;
|
266
269
|
const resolvedDedupeStrategy = isFunction(dedupeStrategy) ? dedupeStrategy(context) : dedupeStrategy;
|
267
270
|
const getDedupeKey = () => {
|
268
271
|
if (!(resolvedDedupeStrategy === "cancel" || resolvedDedupeStrategy === "defer")) return null;
|
269
|
-
|
272
|
+
const dedupeKey$1 = globalOptions.dedupeKey ?? globalOptions.dedupe?.key;
|
273
|
+
if (dedupeKey$1) return isFunction(dedupeKey$1) ? dedupeKey$1(context) : dedupeKey$1;
|
270
274
|
return `${globalOptions.fullURL}-${deterministicHashFn({
|
271
275
|
options: globalOptions,
|
272
276
|
request: globalRequest
|
273
277
|
})}`;
|
274
278
|
};
|
275
279
|
const dedupeKey = getDedupeKey();
|
276
|
-
const dedupeCacheScope = globalOptions.dedupeCacheScope ?? extraOptionDefaults
|
277
|
-
const dedupeCacheScopeKey = globalOptions.dedupeCacheScopeKey ?? extraOptionDefaults
|
280
|
+
const dedupeCacheScope = globalOptions.dedupeCacheScope ?? globalOptions.dedupe?.cacheScope ?? extraOptionDefaults.dedupeCacheScope;
|
281
|
+
const dedupeCacheScopeKey = globalOptions.dedupeCacheScopeKey ?? globalOptions.dedupe?.cacheScopeKey ?? extraOptionDefaults.dedupeCacheScopeKey;
|
278
282
|
if (dedupeCacheScope === "global" && !$GlobalRequestInfoCache$1.has(dedupeCacheScopeKey)) $GlobalRequestInfoCache$1.set(dedupeCacheScopeKey, /* @__PURE__ */ new Map());
|
279
283
|
const $RequestInfoCache = dedupeCacheScope === "global" ? $GlobalRequestInfoCache$1.get(dedupeCacheScopeKey) : $LocalRequestInfoCache;
|
280
284
|
const $RequestInfoCacheOrNull = dedupeKey !== null ? $RequestInfoCache : null;
|
@@ -285,8 +289,8 @@ const createDedupeStrategy = async (context) => {
|
|
285
289
|
if (dedupeKey !== null) await waitFor(.1);
|
286
290
|
const prevRequestInfo = $RequestInfoCacheOrNull?.get(dedupeKey);
|
287
291
|
const getAbortErrorMessage = () => {
|
288
|
-
if (globalOptions.dedupeKey) return `Duplicate request detected - Aborted previous request with key '${dedupeKey}'
|
289
|
-
return `Duplicate request
|
292
|
+
if (globalOptions.dedupeKey) return `Duplicate request detected - Aborted previous request with key '${dedupeKey}'`;
|
293
|
+
return `Duplicate request aborted - Aborted previous request to '${globalOptions.fullURL}'`;
|
290
294
|
};
|
291
295
|
const handleRequestCancelStrategy = () => {
|
292
296
|
if (!(prevRequestInfo && resolvedDedupeStrategy === "cancel")) return;
|
@@ -296,8 +300,7 @@ const createDedupeStrategy = async (context) => {
|
|
296
300
|
return Promise.resolve();
|
297
301
|
};
|
298
302
|
const handleRequestDeferStrategy = async (deferContext) => {
|
299
|
-
const { options: localOptions, request: localRequest } = deferContext;
|
300
|
-
const fetchApi = getFetchImpl(localOptions.customFetchImpl);
|
303
|
+
const { fetchApi, options: localOptions, request: localRequest } = deferContext;
|
301
304
|
const shouldUsePromiseFromCache = prevRequestInfo && resolvedDedupeStrategy === "defer";
|
302
305
|
const streamableContext = {
|
303
306
|
baseConfig,
|
@@ -328,6 +331,25 @@ const createDedupeStrategy = async (context) => {
|
|
328
331
|
};
|
329
332
|
};
|
330
333
|
|
334
|
+
//#endregion
|
335
|
+
//#region src/middlewares.ts
|
336
|
+
const getMiddlewareRegistriesAndKeys = () => {
|
337
|
+
const middlewareRegistries = { fetchMiddleware: /* @__PURE__ */ new Set() };
|
338
|
+
return {
|
339
|
+
middlewareRegistries,
|
340
|
+
middlewareRegistryKeys: Object.keys(middlewareRegistries)
|
341
|
+
};
|
342
|
+
};
|
343
|
+
const composeAllMiddlewares = (middlewareArray) => {
|
344
|
+
let composedMiddleware;
|
345
|
+
for (const currentMiddleware of middlewareArray) {
|
346
|
+
if (!currentMiddleware) continue;
|
347
|
+
const previousMiddleware = composedMiddleware;
|
348
|
+
composedMiddleware = previousMiddleware ? (fetchImpl) => currentMiddleware(previousMiddleware(fetchImpl)) : currentMiddleware;
|
349
|
+
}
|
350
|
+
return composedMiddleware;
|
351
|
+
};
|
352
|
+
|
331
353
|
//#endregion
|
332
354
|
//#region src/plugins.ts
|
333
355
|
const getResolvedPlugins = (context) => {
|
@@ -336,27 +358,11 @@ const getResolvedPlugins = (context) => {
|
|
336
358
|
};
|
337
359
|
const initializePlugins = async (context) => {
|
338
360
|
const { baseConfig, config, initURL, options, request } = context;
|
339
|
-
const
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
const baseHook = baseConfig[key];
|
345
|
-
const instanceHook = config[key];
|
346
|
-
const mainHook = isArray(baseHook) && Boolean(instanceHook) ? [baseHook, instanceHook].flat() : overriddenHook;
|
347
|
-
if (!mainHook) continue;
|
348
|
-
hookRegistries[key].add(mainHook);
|
349
|
-
}
|
350
|
-
};
|
351
|
-
const addPluginHooks = (pluginHooks) => {
|
352
|
-
for (const key of hookRegistryKeyArray) {
|
353
|
-
const pluginHook = pluginHooks[key];
|
354
|
-
if (!pluginHook) continue;
|
355
|
-
hookRegistries[key].add(pluginHook);
|
356
|
-
}
|
357
|
-
};
|
358
|
-
const hookRegistrationOrder = options.hooksRegistrationOrder ?? extraOptionDefaults().hooksRegistrationOrder;
|
359
|
-
if (hookRegistrationOrder === "mainFirst") addMainHooks();
|
361
|
+
const { addMainHooks, addMainMiddlewares, addPluginHooks, addPluginMiddlewares, getResolvedHooks, getResolvedMiddlewares } = setupHooksAndMiddlewares({
|
362
|
+
baseConfig,
|
363
|
+
config,
|
364
|
+
options
|
365
|
+
});
|
360
366
|
const { currentRouteSchemaKey, mainInitURL } = getCurrentRouteSchemaKeyAndMainInitURL({
|
361
367
|
baseExtraOptions: baseConfig,
|
362
368
|
extraOptions: config,
|
@@ -366,15 +372,9 @@ const initializePlugins = async (context) => {
|
|
366
372
|
let resolvedInitURL = mainInitURL;
|
367
373
|
let resolvedOptions = options;
|
368
374
|
let resolvedRequestOptions = request;
|
369
|
-
const executePluginSetupFn = async (
|
370
|
-
if (!
|
371
|
-
const initResult = await
|
372
|
-
baseConfig,
|
373
|
-
config,
|
374
|
-
initURL,
|
375
|
-
options,
|
376
|
-
request
|
377
|
-
});
|
375
|
+
const executePluginSetupFn = async (pluginSetup) => {
|
376
|
+
if (!pluginSetup) return;
|
377
|
+
const initResult = await pluginSetup(context);
|
378
378
|
if (!isPlainObject(initResult)) return;
|
379
379
|
const urlString = initResult.initURL?.toString();
|
380
380
|
if (isString(urlString)) {
|
@@ -386,53 +386,122 @@ const initializePlugins = async (context) => {
|
|
386
386
|
resolvedCurrentRouteSchemaKey = newResult.currentRouteSchemaKey;
|
387
387
|
resolvedInitURL = newResult.mainInitURL;
|
388
388
|
}
|
389
|
-
if (isPlainObject(initResult.request)) resolvedRequestOptions =
|
390
|
-
|
389
|
+
if (isPlainObject(initResult.request)) resolvedRequestOptions = {
|
390
|
+
...resolvedRequestOptions,
|
391
|
+
...initResult.request
|
392
|
+
};
|
393
|
+
if (isPlainObject(initResult.options)) resolvedOptions = {
|
394
|
+
...resolvedOptions,
|
395
|
+
...initResult.options
|
396
|
+
};
|
391
397
|
};
|
392
398
|
const resolvedPlugins = getResolvedPlugins({
|
393
399
|
baseConfig,
|
394
400
|
options
|
395
401
|
});
|
396
402
|
for (const plugin of resolvedPlugins) {
|
397
|
-
await
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
if (hookRegistry.size === 0) continue;
|
405
|
-
const flattenedHookArray = [...hookRegistry].flat();
|
406
|
-
if (flattenedHookArray.length === 0) continue;
|
407
|
-
const hooksExecutionMode = options.hooksExecutionMode ?? extraOptionDefaults().hooksExecutionMode;
|
408
|
-
resolvedHooks[key] = composeAllHooks(flattenedHookArray, hooksExecutionMode);
|
403
|
+
const [, pluginHooks, pluginMiddlewares] = await Promise.all([
|
404
|
+
executePluginSetupFn(plugin.setup),
|
405
|
+
isFunction(plugin.hooks) ? plugin.hooks(context) : plugin.hooks,
|
406
|
+
isFunction(plugin.middlewares) ? plugin.middlewares(context) : plugin.middlewares
|
407
|
+
]);
|
408
|
+
pluginHooks && addPluginHooks(pluginHooks);
|
409
|
+
pluginMiddlewares && addPluginMiddlewares(pluginMiddlewares);
|
409
410
|
}
|
411
|
+
addMainHooks();
|
412
|
+
addMainMiddlewares();
|
413
|
+
const resolvedHooks = getResolvedHooks();
|
414
|
+
const resolvedMiddlewares = getResolvedMiddlewares();
|
410
415
|
return {
|
411
416
|
resolvedCurrentRouteSchemaKey,
|
412
417
|
resolvedHooks,
|
413
418
|
resolvedInitURL,
|
419
|
+
resolvedMiddlewares,
|
414
420
|
resolvedOptions,
|
415
421
|
resolvedRequestOptions
|
416
422
|
};
|
417
423
|
};
|
424
|
+
const setupHooksAndMiddlewares = (context) => {
|
425
|
+
const { baseConfig, config, options } = context;
|
426
|
+
const { hookRegistries, hookRegistryKeys } = getHookRegistriesAndKeys();
|
427
|
+
const { middlewareRegistries, middlewareRegistryKeys } = getMiddlewareRegistriesAndKeys();
|
428
|
+
const addMainHooks = () => {
|
429
|
+
for (const hookName of hookRegistryKeys) {
|
430
|
+
const overriddenHook = options[hookName];
|
431
|
+
const baseHook = baseConfig[hookName];
|
432
|
+
const instanceHook = config[hookName];
|
433
|
+
const mainHook = isArray(baseHook) && instanceHook ? [baseHook, instanceHook].flat() : overriddenHook;
|
434
|
+
mainHook && hookRegistries[hookName].add(mainHook);
|
435
|
+
}
|
436
|
+
};
|
437
|
+
const addPluginHooks = (pluginHooks) => {
|
438
|
+
for (const hookName of hookRegistryKeys) {
|
439
|
+
const pluginHook = pluginHooks[hookName];
|
440
|
+
pluginHook && hookRegistries[hookName].add(pluginHook);
|
441
|
+
}
|
442
|
+
};
|
443
|
+
const addMainMiddlewares = () => {
|
444
|
+
for (const middlewareName of middlewareRegistryKeys) {
|
445
|
+
const baseMiddleware = baseConfig[middlewareName];
|
446
|
+
const instanceMiddleware = config[middlewareName];
|
447
|
+
baseMiddleware && middlewareRegistries[middlewareName].add(baseMiddleware);
|
448
|
+
instanceMiddleware && middlewareRegistries[middlewareName].add(instanceMiddleware);
|
449
|
+
}
|
450
|
+
};
|
451
|
+
const addPluginMiddlewares = (pluginMiddlewares) => {
|
452
|
+
for (const middlewareName of middlewareRegistryKeys) {
|
453
|
+
const pluginMiddleware = pluginMiddlewares[middlewareName];
|
454
|
+
if (!pluginMiddleware) continue;
|
455
|
+
middlewareRegistries[middlewareName].add(pluginMiddleware);
|
456
|
+
}
|
457
|
+
};
|
458
|
+
const getResolvedHooks = () => {
|
459
|
+
const resolvedHooks = {};
|
460
|
+
for (const [hookName, hookRegistry] of Object.entries(hookRegistries)) {
|
461
|
+
if (hookRegistry.size === 0) continue;
|
462
|
+
const flattenedHookArray = [...hookRegistry].flat();
|
463
|
+
if (flattenedHookArray.length === 0) continue;
|
464
|
+
resolvedHooks[hookName] = composeAllHooks(flattenedHookArray, options.hooksExecutionMode ?? extraOptionDefaults.hooksExecutionMode);
|
465
|
+
}
|
466
|
+
return resolvedHooks;
|
467
|
+
};
|
468
|
+
const getResolvedMiddlewares = () => {
|
469
|
+
const resolvedMiddlewares = {};
|
470
|
+
for (const [middlewareName, middlewareRegistry] of Object.entries(middlewareRegistries)) {
|
471
|
+
if (middlewareRegistry.size === 0) continue;
|
472
|
+
const middlewareArray = [...middlewareRegistry];
|
473
|
+
if (middlewareArray.length === 0) continue;
|
474
|
+
resolvedMiddlewares[middlewareName] = composeAllMiddlewares(middlewareArray);
|
475
|
+
}
|
476
|
+
return resolvedMiddlewares;
|
477
|
+
};
|
478
|
+
return {
|
479
|
+
addMainHooks,
|
480
|
+
addMainMiddlewares,
|
481
|
+
addPluginHooks,
|
482
|
+
addPluginMiddlewares,
|
483
|
+
getResolvedHooks,
|
484
|
+
getResolvedMiddlewares
|
485
|
+
};
|
486
|
+
};
|
418
487
|
|
419
488
|
//#endregion
|
420
489
|
//#region src/retry.ts
|
421
490
|
const getLinearDelay = (currentAttemptCount, options) => {
|
422
491
|
const retryDelay = options.retryDelay ?? options.retry?.delay;
|
423
|
-
return (isFunction(retryDelay) ? retryDelay(currentAttemptCount) : retryDelay) ?? extraOptionDefaults
|
492
|
+
return (isFunction(retryDelay) ? retryDelay(currentAttemptCount) : retryDelay) ?? extraOptionDefaults.retryDelay;
|
424
493
|
};
|
425
494
|
const getExponentialDelay = (currentAttemptCount, options) => {
|
426
|
-
const retryDelay = options.retryDelay ?? options.retry?.delay ?? extraOptionDefaults
|
495
|
+
const retryDelay = options.retryDelay ?? options.retry?.delay ?? extraOptionDefaults.retryDelay;
|
427
496
|
const resolvedRetryDelay = isFunction(retryDelay) ? retryDelay(currentAttemptCount) : retryDelay;
|
428
|
-
const maxDelay = options.retryMaxDelay ?? options.retry?.maxDelay ?? extraOptionDefaults
|
497
|
+
const maxDelay = options.retryMaxDelay ?? options.retry?.maxDelay ?? extraOptionDefaults.retryMaxDelay;
|
429
498
|
const exponentialDelay = resolvedRetryDelay * 2 ** currentAttemptCount;
|
430
499
|
return Math.min(exponentialDelay, maxDelay);
|
431
500
|
};
|
432
501
|
const createRetryStrategy = (ctx) => {
|
433
502
|
const { options, request } = ctx;
|
434
503
|
const currentAttemptCount = options["~retryAttemptCount"] ?? 1;
|
435
|
-
const retryStrategy = options.retryStrategy ?? options.retry?.strategy ?? extraOptionDefaults
|
504
|
+
const retryStrategy = options.retryStrategy ?? options.retry?.strategy ?? extraOptionDefaults.retryStrategy;
|
436
505
|
const getDelay = () => {
|
437
506
|
switch (retryStrategy) {
|
438
507
|
case "exponential": return getExponentialDelay(currentAttemptCount, options);
|
@@ -442,13 +511,13 @@ const createRetryStrategy = (ctx) => {
|
|
442
511
|
};
|
443
512
|
const shouldAttemptRetry = async () => {
|
444
513
|
if (isBoolean(request.signal) && request.signal.aborted) return false;
|
445
|
-
const retryCondition = options.retryCondition ?? options.retry?.condition ?? extraOptionDefaults
|
446
|
-
const maximumRetryAttempts = options.retryAttempts ?? options.retry?.attempts ?? extraOptionDefaults
|
514
|
+
const retryCondition = options.retryCondition ?? options.retry?.condition ?? extraOptionDefaults.retryCondition;
|
515
|
+
const maximumRetryAttempts = options.retryAttempts ?? options.retry?.attempts ?? extraOptionDefaults.retryAttempts;
|
447
516
|
const customRetryCondition = await retryCondition(ctx);
|
448
517
|
if (!(currentAttemptCount <= maximumRetryAttempts && customRetryCondition)) return false;
|
449
|
-
const retryMethods = new Set(options.retryMethods ?? options.retry?.methods ?? extraOptionDefaults
|
518
|
+
const retryMethods = new Set(options.retryMethods ?? options.retry?.methods ?? extraOptionDefaults.retryMethods);
|
450
519
|
const includesMethod = isString(ctx.request.method) && retryMethods.size > 0 ? retryMethods.has(ctx.request.method) : true;
|
451
|
-
const retryStatusCodes = new Set(options.retryStatusCodes ?? options.retry?.statusCodes ?? extraOptionDefaults
|
520
|
+
const retryStatusCodes = new Set(options.retryStatusCodes ?? options.retry?.statusCodes ?? extraOptionDefaults.retryStatusCodes);
|
452
521
|
const includesStatusCodes = ctx.response != null && retryStatusCodes.size > 0 ? retryStatusCodes.has(ctx.response.status) : true;
|
453
522
|
return includesMethod && includesStatusCodes;
|
454
523
|
};
|
@@ -485,7 +554,7 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
485
554
|
...baseFetchOptions,
|
486
555
|
...!shouldSkipAutoMergeForRequest && fetchOptions
|
487
556
|
};
|
488
|
-
const { resolvedCurrentRouteSchemaKey, resolvedHooks, resolvedInitURL, resolvedOptions, resolvedRequestOptions } = await initializePlugins({
|
557
|
+
const { resolvedCurrentRouteSchemaKey, resolvedHooks, resolvedInitURL, resolvedMiddlewares, resolvedOptions, resolvedRequestOptions } = await initializePlugins({
|
489
558
|
baseConfig,
|
490
559
|
config,
|
491
560
|
initURL: initURLOrURLObject.toString(),
|
@@ -498,9 +567,10 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
498
567
|
params: resolvedOptions.params,
|
499
568
|
query: resolvedOptions.query
|
500
569
|
});
|
501
|
-
|
570
|
+
const options = {
|
502
571
|
...resolvedOptions,
|
503
572
|
...resolvedHooks,
|
573
|
+
...resolvedMiddlewares,
|
504
574
|
fullURL,
|
505
575
|
initURL: resolvedInitURL,
|
506
576
|
initURLNormalized: normalizedInitURL
|
@@ -512,7 +582,7 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
512
582
|
initURL: resolvedInitURL,
|
513
583
|
method: resolvedRequestOptions.method
|
514
584
|
});
|
515
|
-
|
585
|
+
const request = {
|
516
586
|
...resolvedRequestOptions,
|
517
587
|
method: initMethod,
|
518
588
|
signal: combinedSignal
|
@@ -528,7 +598,7 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
528
598
|
});
|
529
599
|
try {
|
530
600
|
await handleRequestCancelStrategy();
|
531
|
-
await
|
601
|
+
await executeHooks(options.onRequest?.({
|
532
602
|
baseConfig,
|
533
603
|
config,
|
534
604
|
options,
|
@@ -541,10 +611,7 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
541
611
|
options,
|
542
612
|
requestOptions: request
|
543
613
|
});
|
544
|
-
if (shouldApplySchemaOutput) options
|
545
|
-
...options,
|
546
|
-
...extraOptionsValidationResult
|
547
|
-
};
|
614
|
+
if (shouldApplySchemaOutput) Object.assign(options, extraOptionsValidationResult);
|
548
615
|
const validMethod = getMethod({
|
549
616
|
initURL: resolvedInitURL,
|
550
617
|
method: shouldApplySchemaOutput ? requestOptionsValidationResult?.method : request.method
|
@@ -559,27 +626,26 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
559
626
|
body: validBody,
|
560
627
|
headers: shouldApplySchemaOutput ? requestOptionsValidationResult?.headers : resolvedHeaders
|
561
628
|
});
|
562
|
-
request
|
563
|
-
...
|
564
|
-
...
|
565
|
-
...
|
566
|
-
|
567
|
-
|
568
|
-
await executeHooksInTryBlock(options.onRequestReady?.({
|
629
|
+
Object.assign(request, {
|
630
|
+
...validBody && { body: validBody },
|
631
|
+
...validHeaders && { headers: validHeaders },
|
632
|
+
...validMethod && { method: validMethod }
|
633
|
+
});
|
634
|
+
await executeHooks(options.onRequestReady?.({
|
569
635
|
baseConfig,
|
570
636
|
config,
|
571
637
|
options,
|
572
638
|
request
|
573
639
|
}));
|
574
640
|
const response = await handleRequestDeferStrategy({
|
641
|
+
fetchApi: getFetchImpl(options.customFetchImpl, options.fetchMiddleware),
|
575
642
|
options,
|
576
643
|
request
|
577
644
|
});
|
578
645
|
const shouldCloneResponse = resolvedDedupeStrategy === "defer" || options.cloneResponse;
|
579
646
|
if (!response.ok) {
|
580
|
-
const errorData = await resolveResponseData(shouldCloneResponse ? response.clone() : response, options.responseType, options.responseParser);
|
581
647
|
const validErrorData = await handleSchemaValidation(resolvedSchema, "errorData", {
|
582
|
-
inputValue:
|
648
|
+
inputValue: await resolveResponseData(shouldCloneResponse ? response.clone() : response, options.responseType, options.responseParser),
|
583
649
|
response,
|
584
650
|
schemaConfig: resolvedSchemaConfig
|
585
651
|
});
|
@@ -589,21 +655,19 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
589
655
|
response
|
590
656
|
}, { cause: validErrorData });
|
591
657
|
}
|
592
|
-
const successData = await resolveResponseData(shouldCloneResponse ? response.clone() : response, options.responseType, options.responseParser);
|
593
|
-
const validSuccessData = await handleSchemaValidation(resolvedSchema, "data", {
|
594
|
-
inputValue: successData,
|
595
|
-
response,
|
596
|
-
schemaConfig: resolvedSchemaConfig
|
597
|
-
});
|
598
658
|
const successContext = {
|
599
659
|
baseConfig,
|
600
660
|
config,
|
601
|
-
data:
|
661
|
+
data: await handleSchemaValidation(resolvedSchema, "data", {
|
662
|
+
inputValue: await resolveResponseData(shouldCloneResponse ? response.clone() : response, options.responseType, options.responseParser),
|
663
|
+
response,
|
664
|
+
schemaConfig: resolvedSchemaConfig
|
665
|
+
}),
|
602
666
|
options,
|
603
667
|
request,
|
604
668
|
response
|
605
669
|
};
|
606
|
-
await
|
670
|
+
await executeHooks(options.onSuccess?.(successContext), options.onResponse?.({
|
607
671
|
...successContext,
|
608
672
|
error: null
|
609
673
|
}));
|
@@ -639,13 +703,11 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
639
703
|
};
|
640
704
|
const hookError = await executeHooksInCatchBlock([options.onRetry?.(retryContext)], hookInfo);
|
641
705
|
if (hookError) return hookError;
|
642
|
-
|
643
|
-
|
644
|
-
const updatedOptions = {
|
706
|
+
await waitFor(getDelay());
|
707
|
+
return callApi$1(initURLOrURLObject, {
|
645
708
|
...config,
|
646
709
|
"~retryAttemptCount": currentAttemptCount + 1
|
647
|
-
};
|
648
|
-
return callApi$1(initURLOrURLObject, updatedOptions);
|
710
|
+
});
|
649
711
|
}
|
650
712
|
if (shouldThrowOnError) throw error;
|
651
713
|
return generalErrorResult;
|