import prisma from "@calcom/prisma"; import { ApiKey } from "@prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey"; type ResponseData = { data?: ApiKey; message?: string; error?: string; }; async function createApiKey(req: NextApiRequest, res: NextApiResponse) { const { body, method } = req; if (method === "POST") { const safe = schemaApiKey.safeParse(body); if (safe.success && safe.data) { const apiKey = await prisma.apiKey .create({ data: { ...safe.data, user: { connect: { id: 1 } } } }) if (apiKey) { res.status(201).json({ data: apiKey }); } else { // Reject any other HTTP method than POST res.status(405).json({ error: "Only POST Method allowed" }); } // .then((apiKey) => res.status(201).json({ data: apiKey })) // .catch((error) => { // res.status(400).json({ message: "Could not create apiKey", error: error }) // } // ) } } else { // Reject any other HTTP method than POST res.status(405).json({ error: "Only POST Method allowed" }); } } export default withValidApiKey(createApiKey);