mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
29 lines
609 B
TypeScript
29 lines
609 B
TypeScript
import { apiUrl } from '$lib/server/api';
|
|
|
|
const methodsWithoutBody = new Set(['GET', 'HEAD']);
|
|
|
|
export async function proxyBackendRequest(
|
|
prefix: string,
|
|
path: string,
|
|
request: Request,
|
|
url: URL
|
|
) {
|
|
const targetUrl = new URL(`${prefix}/${path}`.replace(/\/+$/, ''), apiUrl);
|
|
targetUrl.search = url.search;
|
|
|
|
const headers = new Headers(request.headers);
|
|
headers.delete('host');
|
|
|
|
const init: RequestInit = {
|
|
method: request.method,
|
|
headers,
|
|
redirect: 'manual'
|
|
};
|
|
|
|
if (!methodsWithoutBody.has(request.method)) {
|
|
init.body = await request.arrayBuffer();
|
|
}
|
|
|
|
return fetch(targetUrl, init);
|
|
}
|