@zayne-labs/callapi 1.10.0 → 1.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/esm/{common-DzOcclPA.d.ts → common-CO8LGZIl.d.ts} +21 -38
- package/dist/esm/index.d.ts +5 -5
- package/dist/esm/index.js +38 -70
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -1
- package/dist/esm/utils/index.js +1 -1
- package/dist/esm/{utils-B5rSU71M.js → utils-zvYT8TK8.js} +28 -31
- package/dist/esm/utils-zvYT8TK8.js.map +1 -0
- package/package.json +6 -6
- package/dist/esm/utils-B5rSU71M.js.map +0 -1
package/README.md
CHANGED
@@ -11,8 +11,8 @@
|
|
11
11
|
<!-- <a href="https://deno.bundlejs.com/badge?q=@zayne-labs/callapi,@zayne-labs/callapi&treeshake=%5B*%5D,%5B%7B+createFetchClient+%7D%5D&config=%7B%22compression%22:%7B%22type%22:%22brotli%22,%22quality%22:11%7D%7D"><img src="https://deno.bundlejs.com/badge?q=@zayne-labs/callapi,@zayne-labs/callapi&treeshake=%5B*%5D,%5B%7B+createFetchClient+%7D%5D&config=%7B%22compression%22:%7B%22type%22:%22brotli%22,%22quality%22:11%7D%7D" alt="bundle size"></a> -->
|
12
12
|
<a href="https://www.npmjs.com/package/@zayne-labs/callapi"><img src="https://img.shields.io/npm/v/@zayne-labs/callapi?style=flat&color=EFBA5F" alt="npm version"></a>
|
13
13
|
<a href="https://github.com/zayne-labs/callapi/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@zayne-labs/callapi?style=flat&color=EFBA5F" alt="license"></a>
|
14
|
-
<a href="https://github.com/zayne-labs/callapi/graphs/commit-activity"><img src="https://img.shields.io/github/commit-activity/m/zayne-labs/callapi?style=flat&color=EFBA5F" alt="commit activity"></a>
|
15
14
|
<a href="https://www.npmjs.com/package/@zayne-labs/callapi"><img src="https://img.shields.io/npm/dm/@zayne-labs/callapi?style=flat&color=EFBA5F" alt="downloads per month"></a>
|
15
|
+
<a href="https://github.com/zayne-labs/callapi/graphs/commit-activity"><img src="https://img.shields.io/github/commit-activity/m/zayne-labs/callapi?style=flat&color=EFBA5F" alt="commit activity"></a>
|
16
16
|
<a href="https://deepwiki.com/zayne-labs/callapi"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"></a>
|
17
17
|
<a href="https://code2tutorial.com/tutorial/02b6c57c-4847-4e76-b91e-d64dde370609/index.md"><img src="https://img.shields.io/badge/Code2Tutorial-blue?color=blue&logo=victoriametrics" alt="Code2Tutorial"></a>
|
18
18
|
</p>
|
@@ -58,7 +58,7 @@ type CustomAuth = {
|
|
58
58
|
prefix: PossibleAuthValueOrGetter;
|
59
59
|
value: PossibleAuthValueOrGetter;
|
60
60
|
};
|
61
|
-
type Auth = BearerOrTokenAuth | BasicAuth | CustomAuth;
|
61
|
+
type Auth = PossibleAuthValueOrGetter | BearerOrTokenAuth | BasicAuth | CustomAuth;
|
62
62
|
//#endregion
|
63
63
|
//#region src/constants/common.d.ts
|
64
64
|
declare const fetchSpecificKeys: (keyof RequestInit | "duplex")[];
|
@@ -170,8 +170,7 @@ type HTTPErrorDetails<TErrorData> = Pick<CallApiExtraOptions, "defaultHTTPErrorM
|
|
170
170
|
};
|
171
171
|
declare class HTTPError<TErrorData = Record<string, unknown>> extends Error {
|
172
172
|
errorData: HTTPErrorDetails<TErrorData>["errorData"];
|
173
|
-
httpErrorSymbol: symbol;
|
174
|
-
isHTTPError: boolean;
|
173
|
+
readonly httpErrorSymbol: symbol;
|
175
174
|
name: "HTTPError";
|
176
175
|
response: HTTPErrorDetails<TErrorData>["response"];
|
177
176
|
constructor(errorDetails: HTTPErrorDetails<TErrorData>, errorOptions?: ErrorOptions);
|
@@ -188,14 +187,14 @@ type ValidationErrorDetails = {
|
|
188
187
|
};
|
189
188
|
declare class ValidationError extends Error {
|
190
189
|
errorData: ValidationErrorDetails["issues"];
|
191
|
-
name:
|
190
|
+
name: "ValidationError";
|
192
191
|
response: ValidationErrorDetails["response"];
|
193
|
-
validationErrorSymbol: symbol;
|
192
|
+
readonly validationErrorSymbol: symbol;
|
194
193
|
constructor(details: ValidationErrorDetails, errorOptions?: ErrorOptions);
|
195
194
|
/**
|
196
|
-
* @description Checks if the given error is an instance of
|
195
|
+
* @description Checks if the given error is an instance of ValidationError
|
197
196
|
* @param error - The error to check
|
198
|
-
* @returns true if the error is an instance of
|
197
|
+
* @returns true if the error is an instance of ValidationError, false otherwise
|
199
198
|
*/
|
200
199
|
static isError(error: unknown): error is ValidationError;
|
201
200
|
}
|
@@ -321,7 +320,8 @@ interface URLOptions {
|
|
321
320
|
}
|
322
321
|
//#endregion
|
323
322
|
//#region src/validation.d.ts
|
324
|
-
type
|
323
|
+
type InferSchemaOutputResult<TSchema, TFallbackResult = unknown> = undefined extends TSchema ? TFallbackResult : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TSchema extends AnyFunction<infer TResult> ? Awaited<TResult> : TFallbackResult;
|
324
|
+
type InferSchemaInputResult<TSchema, TFallbackResult = unknown> = undefined extends TSchema ? TFallbackResult : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TSchema extends AnyFunction<infer TResult> ? Awaited<TResult> : TFallbackResult;
|
325
325
|
interface CallApiSchemaConfig {
|
326
326
|
/**
|
327
327
|
* The base url of the schema. By default it's the baseURL of the callApi instance.
|
@@ -1130,7 +1130,7 @@ interface RetryOptions<TErrorData> {
|
|
1130
1130
|
/**
|
1131
1131
|
* @description Makes a type partial if the output type of TSchema is not provided or has undefined in the union, otherwise makes it required
|
1132
1132
|
*/
|
1133
|
-
type MakeSchemaOptionRequiredIfDefined<TSchemaOption extends CallApiSchema[keyof CallApiSchema], TObject> = undefined extends
|
1133
|
+
type MakeSchemaOptionRequiredIfDefined<TSchemaOption extends CallApiSchema[keyof CallApiSchema], TObject> = undefined extends InferSchemaInputResult<TSchemaOption, undefined> ? TObject : Required<TObject>;
|
1134
1134
|
type ApplyURLBasedConfig<TSchemaConfig extends CallApiSchemaConfig, TSchemaRouteKeys extends string> = TSchemaConfig["prefix"] extends string ? `${TSchemaConfig["prefix"]}${TSchemaRouteKeys}` : TSchemaConfig["baseURL"] extends string ? `${TSchemaConfig["baseURL"]}${TSchemaRouteKeys}` : TSchemaRouteKeys;
|
1135
1135
|
type ApplyStrictConfig<TSchemaConfig extends CallApiSchemaConfig, TSchemaRouteKeys extends string> = TSchemaConfig["strict"] extends true ? TSchemaRouteKeys :
|
1136
1136
|
// eslint-disable-next-line perfectionist/sort-union-types -- Don't sort union types
|
@@ -1148,7 +1148,7 @@ type InferBodyOption<TSchema extends CallApiSchema> = MakeSchemaOptionRequiredIf
|
|
1148
1148
|
/**
|
1149
1149
|
* Body of the request, can be a object or any other supported body type.
|
1150
1150
|
*/
|
1151
|
-
body?:
|
1151
|
+
body?: InferSchemaInputResult<TSchema["body"], Body>;
|
1152
1152
|
}>;
|
1153
1153
|
type MethodUnion = UnmaskType<"CONNECT" | "DELETE" | "GET" | "HEAD" | "OPTIONS" | "PATCH" | "POST" | "PUT" | "TRACE" | AnyString>;
|
1154
1154
|
type InferMethodFromURL<TInitURL> = string extends TInitURL ? MethodUnion : TInitURL extends `@${infer TMethod extends RouteKeyMethods}/${string}` ? Uppercase<TMethod> : MethodUnion;
|
@@ -1157,16 +1157,16 @@ type InferMethodOption<TSchema extends CallApiSchema, TInitURL> = MakeSchemaOpti
|
|
1157
1157
|
* HTTP method for the request.
|
1158
1158
|
* @default "GET"
|
1159
1159
|
*/
|
1160
|
-
method?:
|
1160
|
+
method?: InferSchemaInputResult<TSchema["method"], InferMethodFromURL<TInitURL>>;
|
1161
1161
|
}>;
|
1162
1162
|
type HeadersOption = UnmaskType<Record<"Authorization", CommonAuthorizationHeaders | undefined> | Record<"Content-Type", CommonContentTypes | undefined> | Record<CommonRequestHeaders, string | undefined> | Record<string, string | undefined> | Array<[string, string]>>;
|
1163
1163
|
type InferHeadersOption<TSchema extends CallApiSchema> = MakeSchemaOptionRequiredIfDefined<TSchema["headers"], {
|
1164
1164
|
/**
|
1165
1165
|
* Headers to be used in the request.
|
1166
1166
|
*/
|
1167
|
-
headers?:
|
1167
|
+
headers?: InferSchemaInputResult<TSchema["headers"], HeadersOption> | ((context: {
|
1168
1168
|
baseHeaders: NonNullable<HeadersOption>;
|
1169
|
-
}) =>
|
1169
|
+
}) => InferSchemaInputResult<TSchema["headers"], HeadersOption>);
|
1170
1170
|
}>;
|
1171
1171
|
type InferRequestOptions<TSchema extends CallApiSchema, TInitURL extends InferInitURL<BaseCallApiSchemaRoutes, CallApiSchemaConfig>> = InferBodyOption<TSchema> & InferHeadersOption<TSchema> & InferMethodOption<TSchema, TInitURL>;
|
1172
1172
|
interface Register {}
|
@@ -1197,13 +1197,13 @@ type InferMetaOption<TSchema extends CallApiSchema> = MakeSchemaOptionRequiredIf
|
|
1197
1197
|
* });
|
1198
1198
|
* ```
|
1199
1199
|
*/
|
1200
|
-
meta?:
|
1200
|
+
meta?: InferSchemaInputResult<TSchema["meta"], GlobalMeta>;
|
1201
1201
|
}>;
|
1202
1202
|
type InferQueryOption<TSchema extends CallApiSchema> = MakeSchemaOptionRequiredIfDefined<TSchema["query"], {
|
1203
1203
|
/**
|
1204
1204
|
* Parameters to be appended to the URL (i.e: /:id)
|
1205
1205
|
*/
|
1206
|
-
query?:
|
1206
|
+
query?: InferSchemaInputResult<TSchema["query"], Query>;
|
1207
1207
|
}>;
|
1208
1208
|
type EmptyString = "";
|
1209
1209
|
type EmptyTuple = readonly [];
|
@@ -1213,15 +1213,15 @@ type ExtractRouteParamNames<TCurrentRoute, TParamNamesAccumulator extends String
|
|
1213
1213
|
type ConvertParamNamesToRecord<TParamNames extends StringTuple> = Prettify<TParamNames extends (readonly [infer TFirstParamName extends string, ...infer TRemainingParamNames extends StringTuple]) ? Record<TFirstParamName, AllowedQueryParamValues> & ConvertParamNamesToRecord<TRemainingParamNames> : NonNullable<unknown>>;
|
1214
1214
|
type ConvertParamNamesToTuple<TParamNames extends StringTuple> = TParamNames extends readonly [string, ...infer TRemainingParamNames extends StringTuple] ? [AllowedQueryParamValues, ...ConvertParamNamesToTuple<TRemainingParamNames>] : [];
|
1215
1215
|
type InferParamsFromRoute<TCurrentRoute> = ExtractRouteParamNames<TCurrentRoute> extends StringTuple ? ExtractRouteParamNames<TCurrentRoute> extends EmptyTuple ? Params : ConvertParamNamesToRecord<ExtractRouteParamNames<TCurrentRoute>> | ConvertParamNamesToTuple<ExtractRouteParamNames<TCurrentRoute>> : Params;
|
1216
|
-
type MakeParamsOptionRequired<TParamsSchemaOption extends CallApiSchema["params"], TBaseSchemaRoutes extends BaseCallApiSchemaRoutes, TCurrentRouteSchemaKey extends string, TObject> = MakeSchemaOptionRequiredIfDefined<TParamsSchemaOption, Params extends InferParamsFromRoute<TCurrentRouteSchemaKey> ? TObject : TCurrentRouteSchemaKey extends Extract<keyof TBaseSchemaRoutes, TCurrentRouteSchemaKey> ? undefined extends
|
1216
|
+
type MakeParamsOptionRequired<TParamsSchemaOption extends CallApiSchema["params"], TBaseSchemaRoutes extends BaseCallApiSchemaRoutes, TCurrentRouteSchemaKey extends string, TObject> = MakeSchemaOptionRequiredIfDefined<TParamsSchemaOption, Params extends InferParamsFromRoute<TCurrentRouteSchemaKey> ? TObject : TCurrentRouteSchemaKey extends Extract<keyof TBaseSchemaRoutes, TCurrentRouteSchemaKey> ? undefined extends InferSchemaInputResult<TParamsSchemaOption, null> ? TObject : Required<TObject> : TObject>;
|
1217
1217
|
type InferParamsOption<TSchema extends CallApiSchema, TBaseSchemaRoutes extends BaseCallApiSchemaRoutes, TCurrentRouteSchemaKey extends string> = MakeParamsOptionRequired<TSchema["params"], TBaseSchemaRoutes, TCurrentRouteSchemaKey, {
|
1218
1218
|
/**
|
1219
1219
|
* Parameters to be appended to the URL (i.e: /:id)
|
1220
1220
|
*/
|
1221
|
-
params?:
|
1221
|
+
params?: InferSchemaInputResult<TSchema["params"], InferParamsFromRoute<TCurrentRouteSchemaKey>>;
|
1222
1222
|
}>;
|
1223
1223
|
type InferExtraOptions<TSchema extends CallApiSchema, TBaseSchemaRoutes extends BaseCallApiSchemaRoutes, TCurrentRouteSchemaKey extends string> = InferMetaOption<TSchema> & InferParamsOption<TSchema, TBaseSchemaRoutes, TCurrentRouteSchemaKey> & InferQueryOption<TSchema>;
|
1224
|
-
type InferPluginOptions<TPluginArray extends CallApiPlugin[]> = UnionToIntersection<TPluginArray extends Array<infer TPlugin> ? TPlugin extends CallApiPlugin ? TPlugin["defineExtraOptions"] extends AnyFunction<infer TReturnedSchema> ?
|
1224
|
+
type InferPluginOptions<TPluginArray extends CallApiPlugin[]> = UnionToIntersection<TPluginArray extends Array<infer TPlugin> ? TPlugin extends CallApiPlugin ? TPlugin["defineExtraOptions"] extends AnyFunction<infer TReturnedSchema> ? InferSchemaOutputResult<TReturnedSchema> : never : never : never>;
|
1225
1225
|
type ExtractKeys<TUnion, TSelectedUnion extends TUnion> = Extract<TUnion, TSelectedUnion>;
|
1226
1226
|
type ResultModeOption<TErrorData, TResultMode extends ResultModeUnion> = TErrorData extends false ? {
|
1227
1227
|
resultMode: "onlySuccessWithException";
|
@@ -1272,27 +1272,10 @@ type SharedExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, T
|
|
1272
1272
|
* Supports multiple authentication patterns:
|
1273
1273
|
* - String: Direct authorization header value
|
1274
1274
|
* - Auth object: Structured authentication configuration
|
1275
|
-
* - null: Explicitly removes authorization
|
1276
|
-
*
|
1277
|
-
* @example
|
1278
|
-
* ```ts
|
1279
|
-
* // Bearer token authentication
|
1280
|
-
* auth: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
1281
|
-
*
|
1282
|
-
* // Basic authentication
|
1283
|
-
* auth: "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
|
1284
|
-
*
|
1285
|
-
* // Using Auth object for dynamic authentication
|
1286
|
-
* auth: {
|
1287
|
-
* type: "bearer",
|
1288
|
-
* token: () => getAccessToken()
|
1289
|
-
* }
|
1290
1275
|
*
|
1291
|
-
* // Remove inherited auth from base config
|
1292
|
-
* auth: null
|
1293
1276
|
* ```
|
1294
1277
|
*/
|
1295
|
-
auth?:
|
1278
|
+
auth?: Auth;
|
1296
1279
|
/**
|
1297
1280
|
* Custom function to serialize request body objects into strings.
|
1298
1281
|
*
|
@@ -1797,5 +1780,5 @@ type CallApiConfig<TData = DefaultDataType, TErrorData = DefaultDataType, TResul
|
|
1797
1780
|
type CallApiParameters<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends ThrowOnErrorUnion = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemaRoutes extends BaseCallApiSchemaRoutes = BaseCallApiSchemaRoutes, TSchema extends CallApiSchema = CallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TInitURL extends InitURLOrURLObject = InitURLOrURLObject, TCurrentRouteSchemaKey extends string = string, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TPluginArray extends CallApiPlugin[] = DefaultPluginArray> = [initURL: TInitURL, config?: CallApiConfig<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TBaseSchemaRoutes, TSchema, TBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, TBasePluginArray, TPluginArray>];
|
1798
1781
|
type CallApiResult<TData, TErrorData, TResultMode extends ResultModeUnion, TThrowOnError extends ThrowOnErrorUnion, TResponseType extends ResponseTypeUnion> = Promise<GetCallApiResult<TData, TErrorData, TResultMode, TThrowOnError, TResponseType>>;
|
1799
1782
|
//#endregion
|
1800
|
-
export { AnyFunction, AnyString, ApplyStrictConfig, ApplyURLBasedConfig, BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchemaAndConfig, BaseCallApiSchemaRoutes, CallApiConfig, CallApiExtraOptions, CallApiExtraOptionsForHooks, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, DedupeOptions, DefaultDataType, DefaultPluginArray, DefaultThrowOnError, ErrorContext, GetCurrentRouteSchema, GetCurrentRouteSchemaKey, GetResponseType, HTTPError, Hooks, HooksOrHooksArray, InferInitURL, InferParamsFromRoute,
|
1801
|
-
//# sourceMappingURL=common-
|
1783
|
+
export { AnyFunction, AnyString, ApplyStrictConfig, ApplyURLBasedConfig, BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchemaAndConfig, BaseCallApiSchemaRoutes, CallApiConfig, CallApiExtraOptions, CallApiExtraOptionsForHooks, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, DedupeOptions, DefaultDataType, DefaultPluginArray, DefaultThrowOnError, ErrorContext, GetCurrentRouteSchema, GetCurrentRouteSchemaKey, GetResponseType, HTTPError, Hooks, HooksOrHooksArray, InferInitURL, InferParamsFromRoute, InferSchemaOutputResult, PluginExtraOptions, PluginHooks, PluginHooksWithMoreOptions, PluginSetupContext, PossibleHTTPError, PossibleJavaScriptError, PossibleJavaScriptOrValidationError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeMap, ResponseTypeUnion, ResultModeUnion, RetryOptions, SuccessContext, ThrowOnErrorUnion, URLOptions, ValidationError, Writeable, fallBackRouteSchemaKey };
|
1784
|
+
//# sourceMappingURL=common-CO8LGZIl.d.ts.map
|
package/dist/esm/index.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import { AnyFunction, AnyString, ApplyStrictConfig, ApplyURLBasedConfig, BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchemaAndConfig, BaseCallApiSchemaRoutes, CallApiConfig, CallApiExtraOptions, CallApiExtraOptionsForHooks, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, DedupeOptions, DefaultDataType, DefaultPluginArray, DefaultThrowOnError, ErrorContext, GetCurrentRouteSchema, GetCurrentRouteSchemaKey, GetResponseType, HTTPError, Hooks, HooksOrHooksArray, InferInitURL, InferParamsFromRoute,
|
1
|
+
import { AnyFunction, AnyString, ApplyStrictConfig, ApplyURLBasedConfig, BaseCallApiConfig, BaseCallApiExtraOptions, BaseCallApiSchemaAndConfig, BaseCallApiSchemaRoutes, CallApiConfig, CallApiExtraOptions, CallApiExtraOptionsForHooks, CallApiParameters, CallApiPlugin, CallApiRequestOptions, CallApiRequestOptionsForHooks, CallApiResult, CallApiResultErrorVariant, CallApiResultSuccessVariant, CallApiSchema, CallApiSchemaConfig, DedupeOptions, DefaultDataType, DefaultPluginArray, DefaultThrowOnError, ErrorContext, GetCurrentRouteSchema, GetCurrentRouteSchemaKey, GetResponseType, HTTPError, Hooks, HooksOrHooksArray, InferInitURL, InferParamsFromRoute, InferSchemaOutputResult, PluginExtraOptions, PluginHooks, PluginHooksWithMoreOptions, PluginSetupContext, PossibleHTTPError, PossibleJavaScriptError, PossibleJavaScriptOrValidationError, PossibleValidationError, Register, RequestContext, RequestStreamContext, ResponseContext, ResponseErrorContext, ResponseStreamContext, ResponseTypeMap, ResponseTypeUnion, ResultModeUnion, RetryOptions, SuccessContext, ThrowOnErrorUnion, URLOptions, ValidationError, Writeable, fallBackRouteSchemaKey } from "./common-CO8LGZIl.js";
|
2
2
|
|
3
3
|
//#region src/createFetchClient.d.ts
|
4
4
|
|
5
|
-
declare const createFetchClient: <TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends ThrowOnErrorUnion = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, const TBaseSchemaAndConfig extends BaseCallApiSchemaAndConfig = BaseCallApiSchemaAndConfig, const TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TComputedBaseSchemaConfig extends CallApiSchemaConfig = NonNullable<Writeable<TBaseSchemaAndConfig["config"], "deep">>, TComputedBaseSchemaRoutes extends BaseCallApiSchemaRoutes = Writeable<TBaseSchemaAndConfig["routes"], "deep">>(initBaseConfig?: BaseCallApiConfig<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemaAndConfig, TBasePluginArray>) => <TData = TBaseData, TErrorData = TBaseErrorData, TResultMode extends ResultModeUnion = TBaseResultMode, TThrowOnError extends ThrowOnErrorUnion = TBaseThrowOnError, TResponseType extends ResponseTypeUnion = TBaseResponseType, const TSchemaConfig extends CallApiSchemaConfig = TComputedBaseSchemaConfig, TInitURL extends InferInitURL<TComputedBaseSchemaRoutes, TSchemaConfig> = InferInitURL<TComputedBaseSchemaRoutes, TSchemaConfig>, TCurrentRouteSchemaKey extends GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL> = GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL>, const TSchema extends CallApiSchema = GetCurrentRouteSchema<TComputedBaseSchemaRoutes, TCurrentRouteSchemaKey>, const TPluginArray extends CallApiPlugin[] = TBasePluginArray>(initURL: TInitURL, config?: CallApiConfig<
|
5
|
+
declare const createFetchClient: <TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends ThrowOnErrorUnion = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, const TBaseSchemaAndConfig extends BaseCallApiSchemaAndConfig = BaseCallApiSchemaAndConfig, const TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TComputedBaseSchemaConfig extends CallApiSchemaConfig = NonNullable<Writeable<TBaseSchemaAndConfig["config"], "deep">>, TComputedBaseSchemaRoutes extends BaseCallApiSchemaRoutes = Writeable<TBaseSchemaAndConfig["routes"], "deep">>(initBaseConfig?: BaseCallApiConfig<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemaAndConfig, TBasePluginArray>) => <TData = TBaseData, TErrorData = TBaseErrorData, TResultMode extends ResultModeUnion = TBaseResultMode, TThrowOnError extends ThrowOnErrorUnion = TBaseThrowOnError, TResponseType extends ResponseTypeUnion = TBaseResponseType, const TSchemaConfig extends CallApiSchemaConfig = TComputedBaseSchemaConfig, TInitURL extends InferInitURL<TComputedBaseSchemaRoutes, TSchemaConfig> = InferInitURL<TComputedBaseSchemaRoutes, TSchemaConfig>, TCurrentRouteSchemaKey extends GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL> = GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL>, const TSchema extends CallApiSchema = GetCurrentRouteSchema<TComputedBaseSchemaRoutes, TCurrentRouteSchemaKey>, const TPluginArray extends CallApiPlugin[] = TBasePluginArray>(initURL: TInitURL, config?: CallApiConfig<InferSchemaOutputResult<TSchema["data"], GetResponseType<TData, TResponseType>>, InferSchemaOutputResult<TSchema["errorData"], GetResponseType<TErrorData, TResponseType>>, TResultMode, TThrowOnError, TResponseType, TComputedBaseSchemaRoutes, TSchema, TComputedBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, TBasePluginArray, TPluginArray> | undefined) => CallApiResult<InferSchemaOutputResult<TSchema["data"], TData>, InferSchemaOutputResult<TSchema["errorData"], TErrorData>, TResultMode, TThrowOnError, TResponseType>;
|
6
6
|
declare const callApi: <TData = unknown, TErrorData = unknown, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends ThrowOnErrorUnion = boolean, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, const TSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TInitURL extends ApplyStrictConfig<TSchemaConfig, ApplyURLBasedConfig<TSchemaConfig, AnyString | "@delete/" | "@get/" | "@patch/" | "@post/" | "@put/">> = ApplyStrictConfig<TSchemaConfig, ApplyURLBasedConfig<TSchemaConfig, AnyString | "@delete/" | "@get/" | "@patch/" | "@post/" | "@put/">>, TCurrentRouteSchemaKey extends GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL> = GetCurrentRouteSchemaKey<TSchemaConfig, TInitURL>, const TSchema extends CallApiSchema = GetCurrentRouteSchema<{
|
7
7
|
[x: AnyString]: CallApiSchema | undefined;
|
8
8
|
"@delete/"?: CallApiSchema | undefined;
|
@@ -38,14 +38,14 @@ declare const callApi: <TData = unknown, TErrorData = unknown, TResultMode exten
|
|
38
38
|
"@patch/"?: CallApiSchema | undefined;
|
39
39
|
"@post/"?: CallApiSchema | undefined;
|
40
40
|
"@put/"?: CallApiSchema | undefined;
|
41
|
-
}[TCurrentRouteSchemaKey], "deep">>>, const TPluginArray extends CallApiPlugin[] = DefaultPluginArray>(initURL: TInitURL, config?: CallApiConfig<
|
41
|
+
}[TCurrentRouteSchemaKey], "deep">>>, const TPluginArray extends CallApiPlugin[] = DefaultPluginArray>(initURL: TInitURL, config?: CallApiConfig<InferSchemaOutputResult<TSchema["data"], GetResponseType<TData, TResponseType, ResponseTypeMap<TData>>>, InferSchemaOutputResult<TSchema["errorData"], GetResponseType<TErrorData, TResponseType, ResponseTypeMap<TErrorData>>>, TResultMode, TThrowOnError, TResponseType, {
|
42
42
|
[x: AnyString]: CallApiSchema | undefined;
|
43
43
|
"@delete/"?: CallApiSchema | undefined;
|
44
44
|
"@get/"?: CallApiSchema | undefined;
|
45
45
|
"@patch/"?: CallApiSchema | undefined;
|
46
46
|
"@post/"?: CallApiSchema | undefined;
|
47
47
|
"@put/"?: CallApiSchema | undefined;
|
48
|
-
}, TSchema, CallApiSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, DefaultPluginArray, TPluginArray> | undefined) => CallApiResult<
|
48
|
+
}, TSchema, CallApiSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, DefaultPluginArray, TPluginArray> | undefined) => CallApiResult<InferSchemaOutputResult<TSchema["data"], TData>, InferSchemaOutputResult<TSchema["errorData"], TErrorData>, TResultMode, TThrowOnError, TResponseType>;
|
49
49
|
//#endregion
|
50
50
|
//#region src/defineHelpers.d.ts
|
51
51
|
declare const defineSchema: <const TBaseSchemaRoutes extends BaseCallApiSchemaRoutes, const TSchemaConfig extends CallApiSchemaConfig>(routes: TBaseSchemaRoutes, config?: TSchemaConfig) => {
|
@@ -58,5 +58,5 @@ declare const definePlugin: <const TPlugin extends CallApiPlugin | AnyFunction<C
|
|
58
58
|
declare const defineBaseConfig: <const TBaseConfig extends BaseCallApiConfig>(baseConfig: TBaseConfig) => Writeable<typeof baseConfig, "deep">;
|
59
59
|
declare const defineParameters: <TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends ThrowOnErrorUnion = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemaRoutes extends BaseCallApiSchemaRoutes = BaseCallApiSchemaRoutes, TSchema extends CallApiSchema = CallApiSchema, TBaseSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TSchemaConfig extends CallApiSchemaConfig = CallApiSchemaConfig, TInitURL extends InferInitURL<BaseCallApiSchemaRoutes, TSchemaConfig> = InferInitURL<BaseCallApiSchemaRoutes, TSchemaConfig>, TCurrentRouteSchemaKey extends string = string, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TPluginArray extends CallApiPlugin[] = DefaultPluginArray>(...parameters: CallApiParameters<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TBaseSchemaRoutes, TSchema, TBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, TBasePluginArray, TPluginArray>) => CallApiParameters<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TBaseSchemaRoutes, TSchema, TBaseSchemaConfig, TSchemaConfig, TInitURL, TCurrentRouteSchemaKey, TBasePluginArray, TPluginArray>;
|
60
60
|
//#endregion
|
61
|
-
export { type BaseCallApiConfig, type BaseCallApiExtraOptions, type BaseCallApiSchemaRoutes, type CallApiConfig, type CallApiExtraOptions, type CallApiExtraOptionsForHooks, type CallApiParameters, type CallApiPlugin, type CallApiRequestOptions, type CallApiRequestOptionsForHooks, type CallApiResult, type CallApiResultErrorVariant, type CallApiResultSuccessVariant, type CallApiSchema, type CallApiSchemaConfig, type DedupeOptions, type ErrorContext, HTTPError, type Hooks, type HooksOrHooksArray, type InferParamsFromRoute, type
|
61
|
+
export { type BaseCallApiConfig, type BaseCallApiExtraOptions, type BaseCallApiSchemaRoutes, type CallApiConfig, type CallApiExtraOptions, type CallApiExtraOptionsForHooks, type CallApiParameters, type CallApiPlugin, type CallApiRequestOptions, type CallApiRequestOptionsForHooks, type CallApiResult, type CallApiResultErrorVariant, type CallApiResultSuccessVariant, type CallApiSchema, type CallApiSchemaConfig, type DedupeOptions, type ErrorContext, HTTPError, type Hooks, type HooksOrHooksArray, type InferParamsFromRoute, type InferSchemaOutputResult, type PluginExtraOptions, type PluginHooks, type PluginHooksWithMoreOptions, type PluginSetupContext, type PossibleHTTPError, type PossibleJavaScriptError, type PossibleJavaScriptOrValidationError, type PossibleValidationError, type Register, type RequestContext, type RequestStreamContext, type ResponseContext, type ResponseErrorContext, type ResponseStreamContext, type ResponseTypeUnion, type ResultModeUnion, type RetryOptions, type SuccessContext, type URLOptions, ValidationError, callApi, createFetchClient, defineBaseConfig, defineParameters, definePlugin, defineSchema, defineSchemaConfig, defineSchemaRoutes, fallBackRouteSchemaKey };
|
62
62
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { HTTPError, ValidationError, createCombinedSignal, createTimeoutSignal, defineEnum, deterministicHashFn, extraOptionDefaults, getBody, getFetchImpl, getHeaders, isArray, isBoolean, isFunction, isHTTPErrorInstance, isObject, isPlainObject, isReadableStream, isString, isValidationErrorInstance, requestOptionDefaults, splitBaseConfig, splitConfig, toArray, toQueryString, waitFor } from "./utils-
|
1
|
+
import { HTTPError, ValidationError, createCombinedSignal, createTimeoutSignal, defineEnum, deterministicHashFn, extraOptionDefaults, getBody, getFetchImpl, getHeaders, isArray, isBoolean, isFunction, isHTTPErrorInstance, isObject, isPlainObject, isReadableStream, isString, isValidationErrorInstance, requestOptionDefaults, splitBaseConfig, splitConfig, toArray, toQueryString, waitFor } from "./utils-zvYT8TK8.js";
|
2
2
|
|
3
3
|
//#region src/result.ts
|
4
4
|
const getResponseType = (response, parser) => ({
|
@@ -45,14 +45,11 @@ const getResultModeMap = (details) => {
|
|
45
45
|
};
|
46
46
|
const resolveSuccessResult = (data, info) => {
|
47
47
|
const { response, resultMode } = info;
|
48
|
-
|
48
|
+
return getResultModeMap({
|
49
49
|
data,
|
50
50
|
error: null,
|
51
51
|
response
|
52
|
-
};
|
53
|
-
const resultModeMap = getResultModeMap(details);
|
54
|
-
const successResult = resultModeMap[resultMode ?? "all"]();
|
55
|
-
return successResult;
|
52
|
+
})[resultMode ?? "all"]();
|
56
53
|
};
|
57
54
|
const resolveErrorResult = (error, info) => {
|
58
55
|
const { cloneResponse, message: customErrorMessage, resultMode } = info;
|
@@ -92,8 +89,7 @@ const resolveErrorResult = (error, info) => {
|
|
92
89
|
response: cloneResponse ? response.clone() : response
|
93
90
|
};
|
94
91
|
}
|
95
|
-
const
|
96
|
-
const generalErrorResult = resultModeMap[resultMode ?? "all"]();
|
92
|
+
const generalErrorResult = getResultModeMap(errorDetails)[resultMode ?? "all"]();
|
97
93
|
return {
|
98
94
|
errorDetails,
|
99
95
|
generalErrorResult
|
@@ -270,12 +266,8 @@ const createDedupeStrategy = async (context) => {
|
|
270
266
|
const dedupeStrategy = globalOptions.dedupeStrategy ?? extraOptionDefaults().dedupeStrategy;
|
271
267
|
const resolvedDedupeStrategy = isFunction(dedupeStrategy) ? dedupeStrategy(context) : dedupeStrategy;
|
272
268
|
const getDedupeKey = () => {
|
273
|
-
|
274
|
-
if (
|
275
|
-
if (globalOptions.dedupeKey) {
|
276
|
-
const resolvedDedupeKey = isFunction(globalOptions.dedupeKey) ? globalOptions.dedupeKey(context) : globalOptions.dedupeKey;
|
277
|
-
return resolvedDedupeKey;
|
278
|
-
}
|
269
|
+
if (!(resolvedDedupeStrategy === "cancel" || resolvedDedupeStrategy === "defer")) return null;
|
270
|
+
if (globalOptions.dedupeKey) return isFunction(globalOptions.dedupeKey) ? globalOptions.dedupeKey(context) : globalOptions.dedupeKey;
|
279
271
|
return `${globalOptions.fullURL}-${deterministicHashFn({
|
280
272
|
options: globalOptions,
|
281
273
|
request: globalRequest
|
@@ -298,8 +290,7 @@ const createDedupeStrategy = async (context) => {
|
|
298
290
|
return `Duplicate request detected - Aborted previous request to '${globalOptions.fullURL}' as a new request with identical options was initiated`;
|
299
291
|
};
|
300
292
|
const handleRequestCancelStrategy = () => {
|
301
|
-
|
302
|
-
if (!shouldCancelRequest) return;
|
293
|
+
if (!(prevRequestInfo && resolvedDedupeStrategy === "cancel")) return;
|
303
294
|
const message = getAbortErrorMessage();
|
304
295
|
const reason = new DOMException(message, "AbortError");
|
305
296
|
prevRequestInfo.controller.abort(reason);
|
@@ -321,11 +312,10 @@ const createDedupeStrategy = async (context) => {
|
|
321
312
|
controller: newFetchController,
|
322
313
|
responsePromise
|
323
314
|
});
|
324
|
-
|
315
|
+
return toStreamableResponse({
|
325
316
|
...streamableContext,
|
326
317
|
response: await responsePromise
|
327
318
|
});
|
328
|
-
return streamableResponse;
|
329
319
|
};
|
330
320
|
const removeDedupeKeyFromCache = () => {
|
331
321
|
$RequestInfoCacheOrNull?.delete(dedupeKey);
|
@@ -343,10 +333,9 @@ const createDedupeStrategy = async (context) => {
|
|
343
333
|
//#region src/validation.ts
|
344
334
|
const handleValidatorFunction = async (validator, inputData) => {
|
345
335
|
try {
|
346
|
-
const result = await validator(inputData);
|
347
336
|
return {
|
348
337
|
issues: void 0,
|
349
|
-
value:
|
338
|
+
value: await validator(inputData)
|
350
339
|
};
|
351
340
|
} catch (error) {
|
352
341
|
return {
|
@@ -373,8 +362,7 @@ const routeKeyMethods = defineEnum([
|
|
373
362
|
const handleSchemaValidation = async (schema, validationOptions) => {
|
374
363
|
const { inputValue, response, schemaConfig } = validationOptions;
|
375
364
|
if (!schema || schemaConfig?.disableRuntimeValidation) return inputValue;
|
376
|
-
|
377
|
-
return validResult;
|
365
|
+
return await standardSchemaParser(schema, inputValue, response);
|
378
366
|
};
|
379
367
|
const extraOptionsToBeValidated = [
|
380
368
|
"meta",
|
@@ -474,8 +462,7 @@ const getResolvedSchema = (context) => {
|
|
474
462
|
};
|
475
463
|
const getResolvedSchemaConfig = (context) => {
|
476
464
|
const { baseExtraOptions, extraOptions } = context;
|
477
|
-
|
478
|
-
return resolvedSchemaConfig;
|
465
|
+
return isFunction(extraOptions.schemaConfig) ? extraOptions.schemaConfig({ baseSchemaConfig: baseExtraOptions.schema?.config ?? {} }) : extraOptions.schemaConfig ?? baseExtraOptions.schema?.config;
|
479
466
|
};
|
480
467
|
const getCurrentRouteSchemaKeyAndMainInitURL = (context) => {
|
481
468
|
const { baseExtraOptions, extraOptions, initURL } = context;
|
@@ -500,8 +487,7 @@ const getCurrentRouteSchemaKeyAndMainInitURL = (context) => {
|
|
500
487
|
//#region src/plugins.ts
|
501
488
|
const getResolvedPlugins = (context) => {
|
502
489
|
const { baseConfig, options } = context;
|
503
|
-
|
504
|
-
return resolvedPlugins;
|
490
|
+
return isFunction(options.plugins) ? options.plugins({ basePlugins: baseConfig.plugins ?? [] }) : options.plugins ?? [];
|
505
491
|
};
|
506
492
|
const initializePlugins = async (context) => {
|
507
493
|
const { baseConfig, config, initURL, options, request } = context;
|
@@ -574,8 +560,7 @@ const initializePlugins = async (context) => {
|
|
574
560
|
const flattenedHookArray = [...hookRegistry].flat();
|
575
561
|
if (flattenedHookArray.length === 0) continue;
|
576
562
|
const hooksExecutionMode = options.hooksExecutionMode ?? extraOptionDefaults().hooksExecutionMode;
|
577
|
-
|
578
|
-
resolvedHooks[key] = composedHook;
|
563
|
+
resolvedHooks[key] = composeAllHooks(flattenedHookArray, hooksExecutionMode);
|
579
564
|
}
|
580
565
|
return {
|
581
566
|
resolvedCurrentRouteSchemaKey,
|
@@ -590,8 +575,7 @@ const initializePlugins = async (context) => {
|
|
590
575
|
//#region src/retry.ts
|
591
576
|
const getLinearDelay = (currentAttemptCount, options) => {
|
592
577
|
const retryDelay = options.retryDelay ?? options.retry?.delay;
|
593
|
-
|
594
|
-
return resolveRetryDelay;
|
578
|
+
return (isFunction(retryDelay) ? retryDelay(currentAttemptCount) : retryDelay) ?? extraOptionDefaults().retryDelay;
|
595
579
|
};
|
596
580
|
const getExponentialDelay = (currentAttemptCount, options) => {
|
597
581
|
const retryDelay = options.retryDelay ?? options.retry?.delay ?? extraOptionDefaults().retryDelay;
|
@@ -616,14 +600,12 @@ const createRetryStrategy = (ctx) => {
|
|
616
600
|
const retryCondition = options.retryCondition ?? options.retry?.condition ?? extraOptionDefaults().retryCondition;
|
617
601
|
const maximumRetryAttempts = options.retryAttempts ?? options.retry?.attempts ?? extraOptionDefaults().retryAttempts;
|
618
602
|
const customRetryCondition = await retryCondition(ctx);
|
619
|
-
|
620
|
-
if (!baseShouldRetry) return false;
|
603
|
+
if (!(currentAttemptCount <= maximumRetryAttempts && customRetryCondition)) return false;
|
621
604
|
const retryMethods = new Set(options.retryMethods ?? options.retry?.methods ?? extraOptionDefaults().retryMethods);
|
622
605
|
const includesMethod = isString(ctx.request.method) && retryMethods.size > 0 ? retryMethods.has(ctx.request.method) : true;
|
623
606
|
const retryStatusCodes = new Set(options.retryStatusCodes ?? options.retry?.statusCodes ?? extraOptionDefaults().retryStatusCodes);
|
624
607
|
const includesStatusCodes = Boolean(ctx.response) && retryStatusCodes.size > 0 ? retryStatusCodes.has(ctx.response.status) : true;
|
625
|
-
|
626
|
-
return shouldRetry;
|
608
|
+
return includesMethod && includesStatusCodes;
|
627
609
|
};
|
628
610
|
return {
|
629
611
|
currentAttemptCount,
|
@@ -642,8 +624,7 @@ const mergeUrlWithParams = (url, params) => {
|
|
642
624
|
if (!params) return url;
|
643
625
|
let newUrl = url;
|
644
626
|
if (isArray(params)) {
|
645
|
-
const
|
646
|
-
const matchedParamsArray = urlParts.filter((part) => part.startsWith(colon) || part.startsWith(openBrace) && part.endsWith(closeBrace));
|
627
|
+
const matchedParamsArray = newUrl.split(slash).filter((part) => part.startsWith(colon) || part.startsWith(openBrace) && part.endsWith(closeBrace));
|
647
628
|
for (const [paramIndex, matchedParam] of matchedParamsArray.entries()) {
|
648
629
|
const stringParamValue = String(params[paramIndex]);
|
649
630
|
newUrl = newUrl.replace(matchedParam, stringParamValue);
|
@@ -714,18 +695,15 @@ const getMethod = (ctx) => {
|
|
714
695
|
const normalizeURL = (initURL) => {
|
715
696
|
const methodFromURL = extractMethodFromURL(initURL);
|
716
697
|
if (!methodFromURL) return initURL;
|
717
|
-
|
718
|
-
return normalizedURL;
|
698
|
+
return initURL.replace(`@${methodFromURL}/`, "/");
|
719
699
|
};
|
720
700
|
const getFullAndNormalizedURL = (options) => {
|
721
701
|
const { baseURL, initURL, params, query } = options;
|
722
702
|
const normalizedInitURL = normalizeURL(initURL);
|
723
703
|
const urlWithMergedParams = mergeUrlWithParams(normalizedInitURL, params);
|
724
704
|
const urlWithMergedQueryAndParams = mergeUrlWithQuery(urlWithMergedParams, query);
|
725
|
-
const shouldPrependBaseURL = !urlWithMergedQueryAndParams.startsWith("http") && baseURL;
|
726
|
-
const fullURL = shouldPrependBaseURL ? `${baseURL}${urlWithMergedQueryAndParams}` : urlWithMergedQueryAndParams;
|
727
705
|
return {
|
728
|
-
fullURL,
|
706
|
+
fullURL: !urlWithMergedQueryAndParams.startsWith("http") && baseURL ? `${baseURL}${urlWithMergedQueryAndParams}` : urlWithMergedQueryAndParams,
|
729
707
|
normalizedInitURL
|
730
708
|
};
|
731
709
|
};
|
@@ -738,12 +716,11 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
738
716
|
const callApi$1 = async (...parameters) => {
|
739
717
|
const [initURLOrURLObject, initConfig = {}] = parameters;
|
740
718
|
const [fetchOptions, extraOptions] = splitConfig(initConfig);
|
741
|
-
const
|
719
|
+
const baseConfig = isFunction(initBaseConfig) ? initBaseConfig({
|
742
720
|
initURL: initURLOrURLObject.toString(),
|
743
721
|
options: extraOptions,
|
744
722
|
request: fetchOptions
|
745
723
|
}) : initBaseConfig;
|
746
|
-
const baseConfig = resolvedBaseConfig;
|
747
724
|
const config = initConfig;
|
748
725
|
const [baseFetchOptions, baseExtraOptions] = splitBaseConfig(baseConfig);
|
749
726
|
const shouldSkipAutoMergeForOptions = baseExtraOptions.skipAutoMergeFor === "all" || baseExtraOptions.skipAutoMergeFor === "options";
|
@@ -870,11 +847,10 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
870
847
|
...successContext,
|
871
848
|
error: null
|
872
849
|
}));
|
873
|
-
|
850
|
+
return resolveSuccessResult(successContext.data, {
|
874
851
|
response: successContext.response,
|
875
852
|
resultMode: options.resultMode
|
876
853
|
});
|
877
|
-
return successResult;
|
878
854
|
} catch (error) {
|
879
855
|
const errorInfo = {
|
880
856
|
cloneResponse: options.cloneResponse,
|
@@ -896,14 +872,13 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
896
872
|
};
|
897
873
|
const handleRetryOrGetErrorResult = async () => {
|
898
874
|
const { currentAttemptCount, getDelay, shouldAttemptRetry } = createRetryStrategy(errorContext);
|
899
|
-
|
900
|
-
if (shouldRetry) {
|
875
|
+
if (await shouldAttemptRetry()) {
|
901
876
|
const retryContext = {
|
902
877
|
...errorContext,
|
903
878
|
retryAttemptCount: currentAttemptCount
|
904
879
|
};
|
905
|
-
const hookError
|
906
|
-
if (hookError
|
880
|
+
const hookError = await executeHooksInCatchBlock([options.onRetry?.(retryContext)], hookInfo);
|
881
|
+
if (hookError) return hookError;
|
907
882
|
const delay = getDelay();
|
908
883
|
await waitFor(delay);
|
909
884
|
const updatedOptions = {
|
@@ -915,25 +890,19 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
915
890
|
if (shouldThrowOnError) throw error;
|
916
891
|
return generalErrorResult;
|
917
892
|
};
|
918
|
-
if (isValidationErrorInstance(error))
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
...errorContext,
|
932
|
-
data: null
|
933
|
-
})
|
934
|
-
], hookInfo);
|
935
|
-
return hookError$1 ?? await handleRetryOrGetErrorResult();
|
936
|
-
}
|
893
|
+
if (isValidationErrorInstance(error)) return await executeHooksInCatchBlock([
|
894
|
+
options.onValidationError?.(errorContext),
|
895
|
+
options.onRequestError?.(errorContext),
|
896
|
+
options.onError?.(errorContext)
|
897
|
+
], hookInfo) ?? await handleRetryOrGetErrorResult();
|
898
|
+
if (isHTTPErrorInstance(error)) return await executeHooksInCatchBlock([
|
899
|
+
options.onResponseError?.(errorContext),
|
900
|
+
options.onError?.(errorContext),
|
901
|
+
options.onResponse?.({
|
902
|
+
...errorContext,
|
903
|
+
data: null
|
904
|
+
})
|
905
|
+
], hookInfo) ?? await handleRetryOrGetErrorResult();
|
937
906
|
let message = error?.message;
|
938
907
|
if (error instanceof DOMException && error.name === "AbortError") {
|
939
908
|
message = getAbortErrorMessage();
|
@@ -943,8 +912,7 @@ const createFetchClient = (initBaseConfig = {}) => {
|
|
943
912
|
message = `Request timed out after ${options.timeout}ms`;
|
944
913
|
!shouldThrowOnError && console.error(`${error.name}:`, message);
|
945
914
|
}
|
946
|
-
|
947
|
-
return hookError ?? getCustomizedErrorResult(await handleRetryOrGetErrorResult(), { message });
|
915
|
+
return await executeHooksInCatchBlock([options.onRequestError?.(errorContext), options.onError?.(errorContext)], hookInfo) ?? getCustomizedErrorResult(await handleRetryOrGetErrorResult(), { message });
|
948
916
|
} finally {
|
949
917
|
removeDedupeKeyFromCache();
|
950
918
|
}
|