1 | import { isObject } from './is.js';
|
2 | export const validateAndMerge = (...sources) => {
|
3 | for (const source of sources) {
|
4 | if ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') {
|
5 | throw new TypeError('The `options` argument must be an object');
|
6 | }
|
7 | }
|
8 | return deepMerge({}, ...sources);
|
9 | };
|
10 | export const mergeHeaders = (source1 = {}, source2 = {}) => {
|
11 | const result = new globalThis.Headers(source1);
|
12 | const isHeadersInstance = source2 instanceof globalThis.Headers;
|
13 | const source = new globalThis.Headers(source2);
|
14 | for (const [key, value] of source.entries()) {
|
15 | if ((isHeadersInstance && value === 'undefined') || value === undefined) {
|
16 | result.delete(key);
|
17 | }
|
18 | else {
|
19 | result.set(key, value);
|
20 | }
|
21 | }
|
22 | return result;
|
23 | };
|
24 | export const deepMerge = (...sources) => {
|
25 | let returnValue = {};
|
26 | let headers = {};
|
27 | for (const source of sources) {
|
28 | if (Array.isArray(source)) {
|
29 | if (!Array.isArray(returnValue)) {
|
30 | returnValue = [];
|
31 | }
|
32 | returnValue = [...returnValue, ...source];
|
33 | }
|
34 | else if (isObject(source)) {
|
35 | for (let [key, value] of Object.entries(source)) {
|
36 | if (isObject(value) && key in returnValue) {
|
37 | value = deepMerge(returnValue[key], value);
|
38 | }
|
39 | returnValue = { ...returnValue, [key]: value };
|
40 | }
|
41 | if (isObject(source.headers)) {
|
42 | headers = mergeHeaders(headers, source.headers);
|
43 | returnValue.headers = headers;
|
44 | }
|
45 | }
|
46 | }
|
47 | return returnValue;
|
48 | };
|
49 |
|
\ | No newline at end of file |