2022-03-25 19:17:37 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-26 00:40:43 +00:00
|
|
|
import { ApiKey } from "@prisma/client";
|
2022-03-25 19:17:37 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-26 21:29:30 +00:00
|
|
|
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
|
|
|
type ResponseData = {
|
2022-03-25 19:17:37 +00:00
|
|
|
data?: ApiKey;
|
2022-03-25 18:37:51 +00:00
|
|
|
message?: string;
|
|
|
|
error?: unknown;
|
|
|
|
};
|
|
|
|
|
2022-03-25 19:17:37 +00:00
|
|
|
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
2022-03-25 18:37:51 +00:00
|
|
|
const { query, method } = req;
|
2022-03-26 00:40:43 +00:00
|
|
|
const safe = await schemaQueryIdAsString.safeParse(query);
|
2022-03-26 21:29:30 +00:00
|
|
|
if (method === "GET" && safe.success) {
|
2022-03-25 19:17:37 +00:00
|
|
|
const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
|
2022-03-25 23:42:12 +00:00
|
|
|
if (!apiKey) res.status(404).json({ message: "API key was not found" });
|
2022-03-26 21:29:30 +00:00
|
|
|
else res.status(200).json({ data: apiKey });
|
2022-03-26 23:58:22 +00:00
|
|
|
// Reject any other HTTP method than POST
|
|
|
|
} else res.status(405).json({ message: "Only GET Method allowed" });
|
2022-03-25 18:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-25 19:17:37 +00:00
|
|
|
export default withValidQueryIdString(apiKey);
|