api/routes/index.post.ts

0ffcf34f8c2075baf68e1bfdf58c248fa9d3c61f
wotaku/api/routes/index.post.ts history · blame · raw

68 lines · 1525 bytes

 1/**
 2 *  All Rights Reserved
 3 *
 4 *  Copyright (c) 2025 taskylizard
 5 *
 6 *  All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
 7 */
 8import { fetcher } from 'itty-fetcher'
 9import {
10  FeedbackSchema,
11  getFeedbackOption
12} from '../../docs/.vitepress/types/Feedback'
13
14export default defineEventHandler(async (event) => {
15  const { message, page, type, heading } = await readValidatedBody(
16    event,
17    FeedbackSchema.parseAsync
18  )
19  const env = useRuntimeConfig(event)
20
21  const fields = [
22    {
23      name: 'Page',
24      value: page,
25      inline: true
26    },
27    {
28      name: 'Message',
29      value: message,
30      inline: false
31    }
32  ]
33
34  if (heading) {
35    fields.unshift({
36      name: 'Section',
37      value: heading,
38      inline: true
39    })
40  }
41
42  // FIXME: somehow this is not working, but it worked before
43  // const path = 'feedback'
44  //
45  // const { success } = await env.MY_RATE_LIMITER.limit({ key: path })
46  // if (!success) {
47  //   return new Response('429 Failure – global rate limit exceeded', {
48  //     status: 429
49  //   })
50  // }
51
52  await fetcher()
53    .post(env.WEBHOOK_URL, {
54      username: 'Feedback',
55      embeds: [
56        {
57          color: 3447003,
58          title: getFeedbackOption(type)!.label,
59          fields
60        }
61      ]
62    })
63    .catch((error) => {
64      throw new Error(error)
65    })
66
67  return { status: 'ok' }
68})