From f5e6ef679b62d352be5d39247be8ef93a35d9ab2 Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 30 Mar 2023 12:54:01 +0100 Subject: [PATCH] chore: cleanup functions --- functions/e/[id].ts | 37 +++++++++++++++++++++---------------- functions/p/[id].ts | 37 +++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/functions/e/[id].ts b/functions/e/[id].ts index 805ce1ea9..792a4c879 100644 --- a/functions/e/[id].ts +++ b/functions/e/[id].ts @@ -5,21 +5,26 @@ export const onRequest: PagesFunction = async (context) => { const id = context.params.id as string; const next = await context.next(); - console.debug(next.status); - console.debug("Fetching data for: ", id); - - const rsp = await fetch(`https://api.snort.social/api/v1/og/tag/e/${id}`, { - method: "POST", - body: await next.arrayBuffer(), - headers: { - "content-type": "text/plain" + try { + const rsp = await fetch(`https://api.snort.social/api/v1/og/tag/e/${id}`, { + method: "POST", + body: await next.arrayBuffer(), + headers: { + "content-type": "text/plain" + } + }); + if (rsp.ok) { + const body = await rsp.text(); + if (body.length > 0) { + return new Response(body, { + headers: { + "content-type": "text/html" + } + }); + } } - }); - console.debug(rsp.status, rsp.statusText); - const body = await rsp.text(); - return new Response(body, { - headers: { - "content-type": "text/html" - } - }); + } catch { + // ignore + } + return next; } \ No newline at end of file diff --git a/functions/p/[id].ts b/functions/p/[id].ts index 13123aa69..dd370fc5b 100644 --- a/functions/p/[id].ts +++ b/functions/p/[id].ts @@ -5,21 +5,26 @@ export const onRequest: PagesFunction = async (context) => { const id = context.params.id as string; const next = await context.next(); - console.debug(next.status); - console.debug("Fetching data for: ", id); - - const rsp = await fetch(`https://api.snort.social/api/v1/og/tag/p/${id}`, { - method: "POST", - body: await next.arrayBuffer(), - headers: { - "content-type": "text/plain" + try { + const rsp = await fetch(`https://api.snort.social/api/v1/og/tag/p/${id}`, { + method: "POST", + body: await next.arrayBuffer(), + headers: { + "content-type": "text/plain" + } + }); + if (rsp.ok) { + const body = await rsp.text(); + if (body.length > 0) { + return new Response(body, { + headers: { + "content-type": "text/html" + } + }); + } } - }); - console.debug(rsp.status, rsp.statusText); - const body = await rsp.text(); - return new Response(body, { - headers: { - "content-type": "text/html" - } - }); + } catch { + // ignore + } + return next; } \ No newline at end of file