FMHYedit/.vitepress/routes/index.post.ts
taskylizard a69a834899
feat(api): ratelimiting
because some group of idiots like me so much that they spam my endpoint
2024-07-10 03:47:04 +00:00

41 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { fetcher } from 'itty-fetcher'
import { FeedbackSchema, getFeedbackOption } from '../types/Feedback'
export default defineEventHandler(async (event) => {
const { message, page, type } = await readValidatedBody(
event,
FeedbackSchema.parseAsync
)
const env = useRuntimeConfig(event)
const { pathname } = new URL(event.node.req.url)
const { success } = await env.MY_RATE_LIMITER.limit({ key: pathname })
if (!success) {
return new Response(`429 Failure rate limit exceeded for ${pathname}`, {
status: 429
})
}
let description = `${message}\n\n`
if (page) description += `**Page:** \`${page}\``
await fetcher()
.post(env.WEBHOOK_URL, {
username: 'Feedback',
avatar_url:
'https://i.kym-cdn.com/entries/icons/facebook/000/043/403/cover3.jpg',
embeds: [
{
color: 3447003,
title: getFeedbackOption(type).label,
description
}
]
})
.catch((error) => {
throw new Error(error)
})
return { status: 'ok' }
})