@zayne-labs/callapi 1.6.11 → 1.6.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/common-B_9jiZXC.d.cts +551 -0
- package/dist/cjs/error-lBRMiMeF.d.cts +17 -0
- package/dist/cjs/index.cjs +1 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +18 -0
- package/dist/cjs/options/index.cjs +1 -0
- package/dist/cjs/options/index.cjs.map +1 -0
- package/dist/cjs/options/index.d.cts +25 -0
- package/dist/cjs/utils/index.cjs +1 -0
- package/dist/cjs/utils/index.cjs.map +1 -0
- package/dist/cjs/utils/index.d.cts +16 -0
- package/dist/esm/chunk-DQY5GNZB.js +1 -0
- package/dist/esm/chunk-DQY5GNZB.js.map +1 -0
- package/dist/esm/chunk-MOMG57MZ.js +1 -0
- package/dist/esm/chunk-MOMG57MZ.js.map +1 -0
- package/dist/esm/common-B_9jiZXC.d.ts +551 -0
- package/dist/esm/error-lBRMiMeF.d.ts +17 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/options/index.d.ts +25 -0
- package/dist/esm/options/index.js +1 -0
- package/dist/esm/options/index.js.map +1 -0
- package/dist/esm/utils/index.d.ts +16 -0
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
|
|
3
|
+
type ValueOrFunctionResult<TValue> = TValue | (() => TValue);
|
|
4
|
+
/**
|
|
5
|
+
* Bearer Or Token authentication
|
|
6
|
+
*
|
|
7
|
+
* The value of `bearer` will be added to a header as
|
|
8
|
+
* `auth: Bearer some-auth-token`,
|
|
9
|
+
*
|
|
10
|
+
* The value of `token` will be added to a header as
|
|
11
|
+
* `auth: Token some-auth-token`,
|
|
12
|
+
*/
|
|
13
|
+
type BearerOrTokenAuth = {
|
|
14
|
+
type?: "Bearer";
|
|
15
|
+
bearer?: ValueOrFunctionResult<string | null>;
|
|
16
|
+
token?: never;
|
|
17
|
+
} | {
|
|
18
|
+
type?: "Token";
|
|
19
|
+
bearer?: never;
|
|
20
|
+
token?: ValueOrFunctionResult<string | null>;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Basic auth
|
|
24
|
+
*/
|
|
25
|
+
type BasicAuth = {
|
|
26
|
+
type: "Basic";
|
|
27
|
+
username: ValueOrFunctionResult<string | null | undefined>;
|
|
28
|
+
password: ValueOrFunctionResult<string | null | undefined>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Custom auth
|
|
32
|
+
*
|
|
33
|
+
* @param prefix - prefix of the header
|
|
34
|
+
* @param authValue - value of the header
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* {
|
|
39
|
+
* type: "Custom",
|
|
40
|
+
* prefix: "Token",
|
|
41
|
+
* authValue: "token"
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
type CustomAuth = {
|
|
46
|
+
type: "Custom";
|
|
47
|
+
prefix: ValueOrFunctionResult<string | null | undefined>;
|
|
48
|
+
value: ValueOrFunctionResult<string | null | undefined>;
|
|
49
|
+
};
|
|
50
|
+
type Auth = BearerOrTokenAuth | BasicAuth | CustomAuth;
|
|
51
|
+
|
|
52
|
+
type AnyString = string & {
|
|
53
|
+
z_placeholder?: never;
|
|
54
|
+
};
|
|
55
|
+
type AnyNumber = number & {
|
|
56
|
+
z_placeholder?: never;
|
|
57
|
+
};
|
|
58
|
+
type AnyFunction<TResult = unknown> = (...args: any) => TResult;
|
|
59
|
+
type UnmaskType<TValue> = {
|
|
60
|
+
_: TValue;
|
|
61
|
+
}["_"];
|
|
62
|
+
type Awaitable<TValue> = Promise<TValue> | TValue;
|
|
63
|
+
type CommonRequestHeaders = "Access-Control-Allow-Credentials" | "Access-Control-Allow-Headers" | "Access-Control-Allow-Methods" | "Access-Control-Allow-Origin" | "Access-Control-Expose-Headers" | "Access-Control-Max-Age" | "Age" | "Allow" | "Cache-Control" | "Clear-Site-Data" | "Content-Disposition" | "Content-Encoding" | "Content-Language" | "Content-Length" | "Content-Location" | "Content-Range" | "Content-Security-Policy-Report-Only" | "Content-Security-Policy" | "Cookie" | "Cross-Origin-Embedder-Policy" | "Cross-Origin-Opener-Policy" | "Cross-Origin-Resource-Policy" | "Date" | "ETag" | "Expires" | "Last-Modified" | "Location" | "Permissions-Policy" | "Pragma" | "Retry-After" | "Save-Data" | "Sec-CH-Prefers-Color-Scheme" | "Sec-CH-Prefers-Reduced-Motion" | "Sec-CH-UA-Arch" | "Sec-CH-UA-Bitness" | "Sec-CH-UA-Form-Factor" | "Sec-CH-UA-Full-Version-List" | "Sec-CH-UA-Full-Version" | "Sec-CH-UA-Mobile" | "Sec-CH-UA-Model" | "Sec-CH-UA-Platform-Version" | "Sec-CH-UA-Platform" | "Sec-CH-UA-WoW64" | "Sec-CH-UA" | "Sec-Fetch-Dest" | "Sec-Fetch-Mode" | "Sec-Fetch-Site" | "Sec-Fetch-User" | "Sec-GPC" | "Server-Timing" | "Server" | "Service-Worker-Navigation-Preload" | "Set-Cookie" | "Strict-Transport-Security" | "Timing-Allow-Origin" | "Trailer" | "Transfer-Encoding" | "Upgrade" | "Vary" | "Warning" | "WWW-Authenticate" | "X-Content-Type-Options" | "X-DNS-Prefetch-Control" | "X-Frame-Options" | "X-Permitted-Cross-Domain-Policies" | "X-Powered-By" | "X-Robots-Tag" | "X-XSS-Protection" | AnyString;
|
|
64
|
+
type CommonAuthorizationHeaders = `${"Basic" | "Bearer" | "Token"} ${string}`;
|
|
65
|
+
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
|
+
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;
|
|
76
|
+
/**
|
|
77
|
+
* The schema to use for validating the response error data.
|
|
78
|
+
*/
|
|
79
|
+
errorData?: StandardSchemaV1;
|
|
80
|
+
/**
|
|
81
|
+
* The schema to use for validating the request headers.
|
|
82
|
+
*/
|
|
83
|
+
headers?: StandardSchemaV1<Headers>;
|
|
84
|
+
/**
|
|
85
|
+
* The schema to use for validating the request url.
|
|
86
|
+
*/
|
|
87
|
+
initURL?: StandardSchemaV1<InitURL>;
|
|
88
|
+
/**
|
|
89
|
+
* The schema to use for validating the meta option.
|
|
90
|
+
*/
|
|
91
|
+
meta?: StandardSchemaV1<GlobalMeta>;
|
|
92
|
+
/**
|
|
93
|
+
* The schema to use for validating the request method.
|
|
94
|
+
*/
|
|
95
|
+
method?: StandardSchemaV1<Method>;
|
|
96
|
+
/**
|
|
97
|
+
* The schema to use for validating the request url parameter.
|
|
98
|
+
*/
|
|
99
|
+
params?: StandardSchemaV1<Params>;
|
|
100
|
+
/**
|
|
101
|
+
* The schema to use for validating the request url querys.
|
|
102
|
+
*/
|
|
103
|
+
query?: StandardSchemaV1<Query>;
|
|
104
|
+
}
|
|
105
|
+
interface CallApiValidators<TData = unknown, TErrorData = unknown> {
|
|
106
|
+
/**
|
|
107
|
+
* Custom function to validate the response data.
|
|
108
|
+
*/
|
|
109
|
+
data?: (value: unknown) => TData;
|
|
110
|
+
/**
|
|
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.
|
|
113
|
+
*/
|
|
114
|
+
errorData?: (value: unknown) => TErrorData;
|
|
115
|
+
}
|
|
116
|
+
type InferSchemaResult<TSchema, TData> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TData;
|
|
117
|
+
|
|
118
|
+
type Params = UnmaskType<Record<string, boolean | number | string> | Array<boolean | number | string>>;
|
|
119
|
+
type Query = UnmaskType<Record<string, boolean | number | string>>;
|
|
120
|
+
type InitURL = UnmaskType<string | URL>;
|
|
121
|
+
interface UrlOptions<TSchemas extends CallApiSchemas> {
|
|
122
|
+
/**
|
|
123
|
+
* URL to be used in the request.
|
|
124
|
+
*/
|
|
125
|
+
readonly initURL?: string;
|
|
126
|
+
/**
|
|
127
|
+
* Parameters to be appended to the URL (i.e: /:id)
|
|
128
|
+
*/
|
|
129
|
+
params?: InferSchemaResult<TSchemas["params"], Params>;
|
|
130
|
+
/**
|
|
131
|
+
* Query parameters to append to the URL.
|
|
132
|
+
*/
|
|
133
|
+
query?: InferSchemaResult<TSchemas["query"], Query>;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
type RetryCondition<TErrorData> = (context: ErrorContext<TErrorData>) => boolean | Promise<boolean>;
|
|
137
|
+
interface RetryOptions<TErrorData> {
|
|
138
|
+
/**
|
|
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.
|
|
141
|
+
*/
|
|
142
|
+
readonly "~retryCount"?: number;
|
|
143
|
+
/**
|
|
144
|
+
* Number of allowed retry attempts on HTTP errors
|
|
145
|
+
* @default 0
|
|
146
|
+
*/
|
|
147
|
+
retryAttempts?: number;
|
|
148
|
+
/**
|
|
149
|
+
* Callback whose return value determines if a request should be retried or not
|
|
150
|
+
*/
|
|
151
|
+
retryCondition?: RetryCondition<TErrorData>;
|
|
152
|
+
/**
|
|
153
|
+
* Delay between retries in milliseconds
|
|
154
|
+
* @default 1000
|
|
155
|
+
*/
|
|
156
|
+
retryDelay?: number;
|
|
157
|
+
/**
|
|
158
|
+
* Maximum delay in milliseconds. Only applies to exponential strategy
|
|
159
|
+
* @default 10000
|
|
160
|
+
*/
|
|
161
|
+
retryMaxDelay?: number;
|
|
162
|
+
/**
|
|
163
|
+
* HTTP methods that are allowed to retry
|
|
164
|
+
* @default ["GET", "POST"]
|
|
165
|
+
*/
|
|
166
|
+
retryMethods?: Method[];
|
|
167
|
+
/**
|
|
168
|
+
* HTTP status codes that trigger a retry
|
|
169
|
+
* @default [409, 425, 429, 500, 502, 503, 504]
|
|
170
|
+
*/
|
|
171
|
+
retryStatusCodes?: Array<409 | 425 | 429 | 500 | 502 | 503 | 504 | AnyNumber>;
|
|
172
|
+
/**
|
|
173
|
+
* Strategy to use when retrying
|
|
174
|
+
* @default "linear"
|
|
175
|
+
*/
|
|
176
|
+
retryStrategy?: "exponential" | "linear";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
type DefaultDataType = unknown;
|
|
180
|
+
type DefaultMoreOptions = NonNullable<unknown>;
|
|
181
|
+
type DefaultPluginArray = CallApiPlugin[];
|
|
182
|
+
type DefaultThrowOnError = boolean;
|
|
183
|
+
|
|
184
|
+
type Parser = (responseString: string) => Awaitable<Record<string, unknown>>;
|
|
185
|
+
declare const getResponseType: <TResponse>(response: Response, parser?: Parser) => {
|
|
186
|
+
arrayBuffer: () => Promise<ArrayBuffer>;
|
|
187
|
+
blob: () => Promise<Blob>;
|
|
188
|
+
formData: () => Promise<FormData>;
|
|
189
|
+
json: () => Promise<TResponse>;
|
|
190
|
+
stream: () => ReadableStream<Uint8Array<ArrayBufferLike>> | null;
|
|
191
|
+
text: () => Promise<string>;
|
|
192
|
+
};
|
|
193
|
+
type InitResponseTypeMap<TResponse = unknown> = ReturnType<typeof getResponseType<TResponse>>;
|
|
194
|
+
type ResponseTypeUnion = keyof InitResponseTypeMap | undefined;
|
|
195
|
+
type ResponseTypeMap<TResponse> = {
|
|
196
|
+
[Key in keyof InitResponseTypeMap<TResponse>]: Awaited<ReturnType<InitResponseTypeMap<TResponse>[Key]>>;
|
|
197
|
+
};
|
|
198
|
+
type GetResponseType<TResponse, TResponseType extends ResponseTypeUnion, TComputedMap extends ResponseTypeMap<TResponse> = ResponseTypeMap<TResponse>> = undefined extends TResponseType ? TComputedMap["json"] : TResponseType extends NonNullable<ResponseTypeUnion> ? TComputedMap[TResponseType] : never;
|
|
199
|
+
|
|
200
|
+
type UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : never) extends (param: infer TParam) => void ? TParam : never;
|
|
201
|
+
type InferSchema<TResult> = TResult extends StandardSchemaV1 ? InferSchemaResult<TResult, NonNullable<unknown>> : TResult;
|
|
202
|
+
type InferPluginOptions<TPluginArray extends CallApiPlugin[]> = UnionToIntersection<InferSchema<ReturnType<NonNullable<TPluginArray[number]["createExtraOptions"]>>>>;
|
|
203
|
+
type PluginInitContext<TMoreOptions = DefaultMoreOptions> = WithMoreOptions<TMoreOptions> & {
|
|
204
|
+
initURL: InitURL | undefined;
|
|
205
|
+
options: CombinedCallApiExtraOptions;
|
|
206
|
+
request: CallApiRequestOptionsForHooks;
|
|
207
|
+
};
|
|
208
|
+
type PluginInitResult = Partial<Omit<PluginInitContext, "request"> & {
|
|
209
|
+
request: CallApiRequestOptions;
|
|
210
|
+
}>;
|
|
211
|
+
interface CallApiPlugin<TData = never, TErrorData = never> {
|
|
212
|
+
/**
|
|
213
|
+
* Defines additional options that can be passed to callApi
|
|
214
|
+
*/
|
|
215
|
+
createExtraOptions?: (...params: never[]) => unknown;
|
|
216
|
+
/**
|
|
217
|
+
* A description for the plugin
|
|
218
|
+
*/
|
|
219
|
+
description?: string;
|
|
220
|
+
/**
|
|
221
|
+
* Hooks / Interceptors for the plugin
|
|
222
|
+
*/
|
|
223
|
+
hooks?: InterceptorsOrInterceptorArray<TData, TErrorData>;
|
|
224
|
+
/**
|
|
225
|
+
* A unique id for the plugin
|
|
226
|
+
*/
|
|
227
|
+
id: string;
|
|
228
|
+
/**
|
|
229
|
+
* A function that will be called when the plugin is initialized. This will be called before the any of the other internal functions.
|
|
230
|
+
*/
|
|
231
|
+
init?: (context: PluginInitContext) => Awaitable<PluginInitResult> | Awaitable<void>;
|
|
232
|
+
/**
|
|
233
|
+
* A name for the plugin
|
|
234
|
+
*/
|
|
235
|
+
name: string;
|
|
236
|
+
/**
|
|
237
|
+
* A version for the plugin
|
|
238
|
+
*/
|
|
239
|
+
version?: string;
|
|
240
|
+
}
|
|
241
|
+
declare const definePlugin: <TPlugin extends CallApiPlugin | AnyFunction<CallApiPlugin>>(plugin: TPlugin) => TPlugin;
|
|
242
|
+
type Plugins<TPluginArray extends CallApiPlugin[]> = TPluginArray | ((context: PluginInitContext) => TPluginArray);
|
|
243
|
+
|
|
244
|
+
declare const fetchSpecificKeys: ("body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window")[];
|
|
245
|
+
declare const defaultRetryMethods: ("GET" | "POST")[];
|
|
246
|
+
declare const defaultRetryStatusCodes: Required<BaseCallApiExtraOptions>["retryStatusCodes"];
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* @description Makes a type required if TSchema type is undefined or if the output type of TSchema contains undefined, otherwise keeps it as is
|
|
250
|
+
*/
|
|
251
|
+
type MakeSchemaOptionRequired<TSchema extends StandardSchemaV1 | undefined, TObject> = undefined extends TSchema ? TObject : undefined extends InferSchemaResult<TSchema, NonNullable<unknown>> ? TObject : Required<TObject>;
|
|
252
|
+
type Body = UnmaskType<Record<string, unknown> | RequestInit["body"]>;
|
|
253
|
+
type BodyOption<TSchemas extends CallApiSchemas> = MakeSchemaOptionRequired<TSchemas["body"], {
|
|
254
|
+
/**
|
|
255
|
+
* Body of the request, can be a object or any other supported body type.
|
|
256
|
+
*/
|
|
257
|
+
body?: InferSchemaResult<TSchemas["body"], Body>;
|
|
258
|
+
}>;
|
|
259
|
+
type Method = UnmaskType<"CONNECT" | "DELETE" | "GET" | "HEAD" | "OPTIONS" | "PATCH" | "POST" | "PUT" | "TRACE" | AnyString>;
|
|
260
|
+
type MethodOption<TSchemas extends CallApiSchemas> = MakeSchemaOptionRequired<TSchemas["method"], {
|
|
261
|
+
/**
|
|
262
|
+
* HTTP method for the request.
|
|
263
|
+
* @default "GET"
|
|
264
|
+
*/
|
|
265
|
+
method?: InferSchemaResult<TSchemas["method"], Method>;
|
|
266
|
+
}>;
|
|
267
|
+
type Headers = UnmaskType<Record<"Authorization", CommonAuthorizationHeaders> | Record<"Content-Type", CommonContentTypes> | Record<CommonRequestHeaders, string | undefined> | Record<string, string | undefined> | RequestInit["headers"]>;
|
|
268
|
+
type HeadersOption<TSchemas extends CallApiSchemas> = MakeSchemaOptionRequired<TSchemas["headers"], {
|
|
269
|
+
/**
|
|
270
|
+
* Headers to be used in the request.
|
|
271
|
+
*/
|
|
272
|
+
headers?: InferSchemaResult<TSchemas["headers"], Headers>;
|
|
273
|
+
}>;
|
|
274
|
+
interface Register {
|
|
275
|
+
}
|
|
276
|
+
type GlobalMeta = Register extends {
|
|
277
|
+
meta?: infer TMeta extends Record<string, unknown>;
|
|
278
|
+
} ? TMeta : never;
|
|
279
|
+
type MetaOption<TSchemas extends CallApiSchemas> = {
|
|
280
|
+
/**
|
|
281
|
+
* - An optional field you can fill with additional information,
|
|
282
|
+
* to associate with the request, typically used for logging or tracing.
|
|
283
|
+
*
|
|
284
|
+
* - A good use case for this, would be to use the info to handle specific cases in any of the shared interceptors.
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```ts
|
|
288
|
+
* const callMainApi = callApi.create({
|
|
289
|
+
* baseURL: "https://main-api.com",
|
|
290
|
+
* onResponseError: ({ response, options }) => {
|
|
291
|
+
* if (options.meta?.userId) {
|
|
292
|
+
* console.error(`User ${options.meta.userId} made an error`);
|
|
293
|
+
* }
|
|
294
|
+
* },
|
|
295
|
+
* });
|
|
296
|
+
*
|
|
297
|
+
* const response = await callMainApi({
|
|
298
|
+
* url: "https://example.com/api/data",
|
|
299
|
+
* meta: { userId: "123" },
|
|
300
|
+
* });
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
meta?: InferSchemaResult<TSchemas["meta"], GlobalMeta>;
|
|
304
|
+
};
|
|
305
|
+
type ResultModeOption<TErrorData, TResultMode extends ResultModeUnion> = TErrorData extends false ? {
|
|
306
|
+
resultMode: "onlySuccessWithException";
|
|
307
|
+
} : TErrorData extends false | undefined ? {
|
|
308
|
+
resultMode?: "onlySuccessWithException";
|
|
309
|
+
} : undefined extends TResultMode ? {
|
|
310
|
+
resultMode?: TResultMode;
|
|
311
|
+
} : {
|
|
312
|
+
resultMode: TResultMode;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
type FetchSpecificKeysUnion = Exclude<(typeof fetchSpecificKeys)[number], "body" | "headers" | "method">;
|
|
316
|
+
type CallApiRequestOptions<TSchemas extends CallApiSchemas = DefaultMoreOptions> = BodyOption<TSchemas> & HeadersOption<TSchemas> & MethodOption<TSchemas> & Pick<RequestInit, FetchSpecificKeysUnion>;
|
|
317
|
+
type CallApiRequestOptionsForHooks<TSchemas extends CallApiSchemas = DefaultMoreOptions> = Omit<CallApiRequestOptions<TSchemas>, "headers"> & {
|
|
318
|
+
headers?: Record<string, string | undefined>;
|
|
319
|
+
};
|
|
320
|
+
type WithMoreOptions<TMoreOptions = DefaultMoreOptions> = {
|
|
321
|
+
options: CombinedCallApiExtraOptions & Partial<TMoreOptions>;
|
|
322
|
+
};
|
|
323
|
+
interface Interceptors<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = DefaultMoreOptions> {
|
|
324
|
+
/**
|
|
325
|
+
* 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.
|
|
326
|
+
* It is basically a combination of `onRequestError` and `onResponseError` interceptors
|
|
327
|
+
*/
|
|
328
|
+
onError?: (context: ErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
|
|
329
|
+
/**
|
|
330
|
+
* Interceptor that will be called just before the request is made, allowing for modifications or additional operations.
|
|
331
|
+
*/
|
|
332
|
+
onRequest?: (context: RequestContext & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
|
|
333
|
+
/**
|
|
334
|
+
* Interceptor that will be called when an error occurs during the fetch request.
|
|
335
|
+
*/
|
|
336
|
+
onRequestError?: (context: RequestErrorContext & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
|
|
337
|
+
/**
|
|
338
|
+
* Interceptor that will be called when any response is received from the api, whether successful or not
|
|
339
|
+
*/
|
|
340
|
+
onResponse?: (context: ResponseContext<TData, TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
|
|
341
|
+
/**
|
|
342
|
+
* Interceptor that will be called when an error response is received from the api.
|
|
343
|
+
*/
|
|
344
|
+
onResponseError?: (context: ResponseErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
|
|
345
|
+
/**
|
|
346
|
+
* Interceptor that will be called when a request is retried.
|
|
347
|
+
*/
|
|
348
|
+
onRetry?: (response: ErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
|
|
349
|
+
/**
|
|
350
|
+
* Interceptor that will be called when a successful response is received from the api.
|
|
351
|
+
*/
|
|
352
|
+
onSuccess?: (context: SuccessContext<TData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;
|
|
353
|
+
}
|
|
354
|
+
type InterceptorsOrInterceptorArray<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = DefaultMoreOptions> = {
|
|
355
|
+
[Key in keyof Interceptors<TData, TErrorData, TMoreOptions>]: Interceptors<TData, TErrorData, TMoreOptions>[Key] | Array<Interceptors<TData, TErrorData, TMoreOptions>[Key]>;
|
|
356
|
+
};
|
|
357
|
+
type FetchImpl = UnmaskType<(input: string | Request | URL, init?: RequestInit) => Promise<Response>>;
|
|
358
|
+
type ExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TSchemas extends CallApiSchemas = DefaultMoreOptions, TPluginArray extends CallApiPlugin[] = DefaultPluginArray> = {
|
|
359
|
+
/**
|
|
360
|
+
* Authorization header value.
|
|
361
|
+
*/
|
|
362
|
+
auth?: string | Auth | null;
|
|
363
|
+
/**
|
|
364
|
+
* Base URL to be prepended to all request URLs
|
|
365
|
+
*/
|
|
366
|
+
baseURL?: string;
|
|
367
|
+
/**
|
|
368
|
+
* Custom function to serialize the body object into a string.
|
|
369
|
+
*/
|
|
370
|
+
bodySerializer?: (bodyData: Record<string, unknown>) => string;
|
|
371
|
+
/**
|
|
372
|
+
* Whether or not to clone the response, so response.json() and the like, can be read again else where.
|
|
373
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/clone
|
|
374
|
+
* @default false
|
|
375
|
+
*/
|
|
376
|
+
cloneResponse?: boolean;
|
|
377
|
+
/**
|
|
378
|
+
* Custom fetch implementation
|
|
379
|
+
*/
|
|
380
|
+
customFetchImpl?: FetchImpl;
|
|
381
|
+
/**
|
|
382
|
+
* Custom request key to be used to identify a request in the fetch deduplication strategy.
|
|
383
|
+
* @default the full request url + string formed from the request options
|
|
384
|
+
*/
|
|
385
|
+
dedupeKey?: string;
|
|
386
|
+
/**
|
|
387
|
+
* Defines the deduplication strategy for the request, can be set to "none" | "defer" | "cancel".
|
|
388
|
+
* - If set to "cancel", the previous pending request with the same request key will be cancelled and lets the new request through.
|
|
389
|
+
* - If set to "defer", all new request with the same request key will be share the same response, until the previous one is completed.
|
|
390
|
+
* - If set to "none", deduplication is disabled.
|
|
391
|
+
* @default "cancel"
|
|
392
|
+
*/
|
|
393
|
+
dedupeStrategy?: "cancel" | "defer" | "none";
|
|
394
|
+
/**
|
|
395
|
+
* Default error message to use if none is provided from a response.
|
|
396
|
+
* @default "Failed to fetch data from server!"
|
|
397
|
+
*/
|
|
398
|
+
defaultErrorMessage?: string;
|
|
399
|
+
/**
|
|
400
|
+
* Resolved request URL
|
|
401
|
+
*/
|
|
402
|
+
readonly fullURL?: string;
|
|
403
|
+
/**
|
|
404
|
+
* Defines the mode in which the merged hooks are executed, can be set to "parallel" | "sequential".
|
|
405
|
+
* - If set to "parallel", main and plugin hooks will be executed in parallel.
|
|
406
|
+
* - If set to "sequential", the plugin hooks will be executed first, followed by the main hook.
|
|
407
|
+
* @default "parallel"
|
|
408
|
+
*/
|
|
409
|
+
mergedHooksExecutionMode?: "parallel" | "sequential";
|
|
410
|
+
/**
|
|
411
|
+
* - Controls what order in which the merged hooks execute
|
|
412
|
+
* @default "mainHooksLast"
|
|
413
|
+
*/
|
|
414
|
+
mergedHooksExecutionOrder?: "mainHooksAfterPlugins" | "mainHooksBeforePlugins";
|
|
415
|
+
/**
|
|
416
|
+
* An array of CallApi plugins. It allows you to extend the behavior of the library.
|
|
417
|
+
*/
|
|
418
|
+
plugins?: Plugins<TPluginArray>;
|
|
419
|
+
/**
|
|
420
|
+
* Custom function to parse the response string into a object.
|
|
421
|
+
*/
|
|
422
|
+
responseParser?: (responseString: string) => Awaitable<Record<string, unknown>>;
|
|
423
|
+
/**
|
|
424
|
+
* Expected response type, affects how response is parsed
|
|
425
|
+
* @default "json"
|
|
426
|
+
*/
|
|
427
|
+
responseType?: TResponseType;
|
|
428
|
+
/**
|
|
429
|
+
* Mode of the result, can influence how results are handled or returned.
|
|
430
|
+
* Can be set to "all" | "onlySuccess" | "onlyError" | "onlyResponse".
|
|
431
|
+
* @default "all"
|
|
432
|
+
*/
|
|
433
|
+
resultMode?: TResultMode;
|
|
434
|
+
/**
|
|
435
|
+
* Type-safe schemas for the response validation.
|
|
436
|
+
*/
|
|
437
|
+
schemas?: TSchemas;
|
|
438
|
+
/**
|
|
439
|
+
* If true or the function returns true, throws errors instead of returning them
|
|
440
|
+
* The function is passed the error object and can be used to conditionally throw the error
|
|
441
|
+
* @default false
|
|
442
|
+
*/
|
|
443
|
+
throwOnError?: TThrowOnError | ((context: ErrorContext<TErrorData>) => TThrowOnError);
|
|
444
|
+
/**
|
|
445
|
+
* Request timeout in milliseconds
|
|
446
|
+
*/
|
|
447
|
+
timeout?: number;
|
|
448
|
+
/**
|
|
449
|
+
* Custom validation functions for response validation
|
|
450
|
+
*/
|
|
451
|
+
validators?: CallApiValidators<TData, TErrorData>;
|
|
452
|
+
} & InterceptorsOrInterceptorArray<TData, TErrorData> & Partial<InferPluginOptions<TPluginArray>> & MetaOption<TSchemas> & RetryOptions<TErrorData> & ResultModeOption<TErrorData, TResultMode> & UrlOptions<TSchemas>;
|
|
453
|
+
declare const optionsEnumToExtendFromBase: ("plugins" | "schemas" | "validators")[];
|
|
454
|
+
type CallApiExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TSchemas extends CallApiSchemas = DefaultMoreOptions, TPluginArray extends CallApiPlugin[] = DefaultPluginArray> = CallApiRequestOptions<TSchemas> & ExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray> & {
|
|
455
|
+
/**
|
|
456
|
+
* Options that should extend the base options.
|
|
457
|
+
*/
|
|
458
|
+
extend?: Pick<ExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray>, (typeof optionsEnumToExtendFromBase)[number]>;
|
|
459
|
+
};
|
|
460
|
+
declare const optionsEnumToOmitFromBase: ("dedupeKey" | "extend")[];
|
|
461
|
+
type BaseCallApiExtraOptions<TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemas extends CallApiSchemas = DefaultMoreOptions, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray> = Omit<Partial<CallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemas, TBasePluginArray>>, (typeof optionsEnumToOmitFromBase)[number]>;
|
|
462
|
+
type CombinedCallApiExtraOptions = BaseCallApiExtraOptions & CallApiExtraOptions;
|
|
463
|
+
type CallApiParameters<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TSchemas extends CallApiSchemas = DefaultMoreOptions, TPluginArray extends CallApiPlugin[] = DefaultPluginArray> = [
|
|
464
|
+
initURL: InferSchemaResult<TSchemas["initURL"], InitURL>,
|
|
465
|
+
config?: CallApiExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray>
|
|
466
|
+
];
|
|
467
|
+
type RequestContext = UnmaskType<{
|
|
468
|
+
options: CombinedCallApiExtraOptions;
|
|
469
|
+
request: CallApiRequestOptionsForHooks;
|
|
470
|
+
}>;
|
|
471
|
+
type ResponseContext<TData, TErrorData> = UnmaskType<{
|
|
472
|
+
data: TData;
|
|
473
|
+
error: null;
|
|
474
|
+
options: CombinedCallApiExtraOptions;
|
|
475
|
+
request: CallApiRequestOptionsForHooks;
|
|
476
|
+
response: Response;
|
|
477
|
+
} | {
|
|
478
|
+
data: null;
|
|
479
|
+
error: PossibleHTTPError<TErrorData>;
|
|
480
|
+
options: CombinedCallApiExtraOptions;
|
|
481
|
+
request: CallApiRequestOptionsForHooks;
|
|
482
|
+
response: Response;
|
|
483
|
+
}>;
|
|
484
|
+
type SuccessContext<TData> = UnmaskType<{
|
|
485
|
+
data: TData;
|
|
486
|
+
options: CombinedCallApiExtraOptions;
|
|
487
|
+
request: CallApiRequestOptionsForHooks;
|
|
488
|
+
response: Response;
|
|
489
|
+
}>;
|
|
490
|
+
type PossibleJavascriptErrorNames = "AbortError" | "Error" | "SyntaxError" | "TimeoutError" | "TypeError" | (`${string}Error` & DefaultMoreOptions);
|
|
491
|
+
type PossibleJavaScriptError = UnmaskType<{
|
|
492
|
+
errorData: DOMException | Error | SyntaxError | TypeError;
|
|
493
|
+
message: string;
|
|
494
|
+
name: PossibleJavascriptErrorNames;
|
|
495
|
+
}>;
|
|
496
|
+
type PossibleHTTPError<TErrorData> = UnmaskType<{
|
|
497
|
+
errorData: TErrorData;
|
|
498
|
+
message: string;
|
|
499
|
+
name: "HTTPError";
|
|
500
|
+
}>;
|
|
501
|
+
type RequestErrorContext = UnmaskType<{
|
|
502
|
+
error: PossibleJavaScriptError;
|
|
503
|
+
options: CombinedCallApiExtraOptions;
|
|
504
|
+
request: CallApiRequestOptionsForHooks;
|
|
505
|
+
response: null;
|
|
506
|
+
}>;
|
|
507
|
+
type ResponseErrorContext<TErrorData> = UnmaskType<{
|
|
508
|
+
error: PossibleHTTPError<TErrorData>;
|
|
509
|
+
options: CombinedCallApiExtraOptions;
|
|
510
|
+
request: CallApiRequestOptionsForHooks;
|
|
511
|
+
response: Response;
|
|
512
|
+
}>;
|
|
513
|
+
type ErrorContext<TErrorData> = UnmaskType<{
|
|
514
|
+
error: PossibleHTTPError<TErrorData>;
|
|
515
|
+
options: CombinedCallApiExtraOptions;
|
|
516
|
+
request: CallApiRequestOptionsForHooks;
|
|
517
|
+
response: Response;
|
|
518
|
+
} | {
|
|
519
|
+
error: PossibleJavaScriptError;
|
|
520
|
+
options: CombinedCallApiExtraOptions;
|
|
521
|
+
request: CallApiRequestOptionsForHooks;
|
|
522
|
+
response: null;
|
|
523
|
+
}>;
|
|
524
|
+
type CallApiResultSuccessVariant<TData> = {
|
|
525
|
+
data: TData;
|
|
526
|
+
error: null;
|
|
527
|
+
response: Response;
|
|
528
|
+
};
|
|
529
|
+
type CallApiResultErrorVariant<TErrorData> = {
|
|
530
|
+
data: null;
|
|
531
|
+
error: PossibleHTTPError<TErrorData>;
|
|
532
|
+
response: Response;
|
|
533
|
+
} | {
|
|
534
|
+
data: null;
|
|
535
|
+
error: PossibleJavaScriptError;
|
|
536
|
+
response: null;
|
|
537
|
+
};
|
|
538
|
+
type ResultModeMap<TData = DefaultDataType, TErrorData = DefaultDataType, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TComputedData = GetResponseType<TData, TResponseType>, TComputedErrorData = GetResponseType<TErrorData, TResponseType>> = UnmaskType<{
|
|
539
|
+
all: CallApiResultSuccessVariant<TComputedData> | CallApiResultErrorVariant<TComputedErrorData>;
|
|
540
|
+
allWithException: CallApiResultSuccessVariant<TComputedData>;
|
|
541
|
+
allWithoutResponse: CallApiResultSuccessVariant<TComputedData>["data" | "error"] | CallApiResultErrorVariant<TComputedErrorData>["data" | "error"];
|
|
542
|
+
onlyError: CallApiResultSuccessVariant<TComputedData>["error"] | CallApiResultErrorVariant<TComputedErrorData>["error"];
|
|
543
|
+
onlyResponse: CallApiResultErrorVariant<TComputedErrorData>["response"] | CallApiResultSuccessVariant<TComputedData>["response"];
|
|
544
|
+
onlyResponseWithException: CallApiResultSuccessVariant<TComputedErrorData>["response"];
|
|
545
|
+
onlySuccess: CallApiResultErrorVariant<TComputedErrorData>["data"] | CallApiResultSuccessVariant<TComputedData>["data"];
|
|
546
|
+
onlySuccessWithException: CallApiResultSuccessVariant<TComputedData>["data"];
|
|
547
|
+
}>;
|
|
548
|
+
type ResultModeUnion = keyof ResultModeMap | undefined;
|
|
549
|
+
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> ? ResultModeMap<TData, TErrorData, TResponseType>[TResultMode] : never;
|
|
550
|
+
|
|
551
|
+
export { type BaseCallApiExtraOptions as B, type CallApiSchemas as C, type DefaultPluginArray as D, type ErrorContext as E, type GetCallApiResult as G, type InferSchemaResult as I, type PluginInitContext as P, type ResultModeUnion as R, type SuccessContext as S, type ResponseTypeUnion as a, type CallApiPlugin as b, type CallApiExtraOptions as c, type DefaultDataType as d, type DefaultThrowOnError as e, type DefaultMoreOptions as f, definePlugin as g, type PossibleJavaScriptError as h, type PossibleHTTPError as i, type CallApiParameters as j, type CallApiRequestOptions as k, type CallApiRequestOptionsForHooks as l, type CallApiResultErrorVariant as m, type CallApiResultSuccessVariant as n, type CombinedCallApiExtraOptions as o, type Interceptors as p, type InterceptorsOrInterceptorArray as q, type PossibleJavascriptErrorNames as r, type Register as s, type RequestContext as t, type RequestErrorContext as u, type ResponseContext as v, type ResponseErrorContext as w, type InitURL as x, defaultRetryMethods as y, defaultRetryStatusCodes as z };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type ErrorDetails<TErrorResponse> = {
|
|
2
|
+
defaultErrorMessage: string;
|
|
3
|
+
errorData: TErrorResponse;
|
|
4
|
+
response: Response;
|
|
5
|
+
};
|
|
6
|
+
type ErrorOptions = {
|
|
7
|
+
cause?: unknown;
|
|
8
|
+
};
|
|
9
|
+
declare class HTTPError<TErrorResponse = Record<string, unknown>> extends Error {
|
|
10
|
+
errorData: ErrorDetails<TErrorResponse>["errorData"];
|
|
11
|
+
isHTTPError: boolean;
|
|
12
|
+
name: "HTTPError";
|
|
13
|
+
response: ErrorDetails<TErrorResponse>["response"];
|
|
14
|
+
constructor(errorDetails: ErrorDetails<TErrorResponse>, errorOptions?: ErrorOptions);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { HTTPError as H };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { R as ResultModeUnion, a as ResponseTypeUnion, C as CallApiSchemas, b as CallApiPlugin, D as DefaultPluginArray, I as InferSchemaResult, c as CallApiExtraOptions, G as GetCallApiResult, B as BaseCallApiExtraOptions, d as DefaultDataType, e as DefaultThrowOnError, f as DefaultMoreOptions } from './common-B_9jiZXC.js';
|
|
2
|
+
export { j as CallApiParameters, k as CallApiRequestOptions, l as CallApiRequestOptionsForHooks, m as CallApiResultErrorVariant, n as CallApiResultSuccessVariant, o as CombinedCallApiExtraOptions, E as ErrorContext, p as Interceptors, q as InterceptorsOrInterceptorArray, P as PluginInitContext, i as PossibleHTTPError, h as PossibleJavaScriptError, r as PossibleJavascriptErrorNames, s as Register, t as RequestContext, u as RequestErrorContext, v as ResponseContext, w as ResponseErrorContext, S as SuccessContext, g as definePlugin } from './common-B_9jiZXC.js';
|
|
3
|
+
export { H as HTTPError } from './error-lBRMiMeF.js';
|
|
4
|
+
import '@standard-schema/spec';
|
|
5
|
+
|
|
6
|
+
declare const createFetchClient: <TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemas extends CallApiSchemas = DefaultMoreOptions, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseComputedData = InferSchemaResult<TBaseSchemas["data"], TBaseData>, TBaseComputedErrorData = InferSchemaResult<TBaseSchemas["errorData"], TBaseErrorData>>(baseConfig?: BaseCallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemas, TBasePluginArray>) => {
|
|
7
|
+
<TData = TBaseComputedData, TErrorData = TBaseComputedErrorData, TResultMode extends ResultModeUnion = TBaseResultMode, TThrowOnError extends boolean = TBaseThrowOnError, TResponseType extends ResponseTypeUnion = TBaseResponseType, TSchemas extends CallApiSchemas = TBaseSchemas, TPluginArray extends CallApiPlugin[] = TBasePluginArray, TComputedData = InferSchemaResult<TSchemas["data"], TData>, TComputedErrorData = InferSchemaResult<TSchemas["errorData"], TErrorData>>(initURL: InferSchemaResult<TSchemas["initURL"], string | URL>, config?: CallApiExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray> | undefined): Promise<GetCallApiResult<TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType>>;
|
|
8
|
+
create: <TBaseData = unknown, TBaseErrorData = unknown, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = boolean, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemas extends CallApiSchemas = {}, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseComputedData = InferSchemaResult<TBaseSchemas["data"], TBaseData>, TBaseComputedErrorData = InferSchemaResult<TBaseSchemas["errorData"], TBaseErrorData>>(baseConfig?: BaseCallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemas, TBasePluginArray>) => /*elided*/ any;
|
|
9
|
+
};
|
|
10
|
+
declare const callApi: {
|
|
11
|
+
<TData = unknown, TErrorData = unknown, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = boolean, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TSchemas extends CallApiSchemas = {}, TPluginArray extends CallApiPlugin[] = DefaultPluginArray, TComputedData = InferSchemaResult<TSchemas["data"], TData>, TComputedErrorData = InferSchemaResult<TSchemas["errorData"], TErrorData>>(initURL: InferSchemaResult<TSchemas["initURL"], string | URL>, config?: CallApiExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray> | undefined): Promise<GetCallApiResult<TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType>>;
|
|
12
|
+
create: <TBaseData = unknown, TBaseErrorData = unknown, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = boolean, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemas extends CallApiSchemas = {}, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseComputedData = InferSchemaResult<TBaseSchemas["data"], TBaseData>, TBaseComputedErrorData = InferSchemaResult<TBaseSchemas["errorData"], TBaseErrorData>>(baseConfig?: BaseCallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemas, TBasePluginArray>) => {
|
|
13
|
+
<TData = TBaseComputedData, TErrorData = TBaseComputedErrorData, TResultMode extends ResultModeUnion = TBaseResultMode, TThrowOnError extends boolean = TBaseThrowOnError, TResponseType extends ResponseTypeUnion = TBaseResponseType, TSchemas extends CallApiSchemas = TBaseSchemas, TPluginArray extends CallApiPlugin[] = TBasePluginArray, TComputedData = InferSchemaResult<TSchemas["data"], TData>, TComputedErrorData = InferSchemaResult<TSchemas["errorData"], TErrorData>>(initURL: InferSchemaResult<TSchemas["initURL"], string | URL>, config?: CallApiExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray> | undefined): Promise<GetCallApiResult<TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType>>;
|
|
14
|
+
create: /*elided*/ any;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { BaseCallApiExtraOptions, CallApiExtraOptions, CallApiPlugin, CallApiSchemas, InferSchemaResult, ResultModeUnion, callApi, createFetchClient };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{callApi,createFetchClient,definePlugin}from"./chunk-DQY5GNZB.js";export{HTTPError}from"./chunk-MOMG57MZ.js";//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { d as DefaultDataType, R as ResultModeUnion, a as ResponseTypeUnion, C as CallApiSchemas, b as CallApiPlugin, D as DefaultPluginArray, c as CallApiExtraOptions, I as InferSchemaResult, x as InitURL, G as GetCallApiResult, B as BaseCallApiExtraOptions, e as DefaultThrowOnError, f as DefaultMoreOptions } from '../common-B_9jiZXC.js';
|
|
2
|
+
import '@standard-schema/spec';
|
|
3
|
+
|
|
4
|
+
type CallApiExtraOptionsWithRequiredURL<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = boolean, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TSchemas extends CallApiSchemas = CallApiSchemas, TPluginArray extends CallApiPlugin[] = DefaultPluginArray> = Omit<CallApiExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray>, "initURL"> & {
|
|
5
|
+
initURL: InferSchemaResult<TSchemas["initURL"], InitURL>;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
declare const createFetchClientWithOptions: <TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemas extends CallApiSchemas = DefaultMoreOptions, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseComputedData = InferSchemaResult<TBaseSchemas["data"], TBaseData>, TBaseComputedErrorData = InferSchemaResult<TBaseSchemas["errorData"], TBaseErrorData>>(baseConfig?: BaseCallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemas, TBasePluginArray>) => {
|
|
9
|
+
<TData = TBaseComputedData, TErrorData = TBaseComputedErrorData, TResultMode extends ResultModeUnion = TBaseResultMode, TThrowOnError extends boolean = TBaseThrowOnError, TResponseType extends ResponseTypeUnion = TBaseResponseType, TSchemas extends CallApiSchemas = TBaseSchemas, TPluginArray extends CallApiPlugin[] = TBasePluginArray, TComputedData = InferSchemaResult<TSchemas["data"], TData>, TComputedErrorData = InferSchemaResult<TSchemas["errorData"], TErrorData>>(config: CallApiExtraOptionsWithRequiredURL<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray>): Promise<GetCallApiResult<TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType>>;
|
|
10
|
+
create: <TBaseData_1 = unknown, TBaseErrorData_1 = unknown, TBaseResultMode_1 extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError_1 extends boolean = boolean, TBaseResponseType_1 extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemas_1 extends CallApiSchemas = {}, TBasePluginArray_1 extends CallApiPlugin[] = DefaultPluginArray, TBaseComputedData_1 = InferSchemaResult<TBaseSchemas_1["data"], TBaseData_1>, TBaseComputedErrorData_1 = InferSchemaResult<TBaseSchemas_1["errorData"], TBaseErrorData_1>>(baseConfig?: BaseCallApiExtraOptions<TBaseData_1, TBaseErrorData_1, TBaseResultMode_1, TBaseThrowOnError_1, TBaseResponseType_1, TBaseSchemas_1, TBasePluginArray_1>) => {
|
|
11
|
+
<TData = TBaseComputedData_1, TErrorData_1 = TBaseComputedErrorData_1, TResultMode_1 extends ResultModeUnion = TBaseResultMode_1, TThrowOnError_1 extends boolean = TBaseThrowOnError_1, TResponseType_1 extends ResponseTypeUnion = TBaseResponseType_1, TSchemas_1 extends CallApiSchemas = TBaseSchemas_1, TPluginArray_1 extends CallApiPlugin[] = TBasePluginArray_1, TComputedData_1 = InferSchemaResult<TSchemas_1["data"], TData>, TComputedErrorData_1 = InferSchemaResult<TSchemas_1["errorData"], TErrorData_1>>(initURL: InferSchemaResult<TSchemas_1["initURL"], string | URL>, config?: CallApiExtraOptions<TData, TErrorData_1, TResultMode_1, TThrowOnError_1, TResponseType_1, TSchemas_1, TPluginArray_1> | undefined): Promise<GetCallApiResult<TComputedData_1, TComputedErrorData_1, TResultMode_1, TThrowOnError_1, TResponseType_1>>;
|
|
12
|
+
create: /*elided*/ any;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
declare const callApiWithOptions: {
|
|
16
|
+
<TData = unknown, TErrorData = unknown, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = boolean, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TSchemas extends CallApiSchemas = {}, TPluginArray extends CallApiPlugin[] = DefaultPluginArray, TComputedData = InferSchemaResult<TSchemas["data"], TData>, TComputedErrorData = InferSchemaResult<TSchemas["errorData"], TErrorData>>(config: CallApiExtraOptionsWithRequiredURL<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray>): Promise<GetCallApiResult<TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType>>;
|
|
17
|
+
create: <TBaseData = unknown, TBaseErrorData = unknown, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = boolean, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBaseSchemas extends CallApiSchemas = {}, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseComputedData = InferSchemaResult<TBaseSchemas["data"], TBaseData>, TBaseComputedErrorData = InferSchemaResult<TBaseSchemas["errorData"], TBaseErrorData>>(baseConfig?: BaseCallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBaseSchemas, TBasePluginArray>) => {
|
|
18
|
+
<TData = TBaseComputedData, TErrorData = TBaseComputedErrorData, TResultMode extends ResultModeUnion = TBaseResultMode, TThrowOnError extends boolean = TBaseThrowOnError, TResponseType extends ResponseTypeUnion = TBaseResponseType, TSchemas extends CallApiSchemas = TBaseSchemas, TPluginArray extends CallApiPlugin[] = TBasePluginArray, TComputedData = InferSchemaResult<TSchemas["data"], TData>, TComputedErrorData = InferSchemaResult<TSchemas["errorData"], TErrorData>>(initURL: InferSchemaResult<TSchemas["initURL"], string | URL>, config?: CallApiExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TSchemas, TPluginArray> | undefined): Promise<GetCallApiResult<TComputedData, TComputedErrorData, TResultMode, TThrowOnError, TResponseType>>;
|
|
19
|
+
create: /*elided*/ any;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
declare const defineOptions: <TData = unknown, TErrorData = unknown, TResultMode extends ResultModeUnion = ResultModeUnion>(config: CallApiExtraOptionsWithRequiredURL<TData, TErrorData, TResultMode>) => CallApiExtraOptionsWithRequiredURL<TData, TErrorData, TResultMode>;
|
|
24
|
+
|
|
25
|
+
export { type CallApiExtraOptionsWithRequiredURL, callApiWithOptions, createFetchClientWithOptions, defineOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{hooksEnum as e,initializePlugins as r,mergeUrlWithParamsAndQuery as t,createTimeoutSignal as o,createCombinedSignal as s,createDedupeStrategy as a,createExtensibleSchemasAndValidators as n,resolveResponseData as i,handleValidation as l,resolveSuccessResult as u,createRetryStrategy as d,createFetchClient as c}from"../chunk-DQY5GNZB.js";import{splitBaseConfig as p,splitConfig as y,combineHooks as h,mergeAndResolveHeaders as f,isPlainObject as m,defaultRetryStatusCodes as w,defaultRetryMethods as g,executeHooks as R,HTTPError as E,resolveErrorResult as M,waitUntil as b,isFunction as q,isHTTPErrorInstance as D}from"../chunk-MOMG57MZ.js";var O=O=>{const[S,k]=p(O??{}),C=new Map,v=async c=>{const{initURL:p}=c,[O,x]=y(c),H={};for(const r of Object.keys(e)){const e=h(k[r],x[r]);H[r]=e}const L={baseURL:"",bodySerializer:JSON.stringify,dedupeStrategy:"cancel",defaultErrorMessage:"Failed to fetch data from server!",mergedHooksExecutionMode:"parallel",mergedHooksExecutionOrder:"mainHooksAfterPlugins",responseType:"json",resultMode:"all",retryAttempts:0,retryDelay:1e3,retryMaxDelay:1e4,retryMethods:g,retryStatusCodes:w,retryStrategy:"linear",...k,...x,...H},U=O.body??S.body,$={...S,...O,body:m(U)?L.bodySerializer(U):U,headers:f({auth:L.auth,baseHeaders:S.headers,body:U,headers:O.headers}),signal:O.signal??S.signal},{resolvedHooks:A,resolvedOptions:j,resolvedRequestOptions:T,url:F}=await r({initURL:p,options:L,request:$}),P=`${j.baseURL}${t(F,j.params,j.query)}`,z={...j,...A,fullURL:P,initURL:p.toString()},G=new AbortController,N=null!=z.timeout?o(z.timeout):null,Z=s(T.signal,N,G.signal),B={...T,signal:Z},{handleRequestCancelStrategy:I,handleRequestDeferStrategy:J,removeDedupeKeyFromCache:K}=await a({$RequestInfoCache:C,newFetchController:G,options:z,request:B});await I();try{await R(z.onRequest({options:z,request:B})),B.headers=f({auth:z.auth,baseHeaders:S.headers,body:U,headers:B.headers});const e=await J(),r="defer"===z.dedupeStrategy||z.cloneResponse,{schemas:t,validators:o}=n(z);if(!e.ok){const s=await i(r?e.clone():e,z.responseType,z.responseParser),a=await l(s,t?.errorData,o?.errorData);throw new E({defaultErrorMessage:z.defaultErrorMessage,errorData:a,response:e})}const s=await i(r?e.clone():e,z.responseType,z.responseParser),a={data:await l(s,t?.data,o?.data),options:z,request:B,response:z.cloneResponse?e.clone():e};return await R(z.onSuccess(a),z.onResponse({...a,error:null})),await u({data:a.data,response:a.response,resultMode:z.resultMode})}catch(e){const{apiDetails:r,getErrorResult:t}=M({cloneResponse:z.cloneResponse,defaultErrorMessage:z.defaultErrorMessage,error:e,resultMode:z.resultMode}),o={error:r.error,options:z,request:B,response:r.response},{getDelay:s,shouldAttemptRetry:a}=d(z,o);if(!Z.aborted&&await a()){await R(z.onRetry(o));const e=s();await b(e);const r={...c,"~retryCount":(z["~retryCount"]??0)+1};return await v(r)}const n=q(z.throwOnError)?z.throwOnError(o):z.throwOnError,i=()=>{if(n)throw r.error};if(D(e))return await R(z.onResponseError(o),z.onError(o),z.onResponse({...o,data:null})),i(),t();if(e instanceof DOMException&&"AbortError"===e.name){const{message:r,name:o}=e;return console.error(`${o}:`,r),i(),t()}if(e instanceof DOMException&&"TimeoutError"===e.name){const r=`Request timed out after ${z.timeout}ms`;return console.error(`${e.name}:`,r),i(),t({message:r})}return await R(z.onRequestError(o),z.onError(o)),i(),t()}finally{K()}};return v.create=c,v},S=O(),k=e=>e;export{S as callApiWithOptions,O as createFetchClientWithOptions,k as defineOptions};//# sourceMappingURL=index.js.map
|