chore: no default export

This commit is contained in:
Agusti Fernandez Pardo
2022-03-29 03:23:22 +02:00
parent 75e635cb37
commit 3011e0ecf7
9 changed files with 29 additions and 19 deletions
+1 -1
View File
@@ -12,4 +12,4 @@ const withMiddleware = label(
["sentry","verifyApiKey"] // <-- Provide a list of middleware to call automatically
);
export default withMiddleware;
export { withMiddleware };
+3
View File
@@ -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
+1 -1
View File
@@ -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");
+3 -2
View File
@@ -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)));
+2 -1
View File
@@ -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)));
+2 -1
View File
@@ -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));
+2 -1
View File
@@ -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
View File
@@ -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));