@zayne-labs/callapi 1.8.2 → 1.8.4

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.
@@ -74,27 +74,6 @@ type Auth = BearerOrTokenAuth | BasicAuth | CustomAuth;
74
74
  //#region src/constants/common.d.ts
75
75
  declare const fetchSpecificKeys: (keyof RequestInit | "duplex")[];
76
76
  //#endregion
77
- //#region src/error.d.ts
78
- type ErrorDetails<TErrorData> = {
79
- defaultErrorMessage: CallApiExtraOptions["defaultErrorMessage"];
80
- errorData: TErrorData;
81
- response: Response;
82
- };
83
- declare class HTTPError<TErrorData = Record<string, unknown>> extends Error {
84
- errorData: ErrorDetails<TErrorData>["errorData"];
85
- httpErrorSymbol: symbol;
86
- isHTTPError: boolean;
87
- name: "HTTPError";
88
- response: ErrorDetails<TErrorData>["response"];
89
- constructor(errorDetails: ErrorDetails<TErrorData>, errorOptions?: ErrorOptions);
90
- /**
91
- * @description Checks if the given error is an instance of HTTPError
92
- * @param error - The error to check
93
- * @returns true if the error is an instance of HTTPError, false otherwise
94
- */
95
- static isError<TErrorData>(error: unknown): error is HTTPError<TErrorData>;
96
- }
97
- //#endregion
98
77
  //#region src/types/standard-schema.d.ts
99
78
  /**
100
79
  * The Standard Schema interface.
@@ -195,16 +174,34 @@ declare namespace StandardSchemaV1 {
195
174
  type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
196
175
  }
197
176
  //#endregion
198
- //#region src/validation.d.ts
199
- type InferSchemaResult<TSchema, TFallbackResult = NonNullable<unknown>> = undefined extends TSchema ? TFallbackResult : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TSchema extends AnyFunction<infer TResult> ? Awaited<TResult> : TFallbackResult;
177
+ //#region src/error.d.ts
178
+ type HTTPErrorDetails<TErrorData> = {
179
+ defaultErrorMessage: CallApiExtraOptions["defaultErrorMessage"];
180
+ errorData: TErrorData;
181
+ response: Response;
182
+ };
183
+ declare class HTTPError<TErrorData = Record<string, unknown>> extends Error {
184
+ errorData: HTTPErrorDetails<TErrorData>["errorData"];
185
+ httpErrorSymbol: symbol;
186
+ isHTTPError: boolean;
187
+ name: "HTTPError";
188
+ response: HTTPErrorDetails<TErrorData>["response"];
189
+ constructor(errorDetails: HTTPErrorDetails<TErrorData>, errorOptions?: ErrorOptions);
190
+ /**
191
+ * @description Checks if the given error is an instance of HTTPError
192
+ * @param error - The error to check
193
+ * @returns true if the error is an instance of HTTPError, false otherwise
194
+ */
195
+ static isError<TErrorData>(error: unknown): error is HTTPError<TErrorData>;
196
+ }
200
197
  type ValidationErrorDetails = {
201
198
  issues: readonly StandardSchemaV1.Issue[];
202
199
  response: Response | null;
203
200
  };
204
201
  declare class ValidationError extends Error {
205
- errorData: readonly StandardSchemaV1.Issue[];
202
+ errorData: ValidationErrorDetails["issues"];
206
203
  name: string;
207
- response: Response | null;
204
+ response: ValidationErrorDetails["response"];
208
205
  validationErrorSymbol: symbol;
209
206
  constructor(details: ValidationErrorDetails, errorOptions?: ErrorOptions);
210
207
  /**
@@ -214,6 +211,9 @@ declare class ValidationError extends Error {
214
211
  */
215
212
  static isError(error: unknown): error is ValidationError;
216
213
  }
214
+ //#endregion
215
+ //#region src/validation.d.ts
216
+ type InferSchemaResult<TSchema, TFallbackResult = NonNullable<unknown>> = undefined extends TSchema ? TFallbackResult : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TSchema extends AnyFunction<infer TResult> ? Awaited<TResult> : TFallbackResult;
217
217
  interface CallApiSchemaConfig {
218
218
  /**
219
219
  * The base url of the schema. By default it's the baseURL of the fetch instance.
@@ -299,7 +299,7 @@ type AllowedQueryParamValues = UnmaskType<boolean | number | string>;
299
299
  type Params = UnmaskType<Record<string, AllowedQueryParamValues> | AllowedQueryParamValues[]>;
300
300
  type Query = UnmaskType<Record<string, AllowedQueryParamValues>>;
301
301
  type InitURLOrURLObject = string | URL;
302
- interface UrlOptions {
302
+ interface URLOptions {
303
303
  /**
304
304
  * Base URL to be prepended to all request URLs
305
305
  */
@@ -334,8 +334,8 @@ type PluginInitResult = Partial<Omit<PluginInitContext, "initURL" | "request"> &
334
334
  initURL: InitURLOrURLObject;
335
335
  request: CallApiRequestOptions;
336
336
  }>;
