api/routes/index.post.ts
78 lines · 1938 bytes
1/**
2 * Copyright (c) 2024 taskylizard
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { fetcher } from 'itty-fetcher'
17import {
18 FeedbackSchema,
19 getFeedbackOption
20} from '../../docs/.vitepress/types/Feedback'
21
22export default defineEventHandler(async (event) => {
23 const { message, page, type, heading } = await readValidatedBody(
24 event,
25 FeedbackSchema.parseAsync
26 )
27 const env = useRuntimeConfig(event)
28
29 const fields = [
30 {
31 name: 'Page',
32 value: page,
33 inline: true
34 },
35 {
36 name: 'Message',
37 value: message,
38 inline: false
39 }
40 ]
41
42 if (heading) {
43 fields.unshift({
44 name: 'Section',
45 value: heading,
46 inline: true
47 })
48 }
49
50 // FIXME: somehow this is not working, but it worked before
51 // const path = 'feedback'
52 //
53 // const { success } = await env.MY_RATE_LIMITER.limit({ key: path })
54 // if (!success) {
55 // return new Response('429 Failure – global rate limit exceeded', {
56 // status: 429
57 // })
58 // }
59
60 await fetcher()
61 .post(env.WEBHOOK_URL, {
62 username: 'Feedback',
63 avatar_url:
64 'https://i.kym-cdn.com/entries/icons/facebook/000/043/403/cover3.jpg',
65 embeds: [
66 {
67 color: 3447003,
68 title: getFeedbackOption(type)!.label,
69 fields
70 }
71 ]
72 })
73 .catch((error) => {
74 throw new Error(error)
75 })
76
77 return { status: 'ok' }
78})