/
merge.ts
95 lines (78 loc) 路 3.16 KB
/
merge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import is from '@sindresorhus/is';
import {Options, Method, Defaults, NormalizedOptions, CancelableRequest, Response} from './utils/types';
import knownHookEvents, {Hooks, HookEvent, HookType} from './known-hook-events';
import {Got} from './create';
import {ProxyStream} from './as-stream';
const URLGlobal: typeof URL = typeof URL === 'undefined' ? require('url').URL : URL;
const URLSearchParamsGlobal: typeof URLSearchParams = typeof URLSearchParams === 'undefined' ? require('url').URLSearchParams : URLSearchParams;
export default function merge<Target extends Record<string, unknown>, Source extends Record<string, unknown>>(target: Target, ...sources: Source[]): Target & Source {
for (const source of sources) {
for (const [key, sourceValue] of Object.entries(source)) {
if (is.undefined(sourceValue)) {
continue;
}
const targetValue = target[key];
if (targetValue instanceof URLSearchParamsGlobal && sourceValue instanceof URLSearchParamsGlobal) {
const params = new URLSearchParamsGlobal();
const append = (value: string, key: string): void => 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 URLGlobal(sourceValue as string, targetValue);
} else if (is.plainObject(sourceValue)) {
if (is.plainObject(targetValue)) {
target[key] = merge({}, targetValue, sourceValue);
} else {
target[key] = merge({}, sourceValue);
}
} else if (is.array(sourceValue)) {
target[key] = sourceValue.slice();
} else {
target[key] = sourceValue;
}
}
}
return target as Target & Source;
}
export function mergeOptions<T extends Options>(...sources: T[]): T & {hooks: Partial<Hooks>} {
sources = sources.map(source => {
if (!source) {
return {};
}
if (is.object(source.retry)) {
return source;
}
return {
...source,
retry: {
retries: source.retry
}
};
}) as T[];
const mergedOptions = merge({} as T & {hooks: Partial<Hooks>}, ...sources);
const hooks = knownHookEvents.reduce((accumulator, current) => ({...accumulator, [current]: []}), {}) as Record<HookEvent, HookType[]>;
for (const source of sources) {
// We need to check `source` to allow calling `.extend()` with no arguments.
if (source && source.hooks) {
for (const hook of knownHookEvents) {
hooks[hook] = hooks[hook].concat(source.hooks[hook] || []);
}
}
}
mergedOptions.hooks = hooks as Hooks;
return mergedOptions;
}
export function mergeInstances(instances: Got[], methods?: Method[]): Defaults {
const handlers = instances.map(instance => instance.defaults.handler);
const size = instances.length - 1;
return {
methods,
options: mergeOptions(...instances.map(instance => instance.defaults.options || {})),
handler: <T extends ProxyStream | CancelableRequest<Response>>(options: NormalizedOptions, next: (options: NormalizedOptions) => T) => {
let iteration = 0;
const iterate = (newOptions: NormalizedOptions): T => handlers[++iteration]!(newOptions, iteration === size ? next : iterate);
return iterate(options);
}
};
}