337
- type PluginHooksWithMoreOptions<TMoreOptions = unknown> = HooksOrHooksArray<never, never, TMoreOptions>;
338
- type PluginHooks<TData = never, TErrorData = never, TMoreOptions = unknown> = HooksOrHooksArray<TData, TErrorData, TMoreOptions>;
337
+ type PluginHooksWithMoreOptions<TMoreOptions = unknown> = HooksOrHooksArray<never, never, CallApiSchema, string, TMoreOptions>;
338
+ type PluginHooks<TData = never, TErrorData = never, TMoreOptions = unknown> = HooksOrHooksArray<TData, TErrorData, CallApiSchema, string, TMoreOptions>;
339
339
  interface CallApiPlugin {
340
340
  /**
341
341
  * Defines additional options that can be passed to callApi
@@ -458,51 +458,51 @@ declare global {
458
458
  type PluginExtraOptions<TPluginOptions = unknown> = {
459
459
  options: Partial<TPluginOptions>;
460
460
  };
461
- interface Hooks<TData = DefaultDataType, TErrorData = DefaultDataType, TPluginOptions = unknown> {
461
+ interface Hooks<TData = DefaultDataType, TErrorData = DefaultDataType, TPluginOptions = unknown, TSchema extends CallApiSchema = CallApiSchema, TCurrentRouteKey extends string = string> {
462
462
  /**
463
463
  * Hook that will be called when any error occurs within the request/response lifecycle, regardless of whether the error is from the api or not.
464
464
  * It is basically a combination of `onRequestError` and `onResponseError` hooks
465
465
  */
466
- onError?: (context: ErrorContext<TErrorData> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
466
+ onError?: (context: ErrorContext<TErrorData> & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
467
467
  /**
468
468
  * Hook that will be called just before the request is being made.
469
469
  */
470
- onRequest?: (context: RequestContext & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
470
+ onRequest?: (context: RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
471
471
  /**
472
472
  * Hook that will be called when an error occurs during the fetch request.
473
473
  */
474
- onRequestError?: (context: RequestErrorContext & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
474
+ onRequestError?: (context: RequestErrorContext & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
475
475
  /**
476
476
  * Hook that will be called when upload stream progress is tracked
477
477
  */
478
- onRequestStream?: (context: RequestStreamContext & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
478
+ onRequestStream?: (context: RequestStreamContext & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
479
479
  /**
480
480
  * Hook that will be called when any response is received from the api, whether successful or not
481
481
  */
482
- onResponse?: (context: ResponseContext<TData, TErrorData> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
482
+ onResponse?: (context: ResponseContext<TData, TErrorData> & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
483
483
  /**
484
484
  * Hook that will be called when an error response is received from the api.
485
485
  */
486
- onResponseError?: (context: ResponseErrorContext<TErrorData> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
486
+ onResponseError?: (context: ResponseErrorContext<TErrorData> & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
487
487
  /**
488
488
  * Hook that will be called when download stream progress is tracked
489
489
  */
490
- onResponseStream?: (context: ResponseStreamContext & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
490
+ onResponseStream?: (context: ResponseStreamContext & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
491
491
  /**
492
492
  * Hook that will be called when a request is retried.
493
493
  */
494
- onRetry?: (response: RetryContext<TErrorData> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
494
+ onRetry?: (response: RetryContext<TErrorData> & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
495
495
  /**
496
496
  * Hook that will be called when a successful response is received from the api.
497
497
  */
498
- onSuccess?: (context: SuccessContext<TData> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
498
+ onSuccess?: (context: SuccessContext<TData> & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
499
499
  /**
500
500
  * Hook that will be called when a validation error occurs.
501
501
  */
502
- onValidationError?: (context: ValidationErrorContext & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
502
+ onValidationError?: (context: ValidationErrorContext & RequestContext<TSchema, TCurrentRouteKey> & PluginExtraOptions<TPluginOptions>) => Awaitable<unknown>;
503
503
  }
504
- type HooksOrHooksArray<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = unknown> = { [Key in keyof Hooks<TData, TErrorData, TMoreOptions>]: Hooks<TData, TErrorData, TMoreOptions>[Key] | Array<Hooks<TData, TErrorData, TMoreOptions>[Key]> };
505
- type RequestContext = {
504
+ type HooksOrHooksArray<TData = DefaultDataType, TErrorData = DefaultDataType, TSchema extends CallApiSchema = CallApiSchema, TCurrentRouteKey extends string = string, TMoreOptions = unknown> = { [Key in keyof Hooks<TData, TErrorData, TMoreOptions, TSchema, TCurrentRouteKey>]: Hooks<TData, TErrorData, TMoreOptions, TSchema, TCurrentRouteKey>[Key] | Array<Hooks<TData, TErrorData, TMoreOptions, TSchema, TCurrentRouteKey>[Key]> };
505
+ type RequestContext<TSchema extends CallApiSchema = CallApiSchema, TCurrentRouteKey extends string = string> = {
506
506
  /**
507
507
  * Config object passed to createFetchClient
508
508
  */
@@ -515,13 +515,13 @@ type RequestContext = {
515
515
  * Merged options consisting of extra options from createFetchClient, the callApi instance and default options.
516
516
  *
517
517
  */
518
- options: CombinedCallApiExtraOptions;
518
+ options: InferExtraOptions<TSchema, TCurrentRouteKey> & Omit<CombinedCallApiExtraOptions, keyof InferExtraOptions<CallApiSchema, string>>;
519
519
  /**
520
520
  * Merged request consisting of request options from createFetchClient, the callApi instance and default request options.
521
521
  */
522
- request: CallApiRequestOptionsForHooks;
522
+ request: Omit<CallApiRequestOptionsForHooks, keyof Omit<InferRequestOptions<TSchema, CallApiSchemaConfig, string>, "headers">> & Omit<InferRequestOptions<TSchema, CallApiSchemaConfig, string>, "headers">;
523
523
  };
524
- type ResponseContext<TData, TErrorData> = RequestContext & ({
524
+ type ResponseContext<TData, TErrorData> = {
525
525
  data: null;
526
526
  error: PossibleHTTPError<TErrorData>;
527
527
  response: Response;
@@ -533,44 +533,50 @@ type ResponseContext<TData, TErrorData> = RequestContext & ({
533
533
  data: TData;
534
534
  error: null;
535
535
  response: Response;
536
- });
537
- type ValidationErrorContext = RequestContext & {
536
+ };
537
+ type ValidationErrorContext = {
538
538
  error: ValidationError;
539
539
  response: Response | null;
540
540
  };
541
- type SuccessContext<TData> = RequestContext & {
541
+ type SuccessContext<TData> = {
542
542
  data: TData;
543
543
  response: Response;
544
544
  };
545
- type RequestErrorContext = UnmaskType<RequestContext & {
545
+ type RequestErrorContext = {
546
546
  error: PossibleJavaScriptOrValidationError;
547
547
  response: null;
548
- }>;
549
- type ResponseErrorContext<TErrorData> = UnmaskType<RequestContext & {
548
+ };
549
+ type ResponseErrorContext<TErrorData> = Extract<ErrorContext<TErrorData>, {
550
550
  error: PossibleHTTPError<TErrorData>;
551
- response: Response;
552
551
  }>;
553
- type RetryContext<TErrorData> = UnmaskType<ErrorContext<TErrorData> & {
552
+ type RetryContext<TErrorData> = ErrorContext<TErrorData> & {
554
553
  retryAttemptCount: number;
555
- }>;
556
- type ErrorContext<TErrorData> = UnmaskType<RequestContext & ({
554
+ };
555
+ type ErrorContext<TErrorData> = {
557
556
  error: PossibleHTTPError<TErrorData>;
558
557
  response: Response;
559
558
  } | {
560
559
  error: PossibleJavaScriptOrValidationError;
561
560
  response: Response | null;
562
- })>;
563
- type RequestStreamContext = UnmaskType<RequestContext & {
561
+ };
562
+ type RequestStreamContext = {
564
563
  event: StreamProgressEvent;
565
564
  requestInstance: Request;
566
- }>;
567
- type ResponseStreamContext = UnmaskType<RequestContext & {
565
+ };
566
+ type ResponseStreamContext = {
568
567
  event: StreamProgressEvent;
569
568
  response: Response;
570
- }>;
569
+ };
571
570
  //#endregion
572
571
  //#region src/dedupe.d.ts
573
572
  type DedupeOptions = {
573
+ /**
574
+ * Defines the scope of the deduplication cache, can be set to "global" | "local".
575
+ * - If set to "global", the deduplication cache will be shared across all requests, regardless of whether they shared the same `createFetchClient` or not.
576
+ * - If set to "local", the deduplication cache will be scoped to the current request.
577
+ * @default "local"
578
+ */
579
+ dedupeCacheScope?: "global" | "local";
574
580
  /**
575
581
  * Custom request key to be used to identify a request in the fetch deduplication strategy.
576
582
  * @default the full request url + string formed from the request options
@@ -669,7 +675,7 @@ type InferMethodOption<TSchema extends CallApiSchema, TSchemaConfig extends Call
669
675
  */
670
676
  method?: InferSchemaResult<TSchema["method"], InferMethodFromURL<TInitURL>>;
671
677
  }>>;
672
- type HeadersOption = UnmaskType<Record<"Authorization", CommonAuthorizationHeaders> | Record<"Content-Type", CommonContentTypes> | Record<CommonRequestHeaders, string | undefined> | Record<string, string | undefined> | RequestInit["headers"]>;
678
+ type HeadersOption = UnmaskType<Record<"Authorization", CommonAuthorizationHeaders | undefined> | Record<"Content-Type", CommonContentTypes | undefined> | Record<CommonRequestHeaders, string | undefined> | Record<string, string | undefined> | Array<[string, string]>>;
673
679
  type InferHeadersOption<TSchema extends CallApiSchema> = MakeSchemaOptionRequired<TSchema["headers"], {
674
680
  /**
675
681
  * Headers to be used in the request.
@@ -693,14 +699,14 @@ type InferQueryOption<TSchema extends CallApiSchema> = MakeSchemaOptionRequired<
693
699
  query?: InferSchemaResult<TSchema["query"], Query>;
694
700
  }>;
695
701
  type EmptyString = "";
696
- type InferParamFromPath<TPath> = TPath extends `${infer IgnoredPrefix}:${infer TCurrentParam}/${infer TRemainingPath}` ? TCurrentParam extends EmptyString ? InferParamFromPath<TRemainingPath> : Prettify<Record<TCurrentParam | (Params extends InferParamFromPath<TRemainingPath> ? never : keyof Extract<InferParamFromPath<TRemainingPath>, Record<string, unknown>>), AllowedQueryParamValues>> | [AllowedQueryParamValues, ...(Params extends InferParamFromPath<TRemainingPath> ? [] : Extract<InferParamFromPath<TRemainingPath>, unknown[]>)] : TPath extends `${infer IgnoredPrefix}:${infer TCurrentParam}` ? TCurrentParam extends EmptyString ? Params : Prettify<Record<TCurrentParam, AllowedQueryParamValues>> | [AllowedQueryParamValues] : Params;
697
- type InferParamsOption<TPath, TSchema extends CallApiSchema> = MakeSchemaOptionRequired<TSchema["params"], {
702
+ type InferParamFromRoute<TRoute> = TRoute extends `${infer IgnoredPrefix}:${infer TCurrentParam}/${infer TRemainingPath}` ? TCurrentParam extends EmptyString ? InferParamFromRoute<TRemainingPath> : Prettify<Record<TCurrentParam | (Params extends InferParamFromRoute<TRemainingPath> ? never : keyof Extract<InferParamFromRoute<TRemainingPath>, Record<string, unknown>>), AllowedQueryParamValues>> | [AllowedQueryParamValues, ...(Params extends InferParamFromRoute<TRemainingPath> ? [] : Extract<InferParamFromRoute<TRemainingPath>, unknown[]>)] : TRoute extends `${infer IgnoredPrefix}:${infer TCurrentParam}` ? TCurrentParam extends EmptyString ? Params : Prettify<Record<TCurrentParam, AllowedQueryParamValues>> | [AllowedQueryParamValues] : Params;
703
+ type InferParamsOption<TSchema extends CallApiSchema, TCurrentRouteKey> = MakeSchemaOptionRequired<TSchema["params"], {
698
704
  /**
699
705
  * Parameters to be appended to the URL (i.e: /:id)
700
706
  */
701
- params?: InferSchemaResult<TSchema["params"], InferParamFromPath<TPath>>;
707
+ params?: InferSchemaResult<TSchema["params"], InferParamFromRoute<TCurrentRouteKey>>;
702
708
  }>;
703
- type InferExtraOptions<TSchema extends CallApiSchema, TPath> = InferMetaOption<TSchema> & InferParamsOption<TPath, TSchema> & InferQueryOption<TSchema>;
709
+ type InferExtraOptions<TSchema extends CallApiSchema, TCurrentRouteKey> = InferMetaOption<TSchema> & InferParamsOption<TSchema, TCurrentRouteKey> & InferQueryOption<TSchema>;
704
710
  type InferPluginOptions<TPluginArray extends CallApiPlugin[]> = UnionToIntersection<TPluginArray extends Array<infer TPlugin> ? TPlugin extends CallApiPlugin ? TPlugin["defineExtraOptions"] extends AnyFunction<infer TResult> ? InferSchemaResult<TResult> : never : never : never>;
705
711
  type ExtractKeys<TUnion, TSelectedUnion extends TUnion> = Extract<TUnion, TSelectedUnion>;
706
712
  type ResultModeOption<TErrorData, TResultMode extends ResultModeUnion> = TErrorData extends false ? {
@@ -714,11 +720,13 @@ type ResultModeOption<TErrorData, TResultMode extends ResultModeUnion> = TErrorD
714
720
  } : {
715
721
  resultMode: TResultMode;
716
722
  };
717
- type ThrowOnErrorOption<TErrorData> = TErrorData extends false ? {
723
+ type ThrowOnErrorOption<TErrorData, TThrowOnError extends DefaultThrowOnError> = TErrorData extends false ? {
718
724
  throwOnError: true;
719
725
  } : TErrorData extends false | undefined ? {
720
726
  throwOnError?: true;
721
- } : NonNullable<unknown>;
727
+ } : {
728
+ throwOnError?: TThrowOnError | ((context: ErrorContext<TErrorData>) => TThrowOnError);
729
+ };
722
730
  //#endregion
723
731
  //#region src/types/common.d.ts
724
732
  type FetchSpecificKeysUnion = Exclude<(typeof fetchSpecificKeys)[number], "body" | "headers" | "method">;
@@ -741,14 +749,18 @@ type CallApiRequestOptions = Prettify<{
741
749
  method?: MethodUnion;
742
750
  } & Pick<ModifiedRequestInit, FetchSpecificKeysUnion>>;
743
751
  type CallApiRequestOptionsForHooks = Omit<CallApiRequestOptions, "headers"> & {
744
- headers?: Record<string, string | undefined>;
752
+ headers: Record<string, string | undefined>;
745
753
  };
746
754
  type FetchImpl = UnmaskType<(input: string | Request | URL, init?: RequestInit) => Promise<Response>>;
747
- type SharedExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TPluginArray extends CallApiPlugin[] = DefaultPluginArray> = {
755
+ type SharedExtraOptions<TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion> = DedupeOptions & URLOptions & {
748
756
  /**
749
757
  * Authorization header value.
750
758
  */
751
759
  auth?: string | Auth | null;
760
+ /**
761
+ * Base URL for the request.
762
+ */
763
+ baseURL?: string;
752
764
  /**
753
765
  * Custom function to serialize the body object into a string.
754
766
  */
@@ -832,14 +844,13 @@ type SharedExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, T
832
844
  * The function is passed the error object and can be used to conditionally throw the error
833
845
  * @default false
834
846
  */
835
- throwOnError?: TThrowOnError | ((context: ErrorContext<TErrorData>) => TThrowOnError);
847
+ throwOnError?: TThrowOnError;
836
848
  /**
837
849
  * Request timeout in milliseconds
838
850
  */
839
851
  timeout?: number;
840
- } & DedupeOptions & HooksOrHooksArray<TData, TErrorData, Partial<InferPluginOptions<TPluginArray>>> & Partial<InferPluginOptions<TPluginArray>> & RetryOptions<TErrorData> & ResultModeOption<TErrorData, TResultMode> & ThrowOnErrorOption<TErrorData> & UrlOptions;
841
- type BaseCallApiExtraOptions<TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseSchema extends BaseCallApiSchema = BaseCallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig> = SharedExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBasePluginArray> & {
842
- baseURL?: string;
852
+ };
853
+ type BaseCallApiExtraOptions<TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseSchema extends BaseCallApiSchema = BaseCallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig> = SharedExtraOptions<TBaseResultMode, TBaseThrowOnError, TBaseResponseType> & {
843
854
  /**
844
855
  * An array of base callApi plugins. It allows you to extend the behavior of the library.
845
856
  */
@@ -873,8 +884,8 @@ type BaseCallApiExtraOptions<TBaseData = DefaultDataType, TBaseErrorData = Defau
873
884
  * ```
874
885
  */
875
886
  skipAutoMergeFor?: "all" | "options" | "request";
876
- };
877
- type CallApiExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TPluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseSchema extends BaseCallApiSchema = BaseCallApiSchema, TSchema extends CallApiSchema = CallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TCurrentRouteKey extends string = string> = SharedExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TPluginArray> & {
887
+ } & HooksOrHooksArray<TBaseData, TBaseErrorData, CallApiSchema, string, Partial<InferPluginOptions<TBasePluginArray>>> & Partial<InferPluginOptions<TBasePluginArray>> & ResultModeOption<TBaseErrorData, TBaseResultMode> & RetryOptions<TBaseErrorData> & ThrowOnErrorOption<TBaseErrorData, TBaseThrowOnError>;
888
+ type CallApiExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TPluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseSchema extends BaseCallApiSchema = BaseCallApiSchema, TSchema extends CallApiSchema = CallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TCurrentRouteKey extends string = string> = SharedExtraOptions<TResultMode, TThrowOnError, TResponseType> & {
878
889
  /**
879
890
  * An array of instance CallApi plugins. It allows you to extend the behavior of the library.
880
891
  */
@@ -894,7 +905,7 @@ type CallApiExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType,
894
905
  schemaConfig?: Writeable<TSchemaConfig, "deep"> | ((context: {
895
906
  baseSchemaConfig: NonNullable<Writeable<TBaseSchemaConfig, "deep">>;
896
907
  }) => Writeable<TSchemaConfig, "deep">);
897
- };
908
+ } & HooksOrHooksArray<TData, TErrorData, CallApiSchema, TCurrentRouteKey, Partial<InferPluginOptions<TPluginArray>>> & Partial<InferPluginOptions<TPluginArray>> & ResultModeOption<TErrorData, TResultMode> & RetryOptions<TErrorData> & ThrowOnErrorOption<TErrorData, TThrowOnError>;
898
909
  interface CombinedCallApiExtraOptions extends Omit<CallApiExtraOptions, keyof Hooks>, Hooks {}
899
910
  type BaseCallApiConfig<TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchema extends BaseCallApiSchema = BaseCallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray> = (CallApiRequestOptions & BaseCallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBasePluginArray, TBaseSchema, TBaseSchemaConfig>) | ((context: {
900
911
  initURL: string;
@@ -905,5 +916,5 @@ type CallApiConfig<TData = DefaultDataType, TErrorData = DefaultDataType, TResul
905
916
  type CallApiParameters<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchema extends BaseCallApiSchema = BaseCallApiSchema, TSchema extends CallApiSchema = CallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TInitURL extends InferInitURL<BaseCallApiSchema, TSchemaConfig> = InferInitURL<BaseCallApiSchema, TSchemaConfig>, TCurrentRouteKey extends string = string, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TPluginArray extends CallApiPlugin[] = DefaultPluginArray> = [initURL: TInitURL, config?: CallApiConfig<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TBaseSchema, TSchema, TBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteKey, TBasePluginArray, TPluginArray>];
906
917
  type CallApiResult<TData, TErrorData, TResultMode extends ResultModeUnion, TThrowOnError extends boolean, TResponseType extends ResponseTypeUnion> = Promise<GetCallApiResult<TData, TErrorData, TResultMode, TThrowOnError, TResponseType>>;
907
918
  //#endregion
908
- export { BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchema, CallApiConfig, CallApiExtraOptions, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, CombinedCallApiExtraOptions, DefaultDataType, DefaultPluginArray, DefaultThrowOnError, ErrorContext, GetCurrentRouteKey, HTTPError, Hooks, HooksOrHooksArray, InferInitURL, InferParamFromPath, InferSchemaResult, PluginHooks, PluginHooksWithMoreOptions, PluginInitContext, PossibleHTTPError, PossibleJavaScriptError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeUnion, ResultModeUnion, RetryOptions, SuccessContext, ValidationError, definePlugin, defineSchema };
909
- //# sourceMappingURL=common-Cg-2dq0u.d.ts.map
919
+ export { BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchema, CallApiConfig, CallApiExtraOptions, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, CombinedCallApiExtraOptions, DedupeOptions, DefaultDataType, DefaultPluginArray, DefaultThrowOnError, ErrorContext, GetCurrentRouteKey, HTTPError, Hooks, HooksOrHooksArray, InferInitURL, InferParamFromRoute, InferSchemaResult, PluginHooks, PluginHooksWithMoreOptions, PluginInitContext, PossibleHTTPError, PossibleJavaScriptError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeUnion, ResultModeUnion, RetryOptions, SuccessContext, URLOptions, ValidationError, definePlugin, defineSchema };
920
+ //# sourceMappingURL=common-BR-RZ84M.d.ts.map
@@ -1,4 +1,4 @@
1
- import { BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchema, CallApiConfig, CallApiExtraOptions, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, CombinedCallApiExtraOptions, DefaultDataType, DefaultPluginArray, DefaultThrowOnError, ErrorContext, GetCurrentRouteKey, HTTPError, Hooks, HooksOrHooksArray, InferInitURL, InferParamFromPath, InferSchemaResult, PluginHooks, PluginHooksWithMoreOptions, PluginInitContext, PossibleHTTPError, PossibleJavaScriptError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeUnion, ResultModeUnion, RetryOptions, SuccessContext, ValidationError, definePlugin, defineSchema } from "./common-Cg-2dq0u.js";
1
+ import { BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchema, CallApiConfig, CallApiExtraOptions, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, CombinedCallApiExtraOptions, DedupeOptions, DefaultDataType, DefaultPluginArray, DefaultThrowOnError, ErrorContext, GetCurrentRouteKey, HTTPError, Hooks, HooksOrHooksArray, InferInitURL, InferParamFromRoute, InferSchemaResult, PluginHooks, PluginHooksWithMoreOptions, PluginInitContext, PossibleHTTPError, PossibleJavaScriptError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeUnion, ResultModeUnion, RetryOptions, SuccessContext, URLOptions, ValidationError, definePlugin, defineSchema } from "./common-BR-RZ84M.js";
2
2
 
3
3
  //#region src/createFetchClient.d.ts
4
4
 
@@ -8,5 +8,5 @@ declare const callApi: <TData = unknown, TErrorData = unknown, TResultMode exten
8
8
  //#region src/defineParameters.d.ts
9
9
  declare const defineParameters: <TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchema extends BaseCallApiSchema = BaseCallApiSchema, TSchema extends CallApiSchema = CallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TInitURL extends InferInitURL<BaseCallApiSchema, TSchemaConfig> = InferInitURL<BaseCallApiSchema, TSchemaConfig>, TCurrentRouteKey extends string = string, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TPluginArray extends CallApiPlugin[] = DefaultPluginArray>(...parameters: CallApiParameters<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TBaseSchema, TSchema, TBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteKey, TBasePluginArray, TPluginArray>) => CallApiParameters<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TBaseSchema, TSchema, TBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteKey, TBasePluginArray, TPluginArray>;
10
10
  //#endregion
11
- export { BaseCallApiExtraOptions, BaseCallApiSchema, CallApiExtraOptions, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, CombinedCallApiExtraOptions, ErrorContext, HTTPError, Hooks, HooksOrHooksArray, InferParamFromPath, InferSchemaResult, PluginHooks, PluginHooksWithMoreOptions, PluginInitContext, PossibleHTTPError, PossibleJavaScriptError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeUnion, ResultModeUnion, RetryOptions, SuccessContext, ValidationError, callApi, createFetchClient, defineParameters, definePlugin, defineSchema };
11
+ export { BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchema, CallApiConfig, CallApiExtraOptions, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, CombinedCallApiExtraOptions, DedupeOptions, ErrorContext, HTTPError, Hooks, HooksOrHooksArray, InferParamFromRoute, InferSchemaResult, PluginHooks, PluginHooksWithMoreOptions, PluginInitContext, PossibleHTTPError, PossibleJavaScriptError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeUnion, ResultModeUnion, RetryOptions, SuccessContext, URLOptions, ValidationError, callApi, createFetchClient, defineParameters, definePlugin, defineSchema };
12
12
  //# sourceMappingURL=index.d.ts.map
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { HTTPError, ValidationError, commonDefaults, createCombinedSignal, createTimeoutSignal, dedupeDefaults, defineSchema, getBody, getFetchImpl, getHeaders, handleOptionsValidation, handleValidation, hookDefaults, isArray, isFunction, isHTTPError, isHTTPErrorInstance, isObject, isPlainObject, isReadableStream, isString, isValidationErrorInstance, requestOptionDefaults, responseDefaults, retryDefaults, routeKeyMethods, splitBaseConfig, splitConfig, toQueryString, waitFor } from "./utils-B6o_GJAn.js";
1
+ import { HTTPError, ValidationError, commonDefaults, createCombinedSignal, createTimeoutSignal, dedupeDefaults, defineEnum, getBody, getFetchImpl, getHeaders, hookDefaults, isArray, isFunction, isHTTPError, isHTTPErrorInstance, isObject, isPlainObject, isReadableStream, isString, isValidationErrorInstance, requestOptionDefaults, responseDefaults, retryDefaults, splitBaseConfig, splitConfig, toQueryString, waitFor } from "./utils-BbmhntpS.js";
2
2
 
3
3
  //#region src/result.ts
4
4
  const getResponseType = (response, parser) => ({
@@ -248,7 +248,7 @@ const getAbortErrorMessage = (dedupeKey, fullURL) => {
248
248
  return dedupeKey ? `Duplicate request detected - Aborting previous request with key '${dedupeKey}' as a new request was initiated` : `Duplicate request detected - Aborting previous request to '${fullURL}' as a new request with identical options was initiated`;
249
249
  };
250
250
  const createDedupeStrategy = async (context) => {
251
- const { $RequestInfoCache, baseConfig, config, newFetchController, options: globalOptions, request: globalRequest } = context;
251
+ const { $GlobalRequestInfoCache: $GlobalRequestInfoCache$1, $LocalRequestInfoCache, baseConfig, config, newFetchController, options: globalOptions, request: globalRequest } = context;
252
252
  const dedupeStrategy = globalOptions.dedupeStrategy ?? dedupeDefaults.dedupeStrategy;
253
253
  const generateDedupeKey = () => {
254
254
  const shouldHaveDedupeKey = dedupeStrategy === "cancel" || dedupeStrategy === "defer";
@@ -259,6 +259,11 @@ const createDedupeStrategy = async (context) => {
259
259
  })}`;
260
260
  };
261
261
  const dedupeKey = globalOptions.dedupeKey ?? generateDedupeKey();
262
+ const dedupeCacheScope = globalOptions.dedupeCacheScope ?? dedupeDefaults.dedupeCacheScope;
263
+ const $RequestInfoCache = {
264
+ global: $GlobalRequestInfoCache$1,
265
+ local: $LocalRequestInfoCache
266
+ }[dedupeCacheScope];
262
267
  const $RequestInfoCacheOrNull = dedupeKey !== null ? $RequestInfoCache : null;
263
268
  /******
264
269
  * == Add a small delay to the execution to ensure proper request deduplication when multiple requests with the same key start simultaneously.
@@ -274,24 +279,25 @@ const createDedupeStrategy = async (context) => {
274
279
  prevRequestInfo.controller.abort(reason);
275
280
  return Promise.resolve();
276
281
  };
277
- const handleRequestDeferStrategy = async (options, request) => {
278
- const fetchApi = getFetchImpl(options.customFetchImpl);
282
+ const handleRequestDeferStrategy = async (deferContext) => {
283
+ const { options: localOptions, request: localRequest } = deferContext;
284
+ const fetchApi = getFetchImpl(localOptions.customFetchImpl);
279
285
  const shouldUsePromiseFromCache = prevRequestInfo && dedupeStrategy === "defer";
280
- const requestObjectForStream = isReadableStream(request.body) ? {
281
- ...request,
282
- duplex: request.duplex ?? "half"
283
- } : request;
284
- const requestInstance = new Request(options.fullURL, requestObjectForStream);
286
+ const requestObjectForStream = isReadableStream(localRequest.body) ? {
287
+ ...localRequest,
288
+ duplex: localRequest.duplex ?? "half"
289
+ } : localRequest;
290
+ const requestInstance = new Request(localOptions.fullURL, requestObjectForStream);
285
291
  await toStreamableRequest({
286
292
  baseConfig,
287
293
  config,
288
- options,
289
- request,
294
+ options: localOptions,
295
+ request: localRequest,
290
296
  requestInstance: requestInstance.clone()
291
297
  });
292
298
  const getFetchApiPromise = () => {
293
- if (isReadableStream(request.body)) return fetchApi(requestInstance.clone());
294
- return fetchApi(options.fullURL, request);
299
+ if (isReadableStream(localRequest.body)) return fetchApi(requestInstance.clone());
300
+ return fetchApi(localOptions.fullURL, localRequest);
295
301
  };
296
302
  const responsePromise = shouldUsePromiseFromCache ? prevRequestInfo.responsePromise : getFetchApiPromise();
297
303
  $RequestInfoCacheOrNull?.set(dedupeKey, {
@@ -301,8 +307,8 @@ const createDedupeStrategy = async (context) => {
301
307
  const streamableResponse = toStreamableResponse({
302
308
  baseConfig,
303
309
  config,
304
- options,
305
- request,
310
+ options: localOptions,
311
+ request: localRequest,
306
312
  response: await responsePromise
307
313
  });
308
314
  return streamableResponse;
@@ -333,7 +339,7 @@ const initializePlugins = async (context) => {
333
339
  const clonedHookRegistries = structuredClone(hookRegistries);
334
340
  const addMainHooks = () => {
335
341
  for (const key of Object.keys(clonedHookRegistries)) {
336
- const baseHook = baseConfig[key];
342
+ const baseHook = baseConfig;
337
343
  const instanceHook = config[key];
338
344
  const overriddenHook = options[key];
339
345
  const mainHook = isArray(baseHook) && Boolean(instanceHook) ? [baseHook, instanceHook].flat() : overriddenHook;
@@ -439,6 +445,105 @@ const createRetryStrategy = (ctx) => {
439
445
  };
440
446
  };
441
447
 
448
+ //#endregion
449
+ //#region src/validation.ts
450
+ const handleValidatorFunction = async (validator, inputData) => {
451
+ try {
452
+ const result = await validator(inputData);
453
+ return {
454
+ issues: void 0,
455
+ value: result
456
+ };
457
+ } catch (error) {
458
+ return {
459
+ issues: error,
460
+ value: void 0
461
+ };
462
+ }
463
+ };
464
+ const standardSchemaParser = async (schema, inputData, response) => {
465
+ const result = isFunction(schema) ? await handleValidatorFunction(schema, inputData) : await schema["~standard"].validate(inputData);
466
+ if (result.issues) throw new ValidationError({
467
+ issues: result.issues,
468
+ response: response ?? null
469
+ }, { cause: result.issues });
470
+ return result.value;
471
+ };
472
+ const routeKeyMethods = defineEnum([
473
+ "delete",
474
+ "get",
475
+ "patch",
476
+ "post",
477
+ "put"
478
+ ]);
479
+ const defineSchema = (baseSchema) => {
480
+ return baseSchema;
481
+ };
482
+ const handleValidation = async (schema, validationOptions) => {
483
+ const { inputValue, response, schemaConfig } = validationOptions;
484
+ if (!schema || schemaConfig?.disableRuntimeValidation) return inputValue;
485
+ const validResult = await standardSchemaParser(schema, inputValue, response);
486
+ return validResult;
487
+ };
488
+ const extraOptionsToBeValidated = [
489
+ "meta",
490
+ "params",
491
+ "query"
492
+ ];
493
+ const handleExtraOptionsValidation = async (validationOptions) => {
494
+ const { extraOptions, schema, schemaConfig } = validationOptions;
495
+ const validationResultArray = await Promise.all(extraOptionsToBeValidated.map((propertyKey) => handleValidation(schema?.[propertyKey], {
496
+ inputValue: extraOptions[propertyKey],
497
+ schemaConfig
498
+ })));
499
+ const validatedResultObject = {};
500
+ for (const [index, propertyKey] of extraOptionsToBeValidated.entries()) {
501
+ const validationResult = validationResultArray[index];
502
+ if (validationResult === void 0) continue;
503
+ validatedResultObject[propertyKey] = validationResult;
504
+ }
505
+ return validatedResultObject;
506
+ };
507
+ const requestOptionsToBeValidated = [
508
+ "body",
509
+ "headers",
510
+ "method"
511
+ ];
512
+ const handleRequestOptionsValidation = async (validationOptions) => {
513
+ const { requestOptions, schema, schemaConfig } = validationOptions;
514
+ const validationResultArray = await Promise.all(requestOptionsToBeValidated.map((propertyKey) => handleValidation(schema?.[propertyKey], {
515
+ inputValue: requestOptions[propertyKey],
516
+ schemaConfig
517
+ })));
518
+ const validatedResultObject = {};
519
+ for (const [index, propertyKey] of requestOptionsToBeValidated.entries()) {
520
+ const validationResult = validationResultArray[index];
521
+ if (validationResult === void 0) continue;
522
+ validatedResultObject[propertyKey] = validationResult;
523
+ }
524
+ return validatedResultObject;
525
+ };
526
+ const handleOptionsValidation = async (validationOptions) => {
527
+ const { extraOptions, requestOptions, schema, schemaConfig } = validationOptions;
528
+ if (schemaConfig?.disableRuntimeValidation) return {
529
+ extraOptionsValidationResult: null,
530
+ requestOptionsValidationResult: null
531
+ };
532
+ const [extraOptionsValidationResult, requestOptionsValidationResult] = await Promise.all([handleExtraOptionsValidation({
533
+ extraOptions,
534
+ schema,
535
+ schemaConfig
536
+ }), handleRequestOptionsValidation({
537
+ requestOptions,
538
+ schema,
539
+ schemaConfig
540
+ })]);
541
+ return {
542
+ extraOptionsValidationResult,
543
+ requestOptionsValidationResult
544
+ };
545
+ };
546
+
442
547
  //#endregion
443
548
  //#region src/url.ts
444
549
  const slash = "/";
@@ -507,8 +612,9 @@ const getFullURL = (options) => {
507
612
 
508
613
  //#endregion
509
614
  //#region src/createFetchClient.ts
615
+ const $GlobalRequestInfoCache = /* @__PURE__ */ new Map();
510
616
  const createFetchClient = (initBaseConfig = {}) => {
511
- const $RequestInfoCache = /* @__PURE__ */ new Map();
617
+ const $LocalRequestInfoCache = /* @__PURE__ */ new Map();
512
618
  const callApi$1 = async (...parameters) => {
513
619
  const [initURLOrURLObject, initConfig = {}] = parameters;
514
620
  const [fetchOptions, extraOptions] = splitConfig(initConfig);
@@ -560,10 +666,12 @@ const createFetchClient = (initBaseConfig = {}) => {
560
666
  const combinedSignal = createCombinedSignal(resolvedRequestOptions.signal, timeoutSignal, newFetchController.signal);
561
667
  let request = {
562
668
  ...resolvedRequestOptions,
669
+ headers: resolvedRequestOptions.headers ?? {},
563
670
  signal: combinedSignal
564
671
  };
565
672
  const { dedupeStrategy, handleRequestCancelStrategy, handleRequestDeferStrategy, removeDedupeKeyFromCache } = await createDedupeStrategy({
566
- $RequestInfoCache,
673
+ $GlobalRequestInfoCache,
674
+ $LocalRequestInfoCache,
567
675
  baseConfig,
568
676
  config,
569
677
  newFetchController,
@@ -589,15 +697,16 @@ const createFetchClient = (initBaseConfig = {}) => {
589
697
  ...options,
590
698
  ...extraOptionsValidationResult
591
699
  };
700
+ const rawBody = shouldApplySchemaOutput ? requestOptionsValidationResult?.body : request.body;
592
701
  const validBody = getBody({
593
- body: shouldApplySchemaOutput ? requestOptionsValidationResult?.body : request.body,
702
+ body: rawBody,
594
703
  bodySerializer: options.bodySerializer
595
704
  });
705
+ const resolvedHeaders = isFunction(fetchOptions.headers) ? fetchOptions.headers({ baseHeaders: baseFetchOptions.headers ?? {} }) : fetchOptions.headers ?? baseFetchOptions.headers;
596
706
  const validHeaders = await getHeaders({
597
707
  auth: options.auth,
598
- baseHeaders: request.headers,
599
- body: request.body,
600
- headers: shouldApplySchemaOutput ? requestOptionsValidationResult?.headers : request.headers
708
+ body: rawBody,
709
+ headers: shouldApplySchemaOutput ? requestOptionsValidationResult?.headers : resolvedHeaders
601
710
  });
602
711
  const validMethod = getMethod({
603
712
  initURL: resolvedInitURL,
@@ -610,7 +719,10 @@ const createFetchClient = (initBaseConfig = {}) => {
610
719
  ...Boolean(validHeaders) && { headers: validHeaders },
611
720
  ...Boolean(validMethod) && { method: validMethod }
612
721
  };
613
- const response = await handleRequestDeferStrategy(options, request);
722
+ const response = await handleRequestDeferStrategy({
723
+ options,
724
+ request
725
+ });
614
726
  const shouldCloneResponse = dedupeStrategy === "defer" || options.cloneResponse;
615
727
  if (!response.ok) {
616
728
  const errorData = await resolveResponseData(shouldCloneResponse ? response.clone() : response, options.responseType, options.responseParser);