Skip to content

Commit

Permalink
Pass the response as the second argument to the beforeRedirect hook (
Browse files Browse the repository at this point in the history
  • Loading branch information
clemyan authored and sindresorhus committed Jun 24, 2019
1 parent 0501e00 commit 3557896
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -408,15 +408,15 @@ See the [AWS section](#aws) for an example.
Type: `Function[]`<br>
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');

got('https://example.com', {
hooks: {
beforeRedirect: [
options => {
(options, response) => {
if (options.hostname === 'deadSite') {
options.hostname = 'fallbackSite';
}
Expand Down
2 changes: 1 addition & 1 deletion source/known-hook-events.ts
Expand Up @@ -20,7 +20,7 @@ export type BeforeRequestHook = (options: NormalizedOptions) => void | Promise<v
/**
Called with normalized [request options](https://github.com/sindresorhus/got#options). Got will make no further changes to the request. This is especially useful when you want to avoid dead sites.
*/
export type BeforeRedirectHook = (options: NormalizedOptions) => void | Promise<void>;
export type BeforeRedirectHook = (options: NormalizedOptions, response: Response) => void | Promise<void>;

/**
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.
Expand Down
2 changes: 1 addition & 1 deletion source/request-as-event-emitter.ts
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions test/hooks.ts
@@ -1,3 +1,4 @@
import {URL} from 'url';
import test from 'ava';
import delay = require('delay');
import got from '../source';
Expand Down Expand Up @@ -222,17 +223,21 @@ 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);

await got('redirect', {
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);
}
]
}
Expand Down

0 comments on commit 3557896

Please sign in to comment.