-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathutil.ts
72 lines (65 loc) · 1.86 KB
/
util.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
function parseRecursive(obj: unknown): unknown {
const parsed = Array.isArray(obj)
? obj.map((o) => {
try {
return parseRecursive(o);
} catch {
return o;
}
})
: JSON.parse(obj as string);
/**
* Parsing very large numbers can result in MAX_SAFE_INTEGER
* overflow. In that case we return the number as string instead.
*/
if (typeof parsed === "number" && parsed.toString() !== obj) {
return obj;
}
return parsed;
}
export function parseResponse<TResult>(result: unknown): TResult {
try {
/**
* Try to parse the response if possible
*/
return parseRecursive(result) as TResult;
} catch {
return result as TResult;
}
}
/**
* Deserializes a scan result, excluding the cursor
* which can be string "0" or a big number string.
* Either way, we want it to stay as a string.
*
* @param result
*/
export function deserializeScanResponse<TResult>(result: [string, ...any]): TResult {
return [result[0], ...parseResponse<any[]>(result.slice(1))] as TResult;
}
/**
* Merges multiple Records of headers into a single Record
* Later headers take precedence over earlier ones.
*
* @param headers - Array of header records to merge
* @returns A new Record containing all merged headers
*
* @example
* const defaultHeaders = { 'Content-Type': 'application/json' };
* const customHeaders = { 'Authorization': 'Bearer token' };
* const merged = mergeHeaders(defaultHeaders, customHeaders);
*/
export function mergeHeaders(
...headers: (Record<string, string> | undefined)[]
): Record<string, string> {
const merged: Record<string, string> = {};
for (const header of headers) {
if (!header) continue;
for (const [key, value] of Object.entries(header)) {
if (value !== undefined && value !== null) {
merged[key] = value;
}
}
}
return merged;
}