vigor-fetch 4.0.9 → 4.0.11

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
@@ -90,7 +90,7 @@ const results = await vigor
90
90
  ```ts
91
91
  vigor.fetch(origin?) // -> VigorFetch, origin pre-set if given
92
92
  vigor.retry(task?) // -> VigorRetry, target pre-set if given
93
- vigor.parse(response?) // -> VigorParse, target pre-set if given
93
+ vigor.parse(request|response?) // -> VigorParse, target pre-set if given
94
94
  vigor.all(...tasks) // -> VigorAll, target pre-set if any tasks given
95
95
  vigor.use(plugin, options?)
96
96
  vigor.builders // direct access to each module's sub-builders
@@ -147,7 +147,7 @@ This is equivalent to `new VigorFetchPolicySettings()` etc., just namespaced und
147
147
  | `settings(fn \| config)` | Configures fetch-level settings (below) |
148
148
  | `middlewares(fn \| config)` | Configures the fetch middleware pipeline (below) |
149
149
  | `retry(fn \| config \| VigorRetry)` | Configures the **internal retry engine** used to send the request |
150
- | `parse(fn \| config \| VigorParse)` | Configures the **internal parse engine** used to interpret the response |
150
+ | `parse(fn \| config \| VigorParse)` | Configures the **internal parse engine** used to interpret the request or response |
151
151
  | `cast<T>()` | Overrides the inferred result type with `T` |
152
152
  | `request()` | Executes and returns the parsed result, throwing on failure |
153
153
  | `requestVerbose()` | Executes and returns `{ success, data, error }` |
@@ -347,7 +347,7 @@ There are two independent resilience layers here:
347
347
 
348
348
  | Method | Description |
349
349
  |---|---|
350
- | `target(response)` | Sets the `Response` to parse |
350
+ | `target(request\|response)` | Sets the `Request\|Response` to parse |
351
351
  | `settings(fn \| config)` | General settings (below) |
352
352
  | `middlewares(fn \| config)` | Middleware pipeline (below) |
353
353
  | `strategies(fn \| config)` | Configures the ordered fallback chain of parsing strategies |
@@ -358,13 +358,13 @@ There are two independent resilience layers here:
358
358
 
359
359
  | Method | Description | Default |
360
360
  |---|---|---|
361
- | `raw(bool)` | When `true`, skips parsing entirely and returns the raw `Response` object | `false` |
361
+ | `raw(bool)` | When `true`, skips parsing entirely and returns the raw `Request\|Response` object | `false` |
362
362
  | `default(fn)` | Fallback value/factory when parsing ultimately fails | throws |
363
363
  | `maxRestarts(num)` | Max full pipeline restarts | `3` |
364
364
 
365
365
  ### strategies
366
366
 
367
- Strategies form an ordered list that's tried in sequence until one succeeds (each attempt but the last clones the response first, so a failed strategy doesn't consume the body). If you never configure any, Vigor defaults to content-type detection.
367
+ Strategies form an ordered list that's tried in sequence until one succeeds (each attempt but the last clones the request|response first, so a failed strategy doesn't consume the body). If you never configure any, Vigor defaults to content-type detection.
368
368
 
369
369
  ```ts
370
370
  .strategies(s => s.sniff())
@@ -372,7 +372,7 @@ Strategies form an ordered list that's tried in sequence until one succeeds (eac
372
372
 
373
373
  | Method | Behavior |
374
374
  |---|---|
375
- | `contentType(replace?)` | Parses based on the response's `Content-Type` header (JSON / XML / form-urlencoded / octet-stream / image / audio / video / multipart / text) |
375
+ | `contentType(replace?)` | Parses based on the request|response's `Content-Type` header (JSON / XML / form-urlencoded / octet-stream / image / audio / video / multipart / text) |
376
376
  | `sniff(replace?)` | Tries `json → formData → text → blob` in order until one parses |
377
377
  | `json(replace?)` | Always parses as JSON |
378
378
  | `text(replace?)` | Always parses as text |
@@ -380,7 +380,7 @@ Strategies form an ordered list that's tried in sequence until one succeeds (eac
380
380
  | `blob(replace?)` | Always parses as `Blob` |
381
381
  | `bytes(replace?)` | Always parses as `Uint8Array` |
382
382
  | `formData(replace?)` | Always parses as `FormData` |
383
- | `custom(func, replace?)` | Uses a custom `(response: Response) => Promise<any>` |
383
+ | `custom(func, replace?)` | Uses a custom `(request\|response: Request\|Response) => Promise<any>` |
384
384
 
385
385
  By default each call **appends** to the chain (they concatenate, so you can layer fallbacks: e.g. `contentType()` then `sniff()` as a last resort). Pass `replace: true` as the trailing argument to replace the whole chain instead.
386
386
 
