api/routes/index.post.ts

4c266e755e45d573c74471477f8f454db2d63903
wotaku/api/routes/index.post.ts
taskylizard wotaku 2bea9706e6c 1 year ago

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})
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form