Skip to content

Commit 2fdd48f

Browse files
chore: implement in sendProxy
1 parent 22faf28 commit 2fdd48f

1 file changed

Lines changed: 30 additions & 28 deletions

File tree

src/utils/proxy.ts

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const ignoredHeaders = new Set([
3434
/**
3535
* Proxy the incoming request to a target URL.
3636
*/
37-
export async function proxyRequest(
37+
export async function proxyRequest (
3838
event: H3Event,
3939
target: string,
4040
opts: ProxyOptions = {},
@@ -61,37 +61,39 @@ export async function proxyRequest(
6161
opts.headers,
6262
);
6363

64-
try {
65-
return await sendProxy(event, target, {
66-
...opts,
67-
fetchOptions: {
68-
method,
69-
body,
70-
duplex,
71-
...opts.fetchOptions,
72-
headers: fetchHeaders,
73-
},
74-
});
75-
} catch {
76-
event.node.res.statusCode = 502;
77-
event.node.res.statusMessage = "Bad Gateway";
78-
event.node.res.end();
79-
}
64+
return await sendProxy(event, target, {
65+
...opts,
66+
fetchOptions: {
67+
method,
68+
body,
69+
duplex,
70+
...opts.fetchOptions,
71+
headers: fetchHeaders,
72+
},
73+
});
8074
}
8175

8276
/**
8377
* Make a proxy request to a target URL and send the response back to the client.
8478
*/
85-
export async function sendProxy(
79+
export async function sendProxy (
8680
event: H3Event,
8781
target: string,
8882
opts: ProxyOptions = {},
8983
) {
90-
const response = await _getFetch(opts.fetch)(target, {
91-
headers: opts.headers as HeadersInit,
92-
ignoreResponseError: true, // make $ofetch.raw transparent
93-
...opts.fetchOptions,
94-
});
84+
let response: Response | undefined;
85+
try {
86+
response = await _getFetch(opts.fetch)(target, {
87+
headers: opts.headers as HeadersInit,
88+
ignoreResponseError: true, // make $ofetch.raw transparent
89+
...opts.fetchOptions,
90+
});
91+
} catch (error: unknown) {
92+
event.node.res.statusCode = 502;
93+
event.node.res.statusMessage = "Bad Gateway";
94+
event.node.res.end();
95+
return
96+
}
9597
event.node.res.statusCode = sanitizeStatusCode(
9698
response.status,
9799
event.node.res.statusCode,
@@ -169,7 +171,7 @@ export async function sendProxy(
169171
/**
170172
* Get the request headers object without headers known to cause issues when proxying.
171173
*/
172-
export function getProxyRequestHeaders(event: H3Event) {
174+
export function getProxyRequestHeaders (event: H3Event) {
173175
const headers = Object.create(null);
174176
const reqHeaders = getRequestHeaders(event);
175177
for (const name in reqHeaders) {
@@ -188,7 +190,7 @@ export function fetchWithEvent<
188190
// eslint-disable-next-line @typescript-eslint/no-unused-vars
189191
_R = any,
190192
F extends (req: RequestInfo | URL, opts?: any) => any = typeof fetch,
191-
>(
193+
> (
192194
event: H3Event,
193195
req: RequestInfo | URL,
194196
init?: RequestInit & { context?: H3EventContext },
@@ -206,7 +208,7 @@ export function fetchWithEvent<
206208

207209
// -- internal utils --
208210

209-
function _getFetch<T = typeof fetch>(_fetch?: T) {
211+
function _getFetch<T = typeof fetch> (_fetch?: T) {
210212
if (_fetch) {
211213
return _fetch;
212214
}
@@ -218,7 +220,7 @@ function _getFetch<T = typeof fetch>(_fetch?: T) {
218220
);
219221
}
220222

221-
function rewriteCookieProperty(
223+
function rewriteCookieProperty (
222224
header: string,
223225
map: string | Record<string, string>,
224226
property: string,
@@ -240,7 +242,7 @@ function rewriteCookieProperty(
240242
);
241243
}
242244

243-
function mergeHeaders(
245+
function mergeHeaders (
244246
defaults: HeadersInit,
245247
...inputs: (HeadersInit | RequestHeaders | undefined)[]
246248
) {

0 commit comments

Comments
 (0)