@zayne-labs/callapi 1.7.1 → 1.7.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.
@@ -1,5 +1,3 @@
1
- import { StandardSchemaV1 } from '@standard-schema/spec';
2
-
3
1
  type ValueOrFunctionResult<TValue> = TValue | (() => TValue);
4
2
  /**
5
3
  * Bearer Or Token authentication
@@ -56,6 +54,9 @@ type AnyNumber = number & {
56
54
  z_placeholder?: never;
57
55
  };
58
56
  type AnyFunction<TResult = unknown> = (...args: any) => TResult;
57
+ type Prettify<TObject> = NonNullable<unknown> & {
58
+ [Key in keyof TObject]: TObject[Key];
59
+ };
59
60
  type UnmaskType<TValue> = {
60
61
  _: TValue;
61
62
  }["_"];
@@ -64,56 +65,132 @@ type CommonRequestHeaders = "Access-Control-Allow-Credentials" | "Access-Control
64
65
  type CommonAuthorizationHeaders = `${"Basic" | "Bearer" | "Token"} ${string}`;
65
66
  type CommonContentTypes = "application/epub+zip" | "application/gzip" | "application/json" | "application/ld+json" | "application/octet-stream" | "application/ogg" | "application/pdf" | "application/rtf" | "application/vnd.ms-fontobject" | "application/wasm" | "application/xhtml+xml" | "application/xml" | "application/zip" | "audio/aac" | "audio/mpeg" | "audio/ogg" | "audio/opus" | "audio/webm" | "audio/x-midi" | "font/otf" | "font/ttf" | "font/woff" | "font/woff2" | "image/avif" | "image/bmp" | "image/gif" | "image/jpeg" | "image/png" | "image/svg+xml" | "image/tiff" | "image/webp" | "image/x-icon" | "model/gltf-binary" | "model/gltf+json" | "text/calendar" | "text/css" | "text/csv" | "text/html" | "text/javascript" | "text/plain" | "video/3gpp" | "video/3gpp2" | "video/av1" | "video/mp2t" | "video/mp4" | "video/mpeg" | "video/ogg" | "video/webm" | "video/x-msvideo" | AnyString;
66
67
 
67
- interface CallApiSchemas {
68
- /**
69
- * The schema to use for validating the request body.
70
- */
71
- body?: StandardSchemaV1<Body>;
72
- /**
73
- * The schema to use for validating the response data.
74
- */
75
- data?: StandardSchemaV1;
68
+ type ErrorDetails<TErrorResponse> = {
69
+ defaultErrorMessage: string;
70
+ errorData: TErrorResponse;
71
+ response: Response;
72
+ };
73
+ type ErrorOptions = {
74
+ cause?: unknown;
75
+ };
76
+ declare class HTTPError<TErrorResponse = Record<string, unknown>> {
77
+ cause: ErrorOptions["cause"];
78
+ errorData: ErrorDetails<TErrorResponse>["errorData"];
79
+ isHTTPError: boolean;
80
+ message: string;
81
+ name: "HTTPError";
82
+ response: ErrorDetails<TErrorResponse>["response"];
83
+ constructor(errorDetails: ErrorDetails<TErrorResponse>, errorOptions?: ErrorOptions);
84
+ }
85
+ type PossibleJavaScriptError = UnmaskType<{
86
+ errorData: DOMException | Error | SyntaxError | TypeError;
87
+ message: string;
88
+ name: "AbortError" | "Error" | "SyntaxError" | "TimeoutError" | "TypeError" | (`${string}Error` & {});
89
+ }>;
90
+ type PossibleHTTPError<TErrorData> = UnmaskType<{
91
+ errorData: TErrorData;
92
+ message: string;
93
+ name: "HTTPError";
94
+ }>;
95
+
96
+ /**
97
+ * The Standard Schema interface.
98
+ * @see https://github.com/standard-schema/standard-schema
99
+ */
100
+ interface StandardSchemaV1<Input = unknown, Output = Input> {
76
101
  /**
77
- * The schema to use for validating the response error data.
102
+ * The Standard Schema properties.
78
103
  */
79
- errorData?: StandardSchemaV1;
104
+ readonly "~standard": StandardSchemaV1.Props<Input, Output>;
105
+ }
106
+ declare namespace StandardSchemaV1 {
107
+ /**
108
+ * The Standard Schema properties interface.
109
+ */
110
+ interface Props<Input = unknown, Output = Input> {
111
+ /**
112
+ * Inferred types associated with the schema.
113
+ */
114
+ readonly types?: Types<Input, Output> | undefined;
115
+ /**
116
+ * Validates unknown input values.
117
+ */
118
+ readonly validate: (value: unknown) => Promise<Result<Output>> | Result<Output>;
119
+ /**
120
+ * The vendor name of the schema library.
121
+ */
122
+ readonly vendor: string;
123
+ /**
124
+ * The version number of the standard.
125
+ */
126
+ readonly version: 1;
127
+ }
80
128
  /**
81
- * The schema to use for validating the request headers.
129
+ * The result interface of the validate function.
82
130
  */
83
- headers?: StandardSchemaV1<Headers>;
131
+ type Result<Output> = FailureResult | SuccessResult<Output>;
84
132
  /**
85
- * The schema to use for validating the request url.
133
+ * The result interface if validation succeeds.
86
134
  */
87
- initURL?: StandardSchemaV1<InitURL>;
135
+ interface SuccessResult<Output> {
136
+ /**
137
+ * The non-existent issues.
138
+ */
139
+ readonly issues?: undefined;
140
+ /**
141
+ * The typed output value.
142
+ */
143
+ readonly value: Output;
144
+ }
88
145
  /**
89
- * The schema to use for validating the meta option.
146
+ * The result interface if validation fails.
90
147
  */
91
- meta?: StandardSchemaV1<GlobalMeta>;
148
+ interface FailureResult {
149
+ /**
150
+ * The issues of failed validation.
151
+ */
152
+ readonly issues: readonly Issue[];
153
+ }
92
154
  /**
93
- * The schema to use for validating the request method.
94
- */
95
- method?: StandardSchemaV1<Method>;
155
+ * The issue interface of the failure output.
156
+ */
157
+ interface Issue {
158
+ /**
159
+ * The error message of the issue.
160
+ */
161
+ readonly message: string;
162
+ /**
163
+ * The path of the issue, if any.
164
+ */
165
+ readonly path?: ReadonlyArray<PathSegment | PropertyKey> | undefined;
166
+ }
96
167
  /**
97
- * The schema to use for validating the request url parameter.
168
+ * The path segment interface of the issue.
98
169
  */
99
- params?: StandardSchemaV1<Params>;
170
+ interface PathSegment {
171
+ /**
172
+ * The key representing a path segment.
173
+ */
174
+ readonly key: PropertyKey;
175
+ }
100
176
  /**
101
- * The schema to use for validating the request url querys.
177
+ * The Standard Schema types interface.
102
178
  */
103
- query?: StandardSchemaV1<Query>;
104
- }
105
- interface CallApiValidators<TData = unknown, TErrorData = unknown> {
179
+ interface Types<Input = unknown, Output = Input> {
180
+ /** The input type of the schema. */
181
+ readonly input: Input;
182
+ /** The output type of the schema. */
183
+ readonly output: Output;
184
+ }
106
185
  /**
107
- * Custom function to validate the response data.
186
+ * Infers the input type of a Standard Schema.
108
187
  */
109
- data?: (value: unknown) => TData;
188
+ type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
110
189
  /**
111
- * Custom function to validate the response error data, stemming from the api.
112
- * This only runs if the api actually sends back error status codes, else it will be ignored, in which case you should only use the `responseValidator` option.
190
+ * Infers the output type of a Standard Schema.
113
191
  */
114
- errorData?: (value: unknown) => TErrorData;
192
+ type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
115
193
  }
