Scheduling infrastructure for absolutely everyone.
feat: adds api keys endpoints, new QueryIdAsString as apiKey has a UUID string-like ID unlike the numbers of event-type or others
Agusti Fernandez Pardod9c34f67e628
diff --git a/lib/validations/queryIdString.ts b/lib/validations/queryIdString.tsnew file mode 100644index 00000000000..02c1e6aee55--- /dev/null+++ b/lib/validations/queryIdString.ts@@ -0,0 +1,20 @@+import { withValidation } from "next-validations";+import { z } from "zod";++// Extracted out as utility function so can be reused+// at different endpoints that require this validation.+const schemaQueryId = z+ .object({+ // since nextjs parses query params as strings,+ // we need to cast them to numbers using z.transform() and parseInt()+ id: z.string().uuid()+ })+ .strict();++const withValidQueryIdString = withValidation({+ schema: schemaQueryId,+ type: "Zod",+ mode: "query",+});++export { schemaQueryId, withValidQueryIdString };diff --git a/lib/validations/queryIdTransformParseInt.ts b/lib/validations/queryIdTransformParseInt.tsindex 1bbb5cfa09c..f99ea64e18b 100644--- a/lib/validations/queryIdTransformParseInt.ts+++ b/lib/validations/queryIdTransformParseInt.ts@@ -14,10 +14,10 @@ const schemaQueryId = z}).strict();-const withValidQueryId = withValidation({+const withValidQueryIdTransformParseInt = withValidation({schema: schemaQueryId,type: "Zod",mode: "query",});-export { schemaQueryId, withValidQueryId };+export { schemaQueryId, withValidQueryIdTransformParseInt };diff --git a/next.config.js b/next.config.jsindex 3f24426e894..7cd0ff53d79 100644--- a/next.config.js+++ b/next.config.js@@ -1,7 +1,10 @@+// https://www.npmjs.com/package/next-transpile-modules+// This makes our @calcom/prisma package from the monorepo to be transpiled and usable by APIconst withTM = require("next-transpile-modules")(["@calcom/prisma",]);+// use something like withPlugins([withTM], {}) if more plugins added later.module.exports = withTM({async rewrites() {diff --git a/pages/api/api-keys/[id]/delete.ts b/pages/api/api-keys/[id]/delete.tsindex 7912907b518..488712d91d7 100644--- a/pages/api/api-keys/[id]/delete.ts+++ b/pages/api/api-keys/[id]/delete.ts@@ -1,36 +1,34 @@-import { PrismaClient } from "@prisma/client";-import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";+import { NextApiRequest, NextApiResponse } from "next";-import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";--const prisma = new PrismaClient();+import { schemaQueryId, withValidQueryIdString } from "@lib/validations/queryIdString";type ResponseData = {message?: string;error?: unknown;};-export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {const { query, method } = req;const safe = await schemaQueryId.safeParse(query);if (safe.success) {if (method === "DELETE") {// DELETE WILL DELETE THE EVENT TYPE- prisma.eventType+ prisma.apiKey.delete({ where: { id: safe.data.id } }).then(() => {- // We only remove the event type from the database if there's an existing resource.- res.status(200).json({ message: `event-type with id: ${safe.data.id} deleted successfully` });+ // We only remove the api key from the database if there's an existing resource.+ res.status(200).json({ message: `api-key with id: ${safe.data.id} deleted successfully` });}).catch((error) => {- // This catches the error thrown by prisma.eventType.delete() if the resource is not found.+ // This catches the error thrown by prisma.apiKey.delete() if the resource is not found.res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });});} else {// Reject any other HTTP method than POST- res.status(405).json({ message: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" });+ res.status(405).json({ message: "Only DELETE Method allowed in /api-keys/[id]/delete endpoint" });}}}-export default withValidQueryId(eventType);+export default withValidQueryIdString(apiKey);diff --git a/pages/api/api-keys/[id]/edit.ts b/pages/api/api-keys/[id]/edit.tsindex c4eac80975a..ce97a24b8eb 100644--- a/pages/api/api-keys/[id]/edit.ts+++ b/pages/api/api-keys/[id]/edit.ts@@ -1,37 +1,37 @@-import { PrismaClient, EventType } from "@prisma/client";-import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";-import { schemaEventType, withValidEventType } from "@lib/validations/eventType";-import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";+import { ApiKey } from "@calcom/prisma/client";+import type { NextApiRequest, NextApiResponse } from "next";-const prisma = new PrismaClient();+import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";+import { schemaQueryId, withValidQueryIdString } from "@lib/validations/queryIdString";type ResponseData = {- data?: EventType;+ data?: ApiKey;message?: string;error?: unknown;};-export async function editEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+export async function editApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {const { query, body, method } = req;const safeQuery = await schemaQueryId.safeParse(query);- const safeBody = await schemaEventType.safeParse(body);+ const safeBody = await schemaApiKey.safeParse(body);if (method === "PATCH") {if (safeQuery.success && safeBody.success) {- await prisma.eventType.update({+ await prisma.apiKey.update({where: { id: safeQuery.data.id },data: safeBody.data,- }).then(event => {- res.status(200).json({ data: event });+ }).then(apiKey => {+ res.status(200).json({ data: apiKey });}).catch(error => {res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })});}} else {// Reject any other HTTP method than POST- res.status(405).json({ message: "Only PATCH Method allowed for updating event-types" });+ res.status(405).json({ message: "Only PATCH Method allowed for updating apiKey-types" });}}-export default withValidQueryId(withValidEventType(editEventType));+export default withValidQueryIdString(withValidApiKey(editApiKey));diff --git a/pages/api/api-keys/[id]/index.ts b/pages/api/api-keys/[id]/index.tsindex 087a30cd55c..177ba289d96 100644--- a/pages/api/api-keys/[id]/index.ts+++ b/pages/api/api-keys/[id]/index.ts@@ -1,25 +1,25 @@-import { PrismaClient, EventType } from "@prisma/client";-import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";-import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";+import { ApiKey } from "@calcom/prisma/client";+import type { NextApiRequest, NextApiResponse } from "next";-const prisma = new PrismaClient();+import { schemaQueryId, withValidQueryIdString } from "@lib/validations/queryIdString";type ResponseData = {- data?: EventType;+ data?: ApiKey;message?: string;error?: unknown;};-export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {const { query, method } = req;const safe = await schemaQueryId.safeParse(query);if (safe.success) {if (method === "GET") {- const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });+ const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });- if (event) res.status(200).json({ data: event });- if (!event) res.status(404).json({ message: "Event type not found" });+ if (apiKey) res.status(200).json({ data: apiKey });+ if (!apiKey) res.status(404).json({ message: "Event type not found" });} else {// Reject any other HTTP method than POSTres.status(405).json({ message: "Only GET Method allowed" });@@ -28,4 +28,4 @@ export async function eventType(req: NextApiRequest, res: NextApiResponse<Respon}-export default withValidQueryId(eventType);+export default withValidQueryIdString(apiKey);diff --git a/pages/api/api-keys/index.ts b/pages/api/api-keys/index.tsindex 257fa060a15..d6337207e20 100644--- a/pages/api/api-keys/index.ts+++ b/pages/api/api-keys/index.ts@@ -1,7 +1,7 @@-import { PrismaClient, ApiKey } from "@prisma/client";-import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";++import { ApiKey } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";-const prisma = new PrismaClient();type ResponseData = {data?: ApiKey[];error?: unknown;diff --git a/pages/api/api-keys/new.ts b/pages/api/api-keys/new.tsindex 5c4e210cca3..2e649321c0e 100644--- a/pages/api/api-keys/new.ts+++ b/pages/api/api-keys/new.ts@@ -1,9 +1,9 @@-import { PrismaClient, ApiKey } from "@prisma/client";-import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";++import { ApiKey } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";-const prisma = new PrismaClient();type ResponseData = {data?: ApiKey;message?: string;@@ -18,8 +18,8 @@ async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseDaawait prisma.apiKey.create({ data: { ...safe.data, user: { connect: { id: 1 } }}})- .then((event) => res.status(201).json({ data: event }))- .catch((error) => res.status(400).json({ message: "Could not create event type", error: error }));+ .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 POSTdiff --git a/pages/api/event-types/[id]/delete.ts b/pages/api/event-types/[id]/delete.tsindex 7912907b518..52f2cd74b7c 100644--- a/pages/api/event-types/[id]/delete.ts+++ b/pages/api/event-types/[id]/delete.ts@@ -1,9 +1,9 @@-import { PrismaClient } from "@prisma/client";+import prisma from "@calcom/prisma";+import type { NextApiRequest, NextApiResponse } from "next";-import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";+import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";-const prisma = new PrismaClient();type ResponseData = {message?: string;@@ -33,4 +33,4 @@ export async function eventType(req: NextApiRequest, res: NextApiResponse<Respon}}-export default withValidQueryId(eventType);+export default withValidQueryIdTransformParseInt(eventType);diff --git a/pages/api/event-types/[id]/edit.ts b/pages/api/event-types/[id]/edit.tsindex c4eac80975a..0194678226c 100644--- a/pages/api/event-types/[id]/edit.ts+++ b/pages/api/event-types/[id]/edit.ts@@ -1,10 +1,10 @@-import { PrismaClient, EventType } from "@prisma/client";+import prisma from "@calcom/prisma";++import { EventType } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";import { schemaEventType, withValidEventType } from "@lib/validations/eventType";-import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";--const prisma = new PrismaClient();+import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";type ResponseData = {data?: EventType;@@ -34,4 +34,4 @@ export async function editEventType(req: NextApiRequest, res: NextApiResponse<Re}}-export default withValidQueryId(withValidEventType(editEventType));+export default withValidQueryIdTransformParseInt(withValidEventType(editEventType));diff --git a/pages/api/event-types/[id]/index.ts b/pages/api/event-types/[id]/index.tsindex 087a30cd55c..1c4bc056fba 100644--- a/pages/api/event-types/[id]/index.ts+++ b/pages/api/event-types/[id]/index.ts@@ -1,9 +1,9 @@-import { PrismaClient, EventType } from "@prisma/client";-import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";-import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";+import { EventType } from "@calcom/prisma/client";+import type { NextApiRequest, NextApiResponse } from "next";-const prisma = new PrismaClient();+import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";type ResponseData = {data?: EventType;@@ -28,4 +28,4 @@ export async function eventType(req: NextApiRequest, res: NextApiResponse<Respon}-export default withValidQueryId(eventType);+export default withValidQueryIdTransformParseInt(eventType);diff --git a/pages/api/event-types/index.ts b/pages/api/event-types/index.tsindex 946dcde907b..faad9e43ba9 100644--- a/pages/api/event-types/index.ts+++ b/pages/api/event-types/index.ts@@ -1,7 +1,7 @@-import { PrismaClient, EventType } from "@prisma/client";-import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";++import { EventType } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";-const prisma = new PrismaClient();type ResponseData = {data?: EventType[];error?: unknown;diff --git a/pages/api/event-types/new.ts b/pages/api/event-types/new.tsindex efe01150da2..65d893e96c3 100644--- a/pages/api/event-types/new.ts+++ b/pages/api/event-types/new.ts@@ -1,9 +1,9 @@-import { PrismaClient, EventType } from "@prisma/client";-import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";++import { EventType } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";import { schemaEventType, withValidEventType } from "@lib/validations/eventType";-const prisma = new PrismaClient();type ResponseData = {data?: EventType;message?: string;