From 31851e9acbe1671f46db53c63d8c82eba2bde46a Mon Sep 17 00:00:00 2001 From: Timothy-Gonzalez <105177619+Timothy-Gonzalez@users.noreply.github.com> Date: Wed, 16 Oct 2024 00:50:42 -0500 Subject: [PATCH] Fix default for unspecified specification Changes parameters, query, and body to have empty/unknown default types. This means using them when not defined in spec will result in an error instead of being typed as unknown. As such, this also fixes two missed cases in users and notifications that were caught by this change. --- src/middleware/specification.ts | 13 ++++++++----- src/services/notification/notification-router.ts | 8 +++++++- src/services/user/user-router.ts | 3 +++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/middleware/specification.ts b/src/middleware/specification.ts index 0f069887..37ea6e9a 100644 --- a/src/middleware/specification.ts +++ b/src/middleware/specification.ts @@ -1,5 +1,5 @@ import { RequestHandler } from "express"; -import { AnyZodObject, z, ZodType } from "zod"; +import { AnyZodObject, z, ZodObject, ZodType, ZodUnknown } from "zod"; import StatusCode from "status-code-enum"; import { Response, Request, NextFunction } from "express"; import { registerPathSpecification } from "../common/openapi"; @@ -36,7 +36,7 @@ export interface ResponsesObject { [statusCode: string]: ResponseObject; } -export interface Specification { +export interface Specification { path: string; method: Method; tag: Tag; @@ -53,11 +53,14 @@ export interface Specification = T extends ResponseObject ? z.infer : never; type ResponseBody = InferResponseBody; +// Utility type for a zod object which is really just empty +type ZodEmptyObject = ZodObject>; + export default function specification< - Params extends AnyZodObject, - Query extends AnyZodObject, Responses extends ResponsesObject, - Body extends ZodType, + Params extends AnyZodObject = ZodEmptyObject, + Query extends AnyZodObject = ZodEmptyObject, + Body extends ZodType = ZodUnknown, >(spec: Specification): RequestHandler, ResponseBody, z.infer> { registerPathSpecification(spec); diff --git a/src/services/notification/notification-router.ts b/src/services/notification/notification-router.ts index 5ba279aa..c81b90de 100644 --- a/src/services/notification/notification-router.ts +++ b/src/services/notification/notification-router.ts @@ -4,7 +4,12 @@ import Models from "../../database/models"; import { StaffShift } from "../staff/staff-schemas"; import { Role } from "../auth/auth-schemas"; import { getAuthenticatedUser } from "../../common/auth"; -import { NotificationSendRequestSchema, NotificationSendSchema, NotificationsSchema } from "./notification-schemas"; +import { + NotificationSendRequestSchema, + NotificationSendSchema, + NotificationsSchema, + RegisterDeviceTokenSchema, +} from "./notification-schemas"; import { sendNotification } from "./notification-service"; import specification, { Tag } from "../../middleware/specification"; import { SuccessResponseSchema } from "../../common/schemas"; @@ -41,6 +46,7 @@ notificationsRouter.post( tag: Tag.NOTIFICATION, role: Role.USER, summary: "Registers a device token to be associate with the currently authenticated user", + body: RegisterDeviceTokenSchema, responses: { [StatusCode.SuccessOK]: { description: "Successfully registered", diff --git a/src/services/user/user-router.ts b/src/services/user/user-router.ts index 66c8a6c7..ee52e00a 100644 --- a/src/services/user/user-router.ts +++ b/src/services/user/user-router.ts @@ -200,6 +200,9 @@ userRouter.delete( tag: Tag.USER, role: Role.USER, summary: "Unfollows the specified event", + parameters: z.object({ + id: UserIdSchema, + }), responses: { [StatusCode.SuccessOK]: { description: "Events followed after successfully unfollowing",