diff --git a/lib/helpers/httpMethods.ts b/lib/helpers/httpMethods.ts new file mode 100644 index 0000000000..4bfeb1b1fe --- /dev/null +++ b/lib/helpers/httpMethods.ts @@ -0,0 +1,19 @@ +import { NextMiddleware } from "next-api-middleware"; + +export const httpMethod = ( + allowedHttpMethod: "GET" | "POST" | "PATCH" | "DELETE" +): NextMiddleware => { + return async function (req, res, next) { + if (req.method === allowedHttpMethod || req.method == "OPTIONS") { + await next(); + } else { + res.status(404); + res.end(); + } + }; +}; + +export const postOnly = httpMethod("POST"); +export const getOnly = httpMethod("GET"); +export const patchOnly = httpMethod("PATCH"); +export const deleteOnly = httpMethod("DELETE"); \ No newline at end of file diff --git a/lib/helpers/withMiddleware.ts b/lib/helpers/withMiddleware.ts index d333439df8..7d94e5da7a 100644 --- a/lib/helpers/withMiddleware.ts +++ b/lib/helpers/withMiddleware.ts @@ -2,9 +2,13 @@ import { label } from "next-api-middleware"; import { addRequestId } from "./addRequestid"; import { captureErrors } from "./captureErrors"; import { verifyApiKey } from "./verifyApiKey"; - +import { postOnly, deleteOnly, patchOnly, getOnly } from "./httpMethods"; const withMiddleware = label( { + getOnly, + patchOnly, + postOnly, + deleteOnly, addRequestId, verifyApiKey, sentry: captureErrors, // <-- Optionally alias middleware diff --git a/pages/api/api-keys/[id]/delete.ts b/pages/api/api-keys/[id]/delete.ts index 10e6aaef98..722705a303 100644 --- a/pages/api/api-keys/[id]/delete.ts +++ b/pages/api/api-keys/[id]/delete.ts @@ -1,27 +1,29 @@ -import prisma from "@calcom/prisma"; import type { NextApiRequest, NextApiResponse } from "next"; +import prisma from "@calcom/prisma"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; - type ResponseData = { message?: string; error?: unknown; }; -export async function apiKey(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdAsString.safeParse(query); - if (method === "DELETE" && safe.success && safe.data) { - const apiKey = await prisma.apiKey +export async function deleteApiKey(req: NextApiRequest, res: NextApiResponse) { + const safe = await schemaQueryIdAsString.safeParse(req.query); + if (safe.success) { + const data = await prisma.apiKey .delete({ where: { id: safe.data.id } }) // We only remove the apiKey type from the database if there's an existing resource. - if (apiKey) res.status(200).json({ message: `apiKey with id: ${safe.data.id} deleted successfully` }); + if (data) res.status(200).json({ message: `ApiKey with id: ${safe.data.id} deleted successfully` }); // This catches the error thrown by prisma.apiKey.delete() if the resource is not found. - else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only DELETE Method allowed" }); + else res.status(400).json({ message: `ApiKey with id: ${safe.data.id} was not able to be processed` }); + } } -export default withValidQueryIdString(apiKey); +export default withMiddleware("deleteOnly", "addRequestId")( + withValidQueryIdString( + deleteApiKey + ) +); diff --git a/pages/api/api-keys/[id]/edit.ts b/pages/api/api-keys/[id]/edit.ts index 4fc4572444..d436faf967 100644 --- a/pages/api/api-keys/[id]/edit.ts +++ b/pages/api/api-keys/[id]/edit.ts @@ -1,9 +1,10 @@ import prisma from "@calcom/prisma"; -import { ApiKey } from "@prisma/client"; +import { ApiKey } from "@calcom/prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; type ResponseData = { @@ -17,17 +18,19 @@ export async function editApiKey(req: NextApiRequest, res: NextApiResponse { - res.status(200).json({ data: apiKey }); - }).catch(error => { - res.status(404).json({ message: `apiKey with ID ${safeQuery.data.id} not found and wasn't updated`, error }) - }); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only PATCH Method allowed for updating API keys" }); + }) + if (data) res.status(200).json({ data }); + else (error: unknown) => res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error }) + } } -export default withValidQueryIdString(withValidApiKey(editApiKey)); +export default withMiddleware("patchOnly","addRequestId")( + withValidQueryIdString( + withValidApiKey( + editApiKey) + ) +); diff --git a/pages/api/api-keys/[id]/index.ts b/pages/api/api-keys/[id]/index.ts index 49b3a7f6a2..18d6dbcc69 100644 --- a/pages/api/api-keys/[id]/index.ts +++ b/pages/api/api-keys/[id]/index.ts @@ -1,9 +1,10 @@ import prisma from "@calcom/prisma"; -import { ApiKey } from "@prisma/client"; +import { ApiKey } from "@calcom/prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; type ResponseData = { data?: ApiKey; @@ -11,16 +12,19 @@ type ResponseData = { error?: unknown; }; -export async function apiKey(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdAsString.safeParse(query); - 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" }); - 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 async function apiKeyById(req: NextApiRequest, res: NextApiResponse) { + const safe = await schemaQueryIdAsString.safeParse(req.query); + if (safe.success) { + const data = await prisma.apiKey.findUnique({ where: { id: safe.data.id } }); + + if (data) res.status(200).json({ data }); + else res.status(404).json({ message: "ApiKey was not found" }); + } } -export default withValidQueryIdString(apiKey); +export default withMiddleware("addRequestId","getOnly")( + withValidQueryIdString( + apiKeyById + ) +); diff --git a/pages/api/api-keys/index.ts b/pages/api/api-keys/index.ts index 5155c36c59..eca77dbefe 100644 --- a/pages/api/api-keys/index.ts +++ b/pages/api/api-keys/index.ts @@ -1,18 +1,19 @@ import prisma from "@calcom/prisma"; -import { ApiKey } from "@prisma/client"; +import { ApiKey } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { NextApiRequest, NextApiResponse } from "next"; type ResponseData = { data?: ApiKey[]; error?: unknown; - message?: string; }; -export default async function apiKeys(req: NextApiRequest, res: NextApiResponse) { - const { method } = req; - if (method === "GET") { - const data = await prisma.apiKey.findMany({}); - res.status(200).json({ data }); - } else res.status(405).json({ message: "Only GET Method allowed" }); +async function allApiKeys(req: NextApiRequest, res: NextApiResponse) { + const data = await prisma.apiKey.findMany(); + + if (data) res.status(200).json({ data }); + else res.status(400).json({ error: "No data found" }); } + +export default withMiddleware("addRequestId","getOnly")(allApiKeys); \ No newline at end of file diff --git a/pages/api/api-keys/new.ts b/pages/api/api-keys/new.ts index 8787824fbd..b49b8a7049 100644 --- a/pages/api/api-keys/new.ts +++ b/pages/api/api-keys/new.ts @@ -1,27 +1,28 @@ -import prisma from "@calcom/prisma"; - -import { ApiKey } from "@prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; +import prisma from "@calcom/prisma"; +import { ApiKey } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey"; + type ResponseData = { data?: ApiKey; - message?: string; - error?: string; + error?: object; }; async function createApiKey(req: NextApiRequest, res: NextApiResponse) { - const { body, method } = req; - const safe = schemaApiKey.safeParse(body); - if (method === "POST" && safe.success) { - const apiKey = await prisma.apiKey - .create({ data: { ...safe.data, user: { connect: { id: 1 } } } }) - - if (apiKey) res.status(201).json({ data: apiKey }); - else res.status(404).json({ message: "API Key not created" }); - - } else res.status(405).json({ error: "Only POST Method allowed" }); + const safe = schemaApiKey.safeParse(req.body); + if (safe.success) { + const data = await prisma.apiKey + .create({ data: safe.data }) + if (data) res.status(201).json({ data }) + else (error: unknown) => res.status(400).json({ error: { message: "Could not create apiKey type", error: error } }); + } } -export default withValidApiKey(createApiKey); +export default withMiddleware("addRequestId","postOnly")( + withValidApiKey( + createApiKey + ) +); diff --git a/pages/api/booking-references/[id]/edit.ts b/pages/api/booking-references/[id]/edit.ts index cdfe7a739b..834574a3a1 100644 --- a/pages/api/booking-references/[id]/edit.ts +++ b/pages/api/booking-references/[id]/edit.ts @@ -3,7 +3,7 @@ import prisma from "@calcom/prisma"; import { BookingReference } from "@calcom/prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; -import { schemaBookingReference, withValidBookingReference } from "@lib/validations/bookingReference"; +import { schemaBookingReference, withValidBookingReference } from "@lib/validations/booking-reference"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; type ResponseData = { diff --git a/pages/api/booking-references/[id]/index.ts b/pages/api/booking-references/[id]/index.ts index ec973f5016..8a649fb4d9 100644 --- a/pages/api/booking-references/[id]/index.ts +++ b/pages/api/booking-references/[id]/index.ts @@ -18,7 +18,7 @@ export async function bookingReference(req: NextApiRequest, res: NextApiResponse const data = await prisma.bookingReference.findUnique({ where: { id: safe.data.id } }); if (data) res.status(200).json({ data }); - if (!data) res.status(404).json({ message: "Event type not found" }); + else res.status(404).json({ message: "Event type not found" }); // Reject any other HTTP method than POST } else res.status(405).json({ message: "Only GET Method allowed" }); } diff --git a/pages/api/booking-references/new.ts b/pages/api/booking-references/new.ts index ba21befec8..e792e3f1c7 100644 --- a/pages/api/booking-references/new.ts +++ b/pages/api/booking-references/new.ts @@ -3,7 +3,7 @@ import prisma from "@calcom/prisma"; import { BookingReference } from "@calcom/prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; -import { schemaBookingReference, withValidBookingReference } from "@lib/validations/bookingReference"; +import { schemaBookingReference, withValidBookingReference } from "@lib/validations/booking-reference"; type ResponseData = { data?: BookingReference; diff --git a/pages/api/credentials/[id]/index.ts b/pages/api/credentials/[id]/index.ts index 14ca851867..a15e0463bc 100644 --- a/pages/api/credentials/[id]/index.ts +++ b/pages/api/credentials/[id]/index.ts @@ -18,7 +18,7 @@ export async function credential(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "DELETE" && safe.success && safe.data) { - const dailyEventReference = await prisma.dailyEventReference + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (safe.success) { + const deletedDailyEventReference = await prisma.dailyEventReference .delete({ where: { id: safe.data.id } }) // We only remove the dailyEventReference type from the database if there's an existing resource. - if (dailyEventReference) res.status(200).json({ message: `dailyEventReference with id: ${safe.data.id} deleted successfully` }); + if (deletedDailyEventReference) res.status(200).json({ message: `dailyEventReference with id: ${safe.data.id} deleted successfully` }); // This catches the error thrown by prisma.dailyEventReference.delete() if the resource is not found. else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); // Reject any other HTTP method than POST } else res.status(405).json({ message: "Only DELETE Method allowed" }); } -export default withValidQueryIdTransformParseInt(deleteDailyEventReference); +// export default withValidQueryIdTransformParseInt(deleteDailyEventReference); +export default withMiddleware("deleteOnly")( + withValidQueryIdTransformParseInt( + deleteDailyEventReference + ) +); diff --git a/pages/api/daily-event-references/[id]/index.ts b/pages/api/daily-event-references/[id]/index.ts index c9b243f707..166a661dcc 100644 --- a/pages/api/daily-event-references/[id]/index.ts +++ b/pages/api/daily-event-references/[id]/index.ts @@ -18,7 +18,7 @@ export async function dailyEventReference(req: NextApiRequest, res: NextApiRespo const data = await prisma.dailyEventReference.findUnique({ where: { id: safe.data.id } }); if (data) res.status(200).json({ data }); - if (!data) res.status(404).json({ message: "Event type not found" }); + else res.status(404).json({ message: "Event type not found" }); // Reject any other HTTP method than POST } else res.status(405).json({ message: "Only GET Method allowed" }); } diff --git a/pages/api/destination-calendars/[id]/index.ts b/pages/api/destination-calendars/[id]/index.ts index 95a1409372..95a44a5edf 100644 --- a/pages/api/destination-calendars/[id]/index.ts +++ b/pages/api/destination-calendars/[id]/index.ts @@ -18,7 +18,7 @@ export async function destinationCalendar(req: NextApiRequest, res: NextApiRespo const data = await prisma.destinationCalendar.findUnique({ where: { id: safe.data.id } }); if (data) res.status(200).json({ data }); - if (!data) res.status(404).json({ message: "Event type not found" }); + else res.status(404).json({ message: "Event type not found" }); // Reject any other HTTP method than POST } else res.status(405).json({ message: "Only GET Method allowed" }); } diff --git a/pages/api/event-type-custom-inputs/[id]/index.ts b/pages/api/event-type-custom-inputs/[id]/index.ts index 3b7a926dcd..c53b2e4c06 100644 --- a/pages/api/event-type-custom-inputs/[id]/index.ts +++ b/pages/api/event-type-custom-inputs/[id]/index.ts @@ -18,7 +18,7 @@ export async function eventTypeCustomInput(req: NextApiRequest, res: NextApiResp const data = await prisma.eventTypeCustomInput.findUnique({ where: { id: safe.data.id } }); if (data) res.status(200).json({ data }); - if (!data) res.status(404).json({ message: "Event type not found" }); + else res.status(404).json({ message: "Event type not found" }); // Reject any other HTTP method than POST } else res.status(405).json({ message: "Only GET Method allowed" }); } diff --git a/pages/api/memberships/[id]/index.ts b/pages/api/memberships/[id]/index.ts index 7f56aa3889..8f1496ddc5 100644 --- a/pages/api/memberships/[id]/index.ts +++ b/pages/api/memberships/[id]/index.ts @@ -18,7 +18,7 @@ export async function membership(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "DELETE" && safe.success && safe.data) { - const team = await prisma.team + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (safe.success) { + const data = await prisma.team .delete({ where: { id: safe.data.id } }) // We only remove the team type from the database if there's an existing resource. - if (team) res.status(200).json({ message: `team with id: ${safe.data.id} deleted successfully` }); + if (data) res.status(200).json({ message: `Team with id: ${safe.data.id} deleted successfully` }); // This catches the error thrown by prisma.team.delete() if the resource is not found. - else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only DELETE Method allowed" }); + else res.status(400).json({ message: `Team with id: ${safe.data.id} was not able to be processed` }); + } } -export default withValidQueryIdTransformParseInt(deleteTeam); +export default withMiddleware("deleteOnly", "addRequestId")( + withValidQueryIdTransformParseInt( + deleteTeam + ) +); diff --git a/pages/api/teams/[id]/edit.ts b/pages/api/teams/[id]/edit.ts index c046c97da4..96e2fa5d3a 100644 --- a/pages/api/teams/[id]/edit.ts +++ b/pages/api/teams/[id]/edit.ts @@ -4,6 +4,7 @@ import { Team } from "@calcom/prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; import { schemaTeam, withValidTeam } from "@lib/validations/team"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; type ResponseData = { @@ -17,21 +18,19 @@ export async function editTeam(req: NextApiRequest, res: NextApiResponse { - res.status(200).json({ data: team }); - }).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 teams" }); + }) + if (data) res.status(200).json({ data }); + else (error: unknown) => res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error }) } } -export default withValidQueryIdTransformParseInt(withValidTeam(editTeam)); +export default withMiddleware("patchOnly","addRequestId")( + withValidQueryIdTransformParseInt( + withValidTeam( + editTeam) + ) +); diff --git a/pages/api/teams/[id]/index.ts b/pages/api/teams/[id]/index.ts index cd9cd660d4..9868c043fd 100644 --- a/pages/api/teams/[id]/index.ts +++ b/pages/api/teams/[id]/index.ts @@ -4,6 +4,7 @@ import { Team } 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?: Team; @@ -11,20 +12,19 @@ type ResponseData = { error?: unknown; }; -export async function team(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - - if (method === "GET" && safe.success) { - const team = await prisma.team.findUnique({ where: { id: safe.data.id } }); +export async function teamById(req: NextApiRequest, res: NextApiResponse) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (safe.success) { + const data = await prisma.team.findUnique({ where: { id: safe.data.id } }); - if (team) res.status(200).json({ data: team }); - if (!team) res.status(404).json({ message: "Event type not found" }); - } else { - // Reject any other HTTP method than POST - res.status(405).json({ message: "Only GET Method allowed" }); + if (data) res.status(200).json({ data }); + else res.status(404).json({ message: "Team was not found" }); } } -export default withValidQueryIdTransformParseInt(team); +export default withMiddleware("addRequestId","getOnly")( + withValidQueryIdTransformParseInt( + teamById + ) +); diff --git a/pages/api/teams/index.ts b/pages/api/teams/index.ts index 0b57f21604..1fd88d492f 100644 --- a/pages/api/teams/index.ts +++ b/pages/api/teams/index.ts @@ -1,6 +1,7 @@ import prisma from "@calcom/prisma"; import { Team } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { NextApiRequest, NextApiResponse } from "next"; type ResponseData = { @@ -8,12 +9,11 @@ type ResponseData = { error?: unknown; }; -export default async function team(req: NextApiRequest, res: NextApiResponse) { - try { - const data = await prisma.team.findMany(); - res.status(200).json({ data }); - } catch (error) { - // FIXME: Add zod for validation/error handling - res.status(400).json({ error: error }); - } +async function allTeams(req: NextApiRequest, res: NextApiResponse) { + const data = await prisma.team.findMany(); + + if (data) res.status(200).json({ data }); + else res.status(400).json({ error: "No data found" }); } + +export default withMiddleware("addRequestId","getOnly")(allTeams); \ No newline at end of file diff --git a/pages/api/teams/new.ts b/pages/api/teams/new.ts index ebcb513777..7ae4d0034e 100644 --- a/pages/api/teams/new.ts +++ b/pages/api/teams/new.ts @@ -1,30 +1,28 @@ -import prisma from "@calcom/prisma"; - -import { Team } from "@calcom/prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; +import prisma from "@calcom/prisma"; +import { Team } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; import { schemaTeam, withValidTeam } from "@lib/validations/team"; + type ResponseData = { data?: Team; - message?: string; - error?: string; + error?: object; }; async function createTeam(req: NextApiRequest, res: NextApiResponse) { - const { body, method } = req; - if (method === "POST") { - const safe = schemaTeam.safeParse(body); - if (safe.success && safe.data) { - await prisma.team - .create({ data: safe.data }) - .then((team) => res.status(201).json({ data: team })) - .catch((error) => res.status(400).json({ message: "Could not create team", error: error })); - } - } else { - // Reject any other HTTP method than POST - res.status(405).json({ error: "Only POST Method allowed" }); + const safe = schemaTeam.safeParse(req.body); + if (safe.success) { + const data = await prisma.team + .create({ data: safe.data }) + if (data) res.status(201).json({ data }) + else (error: unknown) => res.status(400).json({ error: { message: "Could not create team type", error: error } }); } } -export default withValidTeam(createTeam); +export default withMiddleware("addRequestId","postOnly")( + withValidTeam( + createTeam + ) +); diff --git a/pages/api/users/[id]/delete.ts b/pages/api/users/[id]/delete.ts index 1a2a2b240e..6542b99134 100644 --- a/pages/api/users/[id]/delete.ts +++ b/pages/api/users/[id]/delete.ts @@ -5,24 +5,25 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; - type ResponseData = { message?: string; error?: unknown; }; export async function deleteUser(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "DELETE" && safe.success && safe.data) { - const user = await prisma.user + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (safe.success) { + const data = await prisma.user .delete({ where: { id: safe.data.id } }) // We only remove the user type from the database if there's an existing resource. - if (user) res.status(200).json({ message: `user with id: ${safe.data.id} deleted successfully` }); + if (data) res.status(200).json({ message: `User with id: ${safe.data.id} deleted successfully` }); // This catches the error thrown by prisma.user.delete() if the resource is not found. - else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only DELETE Method allowed" }); + else res.status(400).json({ message: `User with id: ${safe.data.id} was not able to be processed` }); + } } -export default withMiddleware("addRequestId")((withValidQueryIdTransformParseInt(deleteUser))); +export default withMiddleware("deleteOnly", "addRequestId")( + withValidQueryIdTransformParseInt( + deleteUser + ) +); diff --git a/pages/api/users/[id]/edit.ts b/pages/api/users/[id]/edit.ts index a59a0df940..3e5425b3d9 100644 --- a/pages/api/users/[id]/edit.ts +++ b/pages/api/users/[id]/edit.ts @@ -18,16 +18,19 @@ export async function editUser(req: NextApiRequest, res: NextApiResponse res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error }) + } } -export default withMiddleware("addRequestId")(withValidQueryIdTransformParseInt(withValidUser(editUser))); +export default withMiddleware("patchOnly","addRequestId")( + withValidQueryIdTransformParseInt( + withValidUser( + editUser) + ) +); diff --git a/pages/api/users/[id]/index.ts b/pages/api/users/[id]/index.ts index cbd088e9df..6264acd115 100644 --- a/pages/api/users/[id]/index.ts +++ b/pages/api/users/[id]/index.ts @@ -12,17 +12,19 @@ type ResponseData = { error?: unknown; }; -export async function user(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "GET" && safe.success) { +export async function userById(req: NextApiRequest, res: NextApiResponse) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (safe.success) { const data = await prisma.user.findUnique({ where: { id: safe.data.id } }); if (data) res.status(200).json({ data }); - if (!data) res.status(404).json({ message: "Event type not found" }); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only GET Method allowed" }); + else res.status(404).json({ message: "User was not found" }); + } } -export default withMiddleware("addRequestId")(withValidQueryIdTransformParseInt(user)); +export default withMiddleware("addRequestId","getOnly")( + withValidQueryIdTransformParseInt( + userById + ) +); diff --git a/pages/api/users/index.ts b/pages/api/users/index.ts index 9c7473252f..ef75e93cc7 100644 --- a/pages/api/users/index.ts +++ b/pages/api/users/index.ts @@ -9,10 +9,11 @@ type ResponseData = { error?: unknown; }; -async function user(req: NextApiRequest, res: NextApiResponse) { +async function allUsers(req: NextApiRequest, res: NextApiResponse) { const data = await prisma.user.findMany(); + 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 +export default withMiddleware("addRequestId","getOnly")(allUsers); \ No newline at end of file diff --git a/pages/api/users/new.ts b/pages/api/users/new.ts index 1ad079ca44..6ce94b5df0 100644 --- a/pages/api/users/new.ts +++ b/pages/api/users/new.ts @@ -12,15 +12,17 @@ type ResponseData = { }; async function createUser(req: NextApiRequest, res: NextApiResponse) { - const { body, method } = req; - const safe = schemaUser.safeParse(body); - if (method === "POST" && safe.success) { + const safe = schemaUser.safeParse(req.body); + if (safe.success) { 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 withMiddleware("addRequestId")(withValidUser(createUser)); +export default withMiddleware("addRequestId","postOnly")( + withValidUser( + createUser + ) +);