mirror of
https://github.com/pheralb/svgl.git
synced 2024-11-10 14:46:54 +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';
|
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 });
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user