cal.pub0.org/pages/api/api-keys/new.ts

28 lines
868 B
TypeScript
Raw Normal View History

import prisma from "@calcom/prisma";
2022-03-26 04:28:53 +00:00
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<ResponseData>) {
const { body, method } = req;
2022-03-26 21:29:30 +00:00
const safe = schemaApiKey.safeParse(body);
if (method === "POST" && safe.success) {
const apiKey = await prisma.apiKey
.create({ data: { ...safe.data, user: { connect: { id: 1 } } } })
if (apiKey) res.status(201).json({ data: apiKey });
else res.status(404).json({ message: "API Key not created" });
} else res.status(405).json({ error: "Only POST Method allowed" });
}
export default withValidApiKey(createApiKey);