From 2d95fe62b6b09fc946270e257b4f9428de771e6a Mon Sep 17 00:00:00 2001 From: pheralb Date: Wed, 13 Dec 2023 23:42:55 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Initial=20/api/svgs=20api=20rout?= =?UTF-8?q?e.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/api/svgs/+server.ts | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/routes/api/svgs/+server.ts diff --git a/src/routes/api/svgs/+server.ts b/src/routes/api/svgs/+server.ts new file mode 100644 index 0000000..7b4f6f5 --- /dev/null +++ b/src/routes/api/svgs/+server.ts @@ -0,0 +1,48 @@ +import type { RequestEvent } from './$types'; +import type { iSVG } from '@/types/svg'; + +import { error, json } from '@sveltejs/kit'; + +// Data: +import { svgs } from '@/data/svgs'; + +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 }); + } + + const limit = Number(getParams); + + // Error 400 | if limit is not a number: + if (isNaN(limit)) { + throw error(400, { + message: 'Limit must be a number.' + }); + } + + // Error 400 | If limit is not positive: + if (limit < 1) { + throw error(400, { + message: 'Limit must be a positive number.' + }); + } + + // Error 400 | If limit is greater than the number of svgs: + if (limit > fullSVGRoute.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 }); +};