package/dist/index.cjs CHANGED
@@ -120,16 +120,26 @@ var VigorStatus = class {
120
120
  */
121
121
  _merge(source, target, strategy) {
122
122
  const isObj = (v) => v !== null && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
123
+ const deepClone = (v) => {
124
+ if (Array.isArray(v)) return v.map(deepClone);
125
+ if (isObj(v)) {
126
+ const r = {};
127
+ for (const k of Object.keys(v)) r[k] = deepClone(v[k]);
128
+ return r;
129
+ }
130
+ return v;
131
+ };
123
132
  const merge = (a, b, s) => {
124
- if (b === void 0 || b === null) return a;
133
+ if (b === void 0 || b === null) return deepClone(a);
125
134
  if (s === "replace") return b;
126
135
  if (Array.isArray(a) && Array.isArray(b)) {
127
136
  return s === "replace" ? b : [...a, ...b];
128
137
  }
129
138
  if (isObj(a) && isObj(b)) {
130
- const r = { ...a };
139
+ const r = {};
131
140
  const sObj = isObj(s) ? s : void 0;
132
- for (const k of Object.keys(b)) {
141
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
142
+ for (const k of keys) {
133
143
  r[k] = merge(a[k], b[k], sObj?.[k]);
134
144
  }
135
145
  return r;
@@ -312,7 +322,7 @@ var VigorAll = class _VigorAll extends VigorStatus {
312
322
  }
313
323
  /** Runs a single task through the beforeEach/afterEach/onErrorEach middleware pipeline. */
314
324
  async _runTask(task, policy) {
315
- let ctx = { ...VigorAllEachContextBase };
325
+ let ctx = this._merge(VigorAllEachContextBase, {});
316
326
  try {
317
327
  for (const beforeEach of policy.middlewares.beforeEach) await beforeEach(ctx);
318
328
  ctx.result = await task();
@@ -352,10 +362,12 @@ var VigorAll = class _VigorAll extends VigorStatus {
352
362
  return await this.requestRuntime();
353
363
  }
354
364
  /** Internal execution loop implementing the bounded-concurrency worker pool and the batch-level before/after/onResult pipeline. */
355
- async requestRuntime(restartAttempt = 1) {
365
+ async requestRuntime() {
356
366
  const config = this._merge(this.config, {});
357
367
  const policy = config.policy;
358
- let ctx = { ...VigorAllContextBase, policy };
368
+ let ctx = this._merge(VigorAllContextBase, {
369
+ policy
370
+ });
359
371
  const setResult = (r) => {
360
372
  ctx = { ...ctx, result: r };
361
373
  };
@@ -365,7 +377,8 @@ var VigorAll = class _VigorAll extends VigorStatus {
365
377
  try {
366
378
  if (policy.target.length === 0) throw new VigorAllError("EMPTY_TARGET", {
367
379
  method: "request",
368
- data: {}
380
+ data: {},
381
+ context: ctx
369
382
  });
370
383
  for (const mw of policy.middlewares.before) {
371
384
  if (mw._mode === "fluent") {
@@ -921,10 +934,9 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
921
934
  /** Internal execution loop implementing the attempt/retry/restart lifecycle. */
922
935
  async requestRuntime(restartAttempt = 1) {
923
936
  const config = this._merge(this.config, {});
924
- let ctx = {
925
- ...VigorRetryContextBase,
937
+ let ctx = this._merge(VigorRetryContextBase, {
926
938
  policy: config.policy
927
- };
939
+ });
928
940
  if (isVigorEmpty(ctx.policy.target)) throw new VigorRetryError("VALUE_REQUIRED", {
929
941
  method: "request",
930
942
  data: {
@@ -978,7 +990,8 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
978
990
  method: "request",
979
991
  data: {
980
992
  limit: ctx.policy.settings.timeout
981
- }
993
+ },
994
+ context: ctx
982
995
  })), ctx.policy.settings.timeout);
983
996
  ctx.result = await Promise.race([
984
997
  target(mergedSignals),
@@ -1060,7 +1073,8 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
1060
1073
  data: {
1061
1074
  maxAttempts: ctx.policy.settings.maxAttempts,
1062
1075
  error: ctx.error
1063
- }
1076
+ },
1077
+ context: ctx
1064
1078
  });
1065
1079
  } catch (error) {
1066
1080
  ctx.error = error;
@@ -1400,10 +1414,9 @@ var VigorParse = class _VigorParse extends VigorStatus {
1400
1414
  /** Internal execution loop implementing the strategy fallback and restart handling. */
1401
1415
  async requestRuntime(restartAttempt = 1) {
1402
1416
  const config = this._merge(this.config, {});
1403
- let ctx = {
1404
- ...VigorParseContextBase,
1417
+ let ctx = this._merge(VigorParseContextBase, {
1405
1418
  policy: config.policy
1406
- };
1419
+ });
1407
1420
  if (isVigorDefault(ctx.policy.target)) throw new VigorParseError("VALUE_REQUIRED", {
1408
1421
  method: "request",
1409
1422
  data: { key: "target" },
@@ -1488,7 +1501,8 @@ var VigorParse = class _VigorParse extends VigorStatus {
1488
1501
  data: {
1489
1502
  maxAttempts: ctx.policy.settings.maxRestarts,
1490
1503
  error: ctx.error
1491
- }
1504
+ },
1505
+ context: ctx
1492
1506
  });
1493
1507
  return await this.requestRuntime(restartAttempt + 1);
1494
1508
  }
@@ -1759,17 +1773,17 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1759
1773
  /** Internal execution loop implementing URL building, retry, parsing, and restart handling. */
1760
1774
  async requestRuntime(restartAttempt = 1) {
1761
1775
  const config = this._merge(this.config, {});
1762
- let ctx = {
1763
- ...VigorFetchContextBase,
1776
+ let ctx = this._merge(VigorFetchContextBase, {
1764
1777
  policy: config.policy
1765
- };
1778
+ });
1766
1779
  const throwError = (err) => {
1767
1780
  throw err;
1768
1781
  };
1769
1782
  try {
1770
1783
  if (isVigorEmpty(ctx.policy.origin)) throw new VigorFetchError("VALUE_REQUIRED", {
1771
1784
  method: "request",
1772
- data: { key: "origin" }
1785
+ data: { key: "origin" },
1786
+ context: ctx
1773
1787
  });
1774
1788
  ctx.href = this._buildUrl(ctx.policy.origin, ctx.policy.path, ctx.policy.query, ctx.policy.hash);
1775
1789
  const hasBody = !isVigorEmpty(ctx.policy.body) && ctx.policy.body !== void 0;
@@ -1903,7 +1917,8 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1903
1917
  data: {
1904
1918
  maxAttempts: ctx.policy.settings.maxRestarts,
1905
1919
  error: ctx.error
1906
- }
1920
+ },
1921
+ context: ctx
1907
1922
  });
1908
1923
  return await this.requestRuntime(restartAttempt + 1);
1909
1924
  }
package/dist/index.d.cts CHANGED
@@ -212,17 +212,17 @@ type VigorAllPolicyMiddlewaresFluent<T extends VigorAllSchema<T>> = {
212
212
  __brand: VigorBrand<"All", "Policy<Middlewares<Fluent<Schema">;
213
213
  before: {
214
214
  _mode: "fluent";
215
- func: (ctx: VigorAllBeforeContextSchema<T>) => void;
215
+ func: (ctx: VigorAllBeforeContextSchema<T>) => void | Promise<void>;
216
216
  };
217
217
  after: {
218
218
  _mode: "fluent";
219
- func: (ctx: VigorAllAfterContextSchema<T>) => void;
219
+ func: (ctx: VigorAllAfterContextSchema<T>) => void | Promise<void>;
220
220
  };
221
221
  onResult: {
222
222
  _mode: "fluent";
223
223
  func: (res: VigorAllOnResultContextSchema<T> extends {
224
224
  result: infer R;
225
- } ? R : never) => VigorAllContextSchema<any>["result"];
225
+ } ? R : never) => VigorAllContextSchema<any>["result"] | Promise<VigorAllContextSchema<any>["result"]>;
226
226
  };
227
227
  };
228
228
  type VigorAllPolicyMiddlewaresApi<T extends VigorAllSchema<T>> = {
@@ -380,7 +380,9 @@ type VigorAllContextSchema<T extends VigorAllSchema<T>, R = VigorAllPipeResult<T
380
380
  /** Context shape seen by `before` middlewares — no task has run yet. */
381
381
  type VigorAllBeforeContextSchema<T extends VigorAllSchema<T>> = VigorAllContextSchema<T, VigorDefaultType>;
382
382
  /** Context shape seen by `after` middlewares — every task has settled, reflecting whatever `before` changed. */
383
- type VigorAllAfterContextSchema<T extends VigorAllSchema<T>> = VigorAllPipeStepVoid<VigorAllPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>;
383
+ type VigorAllAfterContextSchema<T extends VigorAllSchema<T>> = VigorDeepMerge<VigorAllPipeStepVoid<VigorAllPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, {
384
+ result: VigorAllPipeInitialContext<T>["result"];
385
+ }>;
384
386
  /** Context shape seen by `onResult` middlewares — additionally threads any `onResult` middlewares already registered earlier in the same chain, so `ctx.result` reflects what the previous `onResult` returned. */
385
387
  type VigorAllOnResultContextSchema<T extends VigorAllSchema<T>> = VigorAllPipeStepResult<VigorAllPipeStepVoid<VigorAllAfterContextSchema<T>, T["policy"]["middlewares"]["after"]>, T["policy"]["middlewares"]["onResult"]>;
386
388
  /** Per-task context, seen by `beforeEach` / `afterEach` / `onErrorEach`. */
@@ -526,21 +528,21 @@ type VigorFetchPolicyMiddlewaresFluent<T extends VigorFetchSchema<T>> = {
526
528
  __brand: VigorBrand<"Fetch", "Policy<Middlewares<Fluent<Schema">;
527
529
  before: {
528
530
  _mode: "fluent";
529
- func: (ctx: VigorFetchBeforeContextSchema<T>) => void;
531
+ func: (ctx: VigorFetchBeforeContextSchema<T>) => void | Promise<void>;
530
532
  };
531
533
  after: {
532
534
  _mode: "fluent";
533
- func: (ctx: VigorFetchAfterContextSchema<T>) => void;
535
+ func: (ctx: VigorFetchAfterContextSchema<T>) => void | Promise<void>;
534
536
  };
535
537
  onResult: {
536
538
  _mode: "fluent";
537
539
  func: (res: VigorFetchOnResultContextSchema<T> extends {
538
540
  result: infer R;
539
- } ? R : never) => VigorFetchContextSchema<any>["result"];
541
+ } ? R : never) => VigorFetchContextSchema<any>["result"] | Promise<VigorFetchContextSchema<any>["result"]>;
540
542
  };
541
543
  onError: {
542
544
  _mode: "fluent";
543
- func: (err: VigorFetchContextSchema<T>["error"]) => void;
545
+ func: (err: VigorFetchContextSchema<T>["error"]) => void | Promise<void>;
544
546
  };
545
547
  };
546
548
  type VigorFetchPolicyMiddlewaresApi<T extends VigorFetchSchema<T>> = {
@@ -715,7 +717,9 @@ type VigorFetchContextSchema<T extends VigorFetchSchema<T>, R = VigorFetchPipeRe
715
717
  /** Context shape seen by `before` middlewares — the request hasn't been sent yet. */
716
718
  type VigorFetchBeforeContextSchema<T extends VigorFetchSchema<T>> = VigorFetchContextSchema<T, VigorDefaultType>;
717
719
  /** Context shape seen by `after` middlewares — reflects the result after `before` has run. */
718
- type VigorFetchAfterContextSchema<T extends VigorFetchSchema<T>> = VigorFetchPipeStepResult<VigorFetchPipeStepVoid<VigorFetchPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, T["policy"]["middlewares"]["after"]>;
720
+ type VigorFetchAfterContextSchema<T extends VigorFetchSchema<T>> = VigorFetchPipeStepResult<VigorDeepMerge<VigorFetchPipeStepVoid<VigorFetchPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, {
721
+ result: VigorFetchPipeInitialContext<T>["result"];
722
+ }>, T["policy"]["middlewares"]["after"]>;
719
723
  /** Context shape seen by `onResult` middlewares — additionally threads any `onResult` middlewares already registered earlier in the same chain, so `ctx.result` reflects what the previous `onResult` returned. */
720
724
  type VigorFetchOnResultContextSchema<T extends VigorFetchSchema<T>> = VigorFetchPipeStepResult<VigorFetchAfterContextSchema<T>, T["policy"]["middlewares"]["onResult"]>;
721
725
 
@@ -1043,29 +1047,29 @@ type VigorRetryPolicyMiddlewaresFluent<T extends VigorRetrySchema<T>> = {
1043
1047
  __brand: VigorBrand<"Retry", "Policy<Middlewares<Fluent<Schema">;
1044
1048
  before: {
1045
1049
  _mode: "fluent";
1046
- func: (ctx: VigorRetryBeforeContextSchema<T>) => void;
1050
+ func: (ctx: VigorRetryBeforeContextSchema<T>) => void | Promise<void>;
1047
1051
  };
1048
1052
  after: {
1049
1053
  _mode: "fluent";
1050
- func: (ctx: VigorRetryAfterContextSchema<T>) => void;
1054
+ func: (ctx: VigorRetryAfterContextSchema<T>) => void | Promise<void>;
1051
1055
  };
1052
1056
  onResult: {
1053
1057
  _mode: "fluent";
1054
1058
  func: (res: VigorRetryOnResultContextSchema<T> extends {
1055
1059
  result: infer R;
1056
- } ? R : never) => VigorRetryContextSchema<any>["result"];
1060
+ } ? R : never) => VigorRetryContextSchema<any>["result"] | Promise<VigorRetryContextSchema<any>["result"]>;
1057
1061
  };
1058
1062
  retryIf: {
1059
1063
  _mode: "fluent";
1060
- func: (err: VigorRetryContextSchema<T>["error"]) => VigorRetryContextSchema<any>["flags"]["doRetry"];
1064
+ func: (err: VigorRetryContextSchema<T>["error"]) => VigorRetryContextSchema<any>["flags"]["doRetry"] | Promise<VigorRetryContextSchema<any>["flags"]["doRetry"]>;
1061
1065
  };
1062
1066
  onRetry: {
1063
1067
  _mode: "fluent";
1064
- func: (err: VigorRetryContextSchema<T>["error"]) => void;
1068
+ func: (err: VigorRetryContextSchema<T>["error"]) => void | Promise<void>;
1065
1069
  };
1066
1070
  onError: {
1067
1071
  _mode: "fluent";
1068
- func: (err: VigorRetryContextSchema<T>["error"]) => void;
1072
+ func: (err: VigorRetryContextSchema<T>["error"]) => void | Promise<void>;
1069
1073
  };
1070
1074
  };
1071
1075
  type VigorRetryPolicyMiddlewaresApi<T extends VigorRetrySchema<T>> = {
@@ -1302,7 +1306,9 @@ type VigorRetryContextSchema<T extends VigorRetrySchema<T>, R = VigorRetryPipeRe
1302
1306
  /** Context shape seen by `before` middlewares — the target hasn't run yet, so there's no result. */
1303
1307
  type VigorRetryBeforeContextSchema<T extends VigorRetrySchema<T>> = VigorRetryContextSchema<T, VigorDefaultType>;
1304
1308
  /** Context shape seen by `after` middlewares — reflects the result after `before` has run. */
1305
- type VigorRetryAfterContextSchema<T extends VigorRetrySchema<T>> = VigorRetryPipeStepResult<VigorRetryPipeStepVoid<VigorRetryPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, T["policy"]["middlewares"]["after"]>;
1309
+ type VigorRetryAfterContextSchema<T extends VigorRetrySchema<T>> = VigorRetryPipeStepResult<VigorDeepMerge<VigorRetryPipeStepVoid<VigorRetryPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, {
1310
+ result: VigorRetryPipeInitialContext<T>["result"];
1311
+ }>, T["policy"]["middlewares"]["after"]>;
1306
1312
  /** Context shape seen by `onResult` middlewares — additionally threads any `onResult` middlewares already registered earlier in the same chain, so `ctx.result` reflects what the previous `onResult` returned. */
1307
1313
  type VigorRetryOnResultContextSchema<T extends VigorRetrySchema<T>> = VigorRetryPipeStepResult<VigorRetryAfterContextSchema<T>, T["policy"]["middlewares"]["onResult"]>;
1308
1314
 
@@ -1680,21 +1686,21 @@ type VigorParsePolicyMiddlewaresFluent<T extends VigorParseSchema<T>> = {
1680
1686
  __brand: VigorBrand<"Parse", "Policy<Middlewares<Fluent<Schema">;
1681
1687
  before: {
1682
1688
  _mode: "fluent";
1683
- func: (ctx: VigorParseBeforeContextSchema<T>) => void;
1689
+ func: (ctx: VigorParseBeforeContextSchema<T>) => void | Promise<void>;
1684
1690
  };
1685
1691
  after: {
1686
1692
  _mode: "fluent";
1687
- func: (ctx: VigorParseAfterContextSchema<T>) => void;
1693
+ func: (ctx: VigorParseAfterContextSchema<T>) => void | Promise<void>;
1688
1694
  };
1689
1695
  onResult: {
1690
1696
  _mode: "fluent";
1691
1697
  func: (res: VigorParseOnResultContextSchema<T> extends {
1692
1698
  result: infer R;
1693
- } ? R : never) => VigorParseContextSchema<any>["result"];
1699
+ } ? R : never) => VigorParseContextSchema<any>["result"] | Promise<VigorParseContextSchema<any>["result"]>;
1694
1700
  };
1695
1701
  onError: {
1696
1702
  _mode: "fluent";
1697
- func: (err: VigorParseContextSchema<T>["error"]) => void;
1703
+ func: (err: VigorParseContextSchema<T>["error"]) => void | Promise<void>;
1698
1704
  };
1699
1705
  };
1700
1706
  type VigorParsePolicyMiddlewaresApi<T extends VigorParseSchema<T>> = {
@@ -1860,7 +1866,9 @@ type VigorParseContextSchema<T extends VigorParseSchema<T>, R = VigorParsePipeRe
1860
1866
  /** Context shape seen by `before` middlewares — parsing hasn't run yet. */
1861
1867
  type VigorParseBeforeContextSchema<T extends VigorParseSchema<T>> = VigorParseContextSchema<T, VigorDefaultType>;
1862
1868
  /** Context shape seen by `after` middlewares — reflects the result after `before` has run. */
1863
- type VigorParseAfterContextSchema<T extends VigorParseSchema<T>> = VigorParsePipeStepResult<VigorParsePipeStepVoid<VigorParsePipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, T["policy"]["middlewares"]["after"]>;
1869
+ type VigorParseAfterContextSchema<T extends VigorParseSchema<T>> = VigorParsePipeStepResult<VigorDeepMerge<VigorParsePipeStepVoid<VigorParsePipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, {
1870
+ result: VigorParsePipeInitialContext<T>["result"];
1871
+ }>, T["policy"]["middlewares"]["after"]>;
1864
1872
  /** Context shape seen by `onResult` middlewares — additionally threads any `onResult` middlewares already registered earlier in the same chain, so `ctx.result` reflects what the previous `onResult` returned. */
1865
1873
  type VigorParseOnResultContextSchema<T extends VigorParseSchema<T>> = VigorParsePipeStepResult<VigorParseAfterContextSchema<T>, T["policy"]["middlewares"]["onResult"]>;
1866
1874
 
package/dist/index.d.ts CHANGED
@@ -212,17 +212,17 @@ type VigorAllPolicyMiddlewaresFluent<T extends VigorAllSchema<T>> = {
212
212
  __brand: VigorBrand<"All", "Policy<Middlewares<Fluent<Schema">;
213
213
  before: {
214
214
  _mode: "fluent";
215
- func: (ctx: VigorAllBeforeContextSchema<T>) => void;
215
+ func: (ctx: VigorAllBeforeContextSchema<T>) => void | Promise<void>;
216
216
  };
217
217
  after: {
218
218
  _mode: "fluent";
219
- func: (ctx: VigorAllAfterContextSchema<T>) => void;
219
+ func: (ctx: VigorAllAfterContextSchema<T>) => void | Promise<void>;
220
220
  };
221
221
  onResult: {
222
222
  _mode: "fluent";
223
223
  func: (res: VigorAllOnResultContextSchema<T> extends {
224
224
  result: infer R;
225
- } ? R : never) => VigorAllContextSchema<any>["result"];
225
+ } ? R : never) => VigorAllContextSchema<any>["result"] | Promise<VigorAllContextSchema<any>["result"]>;
226
226
  };
227
227
  };
228
228
  type VigorAllPolicyMiddlewaresApi<T extends VigorAllSchema<T>> = {
@@ -380,7 +380,9 @@ type VigorAllContextSchema<T extends VigorAllSchema<T>, R = VigorAllPipeResult<T
380
380
  /** Context shape seen by `before` middlewares — no task has run yet. */
381
381
  type VigorAllBeforeContextSchema<T extends VigorAllSchema<T>> = VigorAllContextSchema<T, VigorDefaultType>;
382
382
  /** Context shape seen by `after` middlewares — every task has settled, reflecting whatever `before` changed. */
383
- type VigorAllAfterContextSchema<T extends VigorAllSchema<T>> = VigorAllPipeStepVoid<VigorAllPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>;
383
+ type VigorAllAfterContextSchema<T extends VigorAllSchema<T>> = VigorDeepMerge<VigorAllPipeStepVoid<VigorAllPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, {
384
+ result: VigorAllPipeInitialContext<T>["result"];
385
+ }>;
384
386
  /** Context shape seen by `onResult` middlewares — additionally threads any `onResult` middlewares already registered earlier in the same chain, so `ctx.result` reflects what the previous `onResult` returned. */
385
387
  type VigorAllOnResultContextSchema<T extends VigorAllSchema<T>> = VigorAllPipeStepResult<VigorAllPipeStepVoid<VigorAllAfterContextSchema<T>, T["policy"]["middlewares"]["after"]>, T["policy"]["middlewares"]["onResult"]>;
386
388
  /** Per-task context, seen by `beforeEach` / `afterEach` / `onErrorEach`. */
@@ -526,21 +528,21 @@ type VigorFetchPolicyMiddlewaresFluent<T extends VigorFetchSchema<T>> = {
526
528
  __brand: VigorBrand<"Fetch", "Policy<Middlewares<Fluent<Schema">;
527
529
  before: {
528
530
  _mode: "fluent";
529
- func: (ctx: VigorFetchBeforeContextSchema<T>) => void;
531
+ func: (ctx: VigorFetchBeforeContextSchema<T>) => void | Promise<void>;
530
532
  };
531
533
  after: {
532
534
  _mode: "fluent";
533
- func: (ctx: VigorFetchAfterContextSchema<T>) => void;
535
+ func: (ctx: VigorFetchAfterContextSchema<T>) => void | Promise<void>;
534
536
  };
535
537
  onResult: {
536
538
  _mode: "fluent";
537
539
  func: (res: VigorFetchOnResultContextSchema<T> extends {
538
540
  result: infer R;
539
- } ? R : never) => VigorFetchContextSchema<any>["result"];
541
+ } ? R : never) => VigorFetchContextSchema<any>["result"] | Promise<VigorFetchContextSchema<any>["result"]>;
540
542
  };
541
543
  onError: {
542
544
  _mode: "fluent";
543
- func: (err: VigorFetchContextSchema<T>["error"]) => void;
545
+ func: (err: VigorFetchContextSchema<T>["error"]) => void | Promise<void>;
544
546
  };
545
547
  };
546
548
  type VigorFetchPolicyMiddlewaresApi<T extends VigorFetchSchema<T>> = {
@@ -715,7 +717,9 @@ type VigorFetchContextSchema<T extends VigorFetchSchema<T>, R = VigorFetchPipeRe
715
717
  /** Context shape seen by `before` middlewares — the request hasn't been sent yet. */
716
718
  type VigorFetchBeforeContextSchema<T extends VigorFetchSchema<T>> = VigorFetchContextSchema<T, VigorDefaultType>;
717
719
  /** Context shape seen by `after` middlewares — reflects the result after `before` has run. */
718
- type VigorFetchAfterContextSchema<T extends VigorFetchSchema<T>> = VigorFetchPipeStepResult<VigorFetchPipeStepVoid<VigorFetchPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, T["policy"]["middlewares"]["after"]>;
720
+ type VigorFetchAfterContextSchema<T extends VigorFetchSchema<T>> = VigorFetchPipeStepResult<VigorDeepMerge<VigorFetchPipeStepVoid<VigorFetchPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, {
721
+ result: VigorFetchPipeInitialContext<T>["result"];
722
+ }>, T["policy"]["middlewares"]["after"]>;
719
723
  /** Context shape seen by `onResult` middlewares — additionally threads any `onResult` middlewares already registered earlier in the same chain, so `ctx.result` reflects what the previous `onResult` returned. */
720
724
  type VigorFetchOnResultContextSchema<T extends VigorFetchSchema<T>> = VigorFetchPipeStepResult<VigorFetchAfterContextSchema<T>, T["policy"]["middlewares"]["onResult"]>;
721
725
 
@@ -1043,29 +1047,29 @@ type VigorRetryPolicyMiddlewaresFluent<T extends VigorRetrySchema<T>> = {
1043
1047
  __brand: VigorBrand<"Retry", "Policy<Middlewares<Fluent<Schema">;
1044
1048
  before: {
1045
1049
  _mode: "fluent";
1046
- func: (ctx: VigorRetryBeforeContextSchema<T>) => void;
1050
+ func: (ctx: VigorRetryBeforeContextSchema<T>) => void | Promise<void>;
1047
1051
  };
1048
1052
  after: {
1049
1053
  _mode: "fluent";
1050
- func: (ctx: VigorRetryAfterContextSchema<T>) => void;
1054
+ func: (ctx: VigorRetryAfterContextSchema<T>) => void | Promise<void>;
1051
1055
  };
1052
1056
  onResult: {
1053
1057
  _mode: "fluent";
1054
1058
  func: (res: VigorRetryOnResultContextSchema<T> extends {
1055
1059
  result: infer R;
1056
- } ? R : never) => VigorRetryContextSchema<any>["result"];
1060
+ } ? R : never) => VigorRetryContextSchema<any>["result"] | Promise<VigorRetryContextSchema<any>["result"]>;
1057
1061
  };
1058
1062
  retryIf: {
1059
1063
  _mode: "fluent";
1060
- func: (err: VigorRetryContextSchema<T>["error"]) => VigorRetryContextSchema<any>["flags"]["doRetry"];
1064
+ func: (err: VigorRetryContextSchema<T>["error"]) => VigorRetryContextSchema<any>["flags"]["doRetry"] | Promise<VigorRetryContextSchema<any>["flags"]["doRetry"]>;
1061
1065
  };
1062
1066
  onRetry: {
1063
1067
  _mode: "fluent";
1064
- func: (err: VigorRetryContextSchema<T>["error"]) => void;
1068
+ func: (err: VigorRetryContextSchema<T>["error"]) => void | Promise<void>;
1065
1069
  };
1066
1070
  onError: {
1067
1071
  _mode: "fluent";
1068
- func: (err: VigorRetryContextSchema<T>["error"]) => void;
1072
+ func: (err: VigorRetryContextSchema<T>["error"]) => void | Promise<void>;
1069
1073
  };
1070
1074
  };
1071
1075
  type VigorRetryPolicyMiddlewaresApi<T extends VigorRetrySchema<T>> = {
@@ -1302,7 +1306,9 @@ type VigorRetryContextSchema<T extends VigorRetrySchema<T>, R = VigorRetryPipeRe
1302
1306
  /** Context shape seen by `before` middlewares — the target hasn't run yet, so there's no result. */
1303
1307
  type VigorRetryBeforeContextSchema<T extends VigorRetrySchema<T>> = VigorRetryContextSchema<T, VigorDefaultType>;
1304
1308
  /** Context shape seen by `after` middlewares — reflects the result after `before` has run. */
1305
- type VigorRetryAfterContextSchema<T extends VigorRetrySchema<T>> = VigorRetryPipeStepResult<VigorRetryPipeStepVoid<VigorRetryPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, T["policy"]["middlewares"]["after"]>;
1309
+ type VigorRetryAfterContextSchema<T extends VigorRetrySchema<T>> = VigorRetryPipeStepResult<VigorDeepMerge<VigorRetryPipeStepVoid<VigorRetryPipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, {
1310
+ result: VigorRetryPipeInitialContext<T>["result"];
1311
+ }>, T["policy"]["middlewares"]["after"]>;
1306
1312
  /** Context shape seen by `onResult` middlewares — additionally threads any `onResult` middlewares already registered earlier in the same chain, so `ctx.result` reflects what the previous `onResult` returned. */
1307
1313
  type VigorRetryOnResultContextSchema<T extends VigorRetrySchema<T>> = VigorRetryPipeStepResult<VigorRetryAfterContextSchema<T>, T["policy"]["middlewares"]["onResult"]>;
1308
1314
 
@@ -1680,21 +1686,21 @@ type VigorParsePolicyMiddlewaresFluent<T extends VigorParseSchema<T>> = {
1680
1686
  __brand: VigorBrand<"Parse", "Policy<Middlewares<Fluent<Schema">;
1681
1687
  before: {
1682
1688
  _mode: "fluent";
1683
- func: (ctx: VigorParseBeforeContextSchema<T>) => void;
1689
+ func: (ctx: VigorParseBeforeContextSchema<T>) => void | Promise<void>;
1684
1690
  };
1685
1691
  after: {
1686
1692
  _mode: "fluent";
1687
- func: (ctx: VigorParseAfterContextSchema<T>) => void;
1693
+ func: (ctx: VigorParseAfterContextSchema<T>) => void | Promise<void>;
1688
1694
  };
1689
1695
  onResult: {
1690
1696
  _mode: "fluent";
1691
1697
  func: (res: VigorParseOnResultContextSchema<T> extends {
1692
1698
  result: infer R;
1693
- } ? R : never) => VigorParseContextSchema<any>["result"];
1699
+ } ? R : never) => VigorParseContextSchema<any>["result"] | Promise<VigorParseContextSchema<any>["result"]>;
1694
1700
  };
1695
1701
  onError: {
1696
1702
  _mode: "fluent";
1697
- func: (err: VigorParseContextSchema<T>["error"]) => void;
1703
+ func: (err: VigorParseContextSchema<T>["error"]) => void | Promise<void>;
1698
1704
  };
1699
1705
  };
1700
1706
  type VigorParsePolicyMiddlewaresApi<T extends VigorParseSchema<T>> = {
@@ -1860,7 +1866,9 @@ type VigorParseContextSchema<T extends VigorParseSchema<T>, R = VigorParsePipeRe
1860
1866
  /** Context shape seen by `before` middlewares — parsing hasn't run yet. */
1861
1867
  type VigorParseBeforeContextSchema<T extends VigorParseSchema<T>> = VigorParseContextSchema<T, VigorDefaultType>;
1862
1868
  /** Context shape seen by `after` middlewares — reflects the result after `before` has run. */
1863
- type VigorParseAfterContextSchema<T extends VigorParseSchema<T>> = VigorParsePipeStepResult<VigorParsePipeStepVoid<VigorParsePipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, T["policy"]["middlewares"]["after"]>;
1869
+ type VigorParseAfterContextSchema<T extends VigorParseSchema<T>> = VigorParsePipeStepResult<VigorDeepMerge<VigorParsePipeStepVoid<VigorParsePipeInitialContext<T>, T["policy"]["middlewares"]["before"]>, {
1870
+ result: VigorParsePipeInitialContext<T>["result"];
1871
+ }>, T["policy"]["middlewares"]["after"]>;
1864
1872
  /** Context shape seen by `onResult` middlewares — additionally threads any `onResult` middlewares already registered earlier in the same chain, so `ctx.result` reflects what the previous `onResult` returned. */
1865
1873
  type VigorParseOnResultContextSchema<T extends VigorParseSchema<T>> = VigorParsePipeStepResult<VigorParseAfterContextSchema<T>, T["policy"]["middlewares"]["onResult"]>;
1866
1874
 
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;
@@ -232,7 +242,7 @@ var VigorAll = class _VigorAll extends VigorStatus {
232
242
  }
233
243
  /** Runs a single task through the beforeEach/afterEach/onErrorEach middleware pipeline. */
234
244
  async _runTask(task, policy) {
235
- let ctx = { ...VigorAllEachContextBase };
245
+ let ctx = this._merge(VigorAllEachContextBase, {});
236
246
  try {
237
247
  for (const beforeEach of policy.middlewares.beforeEach) await beforeEach(ctx);
238
248
  ctx.result = await task();
@@ -272,10 +282,12 @@ var VigorAll = class _VigorAll extends VigorStatus {
272
282
  return await this.requestRuntime();
273
283
  }
274
284
  /** Internal execution loop implementing the bounded-concurrency worker pool and the batch-level before/after/onResult pipeline. */
275
- async requestRuntime(restartAttempt = 1) {
285
+ async requestRuntime() {
276
286
  const config = this._merge(this.config, {});
277
287
  const policy = config.policy;
278
- let ctx = { ...VigorAllContextBase, policy };
288
+ let ctx = this._merge(VigorAllContextBase, {
289
+ policy
290
+ });
279
291
  const setResult = (r) => {
280
292
  ctx = { ...ctx, result: r };
281
293
  };
@@ -285,7 +297,8 @@ var VigorAll = class _VigorAll extends VigorStatus {
285
297
  try {
286
298
  if (policy.target.length === 0) throw new VigorAllError("EMPTY_TARGET", {
287
299
  method: "request",
288
- data: {}
300
+ data: {},
301
+ context: ctx
289
302
  });
290
303
  for (const mw of policy.middlewares.before) {
291
304
  if (mw._mode === "fluent") {
@@ -841,10 +854,9 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
841
854
  /** Internal execution loop implementing the attempt/retry/restart lifecycle. */
842
855
  async requestRuntime(restartAttempt = 1) {
843
856
  const config = this._merge(this.config, {});
844
- let ctx = {
845
- ...VigorRetryContextBase,
857
+ let ctx = this._merge(VigorRetryContextBase, {
846
858
  policy: config.policy
847
- };
859
+ });
848
860
  if (isVigorEmpty(ctx.policy.target)) throw new VigorRetryError("VALUE_REQUIRED", {
849
861
  method: "request",
850
862
  data: {
@@ -898,7 +910,8 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
898
910
  method: "request",
899
911
  data: {
900
912
  limit: ctx.policy.settings.timeout
901
- }
913
+ },
914
+ context: ctx
902
915
  })), ctx.policy.settings.timeout);
903
916
  ctx.result = await Promise.race([
904
917
  target(mergedSignals),
@@ -980,7 +993,8 @@ var VigorRetry = class _VigorRetry extends VigorStatus {
980
993
  data: {
981
994
  maxAttempts: ctx.policy.settings.maxAttempts,
982
995
  error: ctx.error
983
- }
996
+ },
997
+ context: ctx
984
998
  });
985
999
  } catch (error) {
986
1000
  ctx.error = error;
@@ -1320,10 +1334,9 @@ var VigorParse = class _VigorParse extends VigorStatus {
1320
1334
  /** Internal execution loop implementing the strategy fallback and restart handling. */
1321
1335
  async requestRuntime(restartAttempt = 1) {
1322
1336
  const config = this._merge(this.config, {});
1323
- let ctx = {
1324
- ...VigorParseContextBase,
1337
+ let ctx = this._merge(VigorParseContextBase, {
1325
1338
  policy: config.policy
1326
- };
1339
+ });
1327
1340
  if (isVigorDefault(ctx.policy.target)) throw new VigorParseError("VALUE_REQUIRED", {
1328
1341
  method: "request",
1329
1342
  data: { key: "target" },
@@ -1408,7 +1421,8 @@ var VigorParse = class _VigorParse extends VigorStatus {
1408
1421
  data: {
1409
1422
  maxAttempts: ctx.policy.settings.maxRestarts,
1410
1423
  error: ctx.error
1411
- }
1424
+ },
1425
+ context: ctx
1412
1426
  });
1413
1427
  return await this.requestRuntime(restartAttempt + 1);
1414
1428
  }
@@ -1679,17 +1693,17 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1679
1693
  /** Internal execution loop implementing URL building, retry, parsing, and restart handling. */
1680
1694
  async requestRuntime(restartAttempt = 1) {
1681
1695
  const config = this._merge(this.config, {});
1682
- let ctx = {
1683
- ...VigorFetchContextBase,
1696
+ let ctx = this._merge(VigorFetchContextBase, {
1684
1697
  policy: config.policy
1685
- };
1698
+ });
1686
1699
  const throwError = (err) => {
1687
1700
  throw err;
1688
1701
  };
1689
1702
  try {
1690
1703
  if (isVigorEmpty(ctx.policy.origin)) throw new VigorFetchError("VALUE_REQUIRED", {
1691
1704
  method: "request",
1692
- data: { key: "origin" }
1705
+ data: { key: "origin" },
1706
+ context: ctx
1693
1707
  });
1694
1708
  ctx.href = this._buildUrl(ctx.policy.origin, ctx.policy.path, ctx.policy.query, ctx.policy.hash);
1695
1709
  const hasBody = !isVigorEmpty(ctx.policy.body) && ctx.policy.body !== void 0;
@@ -1823,7 +1837,8 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
1823
1837
  data: {
1824
1838
  maxAttempts: ctx.policy.settings.maxRestarts,
1825
1839
  error: ctx.error
1826
- }
1840
+ },
1841
+ context: ctx
1827
1842
  });
1828
1843
  return await this.requestRuntime(restartAttempt + 1);
1829
1844
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vigor-fetch",
3
- "version": "4.0.9",
3
+ "version": "4.0.11",
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",
@@ -42,7 +42,7 @@
42
42
  "author": "Uav1010",
43
43
  "license": "MIT",
44
44
  "engines": {
45
- "node": ">=18.0.0"
45
+ "node": ">=18.17.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "tsup": "^8.5.1",