From 3011e0ecf744ca4abec9b32647b7a91f7e85b0dd Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Tue, 29 Mar 2022 03:23:22 +0200 Subject: [PATCH] chore: no default export --- lib/helpers/withMiddleware.ts | 2 +- lib/validations/shared/queryIdString.ts | 3 +++ .../shared/queryIdTransformParseInt.ts | 3 +++ pages/_middleware.ts | 2 +- pages/api/users/[id]/delete.ts | 5 ++-- pages/api/users/[id]/edit.ts | 3 ++- pages/api/users/[id]/index.ts | 3 ++- pages/api/users/index.ts | 3 ++- pages/api/users/new.ts | 24 +++++++++---------- 9 files changed, 29 insertions(+), 19 deletions(-) diff --git a/lib/helpers/withMiddleware.ts b/lib/helpers/withMiddleware.ts index 99f3958af7..d333439df8 100644 --- a/lib/helpers/withMiddleware.ts +++ b/lib/helpers/withMiddleware.ts @@ -12,4 +12,4 @@ const withMiddleware = label( ["sentry","verifyApiKey"] // <-- Provide a list of middleware to call automatically ); -export default withMiddleware; \ No newline at end of file +export { withMiddleware }; \ No newline at end of file diff --git a/lib/validations/shared/queryIdString.ts b/lib/validations/shared/queryIdString.ts index c221606841..e89986dca5 100644 --- a/lib/validations/shared/queryIdString.ts +++ b/lib/validations/shared/queryIdString.ts @@ -5,6 +5,9 @@ import { z } from "zod"; // at different endpoints that require this validation. const schemaQueryIdAsString = z .object({ + // since we added apiKey as query param this is required by next-validations helper + // for query params to work properly and not fail. + apiKey: z.string().cuid(), // since nextjs parses query params as strings, // we need to cast them to numbers using z.transform() and parseInt() id: z.string() diff --git a/lib/validations/shared/queryIdTransformParseInt.ts b/lib/validations/shared/queryIdTransformParseInt.ts index d185d1a255..b1f2eb4835 100644 --- a/lib/validations/shared/queryIdTransformParseInt.ts +++ b/lib/validations/shared/queryIdTransformParseInt.ts @@ -5,6 +5,9 @@ import { z } from "zod"; // at different endpoints that require this validation. const schemaQueryIdParseInt = z .object({ + // since we added apiKey as query param this is required by next-validations helper + // for query params to work properly and not fail. + apiKey: z.string().cuid(), // since nextjs parses query params as strings, // we need to cast them to numbers using z.transform() and parseInt() id: z diff --git a/pages/_middleware.ts b/pages/_middleware.ts index c631934a91..624d392043 100644 --- a/pages/_middleware.ts +++ b/pages/_middleware.ts @@ -3,7 +3,7 @@ import { NextRequest, NextResponse } from "next/server"; // Not much useful yet as prisma.client can't be used in the middlewares (client is not available) // For now we just throw early if no apiKey is passed, // but we could also check if the apiKey is valid if we had prisma here. -export async function middleware({ nextUrl }: NextRequest) { +export async function requireApiKeyAsQueryParams({ nextUrl }: NextRequest) { const response = NextResponse.next(); const apiKey = nextUrl.searchParams.get("apiKey"); diff --git a/pages/api/users/[id]/delete.ts b/pages/api/users/[id]/delete.ts index f7bbf96856..1a2a2b240e 100644 --- a/pages/api/users/[id]/delete.ts +++ b/pages/api/users/[id]/delete.ts @@ -1,7 +1,8 @@ -import prisma from "@calcom/prisma"; import type { NextApiRequest, NextApiResponse } from "next"; +import prisma from "@calcom/prisma"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; @@ -24,4 +25,4 @@ export async function deleteUser(req: NextApiRequest, res: NextApiResponse) { if (data) res.status(200).json({ data }); else res.status(400).json({ error: "No data found" }); } + export default withMiddleware("addRequestId")(user); \ No newline at end of file diff --git a/pages/api/users/new.ts b/pages/api/users/new.ts index 7f319e62d9..1ad079ca44 100644 --- a/pages/api/users/new.ts +++ b/pages/api/users/new.ts @@ -1,26 +1,26 @@ -import prisma from "@calcom/prisma"; - -import { User } from "@calcom/prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; +import prisma from "@calcom/prisma"; +import { User } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import { schemaUser, withValidUser } from "@lib/validations/user"; + type ResponseData = { data?: User; - message?: string; - error?: string; + error?: object; }; async function createUser(req: NextApiRequest, res: NextApiResponse) { const { body, method } = req; const safe = schemaUser.safeParse(body); if (method === "POST" && safe.success) { - await prisma.user - .create({ data: safe.data }) - .then((data) => res.status(201).json({ data })) - .catch((error) => res.status(400).json({ message: "Could not create user type", error: error })); - // Reject any other HTTP method than POST - } else res.status(405).json({ error: "Only POST Method allowed" }); + const data = await prisma.user + .create({ data: safe.data }) + if (data) res.status(201).json({ data }) + else (error: unknown) => res.status(400).json({ error: { message: "Could not create user type", error: error } }); + // Reject any other HTTP method than POST + } else res.status(405).json({ error: { message: "Only POST Method allowed" } }); } -export default withValidUser(createUser); +export default withMiddleware("addRequestId")(withValidUser(createUser));