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

45 lines
1.3 KiB
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;
if (method === "POST") {
const safe = schemaApiKey.safeParse(body);
if (safe.success && safe.data) {
2022-03-26 04:28:53 +00:00
const apiKey = await prisma.apiKey
.create({
data: {
...safe.data, user: { connect: { id: 1 } }
}
})
2022-03-26 04:28:53 +00:00
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);