2024-08-07 12:23:35 +00:00
|
|
|
|
import { fetcher } from 'itty-fetcher'
|
|
|
|
|
|
import {
|
|
|
|
|
|
FeedbackSchema,
|
|
|
|
|
|
getFeedbackOption
|
|
|
|
|
|
} from '../../docs/.vitepress/types/Feedback'
|
|
|
|
|
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
2024-08-25 11:00:20 +00:00
|
|
|
|
const { message, page, type, heading } = await readValidatedBody(
|
2024-08-07 12:23:35 +00:00
|
|
|
|
event,
|
|
|
|
|
|
FeedbackSchema.parseAsync
|
|
|
|
|
|
)
|
|
|
|
|
|
const env = useRuntimeConfig(event)
|
|
|
|
|
|
|
2024-08-25 11:00:20 +00:00
|
|
|
|
const fields = [
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'Page',
|
|
|
|
|
|
value: page,
|
|
|
|
|
|
inline: true
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'Message',
|
|
|
|
|
|
value: message,
|
|
|
|
|
|
inline: false
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
if (heading) {
|
|
|
|
|
|
fields.push({
|
|
|
|
|
|
name: 'Section',
|
|
|
|
|
|
value: heading,
|
|
|
|
|
|
inline: true
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-07 12:23:35 +00:00
|
|
|
|
// FIXME: somehow this is not working, but it worked before
|
|
|
|
|
|
// const path = 'feedback'
|
|
|
|
|
|
//
|
|
|
|
|
|
// const { success } = await env.MY_RATE_LIMITER.limit({ key: path })
|
|
|
|
|
|
// if (!success) {
|
|
|
|
|
|
// return new Response('429 Failure – global rate limit exceeded', {
|
|
|
|
|
|
// status: 429
|
|
|
|
|
|
// })
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2024-08-25 11:00:20 +00:00
|
|
|
|
fields
|
2024-08-07 12:23:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
|
throw new Error(error)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return { status: 'ok' }
|
|
|
|
|
|
})
|