Files
simple-proxy/src/routes/index.ts
2023-10-16 20:01:29 +02:00

35 lines
898 B
TypeScript

import {
getProxyHeaders,
getAfterResponseHeaders,
cleanupHeadersBeforeProxy,
} from '@/utils/headers';
export default defineEventHandler(async (event) => {
// handle cors, if applicable
if (isPreflightRequest(event)) return handleCors(event, {});
// parse destination URL
const destination = getQuery<{ destination?: string }>(event).destination;
if (!destination)
return sendJson({
event,
status: 400,
data: {
error: 'destination query parameter invalid',
},
});
// proxy
cleanupHeadersBeforeProxy(event);
await proxyRequest(event, destination, {
fetchOptions: {
redirect: 'follow',
headers: getProxyHeaders(event.headers),
},
onResponse(outputEvent, response) {
const headers = getAfterResponseHeaders(response.headers, response.url);
setResponseHeaders(outputEvent, headers);
},
});
});