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
Provenance
- Commit
8f28eac- Tree
dbf4764- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
346f898
4 files changed+22 added−1 removed
modified:source/core/constants.ts+2 added
116 unmodified lines117 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.118 119 **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.120 121+ **Warning:** Custom retry requests are not sanitized. If you reuse headers across origins, remove any credentials you do not want forwarded.122+123 @example124 ```125 // Fallback to a different endpoint126 return ky.retry({158 unmodified linesmodified:source/core/Ky.ts+7 added-1 removed
873 unmodified lines874 875 // 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 }884 885 for (const hook of this.#options.hooks.beforeRetry) {886 let hookResult: Awaited<ReturnType<typeof hook>>;14 unmodified lines901 throw hookError;902 }903 904 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 linesmodified:source/types/hooks.ts+4 added
169 unmodified lines170 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.171 172 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.173 174+ **Warning:** Returned `Request` objects are used as-is. If you point one at another origin, remove any credentials you do not want forwarded.175+176 The `retryCount` is always `>= 1`, since this hook is only called during retries, not on the initial request.177 178 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 lines294 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).295 296 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.297 298+ **Warning:** `ky.retry({request})` uses the replacement request as-is. If it targets another origin, remove any credentials you do not want forwarded.299+300 Any non-`ky.retry()` error thrown by `afterResponse` hooks is treated as fatal and will not trigger Ky's retry logic.301 302 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 linesmodified:readme.md+9 added
530 unmodified lines531 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.532 533 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.534 535+> [!WARNING]536+> Returned `Request` objects are used as-is. If you point one at another origin, remove any credentials you do not want forwarded.537+538 The `retryCount` is always `>= 1`, since this hook is only called during retries, not on the initial request.539 540 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`.541 110 unmodified lines652 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).653 654 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.655 656+> [!WARNING]657+> `ky.retry({request})` uses the replacement request as-is. If it targets another origin, remove any credentials you do not want forwarded.658+659 Any non-`ky.retry()` error thrown by `afterResponse` hooks is treated as fatal and will not trigger Ky's retry logic.660 661 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).662 471 unmodified lines1134 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.1135 1136 **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.1137 1138+> [!WARNING]1139+> Custom retry requests are not sanitized. If you reuse headers across origins, remove any credentials you do not want forwarded.1140+1141 #### Example1142 1143 ```js1144 import ky, {isForceRetryError} from 'ky';723 unmodified lines