vigor-fetch 1.0.19 โ 2.0.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 +341 -224
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,289 +1,406 @@
|
|
|
1
1
|
# vigor-fetch
|
|
2
2
|
|
|
3
|
-
**Vigor** is a lightweight TypeScript HTTP utility library.
|
|
4
|
-
|
|
3
|
+
**Vigor** is a lightweight (gzipped ~7.7kb) TypeScript HTTP / Retry utility library.
|
|
4
|
+
Vigor provides a fluent, chainable API for building robust network logic with built-in retry, backoff, interceptors, parsing, and concurrency control.
|
|
5
5
|
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
## Features
|
|
9
9
|
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
- โก **Parallel Requests** โ
|
|
14
|
-
- ๐ **
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
10
|
+
- ๐งฉ **Fluent & Immutable API** โ Fully composable, side-effect-free chaining
|
|
11
|
+
- ๐ **Advanced Retry System** โ Exponential backoff with jitter support.
|
|
12
|
+
- ๐ **Smart Fetch Layer** โ Automatic 429 handling & configurable retry rules
|
|
13
|
+
- โก **Parallel Requests** โ Concurrency-limited task runner
|
|
14
|
+
- ๐ **Smart Response Parsing** โ Auto parsing based on Content-Type
|
|
15
|
+
- โก **Zero Dependencies** โ Built on native Fetch + AbortController
|
|
16
|
+
- ๐ช **Powerful Interceptors** โ Lifecycle hooks for full control flow
|
|
17
|
+
- ๐ง **TypeScript First** โ Fully typed inference across all modules
|
|
18
18
|
---
|
|
19
19
|
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install vigor-fetch
|
|
24
|
+
```
|
|
25
|
+
|
|
20
26
|
## Why Vigor?
|
|
21
27
|
|
|
22
28
|
| Feature | Vigor | native fetch | ky | axios |
|
|
23
29
|
|---|:---:|:---:|:---:|:---:|
|
|
24
|
-
| Zero dependencies | โ
| โ
|
|
|
25
|
-
|
|
|
26
|
-
|
|
|
27
|
-
|
|
|
28
|
-
| Auto response parsing | โ
| โ |
|
|
30
|
+
| Zero dependencies | โ
| โ
| โ | โ
|
|
|
31
|
+
| 429 rate-limit handling | โ
| โ | โ
(manual/config-based) | โ |
|
|
32
|
+
| Retry with jitter | โ
| โ | โ
| โ |
|
|
33
|
+
| Exponential backoff | โ
| โ | โ
| โ |
|
|
34
|
+
| Auto response parsing | โ
| โ | โ
| โ
|
|
|
29
35
|
| Fluent chaining API | โ
| โ | โ
| โ |
|
|
30
|
-
| Concurrency
|
|
31
|
-
| Lifecycle interceptors | โ
| โ |
|
|
36
|
+
| Concurrency control | โ
| โ | โ | โ |
|
|
37
|
+
| Lifecycle interceptors | โ
| โ | partial | โ |
|
|
32
38
|
| Plugin system | โ
| โ | โ | โ |
|
|
33
|
-
| TypeScript-first | โ
| โ | โ
| partial |
|
|
34
39
|
|
|
35
|
-
##
|
|
40
|
+
## Quick Start
|
|
36
41
|
|
|
37
|
-
```
|
|
38
|
-
|
|
42
|
+
```ts
|
|
43
|
+
import vigor from "vigor-fetch";
|
|
44
|
+
const data = await vigor
|
|
45
|
+
.fetch("https://api.example.com")
|
|
46
|
+
.path("api", "v1", "main")
|
|
47
|
+
.request()
|
|
39
48
|
```
|
|
40
49
|
|
|
50
|
+
# ๐ ๏ธ Vigor API Reference (Rewritten)
|
|
51
|
+
|
|
41
52
|
---
|
|
42
53
|
|
|
43
|
-
|
|
54
|
+
# ๐ก vigor.fetch(origin)
|
|
44
55
|
|
|
45
|
-
|
|
46
|
-
import vigor from 'vigor-fetch';
|
|
56
|
+
vigor.fetch(origin: string)
|
|
47
57
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
## Chain Methods
|
|
59
|
+
|
|
60
|
+
| Method | Type | Description |
|
|
61
|
+
|--------|------|-------------|
|
|
62
|
+
| origin | (string) => VigorFetch | Set base URL |
|
|
63
|
+
| path | (...string[]) => VigorFetch | Append URL path segments |
|
|
64
|
+
| query | (object) => VigorFetch | Set query parameters |
|
|
65
|
+
| method | (VigorFetchMethods) => VigorFetch | Set HTTP method |
|
|
66
|
+
| headers | (HeadersInit) => VigorFetch | Set request headers |
|
|
67
|
+
| body | (any) => VigorFetch | Set request body |
|
|
68
|
+
| options | (object) => VigorFetch | Merge fetch options |
|
|
69
|
+
| setting | (fn: (s: VigorFetchSettings) => VigorFetchSettings) => VigorFetch | Settings pipeline |
|
|
70
|
+
| retryConfig | (fn: (r: VigorRetry) => VigorRetry) => VigorFetch | Retry engine config |
|
|
71
|
+
| parseConfig | (fn: (p: VigorParse) => VigorParse) => VigorFetch | Response parser config |
|
|
72
|
+
| interceptors | (fn: (i: VigorFetchInterceptors) => VigorFetchInterceptors) => VigorFetch | Lifecycle hooks |
|
|
73
|
+
| request | () => Promise<T> | Execute request |
|
|
61
74
|
|
|
62
75
|
---
|
|
63
76
|
|
|
64
|
-
##
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
|
71
|
-
|
|
72
|
-
|
|
|
73
|
-
|
|
|
74
|
-
|
|
|
75
|
-
|
|
|
76
|
-
|
|
|
77
|
-
|
|
|
78
|
-
| `.offset(obj)` | Pass options directly to `fetch` |
|
|
79
|
-
| `.maxDelay(ms)` | Maximum wait time per retry (ms) |
|
|
80
|
-
| `.retryHeaders(...str)` | Add custom headers for Rate Limit detection |
|
|
81
|
-
| `.unretry(...int)` | Set HTTP status codes that should not be retried |
|
|
82
|
-
| `.retryConfig(fn)` | Customize the internal VigorRetry configuration |
|
|
83
|
-
| `.parseConfig(fn)` | Customize the internal VigorParse configuration |
|
|
84
|
-
| `.before(...fn)` | Interceptor called before the request |
|
|
85
|
-
| `.after(...fn)` | Interceptor called after response is received (before parsing) |
|
|
86
|
-
| `.result(...fn)` | Interceptor called after parsing is complete |
|
|
87
|
-
| `.onError(...fn)` | Interceptor called on error |
|
|
88
|
-
| `.request()` | Execute the request |
|
|
89
|
-
|
|
90
|
-
**Example**
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
const data = await vigor
|
|
94
|
-
.fetch('https://api.example.com')
|
|
95
|
-
.path('/items')
|
|
96
|
-
.query({ page: 1, limit: 20 })
|
|
97
|
-
.headers({ Authorization: 'Bearer TOKEN' })
|
|
98
|
-
.retryConfig(r => r.count(3).baseDelay(500))
|
|
99
|
-
.before(async (ctx) => {
|
|
100
|
-
console.log('Request started:', ctx.url);
|
|
101
|
-
})
|
|
102
|
-
.onError(async (ctx, err) => {
|
|
103
|
-
console.error('Error occurred:', err.message);
|
|
104
|
-
})
|
|
105
|
-
.request();
|
|
106
|
-
```
|
|
77
|
+
## โ๏ธ fetch().setting(s => s)
|
|
78
|
+
|
|
79
|
+
| Field | Type | Description |
|
|
80
|
+
|------|------|-------------|
|
|
81
|
+
| origin | string | Base URL |
|
|
82
|
+
| path | string[] | URL segments |
|
|
83
|
+
| query | object | Query params |
|
|
84
|
+
| unretry | number[] | Non-retry status codes |
|
|
85
|
+
| retryHeaders | string[] | Retry-related headers |
|
|
86
|
+
| method | string | HTTP method |
|
|
87
|
+
| headers | object | Request headers |
|
|
88
|
+
| body | any | Request body |
|
|
89
|
+
| options | object | Fetch options |
|
|
90
|
+
| default | T | Fallback value |
|
|
107
91
|
|
|
108
92
|
---
|
|
109
93
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
|
115
|
-
|
|
116
|
-
|
|
|
117
|
-
|
|
|
118
|
-
| `.max(ms)` | Maximum wait time per attempt (default: 10000ms) |
|
|
119
|
-
| `.backoff(factor)` | Exponential backoff multiplier (default: 1.3) |
|
|
120
|
-
| `.baseDelay(ms)` | Base delay between retries (default: 1000ms) |
|
|
121
|
-
| `.jitter(ms)` | Random jitter range added to each delay (default: 500ms) |
|
|
122
|
-
| `.before(...fn)` | Interceptor called before each attempt |
|
|
123
|
-
| `.after(...fn)` | Interceptor called after each successful attempt |
|
|
124
|
-
| `.onRetry(...fn)` | Interceptor called when a retry is triggered |
|
|
125
|
-
| `.onError(...fn)` | Interceptor called on final failure |
|
|
126
|
-
| `.request()` | Execute |
|
|
127
|
-
|
|
128
|
-
**Example**
|
|
129
|
-
|
|
130
|
-
```typescript
|
|
131
|
-
const result = await vigor
|
|
132
|
-
.retry(async () => {
|
|
133
|
-
const res = await fetch('https://api.example.com/unstable');
|
|
134
|
-
if (!res.ok) throw new Error('Failed');
|
|
135
|
-
return res.json();
|
|
136
|
-
})
|
|
137
|
-
.count(5)
|
|
138
|
-
.baseDelay(1000)
|
|
139
|
-
.backoff(1.5)
|
|
140
|
-
.jitter(300)
|
|
141
|
-
.onRetry(async (ctx) => {
|
|
142
|
-
console.log(`Retry #${ctx.attempt}, waiting ${ctx.wait}ms`);
|
|
143
|
-
})
|
|
144
|
-
.request();
|
|
145
|
-
```
|
|
94
|
+
## ๐งฉ fetch().interceptors(i => i)
|
|
95
|
+
|
|
96
|
+
| Hook | Signature | Description |
|
|
97
|
+
|------|----------|-------------|
|
|
98
|
+
| before | (ctx, { setOptions, throwError }) => void | Before request |
|
|
99
|
+
| after | (ctx, { throwError }) => void | After success |
|
|
100
|
+
| onError | (ctx, { setResult, throwError }) => void | Error handler |
|
|
101
|
+
| result | (ctx, { setResult, throwError }) => void | Final result hook |
|
|
146
102
|
|
|
147
103
|
---
|
|
148
104
|
|
|
149
|
-
|
|
105
|
+
# ๐ vigor.retry(task)
|
|
150
106
|
|
|
151
|
-
|
|
107
|
+
vigor.retry(task: VigorRetryTask<T>)
|
|
152
108
|
|
|
153
|
-
|
|
154
|
-
|---|---|
|
|
155
|
-
| `application/json` | `response.json()` |
|
|
156
|
-
| `multipart/form-data` | `response.formData()` |
|
|
157
|
-
| `application/octet-stream` | `response.arrayBuffer()` |
|
|
158
|
-
| `image/*`, `video/*`, `audio/*`, `pdf` | `response.blob()` |
|
|
159
|
-
| anything else | `response.text()` |
|
|
109
|
+
## Methods
|
|
160
110
|
|
|
161
|
-
| Method | Description |
|
|
162
|
-
|
|
163
|
-
|
|
|
164
|
-
|
|
|
165
|
-
|
|
|
166
|
-
|
|
|
167
|
-
|
|
|
168
|
-
|
|
|
111
|
+
| Method | Type | Description |
|
|
112
|
+
|--------|------|-------------|
|
|
113
|
+
| target | (fn: VigorRetryTask<T>) => VigorRetry | Set retry function |
|
|
114
|
+
| setting | (fn: (s: VigorRetrySettings) => VigorRetrySettings) => VigorRetry | Retry settings |
|
|
115
|
+
| backoff | (fn: (b: VigorRetryBackoff) => VigorRetryBackoff) => VigorRetry | Backoff strategy |
|
|
116
|
+
| interceptors | (fn: (i: VigorRetryInterceptors) => VigorRetryInterceptors) => VigorRetry | Lifecycle hooks |
|
|
117
|
+
| request | () => Promise<T> | Execute retry flow |
|
|
118
|
+
| createController | () => (error: Error) => void | Abort controller |
|
|
169
119
|
|
|
170
|
-
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## โ๏ธ retry().setting(s => s)
|
|
171
123
|
|
|
172
|
-
|
|
173
|
-
|
|
124
|
+
| Field | Type | Description |
|
|
125
|
+
|------|------|-------------|
|
|
126
|
+
| count | number | Max retry attempts |
|
|
127
|
+
| limit | number | Timeout per attempt |
|
|
128
|
+
| maxDelay | number | Max delay cap |
|
|
129
|
+
| default | T | Fallback value |
|
|
174
130
|
|
|
175
|
-
|
|
176
|
-
const parsed = await vigor.parse(raw).request();
|
|
131
|
+
---
|
|
177
132
|
|
|
178
|
-
|
|
179
|
-
const text = await vigor.parse(raw).type('text').request();
|
|
133
|
+
## ๐ retry().backoff(b => b)
|
|
180
134
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
135
|
+
| Field | Type | Description |
|
|
136
|
+
|------|------|-------------|
|
|
137
|
+
| initialDelay | number | Initial delay |
|
|
138
|
+
| baseDelay | number | Base delay |
|
|
139
|
+
| factor | number | Exponential multiplier |
|
|
140
|
+
| jitter | number | Random noise |
|
|
184
141
|
|
|
185
142
|
---
|
|
186
143
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
|
192
|
-
|
|
193
|
-
|
|
|
194
|
-
|
|
|
195
|
-
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
144
|
+
## ๐งฉ retry().interceptors(i => i)
|
|
145
|
+
|
|
146
|
+
| Hook | Signature | Description |
|
|
147
|
+
|------|----------|-------------|
|
|
148
|
+
| before | (ctx, { setAttempt, throwError, abort }) => void | Before execution |
|
|
149
|
+
| after | (ctx, { setAttempt, setResult, throwError }) => void | After success |
|
|
150
|
+
| onError | (ctx, { setResult, throwError }) => void | Error handling |
|
|
151
|
+
| onRetry | (ctx, { setDelay }) => void | Retry event |
|
|
152
|
+
| retryIf | (ctx, { proceedRetry, cancelRetry }) => void | Retry decision |
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
# โก vigor.all(tasks)
|
|
157
|
+
|
|
158
|
+
vigor.all(tasks: VigorAllTask<T>[])
|
|
159
|
+
|
|
160
|
+
## Methods
|
|
161
|
+
|
|
162
|
+
| Method | Type | Description |
|
|
163
|
+
|--------|------|-------------|
|
|
164
|
+
| target | (...tasks) => VigorAll | Set tasks |
|
|
165
|
+
| setting | (fn: (s: VigorAllSettings) => VigorAllSettings) => VigorAll | Concurrency config |
|
|
166
|
+
| interceptors | (fn: (i: VigorAllInterceptors) => VigorAllInterceptors) => VigorAll | Hooks |
|
|
167
|
+
| request | () => Promise<Array<T | Error>> | Execute all tasks |
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## โ๏ธ all().setting(s => s)
|
|
172
|
+
|
|
173
|
+
| Field | Type | Description |
|
|
174
|
+
|------|------|-------------|
|
|
175
|
+
| concurrency | number | Max parallel tasks |
|
|
176
|
+
| jitter | number | Delay randomness |
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## ๐งฉ all().interceptors(i => i)
|
|
181
|
+
|
|
182
|
+
| Hook | Signature | Description |
|
|
183
|
+
|------|----------|-------------|
|
|
184
|
+
| before | (ctx) => void | Before each task |
|
|
185
|
+
| after | (ctx, { setResult }) => void | After success |
|
|
186
|
+
| onError | (ctx, { setResult }) => void | Error handling |
|
|
187
|
+
| result | (ctx, { setResult }) => void | Final aggregation |
|
|
223
188
|
|
|
224
189
|
---
|
|
225
190
|
|
|
226
|
-
|
|
191
|
+
# ๐งช vigor.parse(response)
|
|
192
|
+
|
|
193
|
+
vigor.parse(response: Response)
|
|
227
194
|
|
|
228
|
-
|
|
229
|
-
|
|
195
|
+
| Method | Type | Description |
|
|
196
|
+
|--------|------|-------------|
|
|
197
|
+
| target | Response | Set response |
|
|
198
|
+
| original | boolean | Return raw response |
|
|
199
|
+
| type | keyof Response | Force parse type |
|
|
200
|
+
| request | () => Promise<T> | Execute parsing |
|
|
201
|
+
|
|
202
|
+
---
|
|
230
203
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
204
|
+
# ๐ vigor.fetch examples
|
|
205
|
+
|
|
206
|
+
## GET request
|
|
207
|
+
```ts
|
|
208
|
+
vigor.fetch("https://api.example.com")
|
|
209
|
+
.path("users", "profile")
|
|
210
|
+
.query({ id: 123 })
|
|
211
|
+
.method("GET")
|
|
212
|
+
.request()
|
|
213
|
+
```
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## POST request
|
|
217
|
+
```ts
|
|
218
|
+
vigor.fetch("https://api.example.com")
|
|
219
|
+
.path("users")
|
|
220
|
+
.method("POST")
|
|
221
|
+
.headers({
|
|
222
|
+
Authorization: "Bearer TOKEN"
|
|
237
223
|
})
|
|
238
|
-
.
|
|
239
|
-
|
|
240
|
-
|
|
224
|
+
.body({
|
|
225
|
+
name: "John",
|
|
226
|
+
age: 30
|
|
241
227
|
})
|
|
242
|
-
.request()
|
|
228
|
+
.request()
|
|
243
229
|
```
|
|
230
|
+
---
|
|
244
231
|
|
|
232
|
+
## fetch + retry + backoff + parse
|
|
233
|
+
```ts
|
|
234
|
+
vigor.fetch("https://api.example.com")
|
|
235
|
+
.path("data")
|
|
236
|
+
.retryConfig(r =>
|
|
237
|
+
r
|
|
238
|
+
.setting(s =>
|
|
239
|
+
s
|
|
240
|
+
.count(3)
|
|
241
|
+
.limit(5000)
|
|
242
|
+
)
|
|
243
|
+
.backoff(b =>
|
|
244
|
+
b
|
|
245
|
+
.factor(2)
|
|
246
|
+
.jitter(300)
|
|
247
|
+
)
|
|
248
|
+
)
|
|
249
|
+
.parseConfig(p =>
|
|
250
|
+
p.original(false)
|
|
251
|
+
)
|
|
252
|
+
.request()
|
|
253
|
+
```
|
|
245
254
|
---
|
|
246
255
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
256
|
+
# ๐ vigor.retry examples
|
|
257
|
+
|
|
258
|
+
## basic retry
|
|
259
|
+
```ts
|
|
260
|
+
vigor.retry(async (ctx, { signal }) => {
|
|
261
|
+
const res = await fetch("https://api.example.com/data", { signal })
|
|
262
|
+
if (!res.ok) throw new Error("failed")
|
|
263
|
+
return res.json()
|
|
264
|
+
})
|
|
265
|
+
.setting(s =>
|
|
266
|
+
s
|
|
267
|
+
.count(5)
|
|
268
|
+
.limit(3000)
|
|
269
|
+
)
|
|
270
|
+
.backoff(b =>
|
|
271
|
+
b
|
|
272
|
+
.baseDelay(500)
|
|
273
|
+
.factor(2)
|
|
274
|
+
)
|
|
275
|
+
.request()
|
|
268
276
|
```
|
|
277
|
+
---
|
|
269
278
|
|
|
279
|
+
## retryIf control
|
|
280
|
+
```ts
|
|
281
|
+
vigor.retry(async () => {
|
|
282
|
+
const res = await fetch("https://api.example.com")
|
|
283
|
+
return res.json()
|
|
284
|
+
})
|
|
285
|
+
.interceptors(i =>
|
|
286
|
+
i.retryIf((ctx, { cancelRetry }) => {
|
|
287
|
+
const result = ctx.runtime.result
|
|
288
|
+
|
|
289
|
+
if (result?.error === "fatal") {
|
|
290
|
+
cancelRetry()
|
|
291
|
+
}
|
|
292
|
+
})
|
|
293
|
+
)
|
|
294
|
+
.request()
|
|
295
|
+
```
|
|
270
296
|
---
|
|
271
297
|
|
|
272
|
-
##
|
|
298
|
+
## abort controller
|
|
299
|
+
```ts
|
|
300
|
+
const retry = vigor.retry(async (ctx, { signal }) => {
|
|
301
|
+
const res = await fetch("https://api.example.com", { signal })
|
|
302
|
+
return res.json()
|
|
303
|
+
})
|
|
273
304
|
|
|
274
|
-
|
|
305
|
+
const abort = retry.createController()
|
|
275
306
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
instance.fetch = (origin, config) =>
|
|
280
|
-
original(origin, config).headers({ Authorization: `Bearer ${options.token}` });
|
|
281
|
-
};
|
|
307
|
+
setTimeout(() => {
|
|
308
|
+
abort(new Error("manual abort"))
|
|
309
|
+
}, 2000)
|
|
282
310
|
|
|
283
|
-
|
|
311
|
+
await retry.request()
|
|
312
|
+
```
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
# โก vigor.all examples
|
|
316
|
+
```ts
|
|
317
|
+
vigor.all([
|
|
318
|
+
async () => fetch("https://api.com/a").then(r => r.json()),
|
|
319
|
+
async () => fetch("https://api.com/b").then(r => r.json()),
|
|
320
|
+
async () => fetch("https://api.com/c").then(r => r.json())
|
|
321
|
+
]).request()
|
|
322
|
+
```
|
|
323
|
+
---
|
|
324
|
+
```ts
|
|
325
|
+
vigor.all([
|
|
326
|
+
async () => "A",
|
|
327
|
+
async () => "B",
|
|
328
|
+
async () => "C",
|
|
329
|
+
async () => "D"
|
|
330
|
+
])
|
|
331
|
+
.setting(s =>
|
|
332
|
+
s
|
|
333
|
+
.concurrency(2)
|
|
334
|
+
.jitter(500)
|
|
335
|
+
)
|
|
336
|
+
.request()
|
|
337
|
+
```
|
|
338
|
+
---
|
|
339
|
+
```ts
|
|
340
|
+
vigor.all([
|
|
341
|
+
async () => "ok1",
|
|
342
|
+
async () => { throw new Error("fail") },
|
|
343
|
+
async () => "ok2"
|
|
344
|
+
]).request()
|
|
345
|
+
```
|
|
346
|
+
---
|
|
284
347
|
|
|
285
|
-
|
|
286
|
-
|
|
348
|
+
# ๐งช vigor.parse examples
|
|
349
|
+
```ts
|
|
350
|
+
const res = await fetch("https://api.com/data")
|
|
351
|
+
|
|
352
|
+
vigor.parse(res).request()
|
|
287
353
|
```
|
|
354
|
+
---
|
|
355
|
+
```ts
|
|
356
|
+
const img = await fetch("https://api.com/image.png")
|
|
357
|
+
|
|
358
|
+
vigor.parse(img)
|
|
359
|
+
.type("blob")
|
|
360
|
+
.request()
|
|
361
|
+
```
|
|
362
|
+
---
|
|
363
|
+
```ts
|
|
364
|
+
const raw = await fetch("https://api.com")
|
|
288
365
|
|
|
366
|
+
vigor.parse(raw)
|
|
367
|
+
.original(true)
|
|
368
|
+
.request()
|
|
369
|
+
```
|
|
370
|
+
---
|
|
371
|
+
|
|
372
|
+
# ๐ฅ full pipeline example
|
|
373
|
+
```ts
|
|
374
|
+
vigor.fetch("https://api.example.com")
|
|
375
|
+
.path("users")
|
|
376
|
+
.query({ page: 1 })
|
|
377
|
+
.method("GET")
|
|
378
|
+
.retryConfig(r =>
|
|
379
|
+
r
|
|
380
|
+
.setting(s =>
|
|
381
|
+
s
|
|
382
|
+
.count(3)
|
|
383
|
+
.limit(5000)
|
|
384
|
+
)
|
|
385
|
+
.backoff(b =>
|
|
386
|
+
b
|
|
387
|
+
.factor(2)
|
|
388
|
+
.jitter(200)
|
|
389
|
+
)
|
|
390
|
+
.interceptors(i =>
|
|
391
|
+
i.onRetry((ctx, { setDelay }) => {
|
|
392
|
+
setDelay(1000)
|
|
393
|
+
})
|
|
394
|
+
)
|
|
395
|
+
)
|
|
396
|
+
.parseConfig(p =>
|
|
397
|
+
p.original(false)
|
|
398
|
+
)
|
|
399
|
+
.interceptors(i =>
|
|
400
|
+
i.result(() => {
|
|
401
|
+
console.log("done")
|
|
402
|
+
})
|
|
403
|
+
)
|
|
404
|
+
.request()
|
|
405
|
+
```
|
|
289
406
|
---
|
package/package.json
CHANGED