diff --git a/src/routes/api/categories/+server.ts b/src/routes/api/categories/+server.ts index bf0fd14..6c20950 100644 --- a/src/routes/api/categories/+server.ts +++ b/src/routes/api/categories/+server.ts @@ -7,9 +7,6 @@ import { ratelimit } from '@/server/redis'; import { svgs } from '@/data/svgs'; export const GET = async ({ request }: RequestEvent) => { - const categories = svgs - .map((svg) => svg.category) - .filter((category, index, array) => array.indexOf(category) === index); const ip = request.headers.get('x-forwarded-for') ?? ''; const { success, reset } = await ratelimit.limit(ip); @@ -25,14 +22,23 @@ export const GET = async ({ request }: RequestEvent) => { }); } + const categoryTotals: Record = {}; + + svgs.forEach((svg) => { + if (typeof svg.category === 'string') { + categoryTotals[svg.category] = (categoryTotals[svg.category] || 0) + 1; + } else if (Array.isArray(svg.category)) { + svg.category.forEach((category) => { + categoryTotals[category] = (categoryTotals[category] || 0) + 1; + }); + } + }); + + const categories = Object.entries(categoryTotals).map(([category, total]) => ({ + category, + total + })); + // Status 200 | If limit is a number: - return json( - categories.map((category) => { - return { - category, - total: svgs.filter((svg) => svg.category === category).length - }; - }), - { status: 200 } - ); + return json(categories, { status: 200 }); };