2022-03-26 00:40:43 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { ApiKey } from "@calcom/prisma/client";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-04-02 01:46:24 +00:00
|
|
|
import { schemaApiKeyBodyParams, withValidApiKey } from "@lib/validations/api-key";
|
2022-03-29 01:59:57 +00:00
|
|
|
|
2022-03-25 18:37:51 +00:00
|
|
|
type ResponseData = {
|
|
|
|
data?: ApiKey;
|
2022-03-30 12:17:55 +00:00
|
|
|
error?: unknown;
|
2022-03-25 18:37:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
2022-03-30 12:17:55 +00:00
|
|
|
const safe = schemaApiKeyBodyParams.safeParse(req.body);
|
2022-03-29 01:59:57 +00:00
|
|
|
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-29 01:59:57 +00:00
|
|
|
}
|
2022-03-25 18:37:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
export default withMiddleware("addRequestId", "HTTP_POST")(withValidApiKey(createApiKey));
|