Skip to content

Commit 64aeb40

Browse files
committedFeb 16, 2020
Fix Init hook logic
That was a regression - it was the same as the `beforeRequest` hook
1 parent 32e609f commit 64aeb40

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed
 

‎source/known-hook-events.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Called with plain request options, right before their normalization. This is esp
77
88
@see [Request migration guide](https://github.com/sindresorhus/got/blob/master/migration-guides.md#breaking-changes) for an example.
99
*/
10-
export type InitHook = (options: NormalizedOptions) => void;
10+
export type InitHook = (options: Options) => void;
1111

1212
/**
1313
Called with normalized [request options](https://github.com/sindresorhus/got#options). Got will make no further changes to the request before it is sent (except the body serialization). This is especially useful in conjunction with [`got.extend()`](https://github.com/sindresorhus/got#instances) when you want to create an API client that, for example, uses HMAC-signing.

‎source/normalize-arguments.ts

+25-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import is from '@sindresorhus/is';
1111
import CacheableLookup from 'cacheable-lookup';
1212
import {Merge} from 'type-fest';
1313
import {UnsupportedProtocolError} from './errors';
14-
import knownHookEvents from './known-hook-events';
14+
import knownHookEvents, {InitHook} from './known-hook-events';
1515
import dynamicRequire from './utils/dynamic-require';
1616
import getBodySize from './utils/get-body-size';
1717
import isFormData from './utils/is-form-data';
@@ -274,21 +274,39 @@ export const normalizeArguments = (url: URLOrOptions, options?: Options, default
274274
options = {};
275275
}
276276

277-
if (is.urlInstance(url) || is.string(url)) {
277+
const runInitHooks = (hooks?: InitHook[], options?: Options): void => {
278+
if (hooks) {
279+
for (const hook of hooks) {
280+
const result = hook(options!);
281+
282+
if (is.promise(result)) {
283+
throw new TypeError('The `init` hook must be a synchronous function');
284+
}
285+
}
286+
}
287+
};
288+
289+
const hasUrl = is.urlInstance(url) || is.string(url);
290+
if (hasUrl) {
278291
if (Reflect.has(options, 'url')) {
279292
throw new TypeError('The `url` option cannot be used if the input is a valid URL.');
280293
}
281294

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

285-
options = mergeOptions(defaults?.options ?? {}, options);
298+
runInitHooks(options.hooks?.init, options);
299+
} else if (Reflect.has(url as object, 'resolve')) {
300+
throw new Error('The legacy `url.Url` is deprecated. Use `URL` instead.');
286301
} else {
287-
if (Reflect.has(url, 'resolve')) {
288-
throw new Error('The legacy `url.Url` is deprecated. Use `URL` instead.');
289-
}
302+
runInitHooks((url as Options).hooks?.init, url as Options);
303+
runInitHooks(options.hooks?.init, options);
304+
}
290305

291-
options = mergeOptions(defaults?.options ?? {}, url, options);
306+
if (hasUrl) {
307+
options = mergeOptions(defaults?.options ?? {}, options);
308+
} else {
309+
options = mergeOptions(defaults?.options ?? {}, url as object, options);
292310
}
293311

294312
// Normalize URL
@@ -335,14 +353,6 @@ export const normalizeArguments = (url: URLOrOptions, options?: Options, default
335353
}
336354
}
337355

338-
for (const hook of normalizedOptions.hooks.init) {
339-
const result = hook(normalizedOptions);
340-
341-
if (is.promise(result)) {
342-
throw new TypeError('The `init` hook must be a synchronous function');
343-
}
344-
}
345-
346356
return normalizedOptions;
347357
};
348358

‎test/arguments.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ test('`context` option is accessible when using hooks', withServer, async (t, se
330330
await got({
331331
context,
332332
hooks: {
333-
init: [
333+
beforeRequest: [
334334
options => {
335335
t.is(options.context, context);
336336
t.false({}.propertyIsEnumerable.call(options, 'context'));

‎test/hooks.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,18 @@ test('catches beforeError errors', async t => {
186186
test('init is called with options', withServer, async (t, server, got) => {
187187
server.get('/', echoHeaders);
188188

189+
const context = {};
190+
189191
await got({
190192
hooks: {
191193
init: [
192194
options => {
193-
t.is(options.url.pathname, '/');
194-
t.is(options.url.hostname, 'localhost');
195+
t.is(options.url, undefined);
196+
t.is(options.context, context);
195197
}
196198
]
197-
}
199+
},
200+
context
198201
});
199202
});
200203

@@ -203,11 +206,13 @@ test('init allows modifications', withServer, async (t, server, got) => {
203206
response.end(request.headers.foo);
204207
});
205208

206-
const {body} = await got({
209+
const {body} = await got('meh', {
210+
headers: {},
207211
hooks: {
208212
init: [
209213
options => {
210-
options.headers.foo = 'bar';
214+
options.url = '';
215+
options.headers!.foo = 'bar';
211216
}
212217
]
213218
}
@@ -679,7 +684,7 @@ test('timeout can be modified using a hook', withServer, async (t, server, got)
679684
await t.throwsAsync(got({
680685
timeout: 1000,
681686
hooks: {
682-
init: [
687+
beforeRequest: [
683688
options => {
684689
options.timeout.request = 500;
685690
}

0 commit comments

Comments
 (0)
Please sign in to comment.