@superutils/fetch 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -5
- package/dist/index.d.ts +68 -6
- package/dist/index.js +4 -5
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ This package enhances the native `fetch` API by providing a streamlined interfac
|
|
|
10
10
|
- Installation
|
|
11
11
|
- Usage
|
|
12
12
|
- [`fetch()`](#fetch): make HTTP requests just like built-in `fetch()`
|
|
13
|
+
- [`Method Specific Functions`](#methods)
|
|
13
14
|
- [`fetchDeferred()`](#fetch-deferred): cancellable and debounced or throttled `fetch()`
|
|
14
15
|
- [`post()`](#post): make post-like requests
|
|
15
16
|
- [`postDeferred()`](#post-deferred) cancellable and debounced or throttled `post()`
|
|
@@ -39,7 +40,7 @@ npm install @superutils/fetch
|
|
|
39
40
|
Make a simple GET request. No need for `response.json()` or `result.data.theActualData` drilling.
|
|
40
41
|
|
|
41
42
|
```typescript
|
|
42
|
-
import
|
|
43
|
+
import fetch from '@superutils/fetch'
|
|
43
44
|
|
|
44
45
|
const theActualData = await fetch('https://dummyjson.com/products/1', {
|
|
45
46
|
method: 'get', // default
|
|
@@ -50,14 +51,38 @@ const theActualData = await fetch.get('https://dummyjson.com/products/1')
|
|
|
50
51
|
console.log(theActualData)
|
|
51
52
|
```
|
|
52
53
|
|
|
54
|
+
<div id="methods"></div>
|
|
55
|
+
|
|
56
|
+
### Method Specific Functions
|
|
57
|
+
|
|
58
|
+
While `fetch()` provides access to all HTTP request methods by specifying it in options (eg: `{ method: 'get' }`), for ease of use you can also use the following:
|
|
59
|
+
|
|
60
|
+
- `fetch.delete(...)`
|
|
61
|
+
- `fetch.get(...)`
|
|
62
|
+
- `fetch.head(...)`
|
|
63
|
+
- `fetch.options(...)`
|
|
64
|
+
- `fetch.patch(...)`
|
|
65
|
+
- `fetch.post(...)`
|
|
66
|
+
- `fetch.put(...)`
|
|
67
|
+
|
|
68
|
+
**Deferred variants:** To debounce/throttle requests.
|
|
69
|
+
|
|
70
|
+
- `fetch.delete.deferred(...)`
|
|
71
|
+
- `fetch.get.deferred(...)`
|
|
72
|
+
- `fetch.head.deferred(...)`
|
|
73
|
+
- `fetch.options.deferred(...)`
|
|
74
|
+
- `fetch.patch.deferred(...)`
|
|
75
|
+
- `fetch.post.deferred(...)`
|
|
76
|
+
- `fetch.put.deferred(...)`
|
|
77
|
+
|
|
53
78
|
<div id="fetch-deferred"></div>
|
|
54
79
|
|
|
55
|
-
### `fetch.get.deferred(deferOptions,
|
|
80
|
+
### `fetch.get.deferred(deferOptions, defaultUrl, defaultOptions)`
|
|
56
81
|
|
|
57
82
|
A practical utility that combines `PromisE.deferred()` from the `@superutils/promise` package with `fetch()`. It's perfect for implementing cancellable, debounced, or throttled search inputs.
|
|
58
83
|
|
|
59
84
|
```typescript
|
|
60
|
-
import { fetchDeferred, ResolveIgnored } from '@superutils/fetch'
|
|
85
|
+
import fetch, { fetchDeferred, ResolveIgnored } from '@superutils/fetch'
|
|
61
86
|
|
|
62
87
|
// Create a debounced search function with a 300ms delay.
|
|
63
88
|
const searchProducts = fetch.get.deferred({
|
|
@@ -104,11 +129,11 @@ setTimeout(() => {
|
|
|
104
129
|
#### Using defaults to reduce redundancy
|
|
105
130
|
|
|
106
131
|
```typescript
|
|
107
|
-
import {
|
|
132
|
+
import fetch, { ResolveIgnored } from '@superutils/fetch'
|
|
108
133
|
|
|
109
134
|
// Create a throttled function to fetch a random quote.
|
|
110
135
|
// The URL and a 3-second timeout are set as defaults, creating a reusable client.
|
|
111
|
-
const getRandomQuote =
|
|
136
|
+
const getRandomQuote = fetch.get.deferred(
|
|
112
137
|
{
|
|
113
138
|
delayMs: 300, // Throttle window
|
|
114
139
|
throttle: true,
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,8 @@ declare enum FetchAs {
|
|
|
17
17
|
response = "response",
|
|
18
18
|
text = "text"
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
/** Custom fetch options (not used by built-in `fetch()`*/
|
|
21
|
+
type FetchCustomOptions = {
|
|
21
22
|
/**
|
|
22
23
|
* Specify how the parse the result. To get raw response use {@link FetchAs.response}.
|
|
23
24
|
* Default: 'json'
|
|
@@ -28,7 +29,7 @@ type FetchConf = {
|
|
|
28
29
|
interceptors?: FetchInterceptors;
|
|
29
30
|
/** Request timeout in milliseconds. */
|
|
30
31
|
timeout?: number;
|
|
31
|
-
};
|
|
32
|
+
} & FetchRetryOptions;
|
|
32
33
|
/** Default args */
|
|
33
34
|
type FetchDeferredArgs = [
|
|
34
35
|
url?: string | URL,
|
|
@@ -230,7 +231,8 @@ type FetchInterceptors = {
|
|
|
230
231
|
/**
|
|
231
232
|
* Fetch request options
|
|
232
233
|
*/
|
|
233
|
-
type FetchOptions = RequestInit &
|
|
234
|
+
type FetchOptions = RequestInit & FetchCustomOptions;
|
|
235
|
+
type FetchOptionsDefaults = Omit<FetchOptionsInterceptor, 'method' | 'retryIf'>;
|
|
234
236
|
/**
|
|
235
237
|
* Fetch options available to interceptors
|
|
236
238
|
*/
|
|
@@ -313,8 +315,9 @@ type PostOptions = Omit<FetchOptions, 'method'> & {
|
|
|
313
315
|
};
|
|
314
316
|
|
|
315
317
|
declare const fetch$1: {
|
|
316
|
-
<
|
|
317
|
-
|
|
318
|
+
<T, TOptions extends FetchOptions = FetchOptions, TReturn = TOptions["as"] extends FetchAs ? FetchResult<T>[TOptions["as"]] : T>(url: string | URL, options?: TOptions): IPromisE<TReturn>;
|
|
319
|
+
/** Default fetch options */
|
|
320
|
+
defaults: FetchOptionsDefaults;
|
|
318
321
|
};
|
|
319
322
|
|
|
320
323
|
/**
|
|
@@ -540,14 +543,73 @@ interface FetchWithMethods extends FetchWithoutMethods {
|
|
|
540
543
|
put: PostMethodFunc;
|
|
541
544
|
}
|
|
542
545
|
/**
|
|
546
|
+
* @function fetch
|
|
547
|
+
*
|
|
543
548
|
* A `fetch()` replacement that simplifies data fetching with automatic JSON parsing, request timeouts, retries,
|
|
544
549
|
* and powerful interceptors. It also includes deferred and throttled request capabilities for complex asynchronous
|
|
545
550
|
* control flows.
|
|
546
551
|
*
|
|
547
552
|
* Will reject promise if response status code is not 2xx (200 <= status < 300).
|
|
548
553
|
*
|
|
554
|
+
* ## Method Specific Functions
|
|
555
|
+
*
|
|
556
|
+
* While `fetch()` provides access to all HTTP request methods by specifying it in options (eg: `{ method: 'get' }`),
|
|
557
|
+
* for ease of use you can also use the following:
|
|
558
|
+
*
|
|
559
|
+
* - `fetch.delete(...)`
|
|
560
|
+
* - `fetch.get(...)`
|
|
561
|
+
* - `fetch.head(...)`
|
|
562
|
+
* - `fetch.options(...)`
|
|
563
|
+
* - `fetch.patch(...)`
|
|
564
|
+
* - `fetch.post(...)`
|
|
565
|
+
* - `fetch.put(...)`
|
|
566
|
+
*
|
|
567
|
+
* **Deferred variants:** To debounce/throttle requests.
|
|
568
|
+
*
|
|
569
|
+
* - `fetch.delete.deferred(...)`
|
|
570
|
+
* - `fetch.get.deferred(...)`
|
|
571
|
+
* - `fetch.head.deferred(...)`
|
|
572
|
+
* - `fetch.options.deferred(...)`
|
|
573
|
+
* - `fetch.patch.deferred(...)`
|
|
574
|
+
* - `fetch.post.deferred(...)`
|
|
575
|
+
* - `fetch.put.deferred(...)`
|
|
576
|
+
*
|
|
577
|
+
* @template T The type of the value that the `fetch` resolves to.
|
|
578
|
+
* @template TReturn Return value type.
|
|
579
|
+
*
|
|
580
|
+
* If `T` is not specified defaults to the following based on the value of `options.as`:
|
|
581
|
+
* - FetchAs.arrayBuffer: `ArrayBuffer`
|
|
582
|
+
* - FetchAs.blob: `Blob`
|
|
583
|
+
* - FetchAs.bytes: `Uint8Array<ArrayBuffer>`
|
|
584
|
+
* - FetchAs.formData: `FormData`
|
|
585
|
+
* - FetchAs.json: `unknown`
|
|
586
|
+
* - FetchAs.text: `string`
|
|
587
|
+
* - FetchAs.response: `Response`
|
|
549
588
|
* @param url
|
|
550
589
|
* @param options (optional) all built-in `fetch()` options such as "method", "headers" and the additionals below.
|
|
590
|
+
*
|
|
591
|
+
* Options' default values (excluding `method` and `retryIf`) can be configured to be EFFECTIVE GLOBALLY.
|
|
592
|
+
*
|
|
593
|
+
* ```typescript
|
|
594
|
+
* fetch.defaults = {
|
|
595
|
+
* as: FetchAs.json,
|
|
596
|
+
* errMsgs: {
|
|
597
|
+
* invalidUrl: 'Invalid URL',
|
|
598
|
+
* parseFailed: 'Failed to parse response as',
|
|
599
|
+
* reqTimedout: 'Request timed out',
|
|
600
|
+
* requestFailed: 'Request failed with status code:',
|
|
601
|
+
* },
|
|
602
|
+
* headers: new Headers([['content-type', 'application/json']]),
|
|
603
|
+
* interceptors: {
|
|
604
|
+
* error: [],
|
|
605
|
+
* request: [],
|
|
606
|
+
* response: [],
|
|
607
|
+
* result: [],
|
|
608
|
+
* },
|
|
609
|
+
* timeout: 0,
|
|
610
|
+
* ........
|
|
611
|
+
* }
|
|
612
|
+
* ```
|
|
551
613
|
* @param options.abortCtrl (optional) if not provided `AbortController` will be instantiated when `timeout` used.
|
|
552
614
|
* @param options.headers (optional) request headers. Default: `{ 'content-type' : 'application/json'}`
|
|
553
615
|
* @param options.interceptors (optional) request interceptor callbacks. See {@link FetchInterceptors} for details.
|
|
@@ -567,4 +629,4 @@ interface FetchWithMethods extends FetchWithoutMethods {
|
|
|
567
629
|
*/
|
|
568
630
|
declare const fetch: FetchWithMethods;
|
|
569
631
|
|
|
570
|
-
export { type FetchArgs, type FetchArgsInterceptor, FetchAs, type
|
|
632
|
+
export { type FetchArgs, type FetchArgsInterceptor, FetchAs, type FetchCustomOptions, type FetchDeferredArgs, type FetchErrMsgs, FetchError, type FetchInterceptorError, type FetchInterceptorRequest, type FetchInterceptorResponse, type FetchInterceptorResult, type FetchInterceptors, type FetchMethodFunc, type FetchOptions, type FetchOptionsDefaults, type FetchOptionsInterceptor, type FetchResult, type FetchRetryOptions, type FetchWithMethods, type FetchWithoutMethods, type Interceptor, type PostArgs, type PostBody, type PostDeferredCallbackArgs, type PostMethodFunc, type PostOptions, fetch as default, fetch, fetchDeferred, mergeFetchOptions, postDeferred };
|
package/dist/index.js
CHANGED
|
@@ -43,9 +43,9 @@ var getResponse = async (...[url, options = {}]) => {
|
|
|
43
43
|
if (!isPositiveInteger(options.retry)) return doFetch();
|
|
44
44
|
const response = PromisE.retry(doFetch, {
|
|
45
45
|
...options,
|
|
46
|
-
retryIf: async (res, count
|
|
46
|
+
retryIf: async (res, count) => {
|
|
47
47
|
var _a;
|
|
48
|
-
return (res == null ? void 0 : res.ok) === false || await ((_a = options == null ? void 0 : options.retryIf) == null ? void 0 : _a.call(options, res, count
|
|
48
|
+
return (res == null ? void 0 : res.ok) === false || await ((_a = options == null ? void 0 : options.retryIf) == null ? void 0 : _a.call(options, res, count)) === true;
|
|
49
49
|
}
|
|
50
50
|
}).catch(
|
|
51
51
|
(err) => Promise.reject(
|
|
@@ -116,7 +116,7 @@ var fetch = (url, options = {}) => {
|
|
|
116
116
|
let timeoutId;
|
|
117
117
|
const promise = new PromisE2(async (resolve, reject) => {
|
|
118
118
|
var _a, _b;
|
|
119
|
-
const _options = mergeFetchOptions_default(defaults, options);
|
|
119
|
+
const _options = mergeFetchOptions_default(fetch.defaults, options);
|
|
120
120
|
if (isEmpty2(_options.method)) _options.method = "get";
|
|
121
121
|
const errorInterceptors = [..._options.interceptors.error];
|
|
122
122
|
const requestInterceptors = [..._options.interceptors.request];
|
|
@@ -202,7 +202,7 @@ var fetch = (url, options = {}) => {
|
|
|
202
202
|
promise.onEarlyFinalize.push(() => abortCtrl == null ? void 0 : abortCtrl.abort());
|
|
203
203
|
return promise;
|
|
204
204
|
};
|
|
205
|
-
|
|
205
|
+
fetch.defaults = {
|
|
206
206
|
as: "json" /* json */,
|
|
207
207
|
errMsgs: {
|
|
208
208
|
invalidUrl: "Invalid URL",
|
|
@@ -226,7 +226,6 @@ var defaults = {
|
|
|
226
226
|
},
|
|
227
227
|
timeout: 0
|
|
228
228
|
};
|
|
229
|
-
fetch.defaults = defaults;
|
|
230
229
|
var fetch_default = fetch;
|
|
231
230
|
|
|
232
231
|
// src/fetchDeferred.ts
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "A lightweight `fetch` wrapper for browsers and Node.js, designed to simplify data fetching and reduce boilerplate.",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"@superutils/core": "^1.0.5",
|
|
6
|
-
"@superutils/promise": "^1.0.
|
|
6
|
+
"@superutils/promise": "^1.0.6"
|
|
7
7
|
},
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"name": "@superutils/fetch",
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@superutils/core": "^1.0.5",
|
|
24
|
-
"@superutils/promise": "^1.0.
|
|
24
|
+
"@superutils/promise": "^1.0.6"
|
|
25
25
|
},
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public"
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"sideEffects": false,
|
|
38
38
|
"type": "module",
|
|
39
39
|
"types": "dist/index.d.ts",
|
|
40
|
-
"version": "1.0.
|
|
40
|
+
"version": "1.0.1"
|
|
41
41
|
}
|