Skip to content
Myr

Tweaks

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 346f898 · Browse files · 4 files+22 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+22 added−1 removed

  • other:+22 added−1 removed

4 files+22 added−1 removed

modified:source/core/constants.ts+2 added
116 unmodified lines
117 This allows you to modify or completely replace the request during a forced retry. The custom request becomes the starting point for the retry - `beforeRetry` hooks can still further modify it if needed.118119 **Note:** The custom request's `signal` will be replaced with Ky's managed signal to handle timeouts and user-provided abort signals correctly. If the original request body has been consumed, you must provide a new body or clone the request before consuming.120121 **Warning:** Custom retry requests are not sanitized. If you reuse headers across origins, remove any credentials you do not want forwarded.122123 @example124 ```125 // Fallback to a different endpoint126 return ky.retry({
158 unmodified lines
modified:source/core/Ky.ts+7 added-1 removed
873 unmodified lines
874875 // Apply custom request from forced retry before beforeRetry hooks876 // Ensure the custom request has the correct managed signal for timeouts and user aborts877 if (error instanceof ForceRetryError && error.customRequest) {878 this.#assignRequest(new globalThis.Request(error.customRequest, this.#options.signal ? {signal: this.#options.signal} : undefined));878 const customRequest = new globalThis.Request(error.customRequest, this.#options.signal ? {signal: this.#options.signal} : undefined);879 // Replacement Requests are authoritative by design. Do not rewrite headers here,880 // even for cross-origin retries. Callers using `ky.retry({request})` explicitly881 // opted into the exact Request they constructed.882 this.#assignRequest(customRequest);883 }884885 for (const hook of this.#options.hooks.beforeRetry) {886 let hookResult: Awaited<ReturnType<typeof hook>>;
14 unmodified lines
901 throw hookError;902 }903904 if (isRequestInstance(hookResult)) {905 // Same contract as `ky.retry({request})`: a Request returned from `beforeRetry`906 // is used as-is rather than being sanitized or otherwise rewritten by Ky.907 this.#assignRequest(hookResult);908 break;909 }910
131 unmodified lines
modified:source/types/hooks.ts+4 added
169 unmodified lines
170 This hook enables you to modify the request right before retry. Ky will make no further changes to the request after this. The hook function receives a state object with the normalized request, options, an error instance, and retry count. You could, for example, modify `request.headers` here.171172 The hook can return a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) to replace the outgoing retry request, or return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to skip the retry and use that response instead. **Note:** Returning a request or response skips remaining `beforeRetry` hooks.173174 **Warning:** Returned `Request` objects are used as-is. If you point one at another origin, remove any credentials you do not want forwarded.175176 The `retryCount` is always `>= 1`, since this hook is only called during retries, not on the initial request.177178 If the request received a response, the error will be of type `HTTPError`. The `Response` object will be available at `error.response`, and the pre-parsed response body will be available at `error.data`. Be aware that some types of errors, such as network errors, inherently mean that a response was not received. In that case, the error will be an instance of `NetworkError` instead of `HTTPError`.179
114 unmodified lines
294 This hook enables you to read and optionally modify the response. The hook function receives a state object with the normalized request, options, a clone of the response, and retry count. The return value of the hook function will be used by Ky as the response object if it's an instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).295296 You can also force a retry by returning [`ky.retry(options)`](#kyretryoptions). This is useful when you need to retry based on the response body content, even if the response has a successful status code. The retry will respect the `retry.limit` option and be observable in `beforeRetry` hooks.297298 **Warning:** `ky.retry({request})` uses the replacement request as-is. If it targets another origin, remove any credentials you do not want forwarded.299300 Any non-`ky.retry()` error thrown by `afterResponse` hooks is treated as fatal and will not trigger Ky's retry logic.301302 The `retryCount` is `0` for the initial request and increments with each retry. This allows you to distinguish between the initial request and retries, which is useful when you need different behavior for retries (e.g., showing a notification only on the final retry).303
62 unmodified lines
modified:readme.md+9 added
530 unmodified lines
531This hook enables you to modify the request right before retry. Ky will make no further changes to the request after this. The hook function receives a state object with the normalized request, options, an error instance, and retry count. You could, for example, modify `request.headers` here.532533The hook can return a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) to replace the outgoing retry request, or return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to skip the retry and use that response instead. **Note:** Returning a request or response skips remaining `beforeRetry` hooks.534535> [!WARNING]536> Returned `Request` objects are used as-is. If you point one at another origin, remove any credentials you do not want forwarded.537538The `retryCount` is always `>= 1`, since this hook is only called during retries, not on the initial request.539540If the request received a response, the error will be of type `HTTPError`. The `Response` object will be available at `error.response`, and the pre-parsed response body will be available at `error.data`. Be aware that some types of errors, such as network errors, inherently mean that a response was not received. In that case, the error will be an instance of `NetworkError` instead of `HTTPError`.541
110 unmodified lines
652This hook enables you to read and optionally modify the response. The hook function receives a state object with the normalized request, options, a clone of the response, and retry count. The return value of the hook function will be used by Ky as the response object if it's an instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).653654You can also force a retry by returning [`ky.retry(options)`](#kyretryoptions). This is useful when you need to retry based on the response body content, even if the response has a successful status code. The retry will respect the `retry.limit` option and be observable in `beforeRetry` hooks.655656> [!WARNING]657> `ky.retry({request})` uses the replacement request as-is. If it targets another origin, remove any credentials you do not want forwarded.658659Any non-`ky.retry()` error thrown by `afterResponse` hooks is treated as fatal and will not trigger Ky's retry logic.660661The `retryCount` is `0` for the initial request and increments with each retry. This allows you to distinguish between the initial request and retries, which is useful when you need different behavior for retries (e.g., showing a notification only on the final retry).662
471 unmodified lines
1134This allows you to modify or completely replace the request during a forced retry. The custom request becomes the starting point for the retry - `beforeRetry` hooks can still further modify it if needed.11351136**Note:** The custom request's `signal` will be replaced with Ky's managed signal to handle timeouts and user-provided abort signals correctly. If the original request body has been consumed, you must provide a new body or clone the request before consuming.11371138> [!WARNING]1139> Custom retry requests are not sanitized. If you reuse headers across origins, remove any credentials you do not want forwarded.11401141#### Example11421143```js1144import ky, {isForceRetryError} from 'ky';
723 unmodified lines