vigor-fetch 4.0.7 → 4.0.9

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/dist/index.js CHANGED
@@ -96,7 +96,10 @@ var VigorAllPolicyMiddlewaresBase = {
96
96
  __brand: createBrand("All", "Policy<Middlewares<Schema"),
97
97
  before: [],
98
98
  after: [],
99
- onError: []
99
+ onResult: [],
100
+ beforeEach: [],
101
+ afterEach: [],
102
+ onErrorEach: []
100
103
  };
101
104
  var VigorAllPolicyMiddlewares = class _VigorAllPolicyMiddlewares extends VigorStatus {
102
105
  constructor(config = VigorAllBase) {
@@ -105,17 +108,26 @@ var VigorAllPolicyMiddlewares = class _VigorAllPolicyMiddlewares extends VigorSt
105
108
  _create(config) {
106
109
  return new _VigorAllPolicyMiddlewares(config);
107
110
  }
111
+ before(type, func) {
112
+ return this._next({ policy: { middlewares: { before: [{ _mode: type, func }] } } });
113
+ }
114
+ after(type, func) {
115
+ return this._next({ policy: { middlewares: { after: [{ _mode: type, func }] } } });
116
+ }
117
+ onResult(type, func) {
118
+ return this._next({ policy: { middlewares: { onResult: [{ _mode: type, func }] } } });
119
+ }
108
120
  /** Registers a middleware that runs before each task is invoked. */
109
- before(func) {
110
- return this._next({ policy: { middlewares: { before: [func] } } });
121
+ beforeEach(func) {
122
+ return this._next({ policy: { middlewares: { beforeEach: [func] } } });
111
123
  }
112
124
  /** Registers a middleware that runs after each task settles successfully. */
113
- after(func) {
114
- return this._next({ policy: { middlewares: { after: [func] } } });
125
+ afterEach(func) {
126
+ return this._next({ policy: { middlewares: { afterEach: [func] } } });
115
127
  }
116
128
  /** Registers a middleware that runs when an individual task fails. */
117
- onError(func) {
118
- return this._next({ policy: { middlewares: { onError: [func] } } });
129
+ onErrorEach(func) {
130
+ return this._next({ policy: { middlewares: { onErrorEach: [func] } } });
119
131
  }
120
132
  };
121
133
 
@@ -200,7 +212,6 @@ var VigorAll = class _VigorAll extends VigorStatus {
200
212
  target(...funcs) {
201
213
  return this._next({ policy: { target: funcs } });
202
214
  }
203
- /** Configures the all-policy's general settings (concurrency, onlySuccess). */
204
215
  settings(input) {
205
216
  if (typeof input === "function" || input instanceof VigorAllPolicySettings) {
206
217
  const result = typeof input === "function" ? input(new VigorAllPolicySettings()) : input;
@@ -210,7 +221,6 @@ var VigorAll = class _VigorAll extends VigorStatus {
210
221
  }
211
222
  return this._next({ policy: { settings: input } });
212
223
  }
213
- /** Configures the all-policy's per-task middleware pipeline. */
214
224
  middlewares(input) {
215
225
  if (typeof input === "function" || input instanceof VigorAllPolicyMiddlewares) {
216
226
  const result = typeof input === "function" ? input(new VigorAllPolicyMiddlewares()) : input;
@@ -220,13 +230,13 @@ var VigorAll = class _VigorAll extends VigorStatus {
220
230
  }
221
231
  return this._next({ policy: { middlewares: input } });
222
232
  }
223
- /** Runs a single task through the before/after/onError middleware pipeline. */
233
+ /** Runs a single task through the beforeEach/afterEach/onErrorEach middleware pipeline. */
224
234
  async _runTask(task, policy) {
225
235
  let ctx = { ...VigorAllEachContextBase };
226
236
  try {
227
- for (const before of policy.middlewares.before) await before(ctx);
237
+ for (const beforeEach of policy.middlewares.beforeEach) await beforeEach(ctx);
228
238
  ctx.result = await task();
229
- for (const after of policy.middlewares.after) await after(ctx);
239
+ for (const afterEach of policy.middlewares.afterEach) await afterEach(ctx);
230
240
  return { success: true, value: ctx.result };
231
241
  } catch (error) {
232
242
  ctx.error = error;
@@ -234,7 +244,7 @@ var VigorAll = class _VigorAll extends VigorStatus {
234
244
  ctx.flags.overrided = true;
235
245
  ctx.result = r;
236
246
  };
237
- for (const onError of policy.middlewares.onError) await onError(ctx, { setResult });
247
+ for (const onErrorEach of policy.middlewares.onErrorEach) await onErrorEach(ctx, { setResult });
238
248
  if (ctx.flags.overrided) return { success: true, value: ctx.result };
239
249
  return { success: false, value: error };
240
250
  }
@@ -261,15 +271,29 @@ var VigorAll = class _VigorAll extends VigorStatus {
261
271
  async requestVerbose() {
262
272
  return await this.requestRuntime();
263
273
  }
264
- /** Internal execution loop implementing the bounded-concurrency worker pool. */
274
+ /** Internal execution loop implementing the bounded-concurrency worker pool and the batch-level before/after/onResult pipeline. */
265
275
  async requestRuntime(restartAttempt = 1) {
266
276
  const config = this._merge(this.config, {});
267
277
  const policy = config.policy;
278
+ let ctx = { ...VigorAllContextBase, policy };
279
+ const setResult = (r) => {
280
+ ctx = { ...ctx, result: r };
281
+ };
282
+ const throwError = (err) => {
283
+ throw err;
284
+ };
268
285
  try {
269
286
  if (policy.target.length === 0) throw new VigorAllError("EMPTY_TARGET", {
270
287
  method: "request",
271
288
  data: {}
272
289
  });
290
+ for (const mw of policy.middlewares.before) {
291
+ if (mw._mode === "fluent") {
292
+ await mw.func(ctx);
293
+ } else {
294
+ ctx = await mw.func(ctx, { throwError });
295
+ }
296
+ }
273
297
  const concurrency = Math.max(1, policy.settings.concurrency);
274
298
  const results = new Array(policy.target.length);
275
299
  let cursor = 0;
@@ -283,10 +307,24 @@ var VigorAll = class _VigorAll extends VigorStatus {
283
307
  await Promise.all(
284
308
  Array.from({ length: Math.min(concurrency, policy.target.length) }, () => worker())
285
309
  );
286
- const result = policy.settings.onlySuccess ? results.filter((r) => r.success).map((r) => r.value) : results.map((r) => r.value);
310
+ ctx.result = policy.settings.onlySuccess ? results.filter((r) => r.success).map((r) => r.value) : results.map((r) => r.value);
311
+ for (const mw of policy.middlewares.after) {
312
+ if (mw._mode === "fluent") {
313
+ await mw.func(ctx);
314
+ } else {
315
+ ctx = await mw.func(ctx, { setResult, throwError });
316
+ }
317
+ }
318
+ for (const mw of policy.middlewares.onResult) {
319
+ if (mw._mode === "fluent") {
320
+ ctx.result = await mw.func(ctx.result);
321
+ } else {
322
+ ctx = await mw.func(ctx, { setResult, throwError });
323
+ }
324
+ }
287
325
  return {
288
326
  success: true,
289
- data: result,
327
+ data: ctx.result,
290
328
  error: null
291
329
  };
292
330
  } catch (error) {
@@ -744,7 +782,6 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
744
782
  policy: { abortSignals: sig }
745
783
  });
746
784
  }
747
- /** Configures the retry policy's general settings (max attempts, timeout, etc). */
748
785
  settings(input) {
749
786
  if (typeof input === "function" || input instanceof VigorRetryPolicySettings) {
750
787
  const result = typeof input === "function" ? input(new VigorRetryPolicySettings()) : input;
@@ -756,7 +793,6 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
756
793
  policy: { settings: input }
757
794
  });
758
795
  }
759
- /** Configures the retry policy's middleware pipeline. */
760
796
  middlewares(input) {
761
797
  if (typeof input === "function" || input instanceof VigorRetryPolicyMiddlewares) {
762
798
  const result = typeof input === "function" ? input(new VigorRetryPolicyMiddlewares()) : input;
@@ -768,7 +804,6 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
768
804
  policy: { middlewares: input }
769
805
  });
770
806
  }
771
- /** Configures the retry policy's delay algorithm. */
772
807
  algorithms(input) {
773
808
  if (typeof input === "function") {
774
809
  const result = input(new VigorRetryPolicyAlgorithms());
@@ -1233,7 +1268,6 @@ var VigorParse = class _VigorParse extends VigorStatus {
1233
1268
  target(response) {
1234
1269
  return this._next({ policy: { target: response } });
1235
1270
  }
1236
- /** Configures the parse policy's general settings. */
1237
1271
  settings(input) {
1238
1272
  if (typeof input === "function" || input instanceof VigorParsePolicySettings) {
1239
1273
  const result = typeof input === "function" ? input(new VigorParsePolicySettings()) : input;
@@ -1243,7 +1277,6 @@ var VigorParse = class _VigorParse extends VigorStatus {
1243
1277
  }
1244
1278
  return this._next({ policy: { settings: input } });
1245
1279
  }
1246
- /** Configures the parse policy's middleware pipeline. */
1247
1280
  middlewares(input) {
1248
1281
  if (typeof input === "function" || input instanceof VigorParsePolicyMiddlewares) {
1249
1282
  const result = typeof input === "function" ? input(new VigorParsePolicyMiddlewares()) : input;
@@ -1253,7 +1286,6 @@ var VigorParse = class _VigorParse extends VigorStatus {
1253
1286
  }
1254
1287
  return this._next({ policy: { middlewares: input } });
1255
1288
  }
1256
- /** Configures the parse policy's fallback chain of parsing strategies. */
1257
1289
  strategies(input) {
1258
1290
  if (typeof input === "function" || input instanceof VigorParsePolicyStrategies) {
1259
1291
  const result = typeof input === "function" ? input(new VigorParsePolicyStrategies()) : input;
@@ -1309,10 +1341,10 @@ var VigorParse = class _VigorParse extends VigorStatus {
1309
1341
  ctx = await middleware.func(ctx, { throwError });
1310
1342
  }
1311
1343
  }
1312
- const response = ctx.response;
1313
1344
  if (ctx.policy.settings.raw) {
1314
- ctx.result = response;
1345
+ ctx.result = ctx.response;
1315
1346
  } else {
1347
+ const response = ctx.response;
1316
1348
  const funcs = ctx.policy.strategies.funcs.length > 0 ? ctx.policy.strategies.funcs : [VigorParseContentTypeStrategy];
1317
1349
  let parsed = false;
1318
1350
  for (const [i, func] of funcs.entries()) {
@@ -1428,7 +1460,7 @@ var VigorFetchContextBase = {
1428
1460
  error: VigorDefault,
1429
1461
  options: VigorDefault,
1430
1462
  flags: {
1431
- overrided: false,
1463
+ overridden: false,
1432
1464
  restarted: false
1433
1465
  },
1434
1466
  record: {},
@@ -1525,7 +1557,6 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1525
1557
  if (mode === "overwrite") return this._next({ policy: { extra: obj } }, { policy: { extra: "replace" } });
1526
1558
  return this._next({ policy: { extra: obj } });
1527
1559
  }
1528
- /** Configures the fetch policy's general settings. */
1529
1560
  settings(input) {
1530
1561
  if (typeof input === "function" || input instanceof VigorFetchPolicySettings) {
1531
1562
  const result = typeof input === "function" ? input(new VigorFetchPolicySettings()) : input;
@@ -1535,7 +1566,6 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1535
1566
  }
1536
1567
  return this._next({ policy: { settings: input } });
1537
1568
  }
1538
- /** Configures the fetch policy's middleware pipeline. */
1539
1569
  middlewares(input) {
1540
1570
  if (typeof input === "function" || input instanceof VigorFetchPolicyMiddlewares) {
1541
1571
  const result = typeof input === "function" ? input(new VigorFetchPolicyMiddlewares()) : input;
@@ -1545,7 +1575,6 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1545
1575
  }
1546
1576
  return this._next({ policy: { middlewares: input } });
1547
1577
  }
1548
- /** Configures the retry engine used to send the underlying request. */
1549
1578
  retry(input) {
1550
1579
  if (typeof input === "function" || input instanceof VigorRetry) {
1551
1580
  const result = typeof input === "function" ? input(new VigorRetry()) : input;
@@ -1553,7 +1582,6 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1553
1582
  }
1554
1583
  return this._next({ policy: { retry: input } }, { policy: { retry: "replace" } });
1555
1584
  }
1556
- /** Configures the parse engine used to interpret the response. */
1557
1585
  parse(input) {
1558
1586
  if (typeof input === "function" || input instanceof VigorParse) {
1559
1587
  const result = typeof input === "function" ? input(new VigorParse()) : input;
@@ -1773,10 +1801,10 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1773
1801
  };
1774
1802
  } catch (error) {
1775
1803
  ctx.error = error;
1776
- ctx.flags.overrided = false;
1804
+ ctx.flags.overridden = false;
1777
1805
  ctx.flags.restarted = false;
1778
1806
  const setResult = (r) => {
1779
- ctx.flags.overrided = true;
1807
+ ctx.flags.overridden = true;
1780
1808
  ctx.result = r;
1781
1809
  };
1782
1810
  const proceedRestart = () => ctx.flags.restarted = true;
@@ -1799,7 +1827,7 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1799
1827
  });
1800
1828
  return await this.requestRuntime(restartAttempt + 1);
1801
1829
  }
1802
- if (ctx.flags.overrided) return {
1830
+ if (ctx.flags.overridden) return {
1803
1831
  success: true,
1804
1832
  data: ctx.result,
1805
1833
  error: null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vigor-fetch",
3
- "version": "4.0.7",
3
+ "version": "4.0.9",
4
4
  "description": "Smart, zero-dependency HTTP client with self-healing retries for rate-limited servers.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -21,7 +21,8 @@
21
21
  },
22
22
  "scripts": {
23
23
  "build": "tsup",
24
- "test": "node test.js"
24
+ "test": "node test.js",
25
+ "typecheck": "tsc --noEmit"
25
26
  },
26
27
  "files": [
27
28
  "dist"