From c9e9d77d0ad498f58927d1b5ff2e295ef68cd552 Mon Sep 17 00:00:00 2001 From: pheralb Date: Thu, 14 Dec 2023 16:15:20 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fix=20svgl=20route=20for=20all?= =?UTF-8?q?=20api=20endpoints.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data/index.ts | 26 +++++++++++++++++++++++--- src/routes/api/svgs/+server.ts | 15 ++++----------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/data/index.ts b/src/data/index.ts index 3232844..e18741f 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -1,7 +1,27 @@ import type { iSVG } from '@/types/svg'; import { svgs } from './svgs'; +// Add id on top of each svg: export const svgsData = svgs.map((svg: iSVG, index: number) => { - svg.id = index; - return svg; -}); \ No newline at end of file + return { id: index, ...svg }; +}); + +// Add full route of each svg, checking if theme support is added: +export const fullRouteSvgsData: iSVG[] = svgsData.map((svg) => { + const url = 'https://svgl.vercel.app'; + if (typeof svg.route === 'object' && svg.route !== null) { + return { + ...svg, + route: { + light: `${url}${svg.route.light}`, + dark: `${url}${svg.route.dark}` + } + }; + } else if (typeof svg.route === 'string') { + return { + ...svg, + route: `${url}${svg.route}` + }; + } + return svg; +}); diff --git a/src/routes/api/svgs/+server.ts b/src/routes/api/svgs/+server.ts index 7b4f6f5..eb02825 100644 --- a/src/routes/api/svgs/+server.ts +++ b/src/routes/api/svgs/+server.ts @@ -1,23 +1,16 @@ import type { RequestEvent } from './$types'; -import type { iSVG } from '@/types/svg'; import { error, json } from '@sveltejs/kit'; // Data: -import { svgs } from '@/data/svgs'; +import { fullRouteSvgsData } from '@/data'; export const GET = ({ url }: RequestEvent) => { const getParams = url.searchParams.get('limit'); - // Modify svg route to add 'svgl.vercel.app' to the beginning: - const fullSVGRoute: iSVG[] = svgs.map((svg) => ({ - ...svg, - route: `https://svgl.vercel.app${svg.route}` - })); - // Status 200 | If no limit is provided, return all svgs: if (!getParams) { - return json(fullSVGRoute, { status: 200 }); + return json(fullRouteSvgsData, { status: 200 }); } const limit = Number(getParams); @@ -37,12 +30,12 @@ export const GET = ({ url }: RequestEvent) => { } // Error 400 | If limit is greater than the number of svgs: - if (limit > fullSVGRoute.length) { + if (limit > fullRouteSvgsData.length) { throw error(400, { message: 'Limit is greater than the number of svgs.' }); } // Status 200 | If limit is a number: - return json(fullSVGRoute.slice(0, limit), { status: 200 }); + return json(fullRouteSvgsData.slice(0, limit), { status: 200 }); };