deno.land / x / ky@v0.31.3 / source / utils / 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
import type {KyHeadersInit, Options} from '../types/options.js';import {isObject} from './is.js';
export const validateAndMerge = (...sources: Array<Partial<Options> | undefined>): Partial<Options> => { for (const source of sources) { if ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') { throw new TypeError('The `options` argument must be an object'); } }
return deepMerge({}, ...sources);};
export const mergeHeaders = (source1: KyHeadersInit = {}, source2: KyHeadersInit = {}) => { const result = new globalThis.Headers(source1 as HeadersInit); const isHeadersInstance = source2 instanceof globalThis.Headers; const source = new globalThis.Headers(source2 as HeadersInit);
for (const [key, value] of source.entries()) { if ((isHeadersInstance && value === 'undefined') || value === undefined) { result.delete(key); } else { result.set(key, value); } }
return result;};
// TODO: Make this strongly-typed (no `any`).export const deepMerge = <T>(...sources: Array<Partial<T> | undefined>): T => { let returnValue: any = {}; let headers = {};
for (const source of sources) { if (Array.isArray(source)) { if (!Array.isArray(returnValue)) { returnValue = []; }
returnValue = [...returnValue, ...source]; } else if (isObject(source)) { for (let [key, value] of Object.entries(source)) { if (isObject(value) && key in returnValue) { value = deepMerge(returnValue[key], value); }
returnValue = {...returnValue, [key]: value}; }
if (isObject((source as any).headers)) { headers = mergeHeaders(headers, (source as any).headers); returnValue.headers = headers; } } }
return returnValue;};
ky

Version Info

Tagged at
a year ago