vigor-fetch 5.0.0 → 5.2.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
@@ -145,6 +145,15 @@ vigor.guards.fetch(err) // isVigorFetchError
145
145
  vigor.guards.all(err) // isVigorAllError
146
146
  ```
147
147
 
148
+ Each guard also has a `find` that looks inside wrapped errors. A request that runs out of attempts fails with `RETRY_EXHAUSTED`, and the error that actually happened (a `FETCH_FAILED`, a `TIMED_OUT`, ...) sits in its `data.error`. `find` walks the error itself, then `data.error` of `RETRY_EXHAUSTED` / `RESTART_EXHAUSTED`, then `cause`, and returns the first error with one of the given codes, or `null`:
149
+
150
+ ```ts
151
+ const failed = vigor.guards.fetch.find(err, "FETCH_FAILED") // same as findVigorFetchError(err, "FETCH_FAILED")
152
+ if (failed) failed.data.status
153
+ ```
154
+
155
+ The guard itself only checks the error you pass: a guard narrows its argument, so it can't say `true` for an error found one level down. Use `find` when you want the underlying error wherever it is, and the guard when the outer code matters (e.g. "did we give up retrying?").
156
+
148
157
  ---
149
158
 
150
159
  ## `vigor.fetch`
@@ -358,6 +367,13 @@ Every stage takes a mode as the first argument: `"fluent"` (return a plain value
358
367
  )
359
368
  ```
360
369
 
