Skip to content

Commit

Permalink
Make URLSearchParams mergeable (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Feb 24, 2019
1 parent c88c473 commit 95c7c2c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -726,6 +726,7 @@ got.mergeOptions(a, b) // => {headers: {cat: 'meow', cow: 'moo', wolf: ['auuu']
Options are deeply merged to a new object. The value of each key is determined as follows:

- If the new property is set to `undefined`, it keeps the old one.
- If both properties are an instances of `URLSearchParams`, a new URLSearchParams instance is created. The values are merged using [`urlSearchParams.append(key, value)`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/append).
- If the parent property is an instance of `URL` and the new value is a `string` or `URL`, a new URL instance is created: [`new URL(new, parent)`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#Syntax).
- If the new property is a plain `Object`:
- If the parent property is a plain `Object` too, both values are merged recursively into a new `Object`.
Expand Down
12 changes: 10 additions & 2 deletions source/merge.ts
@@ -1,4 +1,4 @@
import {URL} from 'url';
import {URL, URLSearchParams} from 'url';
import is from '@sindresorhus/is';
import {Options, Method, NextFunction, Instance, InterfaceWithDefaults} from './utils/types';
import knownHookEvents, {Hooks, HookType, HookEvent} from './known-hook-events';
Expand All @@ -11,7 +11,15 @@ export default function merge<Target extends {[key: string]: unknown}, Source ex
}

const targetValue = target[key];
if (is.urlInstance(targetValue) && (is.urlInstance(sourceValue) || is.string(sourceValue))) {
if (targetValue instanceof URLSearchParams && sourceValue instanceof URLSearchParams) {
const params = new URLSearchParams();

const append = (value: string, key: string) => params.append(key, value);
targetValue.forEach(append);
sourceValue.forEach(append);

target[key] = params;
} else if (is.urlInstance(targetValue) && (is.urlInstance(sourceValue) || is.string(sourceValue))) {
target[key] = new URL(sourceValue as string, targetValue);
} else if (is.plainObject(sourceValue)) {
if (is.plainObject(targetValue)) {
Expand Down
18 changes: 9 additions & 9 deletions source/normalize-arguments.js
Expand Up @@ -60,10 +60,11 @@ const preNormalize = (options, defaults) => {

const {retry} = options;
options.retry = {
retries: 0,
methods: [],
statusCodes: [],
errorCodes: []
retries: () => 0,
methods: new Set(),
statusCodes: new Set(),
errorCodes: new Set(),
maxRetryAfter: undefined
};

if (is.nonEmptyObject(defaults) && retry !== false) {
Expand All @@ -78,7 +79,7 @@ const preNormalize = (options, defaults) => {
}
}

if (options.gotTimeout) {
if (!options.retry.maxRetryAfter && options.gotTimeout) {
options.retry.maxRetryAfter = Math.min(...[options.gotTimeout.request, options.gotTimeout.connection].filter(n => !is.nullOrUndefined(n)));
}

Expand Down Expand Up @@ -153,7 +154,9 @@ const normalize = (url, options, defaults) => {
get: () => baseUrl
});

let searchParams;
let {searchParams} = options;
delete options.searchParams;

if (options.query) {
if (!shownDeprecation) {
console.warn('`options.query` is deprecated. We support it solely for compatibility - it will be removed in Got 11. Use `options.searchParams` instead.');
Expand All @@ -162,9 +165,6 @@ const normalize = (url, options, defaults) => {

searchParams = options.query;
delete options.query;
} else if (options.searchParams) {
searchParams = options.searchParams;
delete options.searchParams;
}

if (is.nonEmptyString(searchParams) || is.nonEmptyObject(searchParams) || searchParams instanceof URLSearchParams) {
Expand Down
15 changes: 15 additions & 0 deletions test/merge-instances.js
@@ -1,3 +1,4 @@
import {URLSearchParams} from 'url';
import test from 'ava';
import got from '../source';
import {createServer} from './helpers/server';
Expand Down Expand Up @@ -134,3 +135,17 @@ test('hooks are passed by though other instances don\'t have them', t => {
const merged = got.mergeInstances(instanceA, instanceB, instanceC);
t.deepEqual(merged.defaults.options.hooks.beforeRequest, instanceA.defaults.options.hooks.beforeRequest);
});

test('URLSearchParams instances are merged', t => {
const instanceA = got.extend({
searchParams: new URLSearchParams({a: '1'})
});

const instanceB = got.extend({
searchParams: new URLSearchParams({b: '2'})
});

const merged = got.mergeInstances(instanceA, instanceB);
t.is(merged.defaults.options.searchParams.get('a'), '1');
t.is(merged.defaults.options.searchParams.get('b'), '2');
});

0 comments on commit 95c7c2c

Please sign in to comment.