Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major refactoring #921

Merged
merged 35 commits into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c29d4d3
init
szmarczak Nov 5, 2019
fcc18f8
Big big changes
szmarczak Nov 6, 2019
b803076
fixes
szmarczak Nov 7, 2019
3ceba12
Merge with master
szmarczak Nov 7, 2019
29fee34
remove unnecessary semicolon
szmarczak Nov 7, 2019
d5a2ff2
enhancements
szmarczak Nov 7, 2019
5236d84
rename stream to isStream
szmarczak Nov 7, 2019
c7dbe1e
throw on legacy url input
szmarczak Nov 7, 2019
f1f203a
enhancements
szmarczak Nov 10, 2019
461e8d9
bug fixes
szmarczak Nov 10, 2019
b044037
fixes
szmarczak Nov 10, 2019
22c36bf
fix option merge
szmarczak Nov 11, 2019
aedac8c
more bug fixes
szmarczak Nov 11, 2019
d7c7d53
fixes
szmarczak Nov 11, 2019
507a3cc
make tests pass
szmarczak Nov 11, 2019
9255df7
remove todo
szmarczak Nov 11, 2019
778cf67
Remove got.create() & update docs
szmarczak Nov 12, 2019
e8ff08b
update docs
szmarczak Nov 12, 2019
0841642
another fix
szmarczak Nov 12, 2019
fe76e8c
nitpick
szmarczak Nov 12, 2019
3def303
types
szmarczak Nov 12, 2019
695ebaf
generic cookiejar object
szmarczak Nov 12, 2019
52496df
make tests pass
szmarczak Nov 12, 2019
c6b22e1
Refactor the got() function, aka: fix bugs
szmarczak Nov 14, 2019
ef32a0e
Throw on null value headers
szmarczak Nov 14, 2019
dde0b76
bug fixes
szmarczak Nov 14, 2019
a0850bd
Improve is usage
szmarczak Nov 14, 2019
0d9baf1
remove useless line
szmarczak Nov 14, 2019
42b2605
comments
szmarczak Nov 14, 2019
3b313a7
call beforeRetry hook when retrying in afterResponse hook
szmarczak Nov 14, 2019
518c00d
nitpicks
szmarczak Nov 15, 2019
7f2f477
nitpicks
szmarczak Nov 15, 2019
5bc7319
nitpicks
szmarczak Nov 15, 2019
3ff5ebb
no unnecessary escape
szmarczak Nov 15, 2019
a7e73f2
Update readme.md
sindresorhus Nov 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ got.post('https://example.com', {
});
```

**Note:** When retrying in a `afterResponse` hook, all remaining `beforeRetry` hooks will be called without the `error` and `retryCount` arguments.
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

###### hooks.afterResponse

Type: `Function[]`<br>
Expand Down
17 changes: 15 additions & 2 deletions source/as-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ export default function asPromise(options: NormalizedOptions) {

try {
for (const [index, hook] of options.hooks.afterResponse.entries()) {
// @ts-ignore
// eslint-disable-next-line no-await-in-loop
response = await hook(response, updatedOptions => {
response = await hook(response, async (updatedOptions: NormalizedOptions) => {
updatedOptions = normalizeArguments(mergeOptions(options, {
...updatedOptions,
retry: {
Expand All @@ -85,7 +86,19 @@ export default function asPromise(options: NormalizedOptions) {
// The loop continues. We don't want duplicates (asPromise recursion).
updatedOptions.hooks.afterResponse = options.hooks.afterResponse.slice(0, index);

return asPromise(updatedOptions);
for (const hook of options.hooks.beforeRetry) {
// eslint-disable-next-line no-await-in-loop
await hook(updatedOptions);
}

const promise = asPromise(updatedOptions);

onCancel(() => {
promise.catch(() => {});
promise.cancel();
});

return promise;
});
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion source/known-hook-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type BeforeRedirectHook = (options: NormalizedOptions, response: Response
/**
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.
*/
export type BeforeRetryHook = (options: NormalizedOptions, error: Error | GotError | ParseError | HTTPError | MaxRedirectsError, retryCount: number) => void | Promise<void>;
export type BeforeRetryHook = (options: NormalizedOptions, error?: Error | GotError | ParseError | HTTPError | MaxRedirectsError, retryCount?: number) => void | Promise<void>;
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

/**
Called with an `Error` instance. The error is passed to the hook right before it's thrown. This is especially useful when you want to have more detailed errors.
Expand Down
38 changes: 38 additions & 0 deletions test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,44 @@ test('afterResponse allows to retry', withServer, async (t, server, got) => {
t.is(statusCode, 200);
});

test('afterResponse allows to retry - `beforeRetry` hook', withServer, async (t, server, got) => {
server.get('/', (request, response) => {
if (request.headers.token !== 'unicorn') {
response.statusCode = 401;
}

response.end();
});

let called = false;

const {statusCode} = await got({
hooks: {
afterResponse: [
(response, retryWithMergedOptions) => {
if (response.statusCode === 401) {
return retryWithMergedOptions({
headers: {
token: 'unicorn'
}
});
}

return response;
}
],
beforeRetry: [
options => {
t.truthy(options);
called = true;
}
]
}
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
});
t.is(statusCode, 200);
t.true(called);
});

test('no infinity loop when retrying on afterResponse', withServer, async (t, server, got) => {
server.get('/', (request, response) => {
if (request.headers.token !== 'unicorn') {
Expand Down