|
| 1 | +import { NextFunction, Request, Response, Router } from 'express'; |
| 2 | +import { ZodTypeAny } from 'zod'; |
| 3 | +import { validateZodSchema } from '../middlewares/validate-zod-schema.middleware'; |
| 4 | +import { RequestZodSchemaType } from '../types'; |
| 5 | +import { |
| 6 | + parseRouteString, |
| 7 | + routeToClassName, |
| 8 | + camelCaseToTitleCase, |
| 9 | +} from './openapi.utils'; |
| 10 | +import { registry } from './swagger-instance'; |
| 11 | + |
| 12 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 13 | +export type MaybePromise = any | Promise<any> | void; |
| 14 | + |
| 15 | +export class MagicRouter { |
| 16 | + private router: Router; |
| 17 | + private rootRoute: string; |
| 18 | + |
| 19 | + constructor(rootRoute: string) { |
| 20 | + this.router = Router(); |
| 21 | + this.rootRoute = rootRoute; |
| 22 | + } |
| 23 | + |
| 24 | + private getPath(path: string) { |
| 25 | + return this.rootRoute + parseRouteString(path); |
| 26 | + } |
| 27 | + |
| 28 | + public get( |
| 29 | + path: string, |
| 30 | + requestType: RequestZodSchemaType = {}, |
| 31 | + responseModel: ZodTypeAny, |
| 32 | + ...middlewares: Array< |
| 33 | + ( |
| 34 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 35 | + req: Request<any, any, any, any, any>, |
| 36 | + res: Response, |
| 37 | + next?: NextFunction, |
| 38 | + ) => MaybePromise |
| 39 | + > |
| 40 | + ): void { |
| 41 | + const bodySchema = requestType.body |
| 42 | + ? registry.register(routeToClassName(this.rootRoute), requestType.body) |
| 43 | + : null; |
| 44 | + |
| 45 | + registry.registerPath({ |
| 46 | + method: 'get', |
| 47 | + path: this.getPath(path), |
| 48 | + description: camelCaseToTitleCase( |
| 49 | + middlewares[middlewares.length - 1].name, |
| 50 | + ), |
| 51 | + summary: camelCaseToTitleCase(middlewares[middlewares.length - 1].name), |
| 52 | + request: { |
| 53 | + params: requestType.params, |
| 54 | + query: requestType.query, |
| 55 | + ...(bodySchema |
| 56 | + ? { |
| 57 | + body: { |
| 58 | + content: { |
| 59 | + 'applicat ion/json': { |
| 60 | + schema: bodySchema, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + : {}), |
| 66 | + }, |
| 67 | + responses: { |
| 68 | + 200: { |
| 69 | + description: '', |
| 70 | + content: { |
| 71 | + 'application/json': { |
| 72 | + schema: responseModel, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + if (Object.keys(requestType).length) { |
| 80 | + this.router.get(path, validateZodSchema(requestType), ...middlewares); |
| 81 | + } else { |
| 82 | + this.router.get(path, ...middlewares); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public post( |
| 87 | + path: string, |
| 88 | + requestType: RequestZodSchemaType = {}, |
| 89 | + responseModel: ZodTypeAny, |
| 90 | + ...middlewares: Array< |
| 91 | + ( |
| 92 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 93 | + req: Request<any, any, any, any, any>, |
| 94 | + res: Response, |
| 95 | + next?: NextFunction, |
| 96 | + ) => MaybePromise |
| 97 | + > |
| 98 | + ): void { |
| 99 | + this.router.post(path, ...middlewares); |
| 100 | + } |
| 101 | + public put( |
| 102 | + path: string, |
| 103 | + ...middlewares: Array< |
| 104 | + (req: Request, res: Response, next: NextFunction) => void |
| 105 | + > |
| 106 | + ): void { |
| 107 | + this.router.put(path, ...middlewares); |
| 108 | + } |
| 109 | + public delete( |
| 110 | + path: string, |
| 111 | + ...middlewares: Array< |
| 112 | + (req: Request, res: Response, next: NextFunction) => void |
| 113 | + > |
| 114 | + ): void { |
| 115 | + this.router.delete(path, ...middlewares); |
| 116 | + } |
| 117 | + public patch( |
| 118 | + path: string, |
| 119 | + ...middlewares: Array< |
| 120 | + ( |
| 121 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 122 | + req: Request<any, any, any, any>, |
| 123 | + res: Response, |
| 124 | + next: NextFunction, |
| 125 | + ) => void |
| 126 | + > |
| 127 | + ): void { |
| 128 | + this.router.patch(path, ...middlewares); |
| 129 | + } |
| 130 | + |
| 131 | + public use(...args: Parameters<Router['use']>): void { |
| 132 | + this.router.use(...args); |
| 133 | + } |
| 134 | + |
| 135 | + // Method to get the router instance |
| 136 | + public getRouter(): Router { |
| 137 | + return this.router; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +export default MagicRouter; |
0 commit comments