From 5f97ea8e4fe964ba85aca587e5ed61438548b763 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Tue, 9 Apr 2024 13:42:18 +0100 Subject: [PATCH] Fix beginner roadmaps not working --- src/pages/[roadmapId]/index.json.ts | 34 ++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/pages/[roadmapId]/index.json.ts b/src/pages/[roadmapId]/index.json.ts index 9035a98b3..5d3584ef5 100644 --- a/src/pages/[roadmapId]/index.json.ts +++ b/src/pages/[roadmapId]/index.json.ts @@ -1,11 +1,39 @@ import type { APIRoute } from 'astro'; -export const GET: APIRoute = async function ({ params, request, props }) { - const { roadmapId } = params; +export const GET: APIRoute = async function ({ params, url, request, props }) { + const { roadmapId: fullRoadmapId } = params; + if (!fullRoadmapId) { + return new Response( + JSON.stringify({ + data: null, + error: { + message: 'Roadmap not found', + }, + }), + { + status: 500, + headers: { + 'Content-Type': 'application/json', + }, + }, + ); + } + + // to account for `roadmap/roadmap-beginner.json` files + const roadmapId = + fullRoadmapId?.indexOf('-beginner') !== -1 + ? fullRoadmapId.replace('-beginner', '') + : fullRoadmapId; + + const fileName = + roadmapId === fullRoadmapId + ? `${roadmapId}.json` + : `${fullRoadmapId}.json`; + console.log(fileName); try { const roadmapJson = await import( - `../../data/roadmaps/${roadmapId}/${roadmapId}.json` + `../../data/roadmaps/${roadmapId}/${fileName}` ); return new Response(JSON.stringify(roadmapJson), {