2023-11-12 16:42:57 +00:00
|
|
|
import { fetcher } from "itty-fetcher";
|
|
|
|
import { FeedbackSchema } from "../types/Feedback";
|
|
|
|
|
|
|
|
const feedbackOptions = [
|
|
|
|
{ label: "🐞 Bug", value: "bug" },
|
|
|
|
{
|
|
|
|
label: "♻️ Suggestion",
|
|
|
|
value: "suggestion",
|
|
|
|
},
|
|
|
|
{ label: "📂 Other", value: "other" },
|
|
|
|
{
|
|
|
|
label: "❤️ Appreciation",
|
|
|
|
value: "appreciate",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2023-11-19 08:57:14 +00:00
|
|
|
function getFeedbackOption(value: string): string {
|
|
|
|
return feedbackOptions.find((option) => option.value === value).label;
|
2023-11-12 16:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
const { message, page, contact, type } = await readValidatedBody(event, FeedbackSchema.parse);
|
|
|
|
const env = useRuntimeConfig(event);
|
|
|
|
|
|
|
|
if (!["bug", "suggestion", "other", "appreciate"].includes(type!) || !message)
|
|
|
|
throw new Error("Invalid input.");
|
|
|
|
|
|
|
|
let description = `${message}\n\n`;
|
2023-11-19 06:46:39 +00:00
|
|
|
if (contact) description += `**Contact:** ${contact}\n`;
|
2023-11-12 16:42:57 +00:00
|
|
|
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,
|
2023-11-19 08:57:14 +00:00
|
|
|
title: getFeedbackOption(type),
|
2023-11-14 17:18:52 +00:00
|
|
|
description,
|
2023-11-12 16:42:57 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
throw new Error(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
return { status: "ok" };
|
|
|
|
});
|