Skip to content

Commit

Permalink
Fix default retry option value when specifying a number (sindresorh…
Browse files Browse the repository at this point in the history
…us#809)

Co-authored-by: Szymon Marczak <sz.marczak@gmail.com>
  • Loading branch information
zero1five and szmarczak committed Jun 25, 2019
1 parent c82447c commit d97532d
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 @@ -52,7 +52,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 d97532d

Please sign in to comment.