-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathutils.ts
142 lines (127 loc) · 3.11 KB
/
utils.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import type {
FetchContext,
FetchHook,
FetchOptions,
FetchRequest,
ResolvedFetchOptions,
ResponseType,
} from "./types";
const payloadMethods = new Set(
Object.freeze(["PATCH", "POST", "PUT", "DELETE"])
);
export function isPayloadMethod(method = "GET") {
return payloadMethods.has(method.toUpperCase());
}
export function isJSONSerializable(value: any) {
if (value === undefined) {
return false;
}
const t = typeof value;
if (t === "string" || t === "number" || t === "boolean" || t === null) {
return true;
}
if (t !== "object") {
return false; // bigint, function, symbol, undefined
}
if (Array.isArray(value)) {
return true;
}
if (value.buffer) {
return false;
}
return (
(value.constructor && value.constructor.name === "Object") ||
typeof value.toJSON === "function"
);
}
const textTypes = new Set([
"image/svg",
"application/xml",
"application/xhtml",
"application/html",
]);
const JSON_RE = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(;.+)?$/i;
// This provides reasonable defaults for the correct parser based on Content-Type header.
export function detectResponseType(_contentType = ""): ResponseType {
if (!_contentType) {
return "json";
}
// Value might look like: `application/json; charset=utf-8`
const contentType = _contentType.split(";").shift() || "";
if (JSON_RE.test(contentType)) {
return "json";
}
// TODO
// if (contentType === 'application/octet-stream') {
// return 'stream'
// }
if (textTypes.has(contentType) || contentType.startsWith("text/")) {
return "text";
}
return "blob";
}
export function resolveFetchOptions<
R extends ResponseType = ResponseType,
T = any,
>(
request: FetchRequest,
input: FetchOptions<R, T> | undefined,
defaults: FetchOptions<R, T> | undefined,
Headers: typeof globalThis.Headers
): ResolvedFetchOptions<R, T> {
// Merge headers
const headers = mergeHeaders(
input?.headers ?? (request as Request)?.headers,
defaults?.headers,
Headers
);
// Merge query/params
let query: Record<string, any> | undefined;
if (defaults?.query || defaults?.params || input?.params || input?.query) {
query = {
...defaults?.params,
...defaults?.query,
...input?.params,
...input?.query,
};
}
return {
...defaults,
...input,
query,
params: query,
headers,
};
}
function mergeHeaders(
input: HeadersInit | undefined,
defaults: HeadersInit | undefined,
Headers: typeof globalThis.Headers
): Headers {
if (!defaults) {
return new Headers(input);
}
const headers = new Headers(defaults);
if (input) {
for (const [key, value] of Symbol.iterator in input || Array.isArray(input)
? input
: new Headers(input)) {
headers.set(key, value);
}
}
return headers;
}
export async function callHooks<C extends FetchContext = FetchContext>(
context: C,
hooks: FetchHook<C> | FetchHook<C>[] | undefined
): Promise<void> {
if (hooks) {
if (Array.isArray(hooks)) {
for (const hook of hooks) {
await hook(context);
}
} else {
await hooks(context);
}
}
}