From 3557896409751e3ced608456e23f5dcfc2419b37 Mon Sep 17 00:00:00 2001 From: Clement Yan Date: Mon, 24 Jun 2019 15:36:59 +0800 Subject: [PATCH] Pass the response as the second argument to the `beforeRedirect` hook (#812) --- readme.md | 4 ++-- source/known-hook-events.ts | 2 +- source/request-as-event-emitter.ts | 2 +- test/hooks.ts | 9 +++++++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 502ea12fb..c52de9442 100644 --- a/readme.md +++ b/readme.md @@ -408,7 +408,7 @@ See the [AWS section](#aws) for an example. Type: `Function[]`
Default: `[]` -Called with [normalized](source/normalize-arguments.ts) [request options](#options). Got will make no further changes to the request. This is especially useful when you want to avoid dead sites. Example: +Called with [normalized](source/normalize-arguments.ts) [request options](#options) and the redirect [response](#response). Got will make no further changes to the request. This is especially useful when you want to avoid dead sites. Example: ```js const got = require('got'); @@ -416,7 +416,7 @@ const got = require('got'); got('https://example.com', { hooks: { beforeRedirect: [ - options => { + (options, response) => { if (options.hostname === 'deadSite') { options.hostname = 'fallbackSite'; } diff --git a/source/known-hook-events.ts b/source/known-hook-events.ts index 124541065..9f0932411 100644 --- a/source/known-hook-events.ts +++ b/source/known-hook-events.ts @@ -20,7 +20,7 @@ export type BeforeRequestHook = (options: NormalizedOptions) => void | Promise void | Promise; +export type BeforeRedirectHook = (options: NormalizedOptions, response: Response) => void | Promise; /** Called with normalized [request options](https://github.com/sindresorhus/got#options), the error and the retry count. Got will make no further changes to the request. This is especially useful when some extra work is required before the next try. diff --git a/source/request-as-event-emitter.ts b/source/request-as-event-emitter.ts index 8e815385d..aecc1b701 100644 --- a/source/request-as-event-emitter.ts +++ b/source/request-as-event-emitter.ts @@ -163,7 +163,7 @@ export default (options: NormalizedOptions, input?: TransformStream) => { for (const hook of options.hooks.beforeRedirect) { // eslint-disable-next-line no-await-in-loop - await hook(redirectOptions); + await hook(redirectOptions, typedResponse); } emitter.emit('redirect', response, redirectOptions); diff --git a/test/hooks.ts b/test/hooks.ts index 25af7476a..c623e9218 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -1,3 +1,4 @@ +import {URL} from 'url'; import test from 'ava'; import delay = require('delay'); import got from '../source'; @@ -222,7 +223,7 @@ test('beforeRequest allows modifications', withServer, async (t, server, got) => t.is(body.foo, 'bar'); }); -test('beforeRedirect is called with options', withServer, async (t, server, got) => { +test('beforeRedirect is called with options and response', withServer, async (t, server, got) => { server.get('/', echoHeaders); server.get('/redirect', redirectEndpoint); @@ -230,9 +231,13 @@ test('beforeRedirect is called with options', withServer, async (t, server, got) responseType: 'json', hooks: { beforeRedirect: [ - options => { + (options, response) => { t.is(options.path, '/'); t.is(options.hostname, 'localhost'); + + t.is(response.statusCode, 302); + t.is(new URL(response.url).pathname, '/redirect'); + t.is(response.redirectUrls.length, 1); } ] }