You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+152Lines changed: 152 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -489,6 +489,8 @@ Default: `[]`
489
489
490
490
This hook enables you to read and optionally modify the response. The hook function receives normalized request, options, a clone of the response, and a state object. 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).
491
491
492
+
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.
493
+
492
494
The `state.retryCount` is `0` for the initial request and increments with each retry. This allows you to distinguish between initial requests and retries, which is useful when you need different behavior for retries (e.g., showing a notification only on the final retry).
This allows you to retry a request based on the response content, even if the response has a successful status code. The retry will respect the `retry.limit` option and skip the `shouldRetry` check. The forced retry is observable in `beforeRetry` hooks, where the error will be a `ForceRetryError` with the error name `'ForceRetryError'`.
866
+
867
+
#### options
868
+
869
+
Type: `object`
870
+
871
+
##### delay
872
+
873
+
Type: `number`
874
+
875
+
Custom delay in milliseconds before retrying. If not provided, uses the default retry delay calculation based on `retry.delay` configuration.
876
+
877
+
**Note:** Custom delays bypass jitter and `backoffLimit`. This is intentional, as custom delays often come from server responses (e.g., `Retry-After` headers) and should be respected exactly as specified.
878
+
879
+
##### code
880
+
881
+
Type: `string`
882
+
883
+
Error code for the retry.
884
+
885
+
This machine-readable identifier will be included in the error message passed to `beforeRetry` hooks, allowing you to distinguish between different types of forced retries.
Original error that caused the retry. This allows you to preserve the error chain when forcing a retry based on caught exceptions. The error will be set as the `cause` of the `ForceRetryError`, enabling proper error chain traversal.
897
+
898
+
```js
899
+
try {
900
+
constdata=awaitresponse.clone().json();
901
+
validateBusinessLogic(data);
902
+
} catch (error) {
903
+
returnky.retry({
904
+
code:'VALIDATION_FAILED',
905
+
cause: error // Preserves original error in chain
906
+
});
907
+
}
908
+
```
909
+
910
+
##### request
911
+
912
+
Type: `Request`
913
+
914
+
Custom request to use for the retry.
915
+
916
+
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.
917
+
918
+
**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.
919
+
920
+
#### Example
921
+
922
+
```js
923
+
importky, {isForceRetryError} from'ky';
924
+
925
+
constapi=ky.extend({
926
+
hooks: {
927
+
afterResponse: [
928
+
async (request, options, response) => {
929
+
// Retry based on response body content
930
+
if (response.status===200) {
931
+
constdata=awaitresponse.clone().json();
932
+
933
+
// Simple retry with default delay
934
+
if (data.error?.code==='TEMPORARY_ERROR') {
935
+
returnky.retry();
936
+
}
937
+
938
+
// Retry with custom delay from API response
939
+
if (data.error?.code==='RATE_LIMIT') {
940
+
returnky.retry({
941
+
delay:data.error.retryAfter*1000,
942
+
code:'RATE_LIMIT'
943
+
});
944
+
}
945
+
946
+
// Retry with a modified request (e.g., fallback endpoint)
Exposed for `instanceof` checks. The error has a `response` property with the [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response), `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request), and `options` property with normalized options (either passed to `ky` when creating an instance with `ky.create()` or directly when performing the request).
0 commit comments