@superutils/fetch 1.5.1 → 1.5.3
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 +69 -3
- package/dist/browser/index.min.js +1 -1123
- package/dist/browser/index.min.js.map +1 -1
- package/dist/index.cjs +53 -32
- package/dist/index.d.cts +164 -146
- package/dist/index.d.ts +164 -146
- package/dist/index.js +53 -29
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import * as _superutils_core from '@superutils/core';
|
|
2
|
+
import { ValueOrPromise, DropFirst } from '@superutils/core';
|
|
1
3
|
import * as _superutils_promise from '@superutils/promise';
|
|
2
4
|
import { RetryOptions, RetryIfFunc, TimeoutOptions, IPromisE_Timeout, DeferredAsyncOptions } from '@superutils/promise';
|
|
3
|
-
export { DeferredAsyncOptions, ResolveError, ResolveIgnored, TIMEOUT_FALLBACK, TIMEOUT_MAX, TimeoutPromise } from '@superutils/promise';
|
|
4
|
-
import { ValueOrPromise, DropFirst } from '@superutils/core';
|
|
5
|
+
export { DeferredAsyncOptions, OnEarlyFinalize, OnFinalize, ResolveError, ResolveIgnored, RetryIfFunc, RetryOptions, TIMEOUT_FALLBACK, TIMEOUT_MAX, TimeoutOptions, TimeoutPromise } from '@superutils/promise';
|
|
5
6
|
|
|
6
7
|
/** Commonly used content types for easier access */
|
|
7
8
|
declare const ContentType: {
|
|
@@ -44,34 +45,36 @@ type Interceptor<T, TArgs extends unknown[]> = (...args: [value: T, ...TArgs]) =
|
|
|
44
45
|
*
|
|
45
46
|
* @returns returning undefined or not returning anything will not override the error
|
|
46
47
|
*
|
|
47
|
-
* @example
|
|
48
|
-
*
|
|
49
|
-
*
|
|
48
|
+
* @example
|
|
49
|
+
* #### Intercept fetch errors to log errors
|
|
50
|
+
* ```javascript
|
|
51
|
+
* import fetch from '@superutils/fetch'
|
|
50
52
|
*
|
|
51
53
|
* // not returning anything or returning undefined will avoid transforming the error.
|
|
52
54
|
* const logError = fetchErr => console.log(fetchErr)
|
|
53
|
-
* const result = await
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
55
|
+
* const result = await fetch.get('https://dummyjson.com/http/400', {
|
|
56
|
+
* interceptors: {
|
|
57
|
+
* error: [logError]
|
|
58
|
+
* }
|
|
57
59
|
* })
|
|
58
60
|
* ```
|
|
59
61
|
*
|
|
60
|
-
* @example
|
|
61
|
-
*
|
|
62
|
-
*
|
|
62
|
+
* @example
|
|
63
|
+
* #### Intercept & transform fetch errors
|
|
64
|
+
* ```javascript
|
|
65
|
+
* import fetch from '@superutils/fetch'
|
|
63
66
|
*
|
|
64
67
|
* // Interceptors can be async functions or just return a promise that resolves to the error.
|
|
65
68
|
* // If the execution of the interceptor fails or promise rejects, it will be ignored.
|
|
66
69
|
* // To transform the error it must directly return an error or a Promise that `resolves` with an error.
|
|
67
70
|
* const transformError = async (fetchErr, url, options) => {
|
|
68
|
-
*
|
|
69
|
-
*
|
|
71
|
+
* fetchErr.message = 'Custom errormessage'
|
|
72
|
+
* return Promise.resolve(fetchErr)
|
|
70
73
|
* }
|
|
71
|
-
* const result = await
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
74
|
+
* const result = await fetch.get('https://dummyjson.com/http/400', {
|
|
75
|
+
* interceptors: {
|
|
76
|
+
* error: [transformError]
|
|
77
|
+
* }
|
|
75
78
|
* })
|
|
76
79
|
* ```
|
|
77
80
|
*/
|
|
@@ -82,19 +85,21 @@ type FetchInterceptorError = Interceptor<FetchError, FetchArgsInterceptor>;
|
|
|
82
85
|
* 1. by returning an API URL (string/URL)
|
|
83
86
|
* 2. by modifying the properties of the options parameter to be used before making the fetch request
|
|
84
87
|
*
|
|
85
|
-
* @example
|
|
88
|
+
* @example
|
|
89
|
+
* #### Intercept and transform fetch request
|
|
86
90
|
* ```typescript
|
|
87
|
-
* import
|
|
91
|
+
* import fetch from '@superutils/fetch'
|
|
88
92
|
*
|
|
89
93
|
* // update API version number
|
|
90
94
|
* const apiV1ToV2 = url => `${url}`.replace('api/v1', 'api/v2')
|
|
91
95
|
* const includeAuthToken = (url, options) => {
|
|
92
|
-
*
|
|
96
|
+
* options.headers.set('x-auth-token', 'my-auth-token')
|
|
93
97
|
* }
|
|
94
|
-
* const
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
+
* const result = await fetch.get('https://dummyjson.com/products', {
|
|
99
|
+
* method: 'post',
|
|
100
|
+
* interceptors: {
|
|
101
|
+
* result: [apiV1ToV2, includeAuthToken]
|
|
102
|
+
* }
|
|
98
103
|
* })
|
|
99
104
|
* ```
|
|
100
105
|
*/
|
|
@@ -106,24 +111,29 @@ type FetchInterceptorRequest = Interceptor<FetchArgs[0], [
|
|
|
106
111
|
*
|
|
107
112
|
* This interceptor can also be used as a transformer by return a different/modified {@link Response}.
|
|
108
113
|
*
|
|
109
|
-
* @example
|
|
110
|
-
*
|
|
111
|
-
*
|
|
114
|
+
* @example
|
|
115
|
+
* #### Intercept and transform response:
|
|
116
|
+
* ```javascript
|
|
117
|
+
* import fetch from '@superutils/fetch'
|
|
112
118
|
*
|
|
113
|
-
* // After successful login, retrieve user
|
|
119
|
+
* // After successful login, retrieve full user details.
|
|
114
120
|
* // This is probably better suited as a result transformer but play along as this is
|
|
115
121
|
* // just a hypothetical scenario ;)
|
|
116
|
-
* const
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
122
|
+
* const getUser = async response => {
|
|
123
|
+
* const authResult = await response.json()
|
|
124
|
+
* const userDetails = await fetch.get('https://dummyjson.com/users/1')
|
|
125
|
+
* const userAuth = { ...userDetails, ...authResult }
|
|
126
|
+
* return new Response(JSON.stringify(userAuth))
|
|
121
127
|
* }
|
|
122
|
-
* const user = await
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
128
|
+
* const user = await fetch.post(
|
|
129
|
+
* 'https://dummyjson.com/user/login',
|
|
130
|
+
* { // data/request body
|
|
131
|
+
* username: 'emilys',
|
|
132
|
+
* password: 'emilyspass',
|
|
133
|
+
* expiresInMins: 30,
|
|
134
|
+
* },
|
|
135
|
+
* { interceptors: { response: [getUser] } }
|
|
136
|
+
* )
|
|
127
137
|
* ```
|
|
128
138
|
*/
|
|
129
139
|
type FetchInterceptorResponse = Interceptor<Response, FetchArgsInterceptor>;
|
|
@@ -140,32 +150,45 @@ type FetchInterceptorResponse = Interceptor<Response, FetchArgsInterceptor>;
|
|
|
140
150
|
* This interceptor can also be used as a transformer by returns a different/modified result.
|
|
141
151
|
*
|
|
142
152
|
*
|
|
143
|
-
* @example
|
|
144
|
-
*
|
|
145
|
-
*
|
|
153
|
+
* @example
|
|
154
|
+
* #### Intercept and transform fetch result
|
|
155
|
+
* ```javascript
|
|
156
|
+
* import fetch from '@superutils/fetch'
|
|
146
157
|
*
|
|
147
|
-
* // first transform result
|
|
148
|
-
* const
|
|
158
|
+
* // first transform result (user object) and ensure user result alwasy contain a hexadecimal crypto balance
|
|
159
|
+
* const ensureBalanceHex = (user = {}) => {
|
|
160
|
+
* user.crypto ??= {}
|
|
161
|
+
* user.crypto.balance ??= '0x0'
|
|
162
|
+
* return user
|
|
163
|
+
* }
|
|
149
164
|
* // then check convert hexadecimal number to BigInt
|
|
150
|
-
* const hexToBigInt =
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* }
|
|
154
|
-
* return data
|
|
165
|
+
* const hexToBigInt = user => {
|
|
166
|
+
* user.crypto.balance = BigInt(user.crypto.balance)
|
|
167
|
+
* return user
|
|
155
168
|
* }
|
|
156
169
|
* // then log balance (no transformation)
|
|
157
|
-
* const logBalance =
|
|
158
|
-
*
|
|
170
|
+
* const logBalance = (result, url) => {
|
|
171
|
+
* // only log balance for single user requests
|
|
172
|
+
* const shouldLog = result?.hasOwnProperty('crypto') && /^[0-9]+$/.test(
|
|
173
|
+
* url?.split('/users/')[1].replace('/', '')
|
|
174
|
+
* )
|
|
175
|
+
* shouldLog && console.log(
|
|
176
|
+
* new Date().toISOString(),
|
|
177
|
+
* '[UserBalance] UserID:', result.id,
|
|
178
|
+
* result.crypto.balance
|
|
179
|
+
* )
|
|
159
180
|
* }
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
181
|
+
* // now we make the actaul fetch request
|
|
182
|
+
* const result = await fetch.get('https://dummyjson.com/users/1', {
|
|
183
|
+
* interceptors: {
|
|
184
|
+
* result: [
|
|
185
|
+
* ensureBalanceHex,
|
|
186
|
+
* hexToBigInt,
|
|
187
|
+
* logBalance
|
|
188
|
+
* ]
|
|
189
|
+
* }
|
|
168
190
|
* })
|
|
191
|
+
* console.log({result})
|
|
169
192
|
* ```
|
|
170
193
|
*/
|
|
171
194
|
type FetchInterceptorResult<Args extends unknown[] = FetchArgsInterceptor> = Interceptor<unknown, Args>;
|
|
@@ -263,7 +286,7 @@ type FetchCustomOptions = {
|
|
|
263
286
|
* - Use `abortCtrl` instead of `signal` to prevent creating internal `AbortController` instance.
|
|
264
287
|
*/
|
|
265
288
|
abortCtrl?: AbortController;
|
|
266
|
-
body?:
|
|
289
|
+
body?: PostArgs[1];
|
|
267
290
|
/**
|
|
268
291
|
* Custom fetch function to use instead of the global `fetch`.
|
|
269
292
|
* Useful for testing or using a different fetch implementation (e.g. `node-fetch` in older Node versions).
|
|
@@ -277,7 +300,7 @@ type FetchCustomOptions = {
|
|
|
277
300
|
* See {@link FetchInterceptors} for more details.
|
|
278
301
|
*/
|
|
279
302
|
interceptors?: FetchInterceptors;
|
|
280
|
-
/** Whether to validate URL before making the request. Default: `
|
|
303
|
+
/** Whether to validate URL before making the request. Default: `false` */
|
|
281
304
|
validateUrl?: boolean;
|
|
282
305
|
} & FetchRetryOptions & TimeoutOptions<[]>;
|
|
283
306
|
/** Default args */
|
|
@@ -303,16 +326,33 @@ type FetchFunc = (...args: FetchArgs) => Promise<Response>;
|
|
|
303
326
|
*/
|
|
304
327
|
type FetchOptions = Omit<RequestInit, 'body'> & FetchCustomOptions;
|
|
305
328
|
/** Default fetch options */
|
|
306
|
-
type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'method' | 'signal' | 'timeout'> & {
|
|
307
|
-
/**
|
|
329
|
+
type FetchOptionsDefault = Omit<FetchOptionsInterceptor, 'abortCtrl' | 'as' | 'body' | 'method' | 'signal' | 'timeout' | 'headers'> & {
|
|
330
|
+
/**
|
|
331
|
+
* Request headers.
|
|
332
|
+
*
|
|
333
|
+
* Deafult:
|
|
334
|
+
* - No default content type set when `fetch()` is directly invoked.
|
|
335
|
+
* - `"content-type": "application/json"`: for `createPostClient()`, `fetch.post()`,
|
|
336
|
+
* `fetch.post.deferred()` and other method specific functions
|
|
337
|
+
*/
|
|
338
|
+
headers: HeadersInit;
|
|
339
|
+
/**
|
|
340
|
+
* Request timeout duration in milliseconds.
|
|
341
|
+
*
|
|
342
|
+
* Default:
|
|
343
|
+
* - `30_000` for `createClient()`, `createPostClient()` and
|
|
344
|
+
* all method specific functions (`fetch.METHOD` & `fetch.METHOD.deferred()`
|
|
345
|
+
* - `2147483647` when `fetch()` invoked directly
|
|
346
|
+
*/
|
|
308
347
|
timeout: number;
|
|
309
348
|
};
|
|
310
349
|
/**
|
|
311
350
|
* Fetch options available to interceptors.
|
|
312
351
|
*
|
|
313
352
|
*/
|
|
314
|
-
type FetchOptionsInterceptor = Omit<FetchOptions, 'as' | 'errMsgs' | 'interceptors' | 'headers' | 'timeout' | keyof FetchRetryOptions> & {
|
|
353
|
+
type FetchOptionsInterceptor = Omit<FetchOptions, 'as' | 'body' | 'errMsgs' | 'interceptors' | 'headers' | 'timeout' | keyof FetchRetryOptions> & {
|
|
315
354
|
as: FetchAs;
|
|
355
|
+
body: PostBody;
|
|
316
356
|
/** Error messages */
|
|
317
357
|
errMsgs: Required<FetchErrMsgs>;
|
|
318
358
|
headers: Headers;
|
|
@@ -350,7 +390,14 @@ type FetchRetryOptions = Omit<Partial<RetryOptions>, 'retry' | 'retryIf'> & {
|
|
|
350
390
|
type PostBody = Record<string, unknown> | BodyInit | null;
|
|
351
391
|
type PostArgs = [
|
|
352
392
|
url: string | URL,
|
|
353
|
-
|
|
393
|
+
/**
|
|
394
|
+
* Post body or a function that returns/resolves post body.
|
|
395
|
+
*
|
|
396
|
+
* PS:
|
|
397
|
+
* - if function provided, it will be executed before executing any request interceptors
|
|
398
|
+
* - if function execution fails it will throw an error and avoid making the fetch request
|
|
399
|
+
*/
|
|
400
|
+
data?: PostBody | (() => ValueOrPromise<PostBody>),
|
|
354
401
|
options?: PostOptions
|
|
355
402
|
];
|
|
356
403
|
/**
|
|
@@ -360,30 +407,10 @@ type PostArgs = [
|
|
|
360
407
|
* ```typescript
|
|
361
408
|
* import fetch, { type PostDeferredCbArgs } from '@superutils/fetch'
|
|
362
409
|
*
|
|
363
|
-
* // test with types
|
|
364
410
|
* type T1 = PostDeferredCbArgs<string | URL, undefined> // expected: [data, options]
|
|
365
|
-
* type T2 = PostDeferredCbArgs<undefined, string> // expected: [url, options]
|
|
411
|
+
* type T2 = PostDeferredCbArgs<string | undefined, string> // expected: [url, options]
|
|
366
412
|
* type T3 = PostDeferredCbArgs // expected: [url, data, options]
|
|
367
413
|
* type T4 = PostDeferredCbArgs<string, string> // expected: [options]
|
|
368
|
-
*
|
|
369
|
-
* const data = { name: 'test' }
|
|
370
|
-
* const url = 'https://domain.com'
|
|
371
|
-
* // test with fetch.post.deferred()
|
|
372
|
-
* const f1 = fetch.post.deferred({}, 'https://domain.com')
|
|
373
|
-
* // expected: [data, options]
|
|
374
|
-
* f1({data: 1}).then(console.log, console.warn)
|
|
375
|
-
*
|
|
376
|
-
* const f2 = fetch.post.deferred({}, undefined, 'dome data')
|
|
377
|
-
* // expected: [url, options]
|
|
378
|
-
* f2('https').then(console.log, console.warn)
|
|
379
|
-
*
|
|
380
|
-
* const f3 = fetch.post.deferred({})
|
|
381
|
-
* // expected: [url, data, options]
|
|
382
|
-
* f3('https://domain.com').then(console.log, console.warn)
|
|
383
|
-
*
|
|
384
|
-
* const f4 = fetch.post.deferred({}, 'url', 'data')
|
|
385
|
-
* // expected: [options]
|
|
386
|
-
* f4().then(console.log, console.warn)
|
|
387
414
|
* ```
|
|
388
415
|
*/
|
|
389
416
|
type PostDeferredCbArgs<DefaultUrl = undefined, DefaultData = undefined, Options = PostArgs[2], PostArgsReq extends unknown[] = Required<PostArgs>, _url = undefined extends DefaultUrl ? undefined : DefaultUrl, _data = undefined extends DefaultData ? undefined : DefaultData> = [_url, _data] extends [PostArgsReq[0], undefined] ? [
|
|
@@ -440,45 +467,41 @@ type ClientData<FixedOptions> = ExtractAs<[FixedOptions]> extends FetchAs.json ?
|
|
|
440
467
|
* The returned client also includes a `.deferred()` method, providing the same debounce, throttle, and sequential
|
|
441
468
|
* execution capabilities found in functions like `fetch.get.deferred()`.
|
|
442
469
|
*
|
|
443
|
-
* @example
|
|
470
|
+
* @example
|
|
471
|
+
* #### Create reusable clients
|
|
444
472
|
* ```javascript
|
|
445
473
|
* import { createClient } from '@superutils/fetch'
|
|
446
474
|
*
|
|
447
475
|
* // Create a "GET" client with default headers and a 5-second timeout
|
|
448
476
|
* const apiClient = createClient(
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
* {
|
|
462
|
-
* // default defer options (can be overridden)
|
|
463
|
-
* delay: 300,
|
|
464
|
-
* retry: 2, // If request fails, retry up to two more times
|
|
465
|
-
* },
|
|
477
|
+
* { method: 'get' }, // fixed options cannot be overridden
|
|
478
|
+
* { // default options can be overridden
|
|
479
|
+
* headers: {
|
|
480
|
+
* Authorization: 'Bearer my-secret-token',
|
|
481
|
+
* 'Content-Type': 'application/json',
|
|
482
|
+
* },
|
|
483
|
+
* timeout: 5000,
|
|
484
|
+
* },
|
|
485
|
+
* {// default defer options (can be overridden)
|
|
486
|
+
* delay: 300,
|
|
487
|
+
* retry: 2, // If request fails, retry up to two more times
|
|
488
|
+
* },
|
|
466
489
|
* )
|
|
467
490
|
*
|
|
468
491
|
* // Use it just like the standard fetch
|
|
469
492
|
* apiClient('https://dummyjson.com/products/1', {
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
*
|
|
493
|
+
* // The 'method' property cannot be overridden as it is used in the fixed options when creating the client.
|
|
494
|
+
* // In TypeScript, the compiler will not allow this property.
|
|
495
|
+
* // In Javascript, it will simply be ignored.
|
|
496
|
+
* // method: 'post',
|
|
497
|
+
* timeout: 3000, // The 'timeout' property can be overridden
|
|
475
498
|
* }).then(console.log, console.warn)
|
|
476
499
|
*
|
|
477
500
|
* // create a deferred client using "apiClient"
|
|
478
501
|
* const deferredClient = apiClient.deferred(
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
502
|
+
* { retry: 0 }, // disable retrying by overriding the `retry` defer option
|
|
503
|
+
* 'https://dummyjson.com/products/1',
|
|
504
|
+
* { timeout: 3000 },
|
|
482
505
|
* )
|
|
483
506
|
* deferredClient({ timeout: 10000 }) // timeout is overridden by individual request
|
|
484
507
|
* .then(console.log, console.warn)
|
|
@@ -503,24 +526,25 @@ commonOptions?: FetchOptions & CommonOptions, commonDeferOptions?: DeferredAsync
|
|
|
503
526
|
* Similar to `createClient`, the returned function comes equipped with a `.deferred()` method, enabling debounced,
|
|
504
527
|
* throttled, or sequential execution.
|
|
505
528
|
*
|
|
506
|
-
* @example
|
|
529
|
+
* @example
|
|
530
|
+
* #### Create reusable clients
|
|
507
531
|
* ```javascript
|
|
508
532
|
* import { createPostClient, FetchAs } from '@superutils/fetch'
|
|
509
533
|
*
|
|
510
534
|
* // Create a POST client with 10-second as the default timeout
|
|
511
535
|
* const postClient = createPostClient(
|
|
512
|
-
* {
|
|
513
|
-
*
|
|
514
|
-
*
|
|
536
|
+
* { // fixed options cannot be overrided by client calls
|
|
537
|
+
* method: 'post',
|
|
538
|
+
* headers: { 'content-type': 'application/json' },
|
|
515
539
|
* },
|
|
516
|
-
* { timeout: 10000 },
|
|
540
|
+
* { timeout: 10000 }, // common options that can be overriden by client calls
|
|
517
541
|
* )
|
|
518
542
|
*
|
|
519
543
|
* // Invoking `postClient()` automatically applies the pre-configured options
|
|
520
544
|
* postClient(
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
545
|
+
* 'https://dummyjson.com/products/add',
|
|
546
|
+
* { title: 'New Product' }, // data/body
|
|
547
|
+
* {}, // other options
|
|
524
548
|
* ).then(console.log)
|
|
525
549
|
*
|
|
526
550
|
* // create a deferred client using "postClient"
|
|
@@ -530,10 +554,7 @@ commonOptions?: FetchOptions & CommonOptions, commonDeferOptions?: DeferredAsync
|
|
|
530
554
|
* onResult: console.log, // prints only successful results
|
|
531
555
|
* },
|
|
532
556
|
* 'https://dummyjson.com/products/add',
|
|
533
|
-
* {
|
|
534
|
-
* method: 'patch',
|
|
535
|
-
* timeout: 3000,
|
|
536
|
-
* },
|
|
557
|
+
* { method: 'patch', timeout: 3000 },
|
|
537
558
|
* )
|
|
538
559
|
* updateProduct({ title: 'New title 1' }) // ignored by debounce
|
|
539
560
|
* updateProduct({ title: 'New title 2' }) // executed
|
|
@@ -574,12 +595,13 @@ declare const executeInterceptors: <T, TArgs extends unknown[]>(value: T, signal
|
|
|
574
595
|
* @param options.as (optional) determines how to parse the result. Default: {@link FetchAs.json}
|
|
575
596
|
* @param options.method (optional) fetch method. Default: `'get'`
|
|
576
597
|
*
|
|
577
|
-
* @example
|
|
578
|
-
*
|
|
598
|
+
* @example
|
|
599
|
+
* #### Make a simple HTTP requests
|
|
600
|
+
* ```javascript
|
|
579
601
|
* import { fetch } from '@superutils/fetch'
|
|
580
602
|
*
|
|
581
603
|
* // no need for `response.json()` or `result.data.data` drilling
|
|
582
|
-
* fetch('https://dummyjson.com/products/1')
|
|
604
|
+
* fetch.get('https://dummyjson.com/products/1')
|
|
583
605
|
* .then(product => console.log(product))
|
|
584
606
|
* ```
|
|
585
607
|
*/
|
|
@@ -594,10 +616,10 @@ declare const fetch$1: {
|
|
|
594
616
|
*
|
|
595
617
|
* Notes:
|
|
596
618
|
* - item properties will be prioritized in the order of sequence they were passed in
|
|
597
|
-
* - the following properties will be merged
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
619
|
+
* - the following properties will be merged:
|
|
620
|
+
* * `errMsgs`
|
|
621
|
+
* * `headers`
|
|
622
|
+
* * `interceptors`
|
|
601
623
|
* - all other properties will simply override previous values
|
|
602
624
|
*
|
|
603
625
|
* @returns combined
|
|
@@ -703,7 +725,7 @@ declare const methods: {
|
|
|
703
725
|
method: "delete";
|
|
704
726
|
}, Options, DefaultOptions, ({
|
|
705
727
|
headers?: HeadersInit | undefined;
|
|
706
|
-
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
|
|
728
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
|
|
707
729
|
};
|
|
708
730
|
/** Make HTTP requests with method PATCH */
|
|
709
731
|
patch: {
|
|
@@ -728,7 +750,7 @@ declare const methods: {
|
|
|
728
750
|
method: "patch";
|
|
729
751
|
}, Options, DefaultOptions, ({
|
|
730
752
|
headers?: HeadersInit | undefined;
|
|
731
|
-
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
|
|
753
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
|
|
732
754
|
};
|
|
733
755
|
/** Make HTTP requests with method POST */
|
|
734
756
|
post: {
|
|
@@ -753,7 +775,7 @@ declare const methods: {
|
|
|
753
775
|
method: "post";
|
|
754
776
|
}, Options, DefaultOptions, ({
|
|
755
777
|
headers?: HeadersInit | undefined;
|
|
756
|
-
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
|
|
778
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
|
|
757
779
|
};
|
|
758
780
|
/** Make HTTP requests with method PUT */
|
|
759
781
|
put: {
|
|
@@ -778,7 +800,7 @@ declare const methods: {
|
|
|
778
800
|
method: "put";
|
|
779
801
|
}, Options, DefaultOptions, ({
|
|
780
802
|
headers?: HeadersInit | undefined;
|
|
781
|
-
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => PostBody), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
|
|
803
|
+
} & Omit<PostOptions, "headers" | "method"> & Partial<Record<"method", never>>) | undefined], T>>(...args: PostDeferredCbArgs<DefaultUrl, DefaultData, Options, [url: string | URL, data: PostBody | (() => _superutils_core.ValueOrPromise<PostBody>), options: PostOptions], undefined extends DefaultUrl ? DefaultUrl & undefined : DefaultUrl, undefined extends DefaultData ? DefaultData & undefined : DefaultData>) => IPromise_Fetch<TReturn>;
|
|
782
804
|
};
|
|
783
805
|
};
|
|
784
806
|
/**
|
|
@@ -856,8 +878,8 @@ declare const methods: {
|
|
|
856
878
|
* import fetch from '@superutils/fetch'
|
|
857
879
|
*
|
|
858
880
|
* fetch('https://dummyjson.com/products/1')
|
|
859
|
-
*
|
|
860
|
-
*
|
|
881
|
+
* .then(response => response.json())
|
|
882
|
+
* .then(console.log, console.error)
|
|
861
883
|
* ```
|
|
862
884
|
*
|
|
863
885
|
* @example
|
|
@@ -867,13 +889,9 @@ declare const methods: {
|
|
|
867
889
|
*
|
|
868
890
|
* // no need for `response.json()` or `result.data.data` drilling
|
|
869
891
|
* fetch.get('https://dummyjson.com/products/1')
|
|
870
|
-
*
|
|
871
|
-
* fetch.get('https://dummyjson.com/products/1')
|
|
872
|
-
* .then(product => console.log(product))
|
|
873
|
-
*
|
|
874
|
-
*
|
|
892
|
+
* .then(product => console.log(product))
|
|
875
893
|
* fetch.post('https://dummyjson.com/products/add', { title: 'Product title' })
|
|
876
|
-
*
|
|
894
|
+
* .then(product => console.log(product))
|
|
877
895
|
* ```
|
|
878
896
|
*
|
|
879
897
|
*
|
|
@@ -903,10 +921,10 @@ declare const methods: {
|
|
|
903
921
|
*
|
|
904
922
|
* // add an interceptor to conditionally include header before making requests
|
|
905
923
|
* interceptors.request.push((url, options) => {
|
|
906
|
-
*
|
|
907
|
-
*
|
|
924
|
+
* // ignore login requests
|
|
925
|
+
* if (`${url}`.includes('/login')) return
|
|
908
926
|
*
|
|
909
|
-
*
|
|
927
|
+
* options.headers.set('x-auth-token', 'my-auth-token')
|
|
910
928
|
* })
|
|
911
929
|
* ```
|
|
912
930
|
*/
|