370
+ **The returned context replaces `ctx`** — at runtime and in the types. Return values are only checked for being context-shaped; what you change is what carries on:
371
+
372
+ - Returning `ctx` (sync or `async`) changes nothing, so the types stay those of the finished builder — a middleware registered before `.cast()`, `.apiMap()` or `.target()` doesn't freeze the result at that point.
373
+ - Every key you change is overwritten from then on: `({ ...ctx, record: { traceId } })` in `before` types `ctx.record` in every later stage, and `({ ...ctx, result: x })` in `after`/`onResult` makes `x` the result (and what the next `onResult` receives). A fluent `onResult`'s return value — awaited — overwrites `result` the same way.
374
+ - Keys the runtime sets itself are reset where it sets them: `result` (and fetch's `response`) at the start of `after`, so changing `result` in `before` has no effect. A `result` changed in `onError` joins the result type as what a recovered request (`setResult`) resolves to.
375
+ - Each stage's context is its own: `retryIf`/`onRetry`/`onError` see what their stage's earlier middlewares changed, not `before`'s/`after`'s, since those may not have run.
376
+
361
377
  | Stage | Runs | Intercept API |
362
378
  |---|---|---|
363
379
  | `before` | Before the request is sent | `throwError`, `setOptions`, `setHeaders`, `setBody` |
@@ -813,7 +829,7 @@ try {
813
829
 
814
830
  `VigorRetryErrors` & co. are unions with **one member per error code**, so narrowing by `code` also narrows `data` to that code's payload. The `{} | null | undefined` tail is everything Vigor didn't create — an error your own target or middleware threw, an abort reason, a network `TypeError`, or even a bare `throw null`. It's there because those really do reach you: a `cancelRetry()`/`breakRetry()`/abort path returns your error as-is, rather than wrapping it.
815
831
 
816
- Narrow with the exported guards — `isVigorRetryError`, `isVigorParseError`, `isVigorFetchError`, `isVigorAllError` (or `vigor.guards.*`):
832
+ Narrow with the exported guards — `isVigorRetryError`, `isVigorParseError`, `isVigorFetchError`, `isVigorAllError` (or `vigor.guards.*`). To reach an error wrapped by `RETRY_EXHAUSTED`, use `findVigor*Error` (or `vigor.guards.*.find`, see [`vigor.guards`](#vigorguards)):
817
833
 
818
834
  ```ts
819
835
  const res = await vigor.retry(task).requestVerbose();
@@ -898,8 +914,8 @@ With an [API map](#route-descriptions) route that declares failure `responses`,
898
914
  ```ts
899
915
  const res = await api.route("POST /users").body("overwrite", { name: "a" }).requestVerbose()
900
916
  if (!res.success) {
901
- const err = isVigorFetchError(res.error, "RETRY_EXHAUSTED") ? res.error.data.error : res.error
902
- if (isVigorFetchError(err, "FETCH_FAILED") && err.data.status === 422) {
917
+ const err = findVigorFetchError(res.error, "FETCH_FAILED") // unwraps RETRY_EXHAUSTED if needed
918
+ if (err && err.data.status === 422) {
903
919
  err.data.parsed // { fields: Record<string, string> } | undefined
904
920
  }
905
921
  }
package/dist/index.cjs CHANGED
@@ -85,6 +85,11 @@ __export(index_exports, {
85
85
  createBrand: () => createBrand,
86
86
  createVigorTimelineRecorder: () => createVigorTimelineRecorder,
87
87
  default: () => index_default,
88
+ findInVigorErrorChain: () => findInVigorErrorChain,
89
+ findVigorAllError: () => findVigorAllError,
90
+ findVigorFetchError: () => findVigorFetchError,
91
+ findVigorParseError: () => findVigorParseError,
92
+ findVigorRetryError: () => findVigorRetryError,
88
93
  isVigorAllError: () => isVigorAllError,
89
94
  isVigorDefault: () => isVigorDefault,
90
95
  isVigorEmpty: () => isVigorEmpty,
@@ -322,6 +327,22 @@ var VigorError = class extends Error {
322
327
  Error.captureStackTrace?.(this, new.target);
323
328
  }
324
329
  };
330
+ function findInVigorErrorChain(error, match) {
331
+ const seen = /* @__PURE__ */ new Set();
332
+ const visit = (current) => {
333
+ if (current === null || current === void 0 || seen.has(current)) return void 0;
334
+ seen.add(current);
335
+ if (match(current)) return current;
336
+ if (typeof current !== "object") return void 0;
337
+ const data = current.data;
338
+ if (current instanceof VigorError && data !== null && typeof data === "object" && "error" in data) {
339
+ const found = visit(data.error);
340
+ if (found !== void 0) return found;
341
+ }
342
+ return visit(current.cause);
343
+ };
344
+ return visit(error);
345
+ }
325
346
 
326
347
  // src/all/error.ts
327
348
  var VigorAllErrorMessageFuncs = {
@@ -341,6 +362,10 @@ var VigorAllError = class extends VigorError {
341
362
  function isVigorAllError(error, ...codes) {
342
363
  return error instanceof VigorAllError && (codes.length === 0 || codes.includes(error.code));
343
364
  }
365
+ function findVigorAllError(error, ...codes) {
366
+ const found = findInVigorErrorChain(error, (e) => isVigorAllError(e, ...codes));
367
+ return found === void 0 ? null : found;
368
+ }
344
369
 
345
370
  // src/core/timeline.ts
346
371
  var internalMiddlewares = /* @__PURE__ */ new WeakSet();
@@ -858,6 +883,10 @@ var VigorRetryError = class extends VigorError {
858
883
  function isVigorRetryError(error, ...codes) {
859
884
  return error instanceof VigorRetryError && (codes.length === 0 || codes.includes(error.code));
860
885
  }
886
+ function findVigorRetryError(error, ...codes) {
887
+ const found = findInVigorErrorChain(error, (e) => isVigorRetryError(e, ...codes));
888
+ return found === void 0 ? null : found;
889
+ }
861
890
 
862
891
  // src/retry/algorithms.ts
863
892
  function VigorJitter(jitter) {
@@ -1562,6 +1591,10 @@ var VigorParseError = class extends VigorError {
1562
1591
  function isVigorParseError(error, ...codes) {
1563
1592
  return error instanceof VigorParseError && (codes.length === 0 || codes.includes(error.code));
1564
1593
  }
1594
+ function findVigorParseError(error, ...codes) {
1595
+ const found = findInVigorErrorChain(error, (e) => isVigorParseError(e, ...codes));
1596
+ return found === void 0 ? null : found;
1597
+ }
1565
1598
 
1566
1599
  // src/parse/strategies.ts
1567
1600
  var VigorParsePolicyStrategiesBase = {
@@ -2054,6 +2087,10 @@ var VigorFetchError = class extends VigorError {
2054
2087
  function isVigorFetchError(error, ...codes) {
2055
2088
  return error instanceof VigorFetchError && (codes.length === 0 || codes.includes(error.code));
2056
2089
  }
2090
+ function findVigorFetchError(error, ...codes) {
2091
+ const found = findInVigorErrorChain(error, (e) => isVigorFetchError(e, ...codes));
2092
+ return found === void 0 ? null : found;
2093
+ }
2057
2094
  function adoptVigorFetchError(error, context) {
2058
2095
  if (!(error instanceof VigorRetryError) && !(error instanceof VigorParseError)) return error;
2059
2096
  const data = error.data;
@@ -2625,6 +2662,7 @@ var VigorFetch = class _VigorFetch extends VigorStatus {
2625
2662
  };
2626
2663
 
2627
2664
  // src/entry/main.ts
2665
+ var guardWithFind = (guard, find) => Object.assign(((...args) => guard(...args)), { find });
2628
2666
  function retryWithTarget(task) {
2629
2667
  return new VigorRetry().target(task);
2630
2668
  }
@@ -2669,10 +2707,10 @@ var vigorBase = {
2669
2707
  }
2670
2708
  },
2671
2709
  guards: {
2672
- retry: isVigorRetryError,
2673
- parse: isVigorParseError,
2674
- fetch: isVigorFetchError,
2675
- all: isVigorAllError
2710
+ retry: guardWithFind(isVigorRetryError, findVigorRetryError),
2711
+ parse: guardWithFind(isVigorParseError, findVigorParseError),
2712
+ fetch: guardWithFind(isVigorFetchError, findVigorFetchError),
2713
+ all: guardWithFind(isVigorAllError, findVigorAllError)
2676
2714
  }
2677
2715
  };
2678
2716
  var cloneVigor = (v) => ({
@@ -2766,6 +2804,11 @@ var index_default = vigor;
2766
2804
  adoptVigorFetchError,
2767
2805
  createBrand,
2768
2806
  createVigorTimelineRecorder,
2807
+ findInVigorErrorChain,
2808
+ findVigorAllError,
2809
+ findVigorFetchError,
2810
+ findVigorParseError,
2811
+ findVigorRetryError,
2769
2812
  isVigorAllError,
2770
2813
  isVigorDefault,
2771
2814
  isVigorEmpty,