vigor-fetch 4.0.8 → 4.0.10

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
@@ -40,16 +40,26 @@ var VigorStatus = class {
40
40
  */
41
41
  _merge(source, target, strategy) {
42
42
  const isObj = (v) => v !== null && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
43
+ const deepClone = (v) => {
44
+ if (Array.isArray(v)) return v.map(deepClone);
45
+ if (isObj(v)) {
46
+ const r = {};
47
+ for (const k of Object.keys(v)) r[k] = deepClone(v[k]);
48
+ return r;
49
+ }
50
+ return v;
51
+ };
43
52
  const merge = (a, b, s) => {
44
- if (b === void 0 || b === null) return a;
53
+ if (b === void 0 || b === null) return deepClone(a);
45
54
  if (s === "replace") return b;
46
55
  if (Array.isArray(a) && Array.isArray(b)) {
47
56
  return s === "replace" ? b : [...a, ...b];
48
57
  }
49
58
  if (isObj(a) && isObj(b)) {
50
- const r = { ...a };
59
+ const r = {};
51
60
  const sObj = isObj(s) ? s : void 0;
52
- for (const k of Object.keys(b)) {
61
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
62
+ for (const k of keys) {
53
63
  r[k] = merge(a[k], b[k], sObj?.[k]);
54
64
  }
55
65
  return r;
@@ -96,7 +106,10 @@ var VigorAllPolicyMiddlewaresBase = {
96
106
  __brand: createBrand("All", "Policy<Middlewares<Schema"),
97
107
  before: [],
98
108
  after: [],
99
- onError: []
109
+ onResult: [],
110
+ beforeEach: [],
111
+ afterEach: [],
112
+ onErrorEach: []
100
113
  };
101
114
  var VigorAllPolicyMiddlewares = class _VigorAllPolicyMiddlewares extends VigorStatus {
102
115
  constructor(config = VigorAllBase) {
@@ -105,17 +118,26 @@ var VigorAllPolicyMiddlewares = class _VigorAllPolicyMiddlewares extends VigorSt
105
118
  _create(config) {
106
119
  return new _VigorAllPolicyMiddlewares(config);
107
120
  }
121
+ before(type, func) {
122
+ return this._next({ policy: { middlewares: { before: [{ _mode: type, func }] } } });
123
+ }
124
+ after(type, func) {
125
+ return this._next({ policy: { middlewares: { after: [{ _mode: type, func }] } } });
126
+ }
127
+ onResult(type, func) {
128
+ return this._next({ policy: { middlewares: { onResult: [{ _mode: type, func }] } } });
129
+ }
108
130
  /** Registers a middleware that runs before each task is invoked. */
109
- before(func) {
110
- return this._next({ policy: { middlewares: { before: [func] } } });
131
+ beforeEach(func) {
132
+ return this._next({ policy: { middlewares: { beforeEach: [func] } } });
111
133
  }
112
134
  /** Registers a middleware that runs after each task settles successfully. */
113
- after(func) {
114
- return this._next({ policy: { middlewares: { after: [func] } } });
135
+ afterEach(func) {
136
+ return this._next({ policy: { middlewares: { afterEach: [func] } } });
115
137
  }
116
138
  /** Registers a middleware that runs when an individual task fails. */
117
- onError(func) {
118
- return this._next({ policy: { middlewares: { onError: [func] } } });
139
+ onErrorEach(func) {
140
+ return this._next({ policy: { middlewares: { onErrorEach: [func] } } });
119
141
  }
120
142
  };
121
143
 
@@ -200,7 +222,6 @@ var VigorAll = class _VigorAll extends VigorStatus {
200
222
  target(...funcs) {
201
223
  return this._next({ policy: { target: funcs } });
202
224
  }
203
- /** Configures the all-policy's general settings (concurrency, onlySuccess). */
204
225
  settings(input) {
205
226
  if (typeof input === "function" || input instanceof VigorAllPolicySettings) {
206
227
  const result = typeof input === "function" ? input(new VigorAllPolicySettings()) : input;
@@ -210,7 +231,6 @@ var VigorAll = class _VigorAll extends VigorStatus {
210
231
  }
211
232
  return this._next({ policy: { settings: input } });
212
233
  }
213
- /** Configures the all-policy's per-task middleware pipeline. */
214
234
  middlewares(input) {
215
235
  if (typeof input === "function" || input instanceof VigorAllPolicyMiddlewares) {
216
236
  const result = typeof input === "function" ? input(new VigorAllPolicyMiddlewares()) : input;
@@ -220,13 +240,13 @@ var VigorAll = class _VigorAll extends VigorStatus {
220
240
  }
221
241
  return this._next({ policy: { middlewares: input } });
222
242
  }
223
- /** Runs a single task through the before/after/onError middleware pipeline. */
243
+ /** Runs a single task through the beforeEach/afterEach/onErrorEach middleware pipeline. */
224
244
  async _runTask(task, policy) {
225
- let ctx = { ...VigorAllEachContextBase };
245
+ let ctx = this._merge(VigorAllEachContextBase, {});
226
246
  try {
227
- for (const before of policy.middlewares.before) await before(ctx);
247
+ for (const beforeEach of policy.middlewares.beforeEach) await beforeEach(ctx);
228
248
  ctx.result = await task();
229
- for (const after of policy.middlewares.after) await after(ctx);
249
+ for (const afterEach of policy.middlewares.afterEach) await afterEach(ctx);
230
250
  return { success: true, value: ctx.result };
231
251
  } catch (error) {
232
252
  ctx.error = error;
@@ -234,7 +254,7 @@ var VigorAll = class _VigorAll extends VigorStatus {
234
254
  ctx.flags.overrided = true;
235
255
  ctx.result = r;
236
256
  };
237
- for (const onError of policy.middlewares.onError) await onError(ctx, { setResult });
257
+ for (const onErrorEach of policy.middlewares.onErrorEach) await onErrorEach(ctx, { setResult });
238
258
  if (ctx.flags.overrided) return { success: true, value: ctx.result };
239
259
  return { success: false, value: error };
240
260
  }
@@ -261,15 +281,32 @@ var VigorAll = class _VigorAll extends VigorStatus {
261
281
  async requestVerbose() {
262
282
  return await this.requestRuntime();
263
283
  }
264
- /** Internal execution loop implementing the bounded-concurrency worker pool. */
265
- async requestRuntime(restartAttempt = 1) {
284
+ /** Internal execution loop implementing the bounded-concurrency worker pool and the batch-level before/after/onResult pipeline. */
285
+ async requestRuntime() {
266
286
  const config = this._merge(this.config, {});
267
287
  const policy = config.policy;
288
+ let ctx = this._merge(VigorAllContextBase, {
289
+ policy
290
+ });
291
+ const setResult = (r) => {
292
+ ctx = { ...ctx, result: r };
293
+ };
294
+ const throwError = (err) => {
295
+ throw err;
296
+ };
268
297
  try {
269
298
  if (policy.target.length === 0) throw new VigorAllError("EMPTY_TARGET", {
270
299
  method: "request",
271
- data: {}
300
+ data: {},
301
+ context: ctx
272
302
  });
303
+ for (const mw of policy.middlewares.before) {
304
+ if (mw._mode === "fluent") {
305
+ await mw.func(ctx);
306
+ } else {
307
+ ctx = await mw.func(ctx, { throwError });
308
+ }
309
+ }
273
310
  const concurrency = Math.max(1, policy.settings.concurrency);
274
311
  const results = new Array(policy.target.length);
275
312
  let cursor = 0;
@@ -283,10 +320,24 @@ var VigorAll = class _VigorAll extends VigorStatus {
283
320
  await Promise.all(
284
321
  Array.from({ length: Math.min(concurrency, policy.target.length) }, () => worker())
285
322
  );
286
- const result = policy.settings.onlySuccess ? results.filter((r) => r.success).map((r) => r.value) : results.map((r) => r.value);
323
+ ctx.result = policy.settings.onlySuccess ? results.filter((r) => r.success).map((r) => r.value) : results.map((r) => r.value);
324
+ for (const mw of policy.middlewares.after) {
325
+ if (mw._mode === "fluent") {
326
+ await mw.func(ctx);
327
+ } else {
328
+ ctx = await mw.func(ctx, { setResult, throwError });
329
+ }
330
+ }
331
+ for (const mw of policy.middlewares.onResult) {
332
+ if (mw._mode === "fluent") {
333
+ ctx.result = await mw.func(ctx.result);
334
+ } else {
335
+ ctx = await mw.func(ctx, { setResult, throwError });
336
+ }
337
+ }
287
338
  return {
288
339
  success: true,
289
- data: result,
340
+ data: ctx.result,
290
341
  error: null
291
342
  };
292
343
  } catch (error) {
@@ -744,7 +795,6 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
744
795
  policy: { abortSignals: sig }
745
796
  });
746
797
  }
747
- /** Configures the retry policy's general settings (max attempts, timeout, etc). */
748
798
  settings(input) {
749
799
  if (typeof input === "function" || input instanceof VigorRetryPolicySettings) {
750
800
  const result = typeof input === "function" ? input(new VigorRetryPolicySettings()) : input;
@@ -756,7 +806,6 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
756
806
  policy: { settings: input }
757
807
  });
758
808
  }
759
- /** Configures the retry policy's middleware pipeline. */
760
809
  middlewares(input) {
761
810
  if (typeof input === "function" || input instanceof VigorRetryPolicyMiddlewares) {
762
811
  const result = typeof input === "function" ? input(new VigorRetryPolicyMiddlewares()) : input;
@@ -768,7 +817,6 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
768
817
  policy: { middlewares: input }
769
818
  });
770
819
  }
771
- /** Configures the retry policy's delay algorithm. */
772
820
  algorithms(input) {
773
821
  if (typeof input === "function") {
774
822
  const result = input(new VigorRetryPolicyAlgorithms());
@@ -806,10 +854,9 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
806
854
  /** Internal execution loop implementing the attempt/retry/restart lifecycle. */
807
855
  async requestRuntime(restartAttempt = 1) {
808
856
  const config = this._merge(this.config, {});
809
- let ctx = {
810
- ...VigorRetryContextBase,
857
+ let ctx = this._merge(VigorRetryContextBase, {
811
858
  policy: config.policy
812
- };
859
+ });
813
860
  if (isVigorEmpty(ctx.policy.target)) throw new VigorRetryError("VALUE_REQUIRED", {
814
861
  method: "request",
815
862
  data: {
@@ -863,7 +910,8 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
863
910
  method: "request",
864
911
  data: {
865
912
  limit: ctx.policy.settings.timeout
866
- }
913
+ },
914
+ context: ctx
867
915
  })), ctx.policy.settings.timeout);
868
916
  ctx.result = await Promise.race([
869
917
  target(mergedSignals),
@@ -945,7 +993,8 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
945
993
  data: {
946
994
  maxAttempts: ctx.policy.settings.maxAttempts,
947
995
  error: ctx.error
948
- }
996
+ },
997
+ context: ctx
949
998
  });
950
999
  } catch (error) {
951
1000
  ctx.error = error;
@@ -1233,7 +1282,6 @@ var VigorParse = class _VigorParse extends VigorStatus {
1233
1282
  target(response) {
1234
1283
  return this._next({ policy: { target: response } });
1235
1284
  }
1236
- /** Configures the parse policy's general settings. */
1237
1285
  settings(input) {
1238
1286
  if (typeof input === "function" || input instanceof VigorParsePolicySettings) {
1239
1287
  const result = typeof input === "function" ? input(new VigorParsePolicySettings()) : input;
@@ -1243,7 +1291,6 @@ var VigorParse = class _VigorParse extends VigorStatus {
1243
1291
  }
1244
1292
  return this._next({ policy: { settings: input } });
1245
1293
  }
1246
- /** Configures the parse policy's middleware pipeline. */
1247
1294
  middlewares(input) {
1248
1295
  if (typeof input === "function" || input instanceof VigorParsePolicyMiddlewares) {
1249
1296
  const result = typeof input === "function" ? input(new VigorParsePolicyMiddlewares()) : input;
@@ -1253,7 +1300,6 @@ var VigorParse = class _VigorParse extends VigorStatus {
1253
1300
  }
1254
1301
  return this._next({ policy: { middlewares: input } });
1255
1302
  }
1256
- /** Configures the parse policy's fallback chain of parsing strategies. */
1257
1303
  strategies(input) {
1258
1304
  if (typeof input === "function" || input instanceof VigorParsePolicyStrategies) {
1259
1305
  const result = typeof input === "function" ? input(new VigorParsePolicyStrategies()) : input;
@@ -1288,10 +1334,9 @@ var VigorParse = class _VigorParse extends VigorStatus {
1288
1334
  /** Internal execution loop implementing the strategy fallback and restart handling. */
1289
1335
  async requestRuntime(restartAttempt = 1) {
1290
1336
  const config = this._merge(this.config, {});
1291
- let ctx = {
1292
- ...VigorParseContextBase,
1337
+ let ctx = this._merge(VigorParseContextBase, {
1293
1338
  policy: config.policy
1294
- };
1339
+ });
1295
1340
  if (isVigorDefault(ctx.policy.target)) throw new VigorParseError("VALUE_REQUIRED", {
1296
1341
  method: "request",
1297
1342
  data: { key: "target" },
@@ -1309,10 +1354,10 @@ var VigorParse = class _VigorParse extends VigorStatus {
1309
1354
  ctx = await middleware.func(ctx, { throwError });
1310
1355
  }
1311
1356
  }
1312
- const response = ctx.response;
1313
1357
  if (ctx.policy.settings.raw) {
1314
- ctx.result = response;
1358
+ ctx.result = ctx.response;
1315
1359
  } else {
1360
+ const response = ctx.response;
1316
1361
  const funcs = ctx.policy.strategies.funcs.length > 0 ? ctx.policy.strategies.funcs : [VigorParseContentTypeStrategy];
1317
1362
  let parsed = false;
1318
1363
  for (const [i, func] of funcs.entries()) {
@@ -1376,7 +1421,8 @@ var VigorParse = class _VigorParse extends VigorStatus {
1376
1421
  data: {
1377
1422
  maxAttempts: ctx.policy.settings.maxRestarts,
1378
1423
  error: ctx.error
1379
- }
1424
+ },
1425
+ context: ctx
1380
1426
  });
1381
1427
  return await this.requestRuntime(restartAttempt + 1);
1382
1428
  }
@@ -1428,7 +1474,7 @@ var VigorFetchContextBase = {
1428
1474
  error: VigorDefault,
1429
1475
  options: VigorDefault,
1430
1476
  flags: {
1431
- overrided: false,
1477
+ overridden: false,
1432
1478
  restarted: false
1433
1479
  },
1434
1480
  record: {},
@@ -1525,7 +1571,6 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1525
1571
  if (mode === "overwrite") return this._next({ policy: { extra: obj } }, { policy: { extra: "replace" } });
1526
1572
  return this._next({ policy: { extra: obj } });
1527
1573
  }
1528
- /** Configures the fetch policy's general settings. */
1529
1574
  settings(input) {
1530
1575
  if (typeof input === "function" || input instanceof VigorFetchPolicySettings) {
1531
1576
  const result = typeof input === "function" ? input(new VigorFetchPolicySettings()) : input;
@@ -1535,7 +1580,6 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1535
1580
  }
1536
1581
  return this._next({ policy: { settings: input } });
1537
1582
  }
1538
- /** Configures the fetch policy's middleware pipeline. */
1539
1583
  middlewares(input) {
1540
1584
  if (typeof input === "function" || input instanceof VigorFetchPolicyMiddlewares) {
1541
1585
  const result = typeof input === "function" ? input(new VigorFetchPolicyMiddlewares()) : input;
@@ -1545,7 +1589,6 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1545
1589
  }
1546
1590
  return this._next({ policy: { middlewares: input } });
1547
1591
  }
1548
- /** Configures the retry engine used to send the underlying request. */
1549
1592
  retry(input) {
1550
1593
  if (typeof input === "function" || input instanceof VigorRetry) {
1551
1594
  const result = typeof input === "function" ? input(new VigorRetry()) : input;
@@ -1553,7 +1596,6 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1553
1596
  }
1554
1597
  return this._next({ policy: { retry: input } }, { policy: { retry: "replace" } });
1555
1598
  }
1556
- /** Configures the parse engine used to interpret the response. */
1557
1599
  parse(input) {
1558
1600
  if (typeof input === "function" || input instanceof VigorParse) {
1559
1601
  const result = typeof input === "function" ? input(new VigorParse()) : input;
@@ -1651,17 +1693,17 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1651
1693
  /** Internal execution loop implementing URL building, retry, parsing, and restart handling. */
1652
1694
  async requestRuntime(restartAttempt = 1) {
1653
1695
  const config = this._merge(this.config, {});
1654
- let ctx = {
1655
- ...VigorFetchContextBase,
1696
+ let ctx = this._merge(VigorFetchContextBase, {
1656
1697
  policy: config.policy
1657
- };
1698
+ });
1658
1699
  const throwError = (err) => {
1659
1700
  throw err;
1660
1701
  };
1661
1702
  try {
1662
1703
  if (isVigorEmpty(ctx.policy.origin)) throw new VigorFetchError("VALUE_REQUIRED", {
1663
1704
  method: "request",
1664
- data: { key: "origin" }
1705
+ data: { key: "origin" },
1706
+ context: ctx
1665
1707
  });
1666
1708
  ctx.href = this._buildUrl(ctx.policy.origin, ctx.policy.path, ctx.policy.query, ctx.policy.hash);
1667
1709
  const hasBody = !isVigorEmpty(ctx.policy.body) && ctx.policy.body !== void 0;
@@ -1773,10 +1815,10 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1773
1815
  };
1774
1816
  } catch (error) {
1775
1817
  ctx.error = error;
1776
- ctx.flags.overrided = false;
1818
+ ctx.flags.overridden = false;
1777
1819
  ctx.flags.restarted = false;
1778
1820
  const setResult = (r) => {
1779
- ctx.flags.overrided = true;
1821
+ ctx.flags.overridden = true;
1780
1822
  ctx.result = r;
1781
1823
  };
1782
1824
  const proceedRestart = () => ctx.flags.restarted = true;
@@ -1795,11 +1837,12 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1795
1837
  data: {
1796
1838
  maxAttempts: ctx.policy.settings.maxRestarts,
1797
1839
  error: ctx.error
1798
- }
1840
+ },
1841
+ context: ctx
1799
1842
  });
1800
1843
  return await this.requestRuntime(restartAttempt + 1);
1801
1844
  }
1802
- if (ctx.flags.overrided) return {
1845
+ if (ctx.flags.overridden) return {
1803
1846
  success: true,
1804
1847
  data: ctx.result,
1805
1848
  error: null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vigor-fetch",
3
- "version": "4.0.8",
3
+ "version": "4.0.10",
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"
@@ -41,7 +42,7 @@
41
42
  "author": "Uav1010",
42
43
  "license": "MIT",
43
44
  "engines": {
44
- "node": ">=18.0.0"
45
+ "node": ">=18.17.0"
45
46
  },
46
47
  "devDependencies": {
47
48
  "tsup": "^8.5.1",