Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Normalize URL
// TODO: drop `optionsToUrl` in Got 12
if (is.string(options.url)) {
options.url = (options.prefixUrl as string) + options.url;
options.url = options.url.replace(/^unix:/, 'http://$&');
if (options.searchParams || options.search) {
options.url = options.url.split('?')[0];
}
options.url = optionsToUrl({
origin: options.url,
...options
});
} else if (!is.urlInstance(options.url)) {
options.url = optionsToUrl({origin: options.prefixUrl as string, ...options});
}
const normalizedOptions = options as NormalizedOptions;
// Make it possible to change `options.prefixUrl`
let prefixUrl = options.prefixUrl as string;
Object.defineProperty(normalizedOptions, 'prefixUrl', {
set: (value: string) => {
if (!normalizedOptions.url.href.startsWith(value)) {
throw new Error(`Cannot change \`prefixUrl\` from ${prefixUrl} to ${value}: ${normalizedOptions.url.href}`);
}
normalizedOptions.url = new URL(value + normalizedOptions.url.href.slice(prefixUrl.length));
prefixUrl = value;
},
for (const [key, sourceValue] of Object.entries(source)) {
if (is.undefined(sourceValue)) {
continue;
}
const targetValue = target[key];
if (targetValue instanceof URLSearchParams && sourceValue instanceof URLSearchParams) {
const params = new URLSearchParams();
const append = (value: string, key: string): void => params.append(key, value);
targetValue.forEach(append);
sourceValue.forEach(append);
// @ts-ignore https://github.com/microsoft/TypeScript/issues/31661
target[key] = params;
} else if (is.urlInstance(targetValue) && (is.urlInstance(sourceValue) || is.string(sourceValue))) {
// @ts-ignore
target[key] = new URL(sourceValue as string, targetValue);
} else if (is.plainObject(sourceValue)) {
if (is.plainObject(targetValue)) {
// @ts-ignore
target[key] = merge({}, targetValue, sourceValue);
} else {
// @ts-ignore
target[key] = merge({}, sourceValue);
}
} else if (is.array(sourceValue)) {
// @ts-ignore
target[key] = sourceValue.slice();
} else {
// @ts-ignore
target[key] = sourceValue;
export const normalizeArguments = (url: URLOrOptions, options?: Options, defaults?: Defaults): NormalizedOptions => {
// Merge options
if (typeof url === 'undefined') {
throw new TypeError('Missing `url` argument');
}
if (typeof options === 'undefined') {
options = {};
}
if (is.urlInstance(url) || is.string(url)) {
if (Reflect.has(options, 'url')) {
throw new TypeError('The `url` option cannot be used if the input is valid URL.');
}
// @ts-ignore URL is not URL
options.url = url;
options = mergeOptions(defaults?.options ?? {}, options);
} else {
if (Reflect.has(url, 'resolve')) {
throw new Error('The legacy `url.Url` is deprecated. Use `URL` instead.');
}
options = mergeOptions(defaults?.options ?? {}, url, options);
}
export const preNormalizeArguments = (options: Options, defaults?: NormalizedOptions): NormalizedOptions => {
// `options.headers`
if (is.undefined(options.headers)) {
options.headers = {};
} else {
options.headers = lowercaseKeys(options.headers);
}
for (const [key, value] of Object.entries(options.headers)) {
if (is.null_(value)) {
throw new TypeError(`Use \`undefined\` instead of \`null\` to delete the \`${key}\` header`);
}
}
// `options.prefixUrl`
if (is.urlInstance(options.prefixUrl) || is.string(options.prefixUrl)) {
options.prefixUrl = options.prefixUrl.toString();
if (options.prefixUrl.length !== 0 && !options.prefixUrl.endsWith('/')) {
options.prefixUrl += '/';
}
} else {
options.prefixUrl = defaults ? defaults.prefixUrl : '';
}
// `options.hooks`
if (is.undefined(options.hooks)) {
options.hooks = {};
}
if (is.object(options.hooks)) {
for (const event of knownHookEvents) {
export default function merge(target: Target, ...sources: Source[]): Merge {
for (const source of sources) {
for (const [key, sourceValue] of Object.entries(source)) {
const targetValue = target[key];
if (is.urlInstance(targetValue) && is.string(sourceValue)) {
// @ts-ignore TS doesn't recognise Target accepts string keys
target[key] = new URL(sourceValue, targetValue);
} else if (is.plainObject(sourceValue)) {
if (is.plainObject(targetValue)) {
// @ts-ignore TS doesn't recognise Target accepts string keys
target[key] = merge({}, targetValue, sourceValue);
} else {
// @ts-ignore TS doesn't recognise Target accepts string keys
target[key] = merge({}, sourceValue);
}
} else if (is.array(sourceValue)) {
// @ts-ignore TS doesn't recognise Target accepts string keys
target[key] = sourceValue.slice();
} else {
// @ts-ignore TS doesn't recognise Target accepts string keys
target[key] = sourceValue;
test('`origin` option', t => {
const url = optionsToUrl({origin});
t.is(url.href, `${origin}/`);
t.true(is.urlInstance(url));
});