From fb4646500c836f37b6da730fb11dea89957004ea Mon Sep 17 00:00:00 2001 From: taskylizard <75871323+taskylizard@users.noreply.github.com> Date: Sun, 12 Nov 2023 16:40:43 +0530 Subject: [PATCH] Better Feedback API --- .vitepress/routes/index.post.ts | 22 ++++---- .vitepress/theme/components/Feedback.vue | 64 +++++++++--------------- .vitepress/types/Feedback.ts | 10 ++++ package.json | 3 +- pnpm-lock.yaml | 7 +++ 5 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 .vitepress/types/Feedback.ts diff --git a/.vitepress/routes/index.post.ts b/.vitepress/routes/index.post.ts index 979fd8ac6..27515c312 100644 --- a/.vitepress/routes/index.post.ts +++ b/.vitepress/routes/index.post.ts @@ -1,10 +1,5 @@ import { fetcher } from "itty-fetcher"; - -interface Feedback { - message: string; - feedbackType?: string; - contactEmail?: string; -} +import { FeedbackSchema } from "../types/Feedback"; const feedbackOptions = [ { label: "🐞 Bug", value: "bug" }, @@ -24,12 +19,15 @@ function getFeedbackOption(value: string) { } export default defineEventHandler(async (event) => { - const { message, contactEmail, feedbackType } = await readBody(event); + const { message, page, contact, type } = await readValidatedBody(event, FeedbackSchema.parse); const env = useRuntimeConfig(event); - if (!["bug", "suggestion", "other", "appreciate"].includes(feedbackType!) || !message) { + if (!["bug", "suggestion", "other", "appreciate"].includes(type!) || !message) throw new Error("Invalid input."); - } + + let description = `${message}\n\n`; + if (contact) description += `**Contact:** ${contact}`; + if (page) description += `**Page:** \`${page}\``; await fetcher() .post(env.WEBHOOK_URL, { @@ -38,8 +36,8 @@ export default defineEventHandler(async (event) => { embeds: [ { color: 3447003, - title: getFeedbackOption(feedbackType).label, - description: contactEmail ? `${message}\n\n**Contact:** ${contactEmail}` : message, + title: getFeedbackOption(type).label, + description: description, }, ], }) @@ -47,5 +45,5 @@ export default defineEventHandler(async (event) => { throw new Error(error); }); - return { status: "success" }; + return { status: "ok" }; }); diff --git a/.vitepress/theme/components/Feedback.vue b/.vitepress/theme/components/Feedback.vue index 91fb4c175..b0a2e5f95 100644 --- a/.vitepress/theme/components/Feedback.vue +++ b/.vitepress/theme/components/Feedback.vue @@ -1,18 +1,14 @@