Skip to content

Commit 2e8e250

Browse files
committed
refactor(helper/proxy): build request init from request
1 parent d1ed0bd commit 2e8e250

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/helper/proxy/index.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ interface ProxyFetch {
2828
): Promise<Response>
2929
}
3030

31+
const buildRequestInitFromRequest = (
32+
request: Request | undefined
33+
): RequestInit & { duplex?: 'half' } => {
34+
if (!request) {
35+
return {}
36+
}
37+
38+
const headers = new Headers(request.headers)
39+
hopByHopHeaders.forEach((header) => {
40+
headers.delete(header)
41+
})
42+
43+
return {
44+
method: request.method,
45+
body: request.body,
46+
duplex: request.body ? 'half' : undefined,
47+
headers,
48+
}
49+
}
50+
3151
/**
3252
* Fetch API wrapper for proxy.
3353
* The parameters and return value are the same as for `fetch` (except for the proxy-specific options).
@@ -68,24 +88,8 @@ interface ProxyFetch {
6888
export const proxy: ProxyFetch = async (input, proxyInit) => {
6989
const { raw, ...requestInit } = proxyInit ?? {}
7090

71-
const requestInitRaw: RequestInit & { duplex?: 'half' } = raw
72-
? {
73-
method: raw.method,
74-
body: raw.body,
75-
headers: raw.headers,
76-
}
77-
: {}
78-
if (requestInitRaw.body) {
79-
requestInitRaw.duplex = 'half'
80-
}
81-
if (requestInitRaw.headers) {
82-
hopByHopHeaders.forEach((header) => {
83-
;(requestInitRaw.headers as Headers).delete(header)
84-
})
85-
}
86-
8791
const req = new Request(input, {
88-
...requestInitRaw,
92+
...buildRequestInitFromRequest(raw),
8993
...requestInit,
9094
})
9195
req.headers.delete('accept-encoding')

0 commit comments

Comments
 (0)