chore: no default export
This commit is contained in:
@@ -12,4 +12,4 @@ const withMiddleware = label(
|
||||
["sentry","verifyApiKey"] // <-- Provide a list of middleware to call automatically
|
||||
);
|
||||
|
||||
export default withMiddleware;
|
||||
export { withMiddleware };
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
@@ -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<Respo
|
||||
} else res.status(405).json({ message: "Only DELETE Method allowed" });
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(deleteUser);
|
||||
export default withMiddleware("addRequestId")((withValidQueryIdTransformParseInt(deleteUser)));
|
||||
|
||||
@@ -4,6 +4,7 @@ import { User } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaUser, withValidUser } from "@lib/validations/user";
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
@@ -29,4 +30,4 @@ export async function editUser(req: NextApiRequest, res: NextApiResponse<Respons
|
||||
} else res.status(405).json({ message: "Only PATCH Method allowed for updating users" });
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(withValidUser(editUser));
|
||||
export default withMiddleware("addRequestId")(withValidQueryIdTransformParseInt(withValidUser(editUser)));
|
||||
|
||||
@@ -4,6 +4,7 @@ import { User } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
|
||||
type ResponseData = {
|
||||
data?: User;
|
||||
@@ -24,4 +25,4 @@ export async function user(req: NextApiRequest, res: NextApiResponse<ResponseDat
|
||||
}
|
||||
|
||||
|
||||
export default withValidQueryIdTransformParseInt(user);
|
||||
export default withMiddleware("addRequestId")(withValidQueryIdTransformParseInt(user));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { User } from "@calcom/prisma/client";
|
||||
import withMiddleware from "@lib/helpers/withMiddleware";
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
type ResponseData = {
|
||||
@@ -14,4 +14,5 @@ async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
if (data) res.status(200).json({ data });
|
||||
else res.status(400).json({ error: "No data found" });
|
||||
}
|
||||
|
||||
export default withMiddleware("addRequestId")(user);
|
||||
+12
-12
@@ -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<ResponseData>) {
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user