mirror of
https://github.com/pheralb/svgl.git
synced 2025-02-06 06:58:04 +08:00
Merge pull request #296 from 1weiho/main
Adjust categories API statistical method
This commit is contained in:
commit
77614234a6
@ -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<string, number> = {};
|
||||
|
||||
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 });
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user