@dcc-bs/communication.bs.js 0.0.5 → 0.1.0
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 +312 -11
- package/dist/communication.bs.d.ts +527 -0
- package/dist/communication.bs.js +2534 -2277
- package/dist/communication.bs.umd.cjs +12 -12
- package/package.json +4 -2
|
@@ -2,6 +2,25 @@ import { default as default_2 } from 'zod';
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { ZodType } from 'zod';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Hook that runs after a response is received.
|
|
7
|
+
* Can be used to transform the response, log, or handle specific status codes.
|
|
8
|
+
*
|
|
9
|
+
* @param response - The Response object received from fetch
|
|
10
|
+
* @returns The original or a modified Response object
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const afterResponse: AfterResponseHook = async (response) => {
|
|
14
|
+
* console.log(`Response status: ${response.status}`);
|
|
15
|
+
* if (response.status === 401) {
|
|
16
|
+
* // Handle unauthorized
|
|
17
|
+
* await refreshToken();
|
|
18
|
+
* }
|
|
19
|
+
* return response;
|
|
20
|
+
* };
|
|
21
|
+
*/
|
|
22
|
+
export declare type AfterResponseHook = (response: Response) => Promise<Response> | Response;
|
|
23
|
+
|
|
5
24
|
export declare class ApiError extends Error {
|
|
6
25
|
$type: "ApiError";
|
|
7
26
|
errorId: ErrorId;
|
|
@@ -37,8 +56,516 @@ declare type ApiResponse<T> = T | ApiError;
|
|
|
37
56
|
|
|
38
57
|
export declare function apiStreamFetch(url: string, options?: ApiFetchOptions): Promise<ApiResponse<ReadableStream<Uint8Array>>>;
|
|
39
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Authentication configuration for the fetcher.
|
|
61
|
+
* Supports Bearer token and Basic authentication methods.
|
|
62
|
+
*
|
|
63
|
+
* @example Bearer Authentication
|
|
64
|
+
* const authConfig: AuthConfig = {
|
|
65
|
+
* type: "bearer",
|
|
66
|
+
* token: "your-api-token-here"
|
|
67
|
+
* };
|
|
68
|
+
*
|
|
69
|
+
* @example Basic Authentication
|
|
70
|
+
* const authConfig: AuthConfig = {
|
|
71
|
+
* type: "basic",
|
|
72
|
+
* username: "user",
|
|
73
|
+
* password: "pass"
|
|
74
|
+
* };
|
|
75
|
+
*/
|
|
76
|
+
export declare type AuthConfig = BearerAuthConfig | BasicAuthConfig;
|
|
77
|
+
|
|
78
|
+
declare type BasicAuthConfig = {
|
|
79
|
+
type: "basic";
|
|
80
|
+
username: string;
|
|
81
|
+
password: string;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
declare type BearerAuthConfig = {
|
|
85
|
+
type: "bearer";
|
|
86
|
+
token: string;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Hook that runs before each request is sent.
|
|
91
|
+
* Can be used to modify the request, log, or perform authentication checks.
|
|
92
|
+
*
|
|
93
|
+
* @param url - The full URL that will be fetched
|
|
94
|
+
* @param options - The RequestInit options that will be passed to fetch
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* const beforeRequest: BeforeRequestHook = async (url, options) => {
|
|
98
|
+
* console.log(`Requesting: ${url}`);
|
|
99
|
+
* // Add dynamic token
|
|
100
|
+
* options.headers = { ...options.headers, 'X-Token': await getToken() };
|
|
101
|
+
* };
|
|
102
|
+
*/
|
|
103
|
+
export declare type BeforeRequestHook = (url: string, options: RequestInit) => Promise<void> | void;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Creates a builder for configuring and creating a customized fetcher.
|
|
107
|
+
* The builder allows you to chain configuration methods and then build a fetcher
|
|
108
|
+
* with all the specified options applied.
|
|
109
|
+
*
|
|
110
|
+
* @param options - Initial configuration options for the fetcher
|
|
111
|
+
* @returns A builder object with methods to configure the fetcher
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* const fetcher = createFetcherBuilder()
|
|
115
|
+
* .setBaseURL("https://api.example.com")
|
|
116
|
+
* .addHeader("Authorization", "Bearer token")
|
|
117
|
+
* .setRequestTimeout(5000)
|
|
118
|
+
* .enableDebug()
|
|
119
|
+
* .build();
|
|
120
|
+
*
|
|
121
|
+
* // Use the fetcher
|
|
122
|
+
* const response = await fetcher("/users", { method: "GET" });
|
|
123
|
+
*/
|
|
124
|
+
export declare function createFetcherBuilder(options?: FetcherBuilderOptions): {
|
|
125
|
+
addHeader: (key: string, value: string) => {
|
|
126
|
+
addHeader: /*elided*/ any;
|
|
127
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
128
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
129
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
130
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
131
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
132
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
133
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
134
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
135
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
136
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
137
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
138
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
139
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
140
|
+
enableDeduplication: () => /*elided*/ any;
|
|
141
|
+
build: () => Fetcher;
|
|
142
|
+
};
|
|
143
|
+
addHeaders: (headers: Record<string, string>) => {
|
|
144
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
145
|
+
addHeaders: /*elided*/ any;
|
|
146
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
147
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
148
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
149
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
150
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
151
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
152
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
153
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
154
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
155
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
156
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
157
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
158
|
+
enableDeduplication: () => /*elided*/ any;
|
|
159
|
+
build: () => Fetcher;
|
|
160
|
+
};
|
|
161
|
+
setBaseURL: (baseURL: string) => {
|
|
162
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
163
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
164
|
+
setBaseURL: /*elided*/ any;
|
|
165
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
166
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
167
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
168
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
169
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
170
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
171
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
172
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
173
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
174
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
175
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
176
|
+
enableDeduplication: () => /*elided*/ any;
|
|
177
|
+
build: () => Fetcher;
|
|
178
|
+
};
|
|
179
|
+
setRequestTimeout: (timeout: number) => {
|
|
180
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
181
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
182
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
183
|
+
setRequestTimeout: /*elided*/ any;
|
|
184
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
185
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
186
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
187
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
188
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
189
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
190
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
191
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
192
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
193
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
194
|
+
enableDeduplication: () => /*elided*/ any;
|
|
195
|
+
build: () => Fetcher;
|
|
196
|
+
};
|
|
197
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => {
|
|
198
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
199
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
200
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
201
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
202
|
+
setRetries: /*elided*/ any;
|
|
203
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
204
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
205
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
206
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
207
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
208
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
209
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
210
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
211
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
212
|
+
enableDeduplication: () => /*elided*/ any;
|
|
213
|
+
build: () => Fetcher;
|
|
214
|
+
};
|
|
215
|
+
setAuth: (config: AuthConfig) => {
|
|
216
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
217
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
218
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
219
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
220
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
221
|
+
setAuth: /*elided*/ any;
|
|
222
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
223
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
224
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
225
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
226
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
227
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
228
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
229
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
230
|
+
enableDeduplication: () => /*elided*/ any;
|
|
231
|
+
build: () => Fetcher;
|
|
232
|
+
};
|
|
233
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => {
|
|
234
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
235
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
236
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
237
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
238
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
239
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
240
|
+
setBeforeRequest: /*elided*/ any;
|
|
241
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
242
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
243
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
244
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
245
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
246
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
247
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
248
|
+
enableDeduplication: () => /*elided*/ any;
|
|
249
|
+
build: () => Fetcher;
|
|
250
|
+
};
|
|
251
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => {
|
|
252
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
253
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
254
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
255
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
256
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
257
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
258
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
259
|
+
setAfterResponse: /*elided*/ any;
|
|
260
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
261
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
262
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
263
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
264
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
265
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
266
|
+
enableDeduplication: () => /*elided*/ any;
|
|
267
|
+
build: () => Fetcher;
|
|
268
|
+
};
|
|
269
|
+
setOnError: (onError: OnErrorHook) => {
|
|
270
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
271
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
272
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
273
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
274
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
275
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
276
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
277
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
278
|
+
setOnError: /*elided*/ any;
|
|
279
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
280
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
281
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
282
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
283
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
284
|
+
enableDeduplication: () => /*elided*/ any;
|
|
285
|
+
build: () => Fetcher;
|
|
286
|
+
};
|
|
287
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => {
|
|
288
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
289
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
290
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
291
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
292
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
293
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
294
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
295
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
296
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
297
|
+
setQueryParams: /*elided*/ any;
|
|
298
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
299
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
300
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
301
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
302
|
+
enableDeduplication: () => /*elided*/ any;
|
|
303
|
+
build: () => Fetcher;
|
|
304
|
+
};
|
|
305
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => {
|
|
306
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
307
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
308
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
309
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
310
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
311
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
312
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
313
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
314
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
315
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
316
|
+
setCredentials: /*elided*/ any;
|
|
317
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
318
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
319
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
320
|
+
enableDeduplication: () => /*elided*/ any;
|
|
321
|
+
build: () => Fetcher;
|
|
322
|
+
};
|
|
323
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => {
|
|
324
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
325
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
326
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
327
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
328
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
329
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
330
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
331
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
332
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
333
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
334
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
335
|
+
setMode: /*elided*/ any;
|
|
336
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
337
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
338
|
+
enableDeduplication: () => /*elided*/ any;
|
|
339
|
+
build: () => Fetcher;
|
|
340
|
+
};
|
|
341
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => {
|
|
342
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
343
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
344
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
345
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
346
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
347
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
348
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
349
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
350
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
351
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
352
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
353
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
354
|
+
setCache: /*elided*/ any;
|
|
355
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
356
|
+
enableDeduplication: () => /*elided*/ any;
|
|
357
|
+
build: () => Fetcher;
|
|
358
|
+
};
|
|
359
|
+
enableDebug: (isEnabled?: boolean) => {
|
|
360
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
361
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
362
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
363
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
364
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
365
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
366
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
367
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
368
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
369
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
370
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
371
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
372
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
373
|
+
enableDebug: /*elided*/ any;
|
|
374
|
+
enableDeduplication: () => /*elided*/ any;
|
|
375
|
+
build: () => Fetcher;
|
|
376
|
+
};
|
|
377
|
+
enableDeduplication: () => {
|
|
378
|
+
addHeader: (key: string, value: string) => /*elided*/ any;
|
|
379
|
+
addHeaders: (headers: Record<string, string>) => /*elided*/ any;
|
|
380
|
+
setBaseURL: (baseURL: string) => /*elided*/ any;
|
|
381
|
+
setRequestTimeout: (timeout: number) => /*elided*/ any;
|
|
382
|
+
setRetries: (retries: number, retryDelay?: number, retryOn?: number[]) => /*elided*/ any;
|
|
383
|
+
setAuth: (config: AuthConfig) => /*elided*/ any;
|
|
384
|
+
setBeforeRequest: (beforeRequest: BeforeRequestHook) => /*elided*/ any;
|
|
385
|
+
setAfterResponse: (afterResponse: AfterResponseHook) => /*elided*/ any;
|
|
386
|
+
setOnError: (onError: OnErrorHook) => /*elided*/ any;
|
|
387
|
+
setQueryParams: (queryParams: Record<string, string | number | boolean>) => /*elided*/ any;
|
|
388
|
+
setCredentials: (credentials: "include" | "omit" | "same-origin") => /*elided*/ any;
|
|
389
|
+
setMode: (mode: "cors" | "no-cors" | "same-origin" | "navigate") => /*elided*/ any;
|
|
390
|
+
setCache: (cache: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached") => /*elided*/ any;
|
|
391
|
+
enableDebug: (isEnabled?: boolean) => /*elided*/ any;
|
|
392
|
+
enableDeduplication: /*elided*/ any;
|
|
393
|
+
build: () => Fetcher;
|
|
394
|
+
};
|
|
395
|
+
build: () => Fetcher;
|
|
396
|
+
};
|
|
397
|
+
|
|
40
398
|
declare type ErrorId = "unexpected_error" | "fetch_failed" | string;
|
|
41
399
|
|
|
400
|
+
export declare type Fetcher = (url: string, options: ApiFetchOptions) => Promise<Response>;
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Configuration options for the fetcher builder.
|
|
404
|
+
* These options are applied to every request made by the fetcher.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* const options: FetcherBuilderOptions = {
|
|
408
|
+
* headers: { "X-API-Key": "secret" },
|
|
409
|
+
* baseURL: "https://api.example.com",
|
|
410
|
+
* timeout: 5000,
|
|
411
|
+
* retries: 3,
|
|
412
|
+
* debug: true
|
|
413
|
+
* };
|
|
414
|
+
*/
|
|
415
|
+
export declare type FetcherBuilderOptions = {
|
|
416
|
+
/**
|
|
417
|
+
* Custom headers to include in every request.
|
|
418
|
+
* These headers can be overridden on a per-request basis.
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* { "X-Custom-Header": "value", "Accept-Language": "en-US" }
|
|
422
|
+
*/
|
|
423
|
+
headers: Record<string, string>;
|
|
424
|
+
/**
|
|
425
|
+
* Base URL to prepend to all request URLs.
|
|
426
|
+
* Useful for API endpoints that share the same domain.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* "https://api.example.com/v1"
|
|
430
|
+
*/
|
|
431
|
+
baseURL?: string;
|
|
432
|
+
/**
|
|
433
|
+
* Request timeout in milliseconds.
|
|
434
|
+
* Requests exceeding this duration will be automatically aborted.
|
|
435
|
+
*
|
|
436
|
+
* @example
|
|
437
|
+
* 5000 // 5 seconds
|
|
438
|
+
*/
|
|
439
|
+
timeout?: number;
|
|
440
|
+
/**
|
|
441
|
+
* Number of retry attempts for failed requests.
|
|
442
|
+
* Only retries on network errors or status codes specified in `retryOn`.
|
|
443
|
+
*
|
|
444
|
+
* @default 0
|
|
445
|
+
* @example
|
|
446
|
+
* 3 // Retry up to 3 times
|
|
447
|
+
*/
|
|
448
|
+
retries?: number;
|
|
449
|
+
/**
|
|
450
|
+
* Delay in milliseconds between retry attempts.
|
|
451
|
+
*
|
|
452
|
+
* @default 1000
|
|
453
|
+
* @example
|
|
454
|
+
* 2000 // Wait 2 seconds between retries
|
|
455
|
+
*/
|
|
456
|
+
retryDelay?: number;
|
|
457
|
+
/**
|
|
458
|
+
* HTTP status codes that should trigger a retry.
|
|
459
|
+
* Only applies when `retries` is set.
|
|
460
|
+
*
|
|
461
|
+
* @example
|
|
462
|
+
* [408, 429, 500, 502, 503, 504] // Retry on timeout and server errors
|
|
463
|
+
*/
|
|
464
|
+
retryOn?: number[];
|
|
465
|
+
/**
|
|
466
|
+
* Hook function that runs before each request.
|
|
467
|
+
* Can be async and can modify the request parameters.
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* async (url, options) => {
|
|
471
|
+
* console.log(`Requesting: ${url}`);
|
|
472
|
+
* // Add dynamic token
|
|
473
|
+
* options.headers = { ...options.headers, 'X-Token': await getToken() };
|
|
474
|
+
* }
|
|
475
|
+
*/
|
|
476
|
+
beforeRequest?: BeforeRequestHook;
|
|
477
|
+
/**
|
|
478
|
+
* Hook function that runs after receiving a response.
|
|
479
|
+
* Can transform the response or perform logging.
|
|
480
|
+
*
|
|
481
|
+
* @example
|
|
482
|
+
* async (response) => {
|
|
483
|
+
* console.log(`Response status: ${response.status}`);
|
|
484
|
+
* return response;
|
|
485
|
+
* }
|
|
486
|
+
*/
|
|
487
|
+
afterResponse?: AfterResponseHook;
|
|
488
|
+
/**
|
|
489
|
+
* Hook function that runs when an error occurs.
|
|
490
|
+
* Useful for error logging and monitoring.
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* (error) => {
|
|
494
|
+
* console.error('Fetch error:', error.message);
|
|
495
|
+
* sendToErrorTracking(error);
|
|
496
|
+
* }
|
|
497
|
+
*/
|
|
498
|
+
onError?: OnErrorHook;
|
|
499
|
+
/**
|
|
500
|
+
* Default query parameters to append to every request URL.
|
|
501
|
+
* Can be overridden on a per-request basis.
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* { api_key: "abc123", format: "json" }
|
|
505
|
+
*/
|
|
506
|
+
queryParams?: Record<string, string | number | boolean>;
|
|
507
|
+
/**
|
|
508
|
+
* Controls whether credentials (cookies, authorization headers) are sent with requests.
|
|
509
|
+
*
|
|
510
|
+
* @default "same-origin"
|
|
511
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
|
|
512
|
+
*/
|
|
513
|
+
credentials?: "include" | "omit" | "same-origin";
|
|
514
|
+
/**
|
|
515
|
+
* Controls the CORS mode for requests.
|
|
516
|
+
*
|
|
517
|
+
* @default "cors"
|
|
518
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
|
|
519
|
+
*/
|
|
520
|
+
mode?: "cors" | "no-cors" | "same-origin" | "navigate";
|
|
521
|
+
/**
|
|
522
|
+
* Controls how the request interacts with the browser's HTTP cache.
|
|
523
|
+
*
|
|
524
|
+
* @default "default"
|
|
525
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
|
|
526
|
+
*/
|
|
527
|
+
cache?: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached";
|
|
528
|
+
/**
|
|
529
|
+
* Enable debug logging for all requests and responses.
|
|
530
|
+
* Logs request details, response status, and errors to the console.
|
|
531
|
+
*
|
|
532
|
+
* @default false
|
|
533
|
+
*/
|
|
534
|
+
debug?: boolean;
|
|
535
|
+
/**
|
|
536
|
+
* Enable request deduplication to prevent duplicate in-flight requests.
|
|
537
|
+
* If the same request is made while a previous one is pending,
|
|
538
|
+
* the pending request's promise will be reused.
|
|
539
|
+
*
|
|
540
|
+
* @default false
|
|
541
|
+
*/
|
|
542
|
+
dedupeRequests?: boolean;
|
|
543
|
+
/**
|
|
544
|
+
* Authentication configuration for the fetcher.
|
|
545
|
+
* Automatically adds Authorization headers to requests.
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* { type: "bearer", token: "your-api-token" }
|
|
549
|
+
* @example
|
|
550
|
+
* { type: "basic", username: "user", password: "pass" }
|
|
551
|
+
*/
|
|
552
|
+
auth?: AuthConfig;
|
|
553
|
+
};
|
|
554
|
+
|
|
42
555
|
export declare function isApiError(response: unknown): response is ApiError;
|
|
43
556
|
|
|
557
|
+
/**
|
|
558
|
+
* Hook that runs when an error occurs during the fetch operation.
|
|
559
|
+
* Can be used for error logging, monitoring, or custom error handling.
|
|
560
|
+
*
|
|
561
|
+
* @param error - The error that occurred during fetch
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* const onError: OnErrorHook = (error) => {
|
|
565
|
+
* console.error('Fetch error:', error.message);
|
|
566
|
+
* sendToErrorTracking(error);
|
|
567
|
+
* };
|
|
568
|
+
*/
|
|
569
|
+
export declare type OnErrorHook = (error: Error) => void;
|
|
570
|
+
|
|
44
571
|
export { }
|