Merge pull request #296 from 1weiho/main

Adjust categories API statistical method
This commit is contained in:
Pablo Hdez 2024-04-13 17:50:40 +01:00 committed by GitHub
commit 77614234a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,9 +7,6 @@ import { ratelimit } from '@/server/redis';
import { svgs } from '@/data/svgs'; import { svgs } from '@/data/svgs';
export const GET = async ({ request }: RequestEvent) => { 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 ip = request.headers.get('x-forwarded-for') ?? '';
const { success, reset } = await ratelimit.limit(ip); const { success, reset } = await ratelimit.limit(ip);
@ -25,14 +22,23 @@ export const GET = async ({ request }: RequestEvent) => {
}); });
} }
// Status 200 | If limit is a number: const categoryTotals: Record<string, number> = {};
return json(
categories.map((category) => { svgs.forEach((svg) => {
return { 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, category,
total: svgs.filter((svg) => svg.category === category).length total
}; }));
}),
{ status: 200 } // Status 200 | If limit is a number:
); return json(categories, { status: 200 });
}; };