Skip to content

Commit

Permalink
Fix default retry option value when specifying a number (#809)
Browse files Browse the repository at this point in the history
Co-authored-by: Szymon Marczak <sz.marczak@gmail.com>
  • Loading branch information
2 people authored and sindresorhus committed Jun 21, 2019
1 parent 3fdabce commit 9c04a7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion source/merge.ts
Expand Up @@ -43,7 +43,24 @@ export default function merge<Target extends Record<string, unknown>, Source ext
}

export function mergeOptions<T extends Options>(...sources: T[]): T & {hooks: Partial<Hooks>} {
const mergedOptions = merge({} as T & {hooks: Partial<Hooks>}, ...sources.map(source => source || {}));
sources = sources.map(source => {
if (!source) {
return {};
}

if (is.object(source.retry)) {
return source;
}

return {
...source,
retry: {
retries: source.retry
}
};
}) as T[];

const mergedOptions = merge({} as T & {hooks: Partial<Hooks>}, ...sources);

const hooks = knownHookEvents.reduce((accumulator, current) => ({...accumulator, [current]: []}), {}) as Record<HookEvent, HookType[]>;

Expand Down
4 changes: 2 additions & 2 deletions test/retry.ts
Expand Up @@ -274,7 +274,7 @@ test('doesn\'t retry when set to false', withServer, async (t, server, got) => {
t.is(retryCount, 0);
});

test('works when defaults.options.retry is not an object', withServer, async (t, server, got) => {
test('works when defaults.options.retry is a number', withServer, async (t, server, got) => {
server.get('/', handler413);

const instance = got.extend({
Expand All @@ -284,7 +284,7 @@ test('works when defaults.options.retry is not an object', withServer, async (t,
const {retryCount} = await instance({
throwHttpErrors: false
});
t.is(retryCount, 0);
t.is(retryCount, 2);
});

test('retry function can throw', withServer, async (t, server, got) => {
Expand Down

0 comments on commit 9c04a7c

Please sign in to comment.