From 808ab27ed46910c9611fc5f8e0d3165b39860967 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Sat, 18 Mar 2023 03:03:55 +0100 Subject: [PATCH] Return empty list if no instances could be fetched --- src/pages/api/instances.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/pages/api/instances.ts b/src/pages/api/instances.ts index 9addd6c..76722bc 100644 --- a/src/pages/api/instances.ts +++ b/src/pages/api/instances.ts @@ -6,16 +6,25 @@ import { APIRoute } from "astro"; export const get: APIRoute = async () => { - const response = await fetch("https://api.joinmastodon.org/servers"); - const instances = await response.json(); + try { + const response = await fetch("https://api.joinmastodon.org/servers"); + const instances = await response.json(); - return new Response( - JSON.stringify(instances.map((instance) => instance.domain)), - { + return new Response( + JSON.stringify(instances.map((instance) => instance.domain)), + { + headers: { + "Cache-Control": "s-maxage=86400, max-age=86400, public", + "Content-Type": "application/json", + }, + }, + ); + } catch (error) { + console.error("Could not fetch instances:", error); + return new Response(JSON.stringify([]), { headers: { - "Cache-Control": "s-maxage=86400, max-age=86400, public", "Content-Type": "application/json", }, - }, - ); + }); + } };