cal.pub0.org/pages/api/api-keys/[id]/index.ts

27 lines
935 B
TypeScript
Raw Normal View History

import prisma from "@calcom/prisma";
import { ApiKey } from "@prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
2022-03-26 21:29:30 +00:00
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = {
data?: ApiKey;
message?: string;
error?: unknown;
};
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdAsString.safeParse(query);
2022-03-26 21:29:30 +00:00
if (method === "GET" && safe.success) {
const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
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 });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
}
export default withValidQueryIdString(apiKey);