116
- type InferSchemaResult<TSchema, TData> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TData;
117
194
 
118
195
  type Params = UnmaskType<Record<string, boolean | number | string> | Array<boolean | number | string>>;
119
196
  type Query = UnmaskType<Record<string, boolean | number | string>>;
@@ -133,53 +210,56 @@ interface UrlOptions<TSchemas extends CallApiSchemas> {
133
210
  query?: InferSchemaResult<TSchemas["query"], Query>;
134
211
  }
135
212
 
136
- type RetryCondition<TErrorData> = (context: ErrorContext<TErrorData>) => Awaitable<boolean>;
137
- interface RetryOptions<TErrorData> {
213
+ interface CallApiSchemas {
138
214
  /**
139
- * Keeps track of the number of times the request has already been retried
140
- * @deprecated This property is used internally to track retries. Please abstain from modifying it.
215
+ * The schema to use for validating the request body.
141
216
  */
142
- readonly ["~retryCount"]?: number;
217
+ body?: StandardSchemaV1<Body>;
143
218
  /**
144
- * Number of allowed retry attempts on HTTP errors
145
- * @default 0
219
+ * The schema to use for validating the response data.
146
220
  */
147
- retryAttempts?: number;
221
+ data?: StandardSchemaV1;
148
222
  /**
149
- * Callback whose return value determines if a request should be retried or not
223
+ * The schema to use for validating the response error data.
150
224
  */
151
- retryCondition?: RetryCondition<TErrorData>;
225
+ errorData?: StandardSchemaV1;
152
226
  /**
153
- * Delay between retries in milliseconds
154
- * @default 1000
227
+ * The schema to use for validating the request headers.
155
228
  */
156
- retryDelay?: number;
229
+ headers?: StandardSchemaV1<Headers>;
157
230
  /**
158
- * Maximum delay in milliseconds. Only applies to exponential strategy
159
- * @default 10000
231
+ * The schema to use for validating the request url.
160
232
  */
161
- retryMaxDelay?: number;
233
+ initURL?: StandardSchemaV1<InitURL>;
162
234
  /**
163
- * HTTP methods that are allowed to retry
164
- * @default ["GET", "POST"]
235
+ * The schema to use for validating the meta option.
165
236
  */
166
- retryMethods?: Method[];
237
+ meta?: StandardSchemaV1<GlobalMeta>;
167
238
  /**
168
- * HTTP status codes that trigger a retry
169
- * @default [409, 425, 429, 500, 502, 503, 504]
239
+ * The schema to use for validating the request method.
170
240
  */
171
- retryStatusCodes?: Array<409 | 425 | 429 | 500 | 502 | 503 | 504 | AnyNumber>;
241
+ method?: StandardSchemaV1<Method>;
172
242
  /**
173
- * Strategy to use when retrying
174
- * @default "linear"
243
+ * The schema to use for validating the request url parameter.
175
244
  */
176
- retryStrategy?: "exponential" | "linear";
245
+ params?: StandardSchemaV1<Params>;
246
+ /**
247
+ * The schema to use for validating the request url querys.
248
+ */
249
+ query?: StandardSchemaV1<Query>;
177
250
  }
178
-
179
- type DefaultDataType = unknown;
180
- type DefaultMoreOptions = NonNullable<unknown>;
181
- type DefaultPluginArray = CallApiPlugin[];
182
- type DefaultThrowOnError = boolean;
251
+ interface CallApiValidators<TData = unknown, TErrorData = unknown> {
252
+ /**
253
+ * Custom function to validate the response data.
254
+ */
255
+ data?: (value: unknown) => TData;
256
+ /**
257
+ * Custom function to validate the response error data, stemming from the api.
258
+ * This only runs if the api actually sends back error status codes, else it will be ignored, in which case you should only use the `responseValidator` option.
259
+ */
260
+ errorData?: (value: unknown) => TErrorData;
261
+ }
262
+ type InferSchemaResult<TSchema, TData> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TData;
183
263
 
184
264
  type Parser = (responseString: string) => Awaitable<Record<string, unknown>>;
185
265
  declare const getResponseType: <TResponse>(response: Response, parser?: Parser) => {
@@ -215,18 +295,6 @@ type StreamProgressEvent = {
215
295
  */
216
296
  transferredBytes: number;
217
297
  };
218
- type RequestStreamContext = {
219
- event: StreamProgressEvent;
220
- options: CombinedCallApiExtraOptions;
221
- request: CallApiRequestOptionsForHooks;
222
- requestInstance: Request;
223
- };
224
- type ResponseStreamContext = {
225
- event: StreamProgressEvent;
226
- options: CombinedCallApiExtraOptions;
227
- request: CallApiRequestOptionsForHooks;
228
- response: Response;
229
- };
230
298
  declare global {
231
299
  interface ReadableStream<R> {
232
300
  [Symbol.asyncIterator]: () => AsyncIterableIterator<R>;
@@ -236,13 +304,9 @@ declare global {
236
304
  type UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : never) extends (param: infer TParam) => void ? TParam : never;
237
305
  type InferSchema<TResult> = TResult extends StandardSchemaV1 ? InferSchemaResult<TResult, NonNullable<unknown>> : TResult;
238
306
  type InferPluginOptions<TPluginArray extends CallApiPlugin[]> = UnionToIntersection<InferSchema<ReturnType<NonNullable<TPluginArray[number]["createExtraOptions"]>>>>;
239
- type PluginInitContext<TMoreOptions = DefaultMoreOptions> = WithMoreOptions<TMoreOptions> & {
240
- baseConfig: BaseCallApiExtraOptions & CallApiRequestOptions;
241
- config: CallApiExtraOptions & CallApiRequestOptions;
307
+ type PluginInitContext<TMoreOptions = DefaultMoreOptions> = Prettify<SharedHookContext & WithMoreOptions<TMoreOptions> & {
242
308
  initURL: InitURL | undefined;
243
- options: CombinedCallApiExtraOptions;
244
- request: CallApiRequestOptionsForHooks;
245
- };
309
+ }>;
246
310
  type PluginInitResult = Partial<Omit<PluginInitContext, "request"> & {
247
311
  request: CallApiRequestOptions;
248
312
  }>;
@@ -258,7 +322,7 @@ interface CallApiPlugin<TData = never, TErrorData = never> {
258
322
  /**
259
323
  * Hooks / Interceptors for the plugin
260
324
  */
261
- hooks?: InterceptorsOrInterceptorArray<TData, TErrorData>;
325
+ hooks?: hooksOrHooksArray<TData, TErrorData>;
262
326
  /**
263
327
  * A unique id for the plugin
264
328
  */
@@ -279,10 +343,160 @@ interface CallApiPlugin<TData = never, TErrorData = never> {
279
343
  declare const definePlugin: <TPlugin extends CallApiPlugin | AnyFunction<CallApiPlugin>>(plugin: TPlugin) => TPlugin;
280
344
  type Plugins<TPluginArray extends CallApiPlugin[]> = TPluginArray;
281
345
 
346
+ type DefaultDataType = unknown;
347
+ type DefaultMoreOptions = NonNullable<unknown>;
348
+ type DefaultPluginArray = CallApiPlugin[];
349
+ type DefaultThrowOnError = boolean;
350
+
351
+ type WithMoreOptions<TMoreOptions = DefaultMoreOptions> = {
352
+ options: CombinedCallApiExtraOptions & Partial<TMoreOptions>;
353
+ };
354
+ type Hooks<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = DefaultMoreOptions> = {
355
+ /**
356
+ * 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.
357
+ * It is basically a combination of `onRequestError` and `onResponseError` hooks
358
+ */
359
+ onError?: (context: ErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
360
+ /**
361
+ * Hook that will be called just before the request is made, allowing for modifications or additional operations.
362
+ */
363
+ onRequest?: (context: Prettify<RequestContext & WithMoreOptions<TMoreOptions>>) => Awaitable<unknown>;
364
+ /**
365
+ * Hook that will be called when an error occurs during the fetch request.
366
+ */
367
+ onRequestError?: (context: Prettify<RequestErrorContext & WithMoreOptions<TMoreOptions>>) => Awaitable<unknown>;
368
+ /**
369
+ * Hook that will be called when upload stream progress is tracked
370
+ */
371
+ onRequestStream?: (context: Prettify<RequestStreamContext & WithMoreOptions<TMoreOptions>>) => Awaitable<unknown>;
372
+ /**
373
+ * Hook that will be called when any response is received from the api, whether successful or not
374
+ */
375
+ onResponse?: (context: ResponseContext<TData, TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
376
+ /**
377
+ * Hook that will be called when an error response is received from the api.
378
+ */
379
+ onResponseError?: (context: Prettify<ResponseErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>>) => Awaitable<unknown>;
380
+ /**
381
+ * Hook that will be called when download stream progress is tracked
382
+ */
383
+ onResponseStream?: (context: Prettify<ResponseStreamContext & WithMoreOptions<TMoreOptions>>) => Awaitable<unknown>;
384
+ /**
385
+ * Hook that will be called when a request is retried.
386
+ */
387
+ onRetry?: (response: ErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
388
+ /**
389
+ * Hook that will be called when a successful response is received from the api.
390
+ */
391
+ onSuccess?: (context: Prettify<SuccessContext<TData> & WithMoreOptions<TMoreOptions>>) => Awaitable<unknown>;
392
+ };
393
+ type hooksOrHooksArray<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = DefaultMoreOptions> = {
394
+ [Key in keyof Hooks<TData, TErrorData, TMoreOptions>]: Hooks<TData, TErrorData, TMoreOptions>[Key] | Array<Hooks<TData, TErrorData, TMoreOptions>[Key]>;
395
+ };
396
+ type SharedHookContext = {
397
+ /**
398
+ * Config object passed to createFetchClient
399
+ */
400
+ baseConfig: BaseCallApiExtraOptions & CallApiRequestOptions;
401
+ /**
402
+ * Config object passed to the callApi instance
403
+ */
404
+ config: CallApiExtraOptions & CallApiRequestOptions;
405
+ /**
406
+ * Merged options consisting of extra options from createFetchClient, the callApi instance and default options.
407
+ *
408
+ */
409
+ options: CombinedCallApiExtraOptions;
410
+ /**
411
+ * Merged request consisting of request options from createFetchClient, the callApi instance and default request options.
412
+ */
413
+ request: CallApiRequestOptionsForHooks;
414
+ };
415
+ type RequestContext = UnmaskType<SharedHookContext>;
416
+ type ResponseContext<TData, TErrorData> = UnmaskType<Prettify<SharedHookContext & {
417
+ data: TData;
418
+ error: null;
419
+ response: Response;
420
+ }> | Prettify<SharedHookContext & {
421
+ data: null;
422
+ error: PossibleHTTPError<TErrorData>;
423
+ response: Response;
424
+ }>>;
425
+ type SuccessContext<TData> = UnmaskType<Prettify<SharedHookContext & {
426
+ data: TData;
427
+ response: Response;
428
+ }>>;
429
+ type RequestErrorContext = UnmaskType<Prettify<SharedHookContext & {
430
+ error: PossibleJavaScriptError;
431
+ response: null;
432
+ }>>;
433
+ type ResponseErrorContext<TErrorData> = UnmaskType<Prettify<SharedHookContext & {
434
+ error: PossibleHTTPError<TErrorData>;
435
+ response: Response;
436
+ }>>;
437
+ type ErrorContext<TErrorData> = UnmaskType<Prettify<SharedHookContext & {
438
+ error: PossibleHTTPError<TErrorData>;
439
+ response: Response;
440
+ }> | Prettify<SharedHookContext & {
441
+ error: PossibleJavaScriptError;
442
+ response: null;
443
+ }>>;
444
+ type RequestStreamContext = UnmaskType<Prettify<SharedHookContext & {
445
+ event: StreamProgressEvent;
446
+ requestInstance: Request;
447
+ }>>;
448
+ type ResponseStreamContext = UnmaskType<Prettify<SharedHookContext & {
449
+ event: StreamProgressEvent;
450
+ response: Response;
451
+ }>>;
452
+
453
+ type RetryCondition<TErrorData> = (context: ErrorContext<TErrorData>) => Awaitable<boolean>;
454
+ interface RetryOptions<TErrorData> {
455
+ /**
456
+ * Keeps track of the number of times the request has already been retried
457
+ * @deprecated This property is used internally to track retries. Please abstain from modifying it.
458
+ */
459
+ readonly ["~retryCount"]?: number;
460
+ /**
461
+ * Number of allowed retry attempts on HTTP errors
462
+ * @default 0
463
+ */
464
+ retryAttempts?: number;
465
+ /**
466
+ * Callback whose return value determines if a request should be retried or not
467
+ */
468
+ retryCondition?: RetryCondition<TErrorData>;
469
+ /**
470
+ * Delay between retries in milliseconds
471
+ * @default 1000
472
+ */
473
+ retryDelay?: number;
474
+ /**
475
+ * Maximum delay in milliseconds. Only applies to exponential strategy
476
+ * @default 10000
477
+ */
478
+ retryMaxDelay?: number;
479
+ /**
480
+ * HTTP methods that are allowed to retry
481
+ * @default ["GET", "POST"]
482
+ */
483
+ retryMethods?: Method[];
484
+ /**
485
+ * HTTP status codes that trigger a retry
486
+ * @default [409, 425, 429, 500, 502, 503, 504]
487
+ */
488
+ retryStatusCodes?: Array<409 | 425 | 429 | 500 | 502 | 503 | 504 | AnyNumber>;
489
+ /**
490
+ * Strategy to use when retrying
491
+ * @default "linear"
492
+ */
493
+ retryStrategy?: "exponential" | "linear";
494
+ }
495
+
282
496
  type ModifiedRequestInit = RequestInit & {
283
- duplex?: "full" | "half" | "none";
497
+ duplex?: "half";
284
498
  };
285
- declare const fetchSpecificKeys: ("body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "duplex")[];
499
+ declare const fetchSpecificKeys: (keyof RequestInit | "duplex")[];
286
500
  declare const getDefaultOptions: () => {
287
501
  baseURL: string;
288
502
  bodySerializer: {
@@ -374,55 +588,10 @@ type ResultModeOption<TErrorData, TResultMode extends ResultModeUnion> = TErrorD
374
588
  };
375
589
 
376
590
  type FetchSpecificKeysUnion = Exclude<(typeof fetchSpecificKeys)[number], "body" | "headers" | "method">;
377
- type CallApiRequestOptions<TSchemas extends CallApiSchemas = DefaultMoreOptions> = BodyOption<TSchemas> & HeadersOption<TSchemas> & MethodOption<TSchemas> & Pick<ModifiedRequestInit, FetchSpecificKeysUnion>;
591
+ type CallApiRequestOptions<TSchemas extends CallApiSchemas = DefaultMoreOptions> = Prettify<BodyOption<TSchemas> & HeadersOption<TSchemas> & MethodOption<TSchemas> & Pick<ModifiedRequestInit, FetchSpecificKeysUnion>>;
378
592
  type CallApiRequestOptionsForHooks<TSchemas extends CallApiSchemas = DefaultMoreOptions> = Omit<CallApiRequestOptions<TSchemas>, "headers"> & {
379
593
  headers?: Record<string, string | undefined>;
380
594
  };
381
- type WithMoreOptions<TMoreOptions = DefaultMoreOptions> = {
382
- options: CombinedCallApiExtraOptions & Partial<TMoreOptions>;
383
- };
384
- interface Interceptors<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = DefaultMoreOptions> {
385
- /**
386
- * Interceptor that will be called when any error occurs within the request/response lifecycle, regardless of whether the error is from the api or not.
387
- * It is basically a combination of `onRequestError` and `onResponseError` interceptors
388
- */
389
- onError?: (context: ErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
390
- /**
391
- * Interceptor that will be called just before the request is made, allowing for modifications or additional operations.
392
- */
393
- onRequest?: (context: RequestContext & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
394
- /**
395
- * Interceptor that will be called when an error occurs during the fetch request.
396
- */
397
- onRequestError?: (context: RequestErrorContext & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
398
- /**
399
- * Interceptor that will be called when upload stream progress is tracked
400
- */
401
- onRequestStream?: (context: RequestStreamContext & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
402
- /**
403
- * Interceptor that will be called when any response is received from the api, whether successful or not
404
- */
405
- onResponse?: (context: ResponseContext<TData, TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
406
- /**
407
- * Interceptor that will be called when an error response is received from the api.
408
- */
409
- onResponseError?: (context: ResponseErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
410
- /**
411
- * Interceptor that will be called when download stream progress is tracked
412
- */
413
- onResponseStream?: (context: ResponseStreamContext & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
414
- /**
415
- * Interceptor that will be called when a request is retried.
416
- */
417
- onRetry?: (response: ErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
418
- /**
419
- * Interceptor that will be called when a successful response is received from the api.
420
- */
421
- onSuccess?: (context: SuccessContext<TData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
422
- }
423
- type InterceptorsOrInterceptorArray<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = DefaultMoreOptions> = {
424
- [Key in keyof Interceptors<TData, TErrorData, TMoreOptions>]: Interceptors<TData, TErrorData, TMoreOptions>[Key] | Array<Interceptors<TData, TErrorData, TMoreOptions>[Key]>;
425
- };
426
595
  type FetchImpl = UnmaskType<(input: string | Request | URL, init?: RequestInit) => Promise<Response>>;
427
596
  type ExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TPluginArray extends CallApiPlugin[] = DefaultPluginArray, TSchemas extends CallApiSchemas = DefaultMoreOptions> = {
428
597
  /**
@@ -469,7 +638,7 @@ type ExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResult
469
638
  * If true, forces the calculation of the total byte size from the request or response body, in case the content-length header is not present or is incorrect.
470
639
  * @default false
471
640
  */
472
- forceStreamSizeCalc?: boolean | {
641
+ forceCalculateStreamSize?: boolean | {
473
642
  request?: boolean;
474
643
  response?: boolean;
475
644
  };
@@ -478,15 +647,15 @@ type ExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResult
478
647
  */
479
648
  readonly fullURL?: string;
480
649
  /**
481
- * Defines the mode in which the merged hooks are executed, can be set to "parallel" | "sequential".
650
+ * Defines the mode in which the composed hooks are executed".
482
651
  * - If set to "parallel", main and plugin hooks will be executed in parallel.
483
652
  * - If set to "sequential", the plugin hooks will be executed first, followed by the main hook.
484
653
  * @default "parallel"
485
654
  */
486
655
  mergedHooksExecutionMode?: "parallel" | "sequential";
487
656
  /**
488
- * - Controls what order in which the merged hooks execute
489
- * @default "mainHooksLast"
657
+ * - Controls what order in which the composed hooks execute
658
+ * @default "mainHooksAfterPlugins"
490
659
  */
491
660
  mergedHooksExecutionOrder?: "mainHooksAfterPlugins" | "mainHooksBeforePlugins";
492
661
  /**
@@ -494,7 +663,7 @@ type ExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResult
494
663
  */
495
664
  plugins?: Plugins<TPluginArray>;
496
665
  /**
497
- * Custom function to parse the response string into a object.
666
+ * Custom function to parse the response string
498
667
  */
499
668
  responseParser?: (responseString: string) => Awaitable<Record<string, unknown>>;
500
669
  /**
@@ -526,27 +695,44 @@ type ExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResult
526
695
  * Custom validation functions for response validation
527
696
  */
528
697
  validators?: CallApiValidators<TData, TErrorData>;
529
- } & InterceptorsOrInterceptorArray<TData, TErrorData> & Partial<InferPluginOptions<TPluginArray>> & MetaOption<TSchemas> & RetryOptions<TErrorData> & ResultModeOption<TErrorData, TResultMode> & UrlOptions<TSchemas>;
530
- declare const optionsEnumToExtendFromBase: ("plugins" | "schemas" | "validators")[];
698
+ } & hooksOrHooksArray<TData, TErrorData> & Partial<InferPluginOptions<TPluginArray>> & MetaOption<TSchemas> & RetryOptions<TErrorData> & ResultModeOption<TErrorData, TResultMode> & UrlOptions<TSchemas>;
531
699
  type CallApiExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TPluginArray extends CallApiPlugin[] = DefaultPluginArray, TSchemas extends CallApiSchemas = DefaultMoreOptions> = ExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TPluginArray, TSchemas> & {
532
- /**
533
- * Options that should extend the base options.
534
- */
535
- extend?: Pick<ExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TPluginArray, TSchemas>, (typeof optionsEnumToExtendFromBase)[number]>;
700
+ plugins?: Plugins<TPluginArray> | ((context: {
701
+ basePlugins: Plugins<TPluginArray>;
702
+ }) => Plugins<TPluginArray>);
703
+ schemas?: TSchemas | ((context: {
704
+ baseSchemas: TSchemas;
705
+ }) => TSchemas);
706
+ validators?: CallApiValidators<TData, TErrorData> | ((context: {
707
+ baseValidators: CallApiValidators<TData, TErrorData>;
708
+ }) => CallApiValidators<TData, TErrorData>);
536
709
  };
537
- declare const optionsEnumToOmitFromBase: ("dedupeKey" | "extend")[];
710
+ declare const optionsEnumToOmitFromBase: "dedupeKey"[];
538
711
  type BaseCallApiExtraOptions<TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseSchemas extends CallApiSchemas = DefaultMoreOptions> = Omit<Partial<CallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBasePluginArray, TBaseSchemas>>, (typeof optionsEnumToOmitFromBase)[number]> & {
539
712
  /**
540
- * If true, the base options will not be merged with the main options by default.
713
+ * Specifies which configuration parts should skip automatic merging between base and main configs.
714
+ * Use this when you need manual control over how configs are combined.
541
715
  *
542
- * It's recommended to set this to true when you want to handle the options merge manually from the createFetchClient config function signature.
716
+ * @values
717
+ * - "all" - Disables automatic merging for both request and options
718
+ * - "options" - Disables automatic merging of options only
719
+ * - "request" - Disables automatic merging of request only
543
720
  *
544
- * This helps prevent main options from overriding base options by default.
545
- * @default false
721
+ * @example
722
+ * ```ts
723
+ * const client = createFetchClient((ctx) => ({
724
+ * skipAutoMergeFor: "options",
725
+ *
726
+ * // Now you can manually merge options in your config function
727
+ * ...ctx.options,
728
+ * }));
729
+ * ```
546
730
  */
547
- mergeMainOptionsManuallyFromBase?: boolean;
731
+ skipAutoMergeFor?: "all" | "options" | "request";
548
732
  };
549
- type CombinedCallApiExtraOptions = Interceptors & Omit<BaseCallApiExtraOptions & CallApiExtraOptions, keyof Interceptors>;
733
+ type CombinedExtraOptionsWithoutHooks = Omit<BaseCallApiExtraOptions & CallApiExtraOptions, keyof Hooks>;
734
+ type ResolvedHooks = Hooks;
735
+ type CombinedCallApiExtraOptions = CombinedExtraOptionsWithoutHooks & ResolvedHooks;
550
736
  type BaseCallApiConfig<TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseSchemas extends CallApiSchemas = DefaultMoreOptions> = (CallApiRequestOptions<TBaseSchemas> & BaseCallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBasePluginArray, TBaseSchemas>) | ((context: {
551
737
  initURL: string;
552
738
  options: CallApiExtraOptions;
@@ -557,63 +743,6 @@ type CallApiParameters<TData = DefaultDataType, TErrorData = DefaultDataType, TR
557
743
  initURL: InferSchemaResult<TSchemas["initURL"], InitURL>,
558
744
  config?: CallApiConfig<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TPluginArray, TSchemas>
559
745
  ];
560
- type RequestContext = UnmaskType<{
561
- options: CombinedCallApiExtraOptions;
562
- request: CallApiRequestOptionsForHooks;
563
- }>;
564
- type ResponseContext<TData, TErrorData> = UnmaskType<{
565
- data: TData;
566
- error: null;
567
- options: CombinedCallApiExtraOptions;
568
- request: CallApiRequestOptionsForHooks;
569
- response: Response;
570
- } | {
571
- data: null;
572
- error: PossibleHTTPError<TErrorData>;
573
- options: CombinedCallApiExtraOptions;
574
- request: CallApiRequestOptionsForHooks;
575
- response: Response;
576
- }>;
577
- type SuccessContext<TData> = UnmaskType<{
578
- data: TData;
579
- options: CombinedCallApiExtraOptions;
580
- request: CallApiRequestOptionsForHooks;
581
- response: Response;
582
- }>;
583
- type PossibleJavascriptErrorNames = "AbortError" | "Error" | "SyntaxError" | "TimeoutError" | "TypeError" | (`${string}Error` & DefaultMoreOptions);
584
- type PossibleJavaScriptError = UnmaskType<{
585
- errorData: DOMException | Error | SyntaxError | TypeError;
586
- message: string;
587
- name: PossibleJavascriptErrorNames;
588
- }>;
589
- type PossibleHTTPError<TErrorData> = UnmaskType<{
590
- errorData: TErrorData;
591
- message: string;
592
- name: "HTTPError";
593
- }>;
594
- type RequestErrorContext = UnmaskType<{
595
- error: PossibleJavaScriptError;
596
- options: CombinedCallApiExtraOptions;
597
- request: CallApiRequestOptionsForHooks;
598
- response: null;
599
- }>;
600
- type ResponseErrorContext<TErrorData> = UnmaskType<{
601
- error: PossibleHTTPError<TErrorData>;
602
- options: CombinedCallApiExtraOptions;
603
- request: CallApiRequestOptionsForHooks;
604
- response: Response;
605
- }>;
606
- type ErrorContext<TErrorData> = UnmaskType<{
607
- error: PossibleHTTPError<TErrorData>;
608
- options: CombinedCallApiExtraOptions;
609
- request: CallApiRequestOptionsForHooks;
610
- response: Response;
611
- } | {
612
- error: PossibleJavaScriptError;
613
- options: CombinedCallApiExtraOptions;
614
- request: CallApiRequestOptionsForHooks;
615
- response: null;
616
- }>;
617
746
  type CallApiResultSuccessVariant<TData> = {
618
747
  data: TData;
619
748
  error: null;
@@ -642,20 +771,4 @@ type ResultModeUnion = keyof ResultModeMap | undefined;
642
771
  type GetCallApiResult<TData, TErrorData, TResultMode extends ResultModeUnion, TThrowOnError extends boolean, TResponseType extends ResponseTypeUnion> = TErrorData extends false | undefined ? ResultModeMap<TData, TErrorData, TResponseType>["onlySuccessWithException"] : ResultModeUnion | undefined extends TResultMode ? TThrowOnError extends true ? ResultModeMap<TData, TErrorData, TResponseType>["allWithException"] : ResultModeMap<TData, TErrorData, TResponseType>["all"] : TResultMode extends NonNullable<ResultModeUnion> ? TResultMode extends "onlySuccess" ? ResultModeMap<TData, TErrorData, TResponseType>["onlySuccessWithException"] : TResultMode extends "onlyResponse" ? ResultModeMap<TData, TErrorData, TResponseType>["onlyResponseWithException"] : ResultModeMap<TData, TErrorData, TResponseType>[TResultMode] : never;
643
772
  type CallApiResult<TData, TErrorData, TResultMode extends ResultModeUnion, TThrowOnError extends boolean, TResponseType extends ResponseTypeUnion> = Promise<GetCallApiResult<TData, TErrorData, TResultMode, TThrowOnError, TResponseType>>;
644
773
 
645
- type ErrorDetails<TErrorResponse> = {
646
- defaultErrorMessage: string;
647
- errorData: TErrorResponse;
648
- response: Response;
649
- };
650
- type ErrorOptions = {
651
- cause?: unknown;
652
- };
653
- declare class HTTPError<TErrorResponse = Record<string, unknown>> extends Error {
654
- errorData: ErrorDetails<TErrorResponse>["errorData"];
655
- isHTTPError: boolean;
656
- name: "HTTPError";
657
- response: ErrorDetails<TErrorResponse>["response"];
658
- constructor(errorDetails: ErrorDetails<TErrorResponse>, errorOptions?: ErrorOptions);
659
- }
660
-
661
- export { type ResponseContext as A, type BaseCallApiConfig as B, type CallApiPlugin as C, type DefaultPluginArray as D, type ErrorContext as E, type ResponseErrorContext as F, HTTPError as H, type InferSchemaResult as I, type PluginInitContext as P, type ResultModeUnion as R, type SuccessContext as S, type ResponseTypeUnion as a, type CallApiSchemas as b, type CallApiConfig as c, type CallApiResult as d, type DefaultDataType as e, type DefaultThrowOnError as f, type DefaultMoreOptions as g, type CallApiParameters as h, definePlugin as i, getDefaultOptions as j, type RetryOptions as k, type BaseCallApiExtraOptions as l, type CallApiExtraOptions as m, type PossibleJavaScriptError as n, type PossibleHTTPError as o, type CallApiRequestOptions as p, type CallApiRequestOptionsForHooks as q, type CallApiResultErrorVariant as r, type CallApiResultSuccessVariant as s, type CombinedCallApiExtraOptions as t, type Interceptors as u, type InterceptorsOrInterceptorArray as v, type PossibleJavascriptErrorNames as w, type Register as x, type RequestContext as y, type RequestErrorContext as z };
774
+ export { type CallApiResultErrorVariant as A, type BaseCallApiConfig as B, type CallApiPlugin as C, type DefaultPluginArray as D, type ErrorContext as E, type CallApiResultSuccessVariant as F, type CombinedCallApiExtraOptions as G, HTTPError as H, type InferSchemaResult as I, type Register as J, type PluginInitContext as P, type ResultModeUnion as R, type SharedHookContext as S, type ResponseTypeUnion as a, type CallApiSchemas as b, type CallApiConfig as c, type CallApiResult as d, type DefaultDataType as e, type DefaultThrowOnError as f, type DefaultMoreOptions as g, type CallApiParameters as h, definePlugin as i, getDefaultOptions as j, type RetryOptions as k, type PossibleHTTPError as l, type PossibleJavaScriptError as m, type Hooks as n, type hooksOrHooksArray as o, type RequestContext as p, type RequestErrorContext as q, type RequestStreamContext as r, type ResponseContext as s, type ResponseErrorContext as t, type ResponseStreamContext as u, type SuccessContext as v, type BaseCallApiExtraOptions as w, type CallApiExtraOptions as x, type CallApiRequestOptions as y, type CallApiRequestOptionsForHooks as z };