Skip to content

Commit

Permalink
Add response to beforeRetry hook and change its parameter to a si…
Browse files Browse the repository at this point in the history
…ngle object (#201)

Co-authored-by: Seth Holladay <me@seth-holladay.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
3 people committed Feb 21, 2020
1 parent d5cb15a commit 3f3e14d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
5 changes: 3 additions & 2 deletions index.d.ts
Expand Up @@ -13,12 +13,13 @@ export type BeforeRequestHook = (
options: NormalizedOptions,
) => Request | Response | void | Promise<Request | Response | void>;

export type BeforeRetryHook = (
export type BeforeRetryHook = (options: {
request: Request,
response: Response,
options: NormalizedOptions,
error: Error,
retryCount: number,
) => void | Promise<void>;
}) => void | Promise<void>;

export type AfterResponseHook = (
request: Request,
Expand Down
11 changes: 6 additions & 5 deletions index.js
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion index.test-d.ts
Expand Up @@ -48,8 +48,9 @@ ky(url, {
}
],
beforeRetry: [
(request, options, error, retryCount) => {
({request, response, options, error, retryCount}) => {
expectType<Request>(request);
expectType<Response>(response);
expectType<NormalizedOptions>(options);
expectType<Error>(error);
expectType<number>(retryCount);
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -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';
Expand All @@ -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}`);
}
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions test/hooks.js
Expand Up @@ -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);
}
]
Expand Down Expand Up @@ -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);
}
]
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 3f3e14d

Please sign in to comment.