feat: inherit all validations from auto-generated zod

This commit is contained in:
Agusti Fernandez Pardo committed 2022-03-30 17:37:51 +02:00
1 parent 064c39b7f6
commit 9aa4b0e30d
19 files changed
+372 -224

No files matched your search

+1 -2
View File
@@ -1,11 +1,10 @@
// import { NextApiResponse } from "next";
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 default async function requireApiKeyAsQueryParams({ nextUrl }: NextRequest, res: NextResponse) {
export default async function requireApiKeyAsQueryParams({ nextUrl }: NextRequest) {
const response = NextResponse.next();
const apiKey = nextUrl.searchParams.get("apiKey");
+29 -18
View File
@@ -2,29 +2,40 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/eventTypes/:id/delete:
* delete:
* description: Remove an existing eventType
* responses:
* 201:
* description: OK, eventType removed successfuly
* model: EventType
* 400:
* description: Bad request. EventType id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteEventType(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function deleteEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const eventType = await prisma.eventType.delete({ where: { id: safe.data.id } });
// We only remove the eventType type from the database if there's an existing resource.
if (eventType)
res.status(200).json({ message: `eventType with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.eventType.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 in /availabilities/[id]/delete endpoint" });
const data = await prisma.eventType.delete({ where: { id: safe.data.id } });
if (data) res.status(200).json({ message: `EventType with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `EventType with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(deleteEventType);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteEventType));
+40 -32
View File
@@ -1,45 +1,53 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { EventType } from "@calcom/prisma/client";
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeResponse } from "@lib/types";
import {
schemaEventTypeBodyParams,
schemaEventTypePublic,
withValidEventType,
} from "@lib/validations/eventType";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: EventType;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/eventTypes/:id/edit:
* patch:
* description: Edits an existing eventType
* responses:
* 201:
* description: OK, eventType edited successfuly
* model: EventType
* 400:
* description: Bad request. EventType body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editEventType(req: NextApiRequest, res: NextApiResponse<EventTypeResponse>) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaEventTypeBodyParams.safeParse(req.body);
export async function editEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaEventType.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const eventType = await prisma.eventType.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaEventTypePublic.parse(eventType);
if (method === "PATCH") {
if (safeQuery.success && safeBody.success) {
await prisma.eventType
.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((event) => {
res.status(200).json({ data: event });
})
.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" });
}
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidEventType(editEventType));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidEventType(editEventType))
);
+29 -19
View File
@@ -1,32 +1,42 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { EventType } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeResponse } from "@lib/types";
import { schemaEventTypePublic } from "@lib/validations/eventType";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: EventType;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/eventTypes/:id:
* get:
* description: find eventType by ID
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: EventType was not found
*/
export async function eventTypeById(req: NextApiRequest, res: NextApiResponse<EventTypeResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
const eventType = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
const data = schemaEventTypePublic.parse(eventType);
if (method === "GET" && safe.success) {
const event = await prisma.eventType.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" });
} else {
// Reject any other HTTP method than POST
res.status(405).json({ message: "Only GET Method allowed" });
}
if (eventType) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "EventType was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(eventType);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(eventTypeById));
+29 -15
View File
@@ -1,21 +1,35 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { EventType } from "@calcom/prisma/client";
type ResponseData = {
data?: EventType[];
message?: string;
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { EventTypesResponse } from "@lib/types";
import { schemaEventTypePublic } from "@lib/validations/eventType";
export default async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { method } = req;
if (method === "GET") {
const data = await prisma.eventType.findMany();
res.status(200).json({ data });
} else {
// Reject any other HTTP method than POST
res.status(405).json({ message: "Only GET Method allowed" });
}
/**
* @swagger
* /api/eventTypes:
* get:
* description: Returns all eventTypes
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No eventTypes were found
*/
async function allEventTypes(_: NextApiRequest, res: NextApiResponse<EventTypesResponse>) {
const eventTypes = await prisma.eventType.findMany();
const data = eventTypes.map((eventType) => schemaEventTypePublic.parse(eventType));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No EventTypes were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allEventTypes);
+35 -22
View File
@@ -1,30 +1,43 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { EventType } from "@calcom/prisma/client";
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeResponse } from "@lib/types";
import {
schemaEventTypeBodyParams,
schemaEventTypePublic,
withValidEventType,
} from "@lib/validations/eventType";
type ResponseData = {
data?: EventType;
message?: string;
error?: string;
};
/**
* @swagger
* /api/eventTypes/new:
* post:
* description: Creates a new eventType
* responses:
* 201:
* description: OK, eventType created
* model: EventType
* 400:
* description: Bad request. EventType body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createEventType(req: NextApiRequest, res: NextApiResponse<EventTypeResponse>) {
const safe = schemaEventTypeBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
if (method === "POST") {
const safe = schemaEventType.safeParse(body);
if (safe.success && safe.data) {
await prisma.eventType
.create({ data: safe.data })
.then((event) => res.status(201).json({ data: event }))
.catch((error) => res.status(400).json({ message: "Could not create event type", error: error }));
}
} else {
// Reject any other HTTP method than POST
res.status(405).json({ error: "Only POST Method allowed" });
}
const eventType = await prisma.eventType.create({ data: safe.data });
const data = schemaEventTypePublic.parse(eventType);
if (data) res.status(201).json({ data, message: "EventType created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new eventType",
error,
});
}
export default withValidEventType(createEventType);
export default withMiddleware("HTTP_POST")(withValidEventType(createEventType));