Skip to content

Commit 390b145

Browse files
authoredOct 9, 2020
Set default value for an options object (#1495)
1 parent 87dadd5 commit 390b145

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed
 

‎source/create.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const create = (defaults: InstanceDefaults): Got => {
125125
}));
126126

127127
// Got interface
128-
const got: Got = ((url: string | URL, options?: Options, _defaults?: Defaults): GotReturn => {
128+
const got: Got = ((url: string | URL, options: Options = {}, _defaults?: Defaults): GotReturn => {
129129
let iteration = 0;
130130
const iterateHandlers = (newOptions: NormalizedOptions): GotReturn => {
131131
return defaults.handlers[iteration++](
@@ -152,7 +152,7 @@ const create = (defaults: InstanceDefaults): Got => {
152152
let initHookError: Error | undefined;
153153
try {
154154
callInitHooks(defaults.options.hooks.init, options);
155-
callInitHooks(options?.hooks?.init, options);
155+
callInitHooks(options.hooks?.init, options);
156156
} catch (error) {
157157
initHookError = error;
158158
}
@@ -167,10 +167,10 @@ const create = (defaults: InstanceDefaults): Got => {
167167

168168
return iterateHandlers(normalizedOptions);
169169
} catch (error) {
170-
if (options?.isStream) {
170+
if (options.isStream) {
171171
throw error;
172172
} else {
173-
return createRejection(error, defaults.options.hooks.beforeError, options?.hooks?.beforeError);
173+
return createRejection(error, defaults.options.hooks.beforeError, options.hooks?.beforeError);
174174
}
175175
}
176176
}) as Got;

‎test/create.ts

+18
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ test('does not include the `request` option in normalized `http` options', withS
231231
t.true(isCalled);
232232
});
233233

234+
test('should pass an options object into an initialization hook after .extend', withServer, async (t, server, got) => {
235+
t.plan(1);
236+
237+
server.get('/', echoHeaders);
238+
239+
const instance = got.extend({
240+
hooks: {
241+
init: [
242+
options => {
243+
t.deepEqual(options, {});
244+
}
245+
]
246+
}
247+
});
248+
249+
await instance('');
250+
});
251+
234252
test('hooks aren\'t overriden when merging options', withServer, async (t, server, got) => {
235253
server.get('/', echoHeaders);
236254

0 commit comments

Comments
 (0)
Please sign in to comment.