@zayne-labs/callapi 1.11.46 → 1.12.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/dist/index.js CHANGED
@@ -1,6 +1,4 @@
1
- import { C as isArray, E as isFunction, O as isReadableStream, T as isBoolean, _ as handleConfigValidation, a as getBody, c as getMethod, d as splitBaseConfig, f as splitConfig, h as getCurrentRouteSchemaKeyAndMainInitURL, i as createTimeoutSignal, k as isString, l as getResolvedHeaders, m as getFullAndNormalizedURL, o as getFetchImpl, p as waitFor, r as createCombinedSignal, s as getHeaders, t as extraOptionDefaults, u as omitKeys, v as handleSchemaValidation, y as HTTPError } from "./defaults-BlLod7dh.js";
2
- import { a as isValidationErrorInstance, n as isHTTPErrorInstance } from "./guards-WJT9aIc_.js";
3
-
1
+ import { A as getFullAndNormalizedURL, B as isFunction, H as isString, M as handleConfigValidation, N as handleSchemaValidation, P as HTTPError, R as isArray, V as isReadableStream, a as getBody, c as getMethod, d as splitBaseConfig, f as splitConfig, g as isHTTPErrorInstance, i as createTimeoutSignal, j as getCurrentRouteSchemaKeyAndMainInitURL, l as getResolvedHeaders, o as getFetchImpl, p as waitUntil, r as createCombinedSignal, s as getHeaders, t as extraOptionDefaults, u as omitKeys, y as isValidationErrorInstance, z as isBoolean } from "./defaults-B0Zvy-uW.js";
4
2
  //#region src/result.ts
5
3
  const getResponseType = (response, responseParser) => ({
6
4
  arrayBuffer: () => response.arrayBuffer(),
@@ -109,7 +107,6 @@ const getCustomizedErrorResult = (errorResult, customErrorInfo) => {
109
107
  }
110
108
  };
111
109
  };
112
-
113
110
  //#endregion
114
111
  //#region src/hooks.ts
115
112
  const getHookRegistriesAndKeys = () => {
@@ -159,7 +156,6 @@ const executeHooksInCatchBlock = async (hookResultsOrPromise, hookInfo) => {
159
156
  return errorResult;
160
157
  }
161
158
  };
162
-
163
159
  //#endregion
164
160
  //#region src/stream.ts
165
161
  const createProgressEvent = (options) => {
@@ -263,7 +259,6 @@ const toStreamableResponse = (context) => {
263
259
  } });
264
260
  return new Response(stream, response);
265
261
  };
266
-
267
262
  //#endregion
268
263
  //#region src/dedupe.ts
