diff --git a/source/normalize-arguments.ts b/source/normalize-arguments.ts index 831b7981c..8df083614 100644 --- a/source/normalize-arguments.ts +++ b/source/normalize-arguments.ts @@ -293,11 +293,20 @@ export const normalizeArguments = (url: URLOrOptions, options?: Options, default options = {}; } + const {url: optionsUrl} = options; + // @ts-ignore URL is not URL options.url = url; runInitHooks(defaults?.options.hooks.init, options); runInitHooks(options.hooks?.init, options); + + url = options.url as URLOrOptions; + if (optionsUrl) { + options.url = optionsUrl; + } else { + delete options.url; + } } else if (Reflect.has(url as object, 'resolve')) { throw new Error('The legacy `url.Url` is deprecated. Use `URL` instead.'); } else { diff --git a/test/normalize-arguments.ts b/test/normalize-arguments.ts index 4f60b124c..8f40f1149 100644 --- a/test/normalize-arguments.ts +++ b/test/normalize-arguments.ts @@ -1,5 +1,6 @@ import test from 'ava'; -import got from '../source'; +import got, {Options} from '../source'; +import {normalizeArguments} from '../source/normalize-arguments'; test('should merge options replacing responseType', t => { const responseType = 'json'; @@ -9,3 +10,14 @@ test('should merge options replacing responseType', t => { t.is(options.responseType, responseType); }); + +test('should be able to reuse options', t => { + const options: Options = {}; + normalizeArguments('http://localhost', options); + t.notThrows(() => normalizeArguments('http://localhost', options)); +}); + +test.failing('should handle frozen objects', t => { + const options: Options = Object.freeze({}); + t.notThrows(() => normalizeArguments('http://localhost', options)); +});