diff --git a/source/create.ts b/source/create.ts index 158ffdb7c..92e21cf95 100644 --- a/source/create.ts +++ b/source/create.ts @@ -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++]( @@ -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; } @@ -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; diff --git a/test/create.ts b/test/create.ts index 75f47cdc1..5f7ffc68b 100644 --- a/test/create.ts +++ b/test/create.ts @@ -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);