@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 CHANGED
@@ -14,9 +14,11 @@ For full API reference check out the [docs page](https://alien45.github.io/super
14
14
 
15
15
  - [Features](#features)
16
16
  - [Installation](#installation)
17
+ - [NPM](#npm)
18
+ - [CDN / Browser](#cdn--browser)
17
19
  - [Usage](#usage)
18
20
  - [`fetch()`](#fetch): drop-in replacement for built-in `fetch()`
19
- - [`PromisE Features`](#promise-features): status, early finalization etc
21
+ - [`TimeoutPromise` Instance](#promise-features): finer control over the request
20
22
  - [`Method Specific Functions`](#methods)
21
23
  - [`fetch.get()`](#fetch-get)
22
24
  - [`fetch.get.deferred()`](#fetch-deferred): cancellable and debounced or throttled `fetch()`
@@ -39,10 +41,57 @@ For full API reference check out the [docs page](https://alien45.github.io/super
39
41
 
40
42
  ## Installation
41
43
 
44
+ ### NPM
45
+
46
+ Install using your favorite package manager (e.g., `npm`, `yarn`, `pnpm`, `bun`, etc.):
47
+
42
48
  ```bash
43
49
  npm install @superutils/fetch
44
50
  ```
45
51
 
52
+ Dependency: `@superutils/core` and `@superutils/promise` will be automatically installed by package manager
53
+
54
+ ### CDN / Browser
55
+
56
+ If you are not using a bundler, you can include the browser build directly:
57
+
58
+ ```xml
59
+ <script src="https://cdn.jsdelivr.net/npm/@superutils/fetch/dist/browser/index.min.js"></script>
60
+ ```
61
+
62
+ OR,
63
+
64
+ ```xml
65
+ <script src="https://cdn.jsdelivr.net/npm/@superutils/fetch@latest/dist/browser/index.min.js"></script>
66
+ ```
67
+
68
+ This will expose a global namespace with the following:
69
+
70
+ ```javascript
71
+ // Namespace: default export (function) from '@superutils/fetch' and all the exports as properties
72
+ superutils.fetch
73
+ // Namespace: default export (class) from '@superutils/promise' and all the exports as properties
74
+ superutils.PromisE
75
+
76
+ const { fetch, PromisE } = superutils
77
+
78
+ // Fetch usage
79
+ fetch('url', { method: 'get', timeout: 10_000 })
80
+ fetch.get()
81
+ fetch.createClient({ method: 'post', timeout: 30_000 }, {}, { delay: 500 })
82
+
83
+ // PromisE usage
84
+ new PromisE()
85
+ await PromisE.delay(1000)
86
+ const handleChange = PromisE.deferredCallback(
87
+ event => console.log({ value: event.target.value }),
88
+ { delay: 300 },
89
+ )
90
+ ```
91
+
92
+ The `@superutils/fetch` browser build includes `PromisE` most (if not all) of it is used internally.
93
+ Loading `@superutils/promise` separately will take precedence and override it.
94
+
46
95
  ## Usage
47
96
 
48
97
  <div id="fetch"></div>
@@ -61,9 +110,9 @@ fetch('https://dummyjson.com/products/1')
61
110
 
62
111
  <div id="promise-features"></div>
63
112
 
64
- ### PromisE Instance: status, early cancellation
113
+ ### `TimeoutPromise` Instance (extends `PromisE`): finer control over the request
65
114
 
66
- All fetch calls return a `PromisE` (`@superutils/promise`) instance which means they come with additional features available in `PromisE`:
115
+ All fetch calls return a `TimeoutPromise` instance from (`@superutils/promise`) which means they come with additional features available in `PromisE`:
67
116
 
68
117
  1. Status tracking: all instances come with `.pending`, `.resolved` and `.rejected` attributes that indicate the current state of the promise.
69
118
 
@@ -78,6 +127,8 @@ request.then(() => {
78
127
  console.log(request.resolved) // true
79
128
  console.log(request.pending) // false
80
129
  console.log(request.rejected) // false
130
+ console.log(request.aborted) // false
131
+ console.log(request.timedout) // false
81
132
  })
82
133
  ```
83
134
 
@@ -101,6 +152,21 @@ request.onEarlyFinalize.push((resolved, valueOrReason) =>
101
152
  request.reject(new Error('No longer needed'))
102
153
  ```
103
154
 
155
+ 3. `abortCtrl`: an `AbortController` instance either provided in the options or created internally.
156
+
157
+ 4. `cancelAbort()`: function internally stop listening to abort signals. PS: the signal that has been passed to built-in `fetch` cannot be undone.
158
+
159
+ 5. `clearTimeout()`: function to clear timeout, effectively disabling abort on timeout.
160
+
161
+ 6. Promises:
162
+
163
+ - `data`: the fetch result promise and also an instance of `PromisE`.
164
+ - `timeout`: a promise that automatically rejects if request has not completed within provided `options.timeout` duration.
165
+
166
+ Both of these promises can be externally finalized which will result in the fetch/timeout promise to be resolved or rejected and `abortCtrl` aborted.
167
+
168
+ 7. `options`: all options provided to the fetch function
169
+
104
170
  <div id="methods"></div>
105
171
 
106
172
  ### Method Specific Functions