Skip to content

Commit

Permalink
Set default value for an options object (#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc authored Oct 9, 2020
1 parent 87dadd5 commit 390b145
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const create = (defaults: InstanceDefaults): Got => {
}));

// Got interface
const got: Got = ((url: string | URL, options?: Options, _defaults?: Defaults): GotReturn => {
const got: Got = ((url: string | URL, options: Options = {}, _defaults?: Defaults): GotReturn => {
let iteration = 0;
const iterateHandlers = (newOptions: NormalizedOptions): GotReturn => {
return defaults.handlers[iteration++](
Expand All @@ -152,7 +152,7 @@ const create = (defaults: InstanceDefaults): Got => {
let initHookError: Error | undefined;
try {
callInitHooks(defaults.options.hooks.init, options);
callInitHooks(options?.hooks?.init, options);
callInitHooks(options.hooks?.init, options);
} catch (error) {
initHookError = error;
}
Expand All @@ -167,10 +167,10 @@ const create = (defaults: InstanceDefaults): Got => {

return iterateHandlers(normalizedOptions);
} catch (error) {
if (options?.isStream) {
if (options.isStream) {
throw error;
} else {
return createRejection(error, defaults.options.hooks.beforeError, options?.hooks?.beforeError);
return createRejection(error, defaults.options.hooks.beforeError, options.hooks?.beforeError);
}
}
}) as Got;
Expand Down
18 changes: 18 additions & 0 deletions test/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ test('does not include the `request` option in normalized `http` options', withS
t.true(isCalled);
});

test('should pass an options object into an initialization hook after .extend', withServer, async (t, server, got) => {
t.plan(1);

server.get('/', echoHeaders);

const instance = got.extend({
hooks: {
init: [
options => {
t.deepEqual(options, {});
}
]
}
});

await instance('');
});

test('hooks aren\'t overriden when merging options', withServer, async (t, server, got) => {
server.get('/', echoHeaders);

Expand Down

0 comments on commit 390b145

Please sign in to comment.