From aef257cc7fe12c91c290efca0724a0264ffbc67d Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 6 Dec 2023 21:42:05 -0800 Subject: feat(proxy): proxy all content --- src/routes/api/proxy/+server.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/routes/api/proxy') diff --git a/src/routes/api/proxy/+server.ts b/src/routes/api/proxy/+server.ts index 1c4a25dc..c4bf74a6 100644 --- a/src/routes/api/proxy/+server.ts +++ b/src/routes/api/proxy/+server.ts @@ -1,2 +1,18 @@ -export const GET = async ({ url }) => - new Response(await (await fetch(url.searchParams.get('url') as string)).blob()); +export const GET = async ({ url }) => { + const response = await fetch(url.searchParams.get('url') as string); + const contentType = response.headers.get('content-type') as string; + + if (contentType.includes('application/json')) { + return Response.json(await response.json(), { + headers: { 'Content-Type': 'application/json' } + }); + } else if (contentType.includes('text/html') || contentType.includes('text/plain')) { + return new Response(await response.text(), { + headers: { 'Content-Type': contentType } + }); + } else { + return new Response(await response.blob(), { + headers: { 'Content-Type': contentType } + }); + } +}; -- cgit v1.2.3