-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp.ts
323 lines (269 loc) · 8.67 KB
/
http.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/
export type Fetcher = (
input: RequestInfo | URL,
init?: RequestInit,
) => Promise<Response>;
export type Awaitable<T> = T | Promise<T>;
const DEFAULT_FETCHER: Fetcher = (input, init) => {
// If input is a Request and init is undefined, Bun will discard the method,
// headers, body and other options that were set on the request object.
// Node.js and browers would ignore an undefined init value. This check is
// therefore needed for interop with Bun.
if (init == null) {
return fetch(input);
} else {
return fetch(input, init);
}
};
export type RequestInput = {
/**
* The URL the request will use.
*/
url: URL;
/**
* Options used to create a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request).
*/
options?: RequestInit | undefined;
};
export interface HTTPClientOptions {
fetcher?: Fetcher;
}
export type BeforeRequestHook = (req: Request) => Awaitable<Request | void>;
export type RequestErrorHook = (err: unknown, req: Request) => Awaitable<void>;
export type ResponseHook = (res: Response, req: Request) => Awaitable<void>;
export class HTTPClient {
private fetcher: Fetcher;
private requestHooks: BeforeRequestHook[] = [];
private requestErrorHooks: RequestErrorHook[] = [];
private responseHooks: ResponseHook[] = [];
constructor(private options: HTTPClientOptions = {}) {
this.fetcher = options.fetcher || DEFAULT_FETCHER;
}
async request(request: Request): Promise<Response> {
let req = request;
for (const hook of this.requestHooks) {
const nextRequest = await hook(req);
if (nextRequest) {
req = nextRequest;
}
}
try {
const res = await this.fetcher(req);
for (const hook of this.responseHooks) {
await hook(res, req);
}
return res;
} catch (err) {
for (const hook of this.requestErrorHooks) {
await hook(err, req);
}
throw err;
}
}
/**
* Registers a hook that is called before a request is made. The hook function
* can mutate the request or return a new request. This may be useful to add
* additional information to request such as request IDs and tracing headers.
*/
addHook(hook: "beforeRequest", fn: BeforeRequestHook): this;
/**
* Registers a hook that is called when a request cannot be made due to a
* network error.
*/
addHook(hook: "requestError", fn: RequestErrorHook): this;
/**
* Registers a hook that is called when a response has been received from the
* server.
*/
addHook(hook: "response", fn: ResponseHook): this;
addHook(
...args:
| [hook: "beforeRequest", fn: BeforeRequestHook]
| [hook: "requestError", fn: RequestErrorHook]
| [hook: "response", fn: ResponseHook]
) {
if (args[0] === "beforeRequest") {
this.requestHooks.push(args[1]);
} else if (args[0] === "requestError") {
this.requestErrorHooks.push(args[1]);
} else if (args[0] === "response") {
this.responseHooks.push(args[1]);
} else {
throw new Error(`Invalid hook type: ${args[0]}`);
}
return this;
}
/** Removes a hook that was previously registered with `addHook`. */
removeHook(hook: "beforeRequest", fn: BeforeRequestHook): this;
/** Removes a hook that was previously registered with `addHook`. */
removeHook(hook: "requestError", fn: RequestErrorHook): this;
/** Removes a hook that was previously registered with `addHook`. */
removeHook(hook: "response", fn: ResponseHook): this;
removeHook(
...args:
| [hook: "beforeRequest", fn: BeforeRequestHook]
| [hook: "requestError", fn: RequestErrorHook]
| [hook: "response", fn: ResponseHook]
): this {
let target: unknown[];
if (args[0] === "beforeRequest") {
target = this.requestHooks;
} else if (args[0] === "requestError") {
target = this.requestErrorHooks;
} else if (args[0] === "response") {
target = this.responseHooks;
} else {
throw new Error(`Invalid hook type: ${args[0]}`);
}
const index = target.findIndex((v) => v === args[1]);
if (index >= 0) {
target.splice(index, 1);
}
return this;
}
clone(): HTTPClient {
const child = new HTTPClient(this.options);
child.requestHooks = this.requestHooks.slice();
child.requestErrorHooks = this.requestErrorHooks.slice();
child.responseHooks = this.responseHooks.slice();
return child;
}
}
export type StatusCodePredicate = number | string | (number | string)[];
// A semicolon surrounded by optional whitespace characters is used to separate
// segments in a media type string.
const mediaParamSeparator = /\s*;\s*/g;
export function matchContentType(response: Response, pattern: string): boolean {
// `*` is a special case which means anything is acceptable.
if (pattern === "*") {
return true;
}
let contentType =
response.headers.get("content-type")?.trim() || "application/octet-stream";
contentType = contentType.toLowerCase();
const wantParts = pattern.toLowerCase().trim().split(mediaParamSeparator);
const [wantType = "", ...wantParams] = wantParts;
if (wantType.split("/").length !== 2) {
return false;
}
const gotParts = contentType.split(mediaParamSeparator);
const [gotType = "", ...gotParams] = gotParts;
const [type = "", subtype = ""] = gotType.split("/");
if (!type || !subtype) {
return false;
}
if (
wantType !== "*/*" &&
gotType !== wantType &&
`${type}/*` !== wantType &&
`*/${subtype}` !== wantType
) {
return false;
}
if (gotParams.length < wantParams.length) {
return false;
}
const params = new Set(gotParams);
for (const wantParam of wantParams) {
if (!params.has(wantParam)) {
return false;
}
}
return true;
}
const codeRangeRE = new RegExp("^[0-9]xx$", "i");
export function matchStatusCode(
response: Response,
codes: StatusCodePredicate,
): boolean {
const actual = `${response.status}`;
const expectedCodes = Array.isArray(codes) ? codes : [codes];
if (!expectedCodes.length) {
return false;
}
return expectedCodes.some((ec) => {
const code = `${ec}`;
if (code === "default") {
return true;
}
if (!codeRangeRE.test(`${code}`)) {
return code === actual;
}
const expectFamily = code.charAt(0);
if (!expectFamily) {
throw new Error("Invalid status code range");
}
const actualFamily = actual.charAt(0);
if (!actualFamily) {
throw new Error(`Invalid response status code: ${actual}`);
}
return actualFamily === expectFamily;
});
}
export function matchResponse(
response: Response,
code: StatusCodePredicate,
contentTypePattern: string,
): boolean {
return (
matchStatusCode(response, code) &&
matchContentType(response, contentTypePattern)
);
}
/**
* Uses various heurisitics to determine if an error is a connection error.
*/
export function isConnectionError(err: unknown): boolean {
if (typeof err !== "object" || err == null) {
return false;
}
// Covers fetch in Deno as well
const isBrowserErr =
err instanceof TypeError &&
err.message.toLowerCase().startsWith("failed to fetch");
const isNodeErr =
err instanceof TypeError &&
err.message.toLowerCase().startsWith("fetch failed");
const isBunErr = "name" in err && err.name === "ConnectionError";
const isGenericErr =
"code" in err &&
typeof err.code === "string" &&
err.code.toLowerCase() === "econnreset";
return isBrowserErr || isNodeErr || isGenericErr || isBunErr;
}
/**
* Uses various heurisitics to determine if an error is a timeout error.
*/
export function isTimeoutError(err: unknown): boolean {
if (typeof err !== "object" || err == null) {
return false;
}
// Fetch in browser, Node.js, Bun, Deno
const isNative = "name" in err && err.name === "TimeoutError";
const isLegacyNative = "code" in err && err.code === 23;
// Node.js HTTP client and Axios
const isGenericErr =
"code" in err &&
typeof err.code === "string" &&
err.code.toLowerCase() === "econnaborted";
return isNative || isLegacyNative || isGenericErr;
}
/**
* Uses various heurisitics to determine if an error is a abort error.
*/
export function isAbortError(err: unknown): boolean {
if (typeof err !== "object" || err == null) {
return false;
}
// Fetch in browser, Node.js, Bun, Deno
const isNative = "name" in err && err.name === "AbortError";
const isLegacyNative = "code" in err && err.code === 20;
// Node.js HTTP client and Axios
const isGenericErr =
"code" in err &&
typeof err.code === "string" &&
err.code.toLowerCase() === "econnaborted";
return isNative || isLegacyNative || isGenericErr;
}