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

Set default value for an options object #1495

Merged
merged 1 commit into from Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions source/create.ts
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
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