Skip to content

Commit

Permalink
Fix Init hooks not being called if extended
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Feb 16, 2020
1 parent 68b0d80 commit acefaa4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
28 changes: 17 additions & 11 deletions source/normalize-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,10 @@ export const normalizeArguments = (url: URLOrOptions, options?: Options, default
throw new TypeError('Missing `url` argument');
}

if (typeof options === 'undefined') {
options = {};
}

const runInitHooks = (hooks?: InitHook[], options?: Options): void => {
if (hooks) {
if (hooks && options) {
for (const hook of hooks) {
const result = hook(options!);
const result = hook(options);

if (is.promise(result)) {
throw new TypeError('The `init` hook must be a synchronous function');
Expand All @@ -288,25 +284,35 @@ export const normalizeArguments = (url: URLOrOptions, options?: Options, default

const hasUrl = is.urlInstance(url) || is.string(url);
if (hasUrl) {
if (Reflect.has(options, 'url')) {
throw new TypeError('The `url` option cannot be used if the input is a valid URL.');
if (options) {
if (Reflect.has(options, 'url')) {
throw new TypeError('The `url` option cannot be used if the input is a valid URL.');
}
} else {
options = {};
}

// @ts-ignore URL is not URL
options.url = url;

runInitHooks(defaults?.options.hooks.init, options);
runInitHooks(options.hooks?.init, options);
} else if (Reflect.has(url as object, 'resolve')) {
throw new Error('The legacy `url.Url` is deprecated. Use `URL` instead.');
} else {
runInitHooks(defaults?.options.hooks.init, url as Options);
runInitHooks((url as Options).hooks?.init, url as Options);
runInitHooks(options.hooks?.init, options);

if (options) {
runInitHooks(defaults?.options.hooks.init, options);
runInitHooks(options.hooks?.init, options);
}
}

if (hasUrl) {
options = mergeOptions(defaults?.options ?? {}, options);
options = mergeOptions(defaults?.options ?? {}, options ?? {});
} else {
options = mergeOptions(defaults?.options ?? {}, url as object, options);
options = mergeOptions(defaults?.options ?? {}, url as object, options ?? {});
}

// Normalize URL
Expand Down
19 changes: 19 additions & 0 deletions test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,25 @@ test('init is called with options', withServer, async (t, server, got) => {
});
});

test('init from defaults is called with options', withServer, async (t, server, got) => {
server.get('/', echoHeaders);

const context = {};

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

await instance({context});
});

test('init allows modifications', withServer, async (t, server, got) => {
server.get('/', (request, response) => {
response.end(request.headers.foo);
Expand Down

0 comments on commit acefaa4

Please sign in to comment.