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

25 lines
855 B
TypeScript
Raw Normal View History

import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { ApiKey } from "@calcom/prisma/client";
2022-03-30 12:17:55 +00:00
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { schemaApiKeyBodyParams, withValidApiKey } from "@lib/validations/api-key";
type ResponseData = {
data?: ApiKey;
2022-03-30 12:17:55 +00:00
error?: unknown;
};
async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
2022-03-30 12:17:55 +00:00
const safe = schemaApiKeyBodyParams.safeParse(req.body);
if (safe.success) {
2022-03-30 12:17:55 +00:00
const data = await prisma.apiKey.create({ data: safe.data });
if (data) res.status(201).json({ data });
else
(error: unknown) => res.status(400).json({ error: { message: "Could not create apiKey type", error } });
}
}
2022-03-30 12:17:55 +00:00
export default withMiddleware("addRequestId", "HTTP_POST")(withValidApiKey(createApiKey));