269
264
  const createDedupeStrategy = async (context) => {
@@ -284,8 +279,7 @@ const createDedupeStrategy = async (context) => {
284
279
  if (!dedupeKey) return;
285
280
  const dedupeCacheScope = globalOptions.dedupeCacheScope ?? extraOptionDefaults.dedupeCacheScope;
286
281
  const dedupeCacheScopeKey = getDedupeCacheScopeKey();
287
- if (dedupeCacheScope === "global" && !$GlobalRequestInfoCache.has(dedupeCacheScopeKey)) $GlobalRequestInfoCache.set(dedupeCacheScopeKey, /* @__PURE__ */ new Map());
288
- const $RequestInfoCache = dedupeCacheScope === "global" ? $GlobalRequestInfoCache.get(dedupeCacheScopeKey) : $LocalRequestInfoCache;
282
+ const $RequestInfoCache = dedupeCacheScope === "global" ? $GlobalRequestInfoCache.get(dedupeCacheScopeKey) ?? $GlobalRequestInfoCache.set(dedupeCacheScopeKey, /* @__PURE__ */ new Map()).get(dedupeCacheScopeKey) : $LocalRequestInfoCache;
289
283
  return {
290
284
  delete: () => $RequestInfoCache?.delete(dedupeKey),
291
285
  get: () => $RequestInfoCache?.get(dedupeKey),
@@ -317,7 +311,7 @@ const createDedupeStrategy = async (context) => {
317
311
  * simultaneously (same problem as microtasks). Any non-zero value (even 0.0000000001) forces
318
312
  * proper sequential task queue scheduling, ensuring each request gets its own task slot.
319
313
  */
320
- if (dedupeKey !== null) await waitFor(.001);
314
+ if (dedupeKey !== null) await waitUntil(.01);
321
315
  const prevRequestInfo = $RequestInfoCache?.get();
322
316
  const getAbortErrorMessage = () => {
323
317
  if (globalOptions.dedupeKey) return `Duplicate request detected - Aborted previous request with key '${dedupeKey}'`;
@@ -360,7 +354,6 @@ const createDedupeStrategy = async (context) => {
360
354
  resolvedDedupeStrategy
361
355
  };
362
356
  };
363
-
364
357
  //#endregion
365
358
  //#region src/middlewares.ts
366
359
  const getMiddlewareRegistriesAndKeys = () => {
@@ -389,7 +382,6 @@ const composeMiddlewaresFromArray = (middlewareArray) => {
389
382
  }
390
383
  return composedMiddleware;
391
384
  };
392
-
393
385
  //#endregion
394
386
  //#region src/plugins.ts
395
387
  const getResolvedPlugins = (context) => {
@@ -397,30 +389,16 @@ const getResolvedPlugins = (context) => {
397
389
  return isFunction(options.plugins) ? options.plugins({ basePlugins: baseConfig.plugins ?? [] }) : options.plugins ?? [];
398
390
  };
399
391
  const initializePlugins = async (setupContext) => {
400
- const { baseConfig, config, initURL, options, request } = setupContext;
392
+ const { baseConfig, config, currentRouteSchemaKey, mainInitURL, options, request } = setupContext;
401
393
  const { addMainHooks, addMainMiddlewares, addPluginHooks, addPluginMiddlewares, getResolvedHooks, getResolvedMiddlewares } = setupHooksAndMiddlewares({
402
394
  baseConfig,
403
395
  config,
404
396
  options
405
397
  });
406
- const initURLResult = getCurrentRouteSchemaKeyAndMainInitURL({
407
- baseExtraOptions: baseConfig,
408
- extraOptions: config,
409
- initURL
410
- });
411
- let resolvedCurrentRouteSchemaKey = initURLResult.currentRouteSchemaKey;
412
- let resolvedInitURL = initURLResult.mainInitURL;
398
+ let resolvedCurrentRouteSchemaKey = currentRouteSchemaKey;
399
+ let resolvedInitURL = mainInitURL;
413
400
  const resolvedOptions = options;
414
- const resolvedRequest = Object.assign(request, {
415
- headers: getResolvedHeaders({
416
- baseHeaders: baseConfig.headers,
417
- headers: config.headers
418
- }),
419
- method: getMethod({
420
- initURL: resolvedInitURL,
421
- method: request.method
422
- })
423
- });
401
+ const resolvedRequest = request;
424
402
  const executePluginSetupFn = async (pluginSetup) => {
425
403
  if (!pluginSetup) return;
426
404
  const initResult = await pluginSetup(setupContext);
@@ -527,7 +505,27 @@ const setupHooksAndMiddlewares = (context) => {
527
505
  getResolvedMiddlewares
528
506
  };
529
507
  };
530
-
508
+ //#endregion
509
+ //#region src/refetch.ts
510
+ const createRefetchManager = (ctx) => {
511
+ const { callApi, callApiArgs, options } = ctx;
512
+ const currentRefetchCount = options["~refetchCount"] ?? 1;
513
+ const refetch = async (refetchOptionOverrides) => {
514
+ const maxRefetchAttempts = refetchOptionOverrides?.refetchAttempts ?? options.refetchAttempts ?? extraOptionDefaults.refetchAttempts;
515
+ if (!(currentRefetchCount <= maxRefetchAttempts)) {
516
+ const message = `Maximum refetch attempts (${maxRefetchAttempts}) exceeded. This prevents infinite loops. Increase 'maxRefetchAttempts' if needed.`;
517
+ console.error(message);
518
+ return null;
519
+ }
520
+ const updatedConfig = {
521
+ ...callApiArgs.config,
522
+ ...refetchOptionOverrides,
523
+ "~refetchCount": currentRefetchCount + 1
524
+ };
525
+ return callApi(callApiArgs.initURL, updatedConfig);
526
+ };
527
+ return { refetch };
528
+ };
531
529
  //#endregion
532
530
  //#region src/retry.ts
533
531
  const getLinearDelay = (currentAttemptCount, options) => {
@@ -556,9 +554,9 @@ const createRetryManager = (ctx) => {
556
554
  const shouldAttemptRetry = async () => {
557
555
  if (isBoolean(request.signal) && request.signal.aborted) return false;
558
556
  const retryCondition = options.retryCondition ?? extraOptionDefaults.retryCondition;
559
- const maximumRetryAttempts = options.retryAttempts ?? extraOptionDefaults.retryAttempts;
557
+ const maxRetryAttempts = options.retryAttempts ?? extraOptionDefaults.retryAttempts;
560
558
  const customRetryCondition = await retryCondition(errorContext);
561
- if (!(currentAttemptCount <= maximumRetryAttempts && customRetryCondition)) return false;
559
+ if (!(currentAttemptCount <= maxRetryAttempts && customRetryCondition)) return false;
562
560
  const retryMethods = new Set(options.retryMethods ?? extraOptionDefaults.retryMethods);
563
561
  const includesMethod = isString(request.method) && retryMethods.size > 0 ? retryMethods.has(request.method) : true;
564
562
  const retryStatusCodes = new Set(options.retryStatusCodes ?? extraOptionDefaults.retryStatusCodes);
@@ -572,7 +570,7 @@ const createRetryManager = (ctx) => {
572
570
  };
573
571
  const hookError = await executeHooksInCatchBlock([options.onRetry?.(retryContext)], hookInfo);
574
572
  if (hookError) return hookError;
575
- await waitFor(getDelay());
573
+ await waitUntil(getDelay());
576
574
  const updatedConfig = {
577
575
  ...callApiArgs.config,
578
576
  "~retryAttemptCount": currentAttemptCount + 1
@@ -586,7 +584,6 @@ const createRetryManager = (ctx) => {
586
584
  };
587
585
  return { handleRetryOrGetErrorResult };
588
586
  };
589
-
590
587
  //#endregion
591
588
  //#region src/createFetchClient.ts
592
589
  const $GlobalRequestInfoCache = /* @__PURE__ */ new Map();
@@ -612,12 +609,27 @@ const createFetchClientWithContext = () => {
612
609
  ...baseFetchOptions,
613
610
  ...!shouldSkipAutoMergeForRequest && fetchOptions
614
611
  };
612
+ const initURLResult = getCurrentRouteSchemaKeyAndMainInitURL({
613
+ baseExtraOptions: baseConfig,
614
+ extraOptions: config,
615
+ initURL: initURL.toString()
616
+ });
615
617
  const { resolvedCurrentRouteSchemaKey, resolvedHooks, resolvedInitURL, resolvedMiddlewares, resolvedOptions, resolvedRequest } = await initializePlugins({
616
618
  baseConfig,
617
619
  config,
618
- initURL: initURL.toString(),
620
+ ...initURLResult,
619
621
  options: mergedExtraOptions,
620
- request: mergedRequestOptions
622
+ request: {
623
+ ...mergedRequestOptions,
624
+ headers: getResolvedHeaders({
625
+ baseHeaders: baseConfig.headers,
626
+ headers: config.headers
627
+ }),
628
+ method: getMethod({
629
+ initURL: initURLResult.initURL,
630
+ method: mergedRequestOptions.method
631
+ })
632
+ }
621
633
  });
622
634
  const { fullURL, normalizedInitURL } = getFullAndNormalizedURL({
623
635
  baseURL: resolvedOptions.baseURL,
@@ -625,7 +637,7 @@ const createFetchClientWithContext = () => {
625
637
  params: resolvedOptions.params,
626
638
  query: resolvedOptions.query
627
639
  });
628
- const options = {
640
+ const initOptions = {
629
641
  ...resolvedOptions,
630
642
  ...resolvedHooks,
631
643
  ...resolvedMiddlewares,
@@ -633,6 +645,18 @@ const createFetchClientWithContext = () => {
633
645
  initURL: resolvedInitURL,
634
646
  initURLNormalized: normalizedInitURL
635
647
  };
648
+ const { refetch } = createRefetchManager({
649
+ callApi,
650
+ callApiArgs: {
651
+ config,
652
+ initURL
653
+ },
654
+ options: initOptions
655
+ });
656
+ const options = {
657
+ ...initOptions,
658
+ refetch
659
+ };
636
660
  const newFetchController = new AbortController();
637
661
  const combinedSignal = createCombinedSignal(createTimeoutSignal(options.timeout), resolvedRequest.signal, newFetchController.signal);
638
662
  const request = {
@@ -664,7 +688,7 @@ const createFetchClientWithContext = () => {
664
688
  request
665
689
  });
666
690
  Object.assign(options, extraOptionsValidationResult);
667
- Object.assign(request, {
691
+ const modifiedRequestOptionsValidationResult = {
668
692
  body: getBody({
669
693
  body: requestOptionsValidationResult.body,
670
694
  bodySerializer: options.bodySerializer,
@@ -679,7 +703,8 @@ const createFetchClientWithContext = () => {
679
703
  initURL: resolvedInitURL,
680
704
  method: requestOptionsValidationResult.method
681
705
  })
682
- });
706
+ };
707
+ Object.assign(request, modifiedRequestOptionsValidationResult);
683
708
  const readyRequestContext = {
684
709
  baseConfig,
685
710
  config,
@@ -804,7 +829,7 @@ const createFetchClientWithContext = () => {
804
829
  };
805
830
  const createFetchClient = createFetchClientWithContext();
806
831
  const callApi = createFetchClient();
807
-
808
832
  //#endregion
809
833
  export { callApi, createFetchClient, createFetchClientWithContext };
834
+
810
835
  //# sourceMappingURL=index.js.map