@npora/request 1.3.0 → 1.5.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 CHANGED
@@ -115,6 +115,7 @@ api.optionsResponse(url, config)
115
115
  import {
116
116
  authPlugin,
117
117
  cachePlugin,
118
+ circuitBreakerPlugin,
118
119
  createClient,
119
120
  retryPlugin
120
121
  } from '@npora/request'
@@ -127,6 +128,7 @@ const request = createClient()
127
128
  jitter: true,
128
129
  maxElapsedTime: 10000
129
130
  }))
131
+ .use(circuitBreakerPlugin())
130
132
  .use(cache)
131
133
  .use(authPlugin({
132
134
  token: () => accessToken,
@@ -160,6 +162,7 @@ Built-in plugins:
160
162
 
161
163
  - `retryPlugin()`
162
164
  - `cachePlugin()`
165
+ - `circuitBreakerPlugin()`
163
166
  - `authPlugin()`
164
167
  - `loggerPlugin()`
165
168
  - `uploadPlugin()`
@@ -168,6 +171,23 @@ Built-in plugins:
168
171
  Plugin-owned request options belong under `extensions`. Third-party plugins can
169
172
  augment `RequestExtensions` through TypeScript module augmentation.
170
173
 
174
+ Protect a failing upstream after retries are exhausted:
175
+
176
+ ```ts
177
+ const breaker = circuitBreakerPlugin({
178
+ failureThreshold: 5,
179
+ resetTimeout: 30000
180
+ })
181
+
182
+ const request = createClient()
183
+ .use(retryPlugin({ retries: 2 }))
184
+ .use(breaker)
185
+ ```
186
+
187
+ Circuits are isolated by request origin by default. Open circuits reject with
188
+ the stable `CIRCUIT_OPEN` error code and permit a bounded half-open probe after
189
+ the recovery window.
190
+
171
191
  Inject a structured logger when request correlation and timing are needed:
172
192
 
173
193
  ```ts
@@ -200,6 +220,24 @@ api.interceptors.error.use(error => error)
200
220
  An optional numeric priority controls execution order. Higher priorities run
201
221
  first; equal priorities preserve registration order.
202
222
 
223
+ ## Request testing
224
+
225
+ `MockAdapter` supports method-aware routes, dynamic or one-time responses,
226
+ query/header matching, delay, timeout and network-error simulation, and request
227
+ history:
228
+
229
+ ```ts
230
+ const adapter = new MockAdapter()
231
+
232
+ adapter
233
+ .onGet('/users/1')
234
+ .replyOnce(503, { message: 'busy' })
235
+ .onGet('/users/1')
236
+ .reply(200, { id: 1, name: 'Npora' })
237
+
238
+ const api = createClient({ adapter })
239
+ ```
240
+
203
241
  ## Errors
204
242
 
205
243
  ```ts
@@ -224,6 +262,7 @@ Stable request error codes:
224
262
  - `TIMEOUT_ERROR`
225
263
  - `ABORT_ERROR`
226
264
  - `PARSER_ERROR`
265
+ - `CIRCUIT_OPEN`
227
266
 
228
267
  ## Documentation
229
268