diff --git a/index.d.ts b/index.d.ts index 55b6947d..997870cc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,12 +13,13 @@ export type BeforeRequestHook = ( options: NormalizedOptions, ) => Request | Response | void | Promise; -export type BeforeRetryHook = ( +export type BeforeRetryHook = (options: { request: Request, + response: Response, options: NormalizedOptions, error: Error, retryCount: number, -) => void | Promise; +}) => void | Promise; export type AfterResponseHook = ( request: Request, diff --git a/index.js b/index.js index a8b2ff0f..97f1023f 100644 --- a/index.js +++ b/index.js @@ -351,12 +351,13 @@ class Ky { for (const hook of this._options.hooks.beforeRetry) { // eslint-disable-next-line no-await-in-loop - const hookResult = await hook( - this.request, - this._options, + const hookResult = await hook({ + request: this.request, + options: this._options, error, - this._retryCount - ); + response: error.response.clone(), + retryCount: this._retryCount + }); // If `stop` is returned from the hook, the retry process is stopped if (hookResult === stop) { diff --git a/index.test-d.ts b/index.test-d.ts index 9568e096..1931b815 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -48,8 +48,9 @@ ky(url, { } ], beforeRetry: [ - (request, options, error, retryCount) => { + ({request, response, options, error, retryCount}) => { expectType(request); + expectType(response); expectType(options); expectType(error); expectType(retryCount); diff --git a/readme.md b/readme.md index 5ed1c83a..ec648c2e 100644 --- a/readme.md +++ b/readme.md @@ -253,7 +253,7 @@ const api = ky.extend({ Type: `Function[]`\ Default: `[]` -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 the normalized request and options, an error instance and the retry count as arguments. You could, for example, modify `request.headers` here. +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 the normalized request and options, the failed response, an error instance and the retry count as arguments. You could, for example, modify `request.headers` here. ```js import ky from 'ky'; @@ -262,7 +262,7 @@ import ky from 'ky'; await ky('https://example.com', { hooks: { beforeRetry: [ - async (request, options, errors, retryCount) => { + async ({request, response, options, errors, retryCount}) => { const token = await ky('https://example.com/refresh-token'); request.headers.set('Authorization', `token ${token}`); } @@ -395,7 +395,7 @@ import ky from 'ky'; await ky('https://example.com', { hooks: { beforeRetry: [ - async (request, options, errors, retryCount) => { + async ({request, response, options, errors, retryCount}) => { const shouldStopRetry = await ky('https://example.com/api'); if (shouldStopRetry) { return ky.stop; diff --git a/test/hooks.js b/test/hooks.js index a7abd8e1..ab077f7e 100644 --- a/test/hooks.js +++ b/test/hooks.js @@ -304,7 +304,7 @@ test('beforeRetry hook is never called for the initial request', async t => { .get(server.url, { hooks: { beforeRetry: [ - (_input, options) => { + ({options}) => { options.headers.set('unicorn', fixture); } ] @@ -337,7 +337,7 @@ test('beforeRetry hook allows modifications of non initial requests', async t => .get(server.url, { hooks: { beforeRetry: [ - request => { + ({request}) => { request.headers.set('unicorn', fixture); } ] @@ -367,7 +367,7 @@ test('beforeRetry hook is called with error and retryCount', async t => { await ky.get(server.url, { hooks: { beforeRetry: [ - (_input, options, error, retryCount) => { + ({error, retryCount}) => { t.truthy(error); t.true(retryCount >= 1); } @@ -395,7 +395,7 @@ test('beforeRetry hook can cancel retries by returning `stop`', async t => { await ky.get(server.url, { hooks: { beforeRetry: [ - (_input, options, error, retryCount) => { + ({error, retryCount}) => { t.truthy(error); t.is(retryCount, 1);