-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.ts
42 lines (34 loc) · 1.03 KB
/
request.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
import type { Request, Response } from 'express';
export default function createFetchRequest(req: Request, res: Response) {
const origin = `${req.protocol}://${req.get("host")}`;
const url = new URL(req.originalUrl || req.url, origin);
const controller = new AbortController();
res.on("close", () => controller.abort());
const headers = new Headers();
for (const [key, values] of Object.entries(req.headers)) {
if (values) {
if (Array.isArray(values)) {
for (const value of values) {
headers.append(key, value);
}
} else {
headers.set(key, values);
}
}
}
interface Init {
method: string;
headers: Headers;
signal: AbortSignal;
body?: string;
}
const init: Init = {
method: req.method,
headers,
signal: controller.signal,
};
if (req.method !== "GET" && req.method !== "HEAD") {
init.body = req.body;
}
return new Request(url.href, init);
}