|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | + |
| 3 | +import "reflect-metadata"; |
| 4 | + |
| 5 | +// --- Sample Express interfaces for type safety --- |
| 6 | +export interface Request { |
| 7 | + query: any; |
| 8 | + params: any; |
| 9 | + body?: any; |
| 10 | + [key: string]: any; |
| 11 | +} |
| 12 | +export interface Response { |
| 13 | + json: (body: any) => void; |
| 14 | + headersSent: boolean; |
| 15 | + [key: string]: any; |
| 16 | +} |
| 17 | +export type NextFunction = (err?: any) => void; |
| 18 | +export type RequestHandler = ( |
| 19 | + req: Request, |
| 20 | + res: Response, |
| 21 | + next: NextFunction, |
| 22 | +) => any; |
| 23 | +export interface Router { |
| 24 | + [method: string]: (path: string, ...handlers: RequestHandler[]) => void; |
| 25 | +} |
| 26 | +// --- End sample Express interfaces --- |
| 27 | + |
| 28 | +const ROUTES_KEY = Symbol("routes"); |
| 29 | +const MIDDLEWARE_KEY = Symbol("middlewares"); |
| 30 | +const PARAMS_KEY = Symbol("params"); |
| 31 | + |
| 32 | +export type HttpMethod = |
| 33 | + | "get" |
| 34 | + | "post" |
| 35 | + | "put" |
| 36 | + | "patch" |
| 37 | + | "delete" |
| 38 | + | "options" |
| 39 | + | "head" |
| 40 | + | "trace" |
| 41 | + | "connect"; |
| 42 | + |
| 43 | +interface RouteDefinition { |
| 44 | + path: string; |
| 45 | + method: HttpMethod; |
| 46 | + handlerName: string; |
| 47 | +} |
| 48 | + |
| 49 | +interface ParamDefinition { |
| 50 | + index: number; |
| 51 | + type: "query" | "param" | "body" | "req" | "res"; |
| 52 | + key?: string; |
| 53 | +} |
| 54 | + |
| 55 | +// ---- Route Decorators ---- |
| 56 | +function createRouteDecorator(method: HttpMethod) { |
| 57 | + return (path: string): MethodDecorator => { |
| 58 | + return (target, propertyKey, descriptor) => { |
| 59 | + const routes: RouteDefinition[] = |
| 60 | + Reflect.getMetadata(ROUTES_KEY, target.constructor) || []; |
| 61 | + routes.push({ |
| 62 | + path, |
| 63 | + method, |
| 64 | + handlerName: propertyKey as string, |
| 65 | + }); |
| 66 | + Reflect.defineMetadata(ROUTES_KEY, routes, target.constructor); |
| 67 | + }; |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +export const Get = createRouteDecorator("get"); |
| 72 | +export const Post = createRouteDecorator("post"); |
| 73 | +export const Put = createRouteDecorator("put"); |
| 74 | +export const Patch = createRouteDecorator("patch"); |
| 75 | +export const Delete = createRouteDecorator("delete"); |
| 76 | +export const Options = createRouteDecorator("options"); |
| 77 | +export const Head = createRouteDecorator("head"); |
| 78 | +export const Trace = createRouteDecorator("trace"); |
| 79 | +export const Connect = createRouteDecorator("connect"); |
| 80 | + |
| 81 | +// ---- Controller Decorator ---- |
| 82 | +export function Controller(basePath: string): ClassDecorator { |
| 83 | + return (target) => { |
| 84 | + Reflect.defineMetadata("basePath", basePath, target); |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +// ---- Middleware Decorator ---- |
| 89 | +export function Use(...middlewares: RequestHandler[]): MethodDecorator { |
| 90 | + return (target, propertyKey, descriptor) => { |
| 91 | + const existing: RequestHandler[] = |
| 92 | + Reflect.getMetadata(MIDDLEWARE_KEY, target, propertyKey as string) || []; |
| 93 | + Reflect.defineMetadata( |
| 94 | + MIDDLEWARE_KEY, |
| 95 | + [...existing, ...middlewares], |
| 96 | + target, |
| 97 | + propertyKey as string, |
| 98 | + ); |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +// ---- Parameter Decorators ---- |
| 103 | +function createParamDecorator(type: ParamDefinition["type"], key?: string) { |
| 104 | + return (paramKey?: string): ParameterDecorator => { |
| 105 | + return (target, propertyKey, parameterIndex) => { |
| 106 | + const params: ParamDefinition[] = |
| 107 | + Reflect.getMetadata(PARAMS_KEY, target, propertyKey as string) || []; |
| 108 | + // Ensure parameters are ordered by index |
| 109 | + params.push({ index: parameterIndex, type, key: paramKey || key }); |
| 110 | + params.sort((a, b) => a.index - b.index); |
| 111 | + Reflect.defineMetadata(PARAMS_KEY, params, target, propertyKey as string); |
| 112 | + }; |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +export const Query = createParamDecorator("query"); |
| 117 | +export const Param = createParamDecorator("param"); |
| 118 | +export const Body = createParamDecorator("body"); |
| 119 | +export const Req = createParamDecorator("req"); |
| 120 | +export const Res = createParamDecorator("res"); |
| 121 | + |
| 122 | +// Custom HTTP status code decorator |
| 123 | +const HTTP_CODE_KEY = Symbol("httpCode"); |
| 124 | +export function HttpCode(status: number): MethodDecorator { |
| 125 | + return (target, propertyKey, descriptor) => { |
| 126 | + Reflect.defineMetadata( |
| 127 | + HTTP_CODE_KEY, |
| 128 | + status, |
| 129 | + target, |
| 130 | + propertyKey as string, |
| 131 | + ); |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +// Custom header decorator |
| 136 | +const HEADER_KEY = Symbol("headers"); |
| 137 | +export function Header(name: string, value: string): MethodDecorator { |
| 138 | + return (target, propertyKey, descriptor) => { |
| 139 | + const headers: Record<string, string> = |
| 140 | + Reflect.getMetadata(HEADER_KEY, target, propertyKey as string) || {}; |
| 141 | + headers[name] = value; |
| 142 | + Reflect.defineMetadata(HEADER_KEY, headers, target, propertyKey as string); |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +// Before/After hooks (not Express middleware, but can be used for logging, etc.) |
| 147 | +const BEFORE_KEY = Symbol("before"); |
| 148 | +const AFTER_KEY = Symbol("after"); |
| 149 | +export function Before(fn: Function): MethodDecorator { |
| 150 | + return (target, propertyKey, descriptor) => { |
| 151 | + const hooks: Function[] = |
| 152 | + Reflect.getMetadata(BEFORE_KEY, target, propertyKey as string) || []; |
| 153 | + hooks.push(fn); |
| 154 | + Reflect.defineMetadata(BEFORE_KEY, hooks, target, propertyKey as string); |
| 155 | + }; |
| 156 | +} |
| 157 | +export function After(fn: Function): MethodDecorator { |
| 158 | + return (target, propertyKey, descriptor) => { |
| 159 | + const hooks: Function[] = |
| 160 | + Reflect.getMetadata(AFTER_KEY, target, propertyKey as string) || []; |
| 161 | + hooks.push(fn); |
| 162 | + Reflect.defineMetadata(AFTER_KEY, hooks, target, propertyKey as string); |
| 163 | + }; |
| 164 | +} |
| 165 | + |
| 166 | +// ---- Register Controllers ---- |
| 167 | +export function registerControllers(router: Router, controllers: any[]) { |
| 168 | + controllers.forEach((ControllerClass) => { |
| 169 | + const instance = new ControllerClass(); |
| 170 | + const basePath: string = |
| 171 | + Reflect.getMetadata("basePath", ControllerClass) || ""; |
| 172 | + const routes: RouteDefinition[] = |
| 173 | + Reflect.getMetadata(ROUTES_KEY, ControllerClass) || []; |
| 174 | + |
| 175 | + routes.forEach(({ path, method, handlerName }) => { |
| 176 | + const middlewares: RequestHandler[] = |
| 177 | + Reflect.getMetadata(MIDDLEWARE_KEY, instance, handlerName) || []; |
| 178 | + const params: ParamDefinition[] = |
| 179 | + Reflect.getMetadata(PARAMS_KEY, instance, handlerName) || []; |
| 180 | + |
| 181 | + const httpCode: number | undefined = Reflect.getMetadata( |
| 182 | + HTTP_CODE_KEY, |
| 183 | + instance, |
| 184 | + handlerName, |
| 185 | + ); |
| 186 | + const headers: Record<string, string> = |
| 187 | + Reflect.getMetadata(HEADER_KEY, instance, handlerName) || {}; |
| 188 | + const beforeHooks: Function[] = |
| 189 | + Reflect.getMetadata(BEFORE_KEY, instance, handlerName) || []; |
| 190 | + const afterHooks: Function[] = |
| 191 | + Reflect.getMetadata(AFTER_KEY, instance, handlerName) || []; |
| 192 | + |
| 193 | + const handler: RequestHandler = async (req, res, next) => { |
| 194 | + try { |
| 195 | + for (const fn of beforeHooks) await fn(req, res); |
| 196 | + const args: any[] = []; |
| 197 | + if (params.length) { |
| 198 | + params.forEach(({ index, type, key }) => { |
| 199 | + switch (type) { |
| 200 | + case "query": |
| 201 | + args[index] = key ? req.query[key] : req.query; |
| 202 | + break; |
| 203 | + case "param": |
| 204 | + args[index] = key ? req.params[key] : req.params; |
| 205 | + break; |
| 206 | + case "body": |
| 207 | + args[index] = key ? req.body?.[key] : req.body; |
| 208 | + break; |
| 209 | + case "req": |
| 210 | + args[index] = req; |
| 211 | + break; |
| 212 | + case "res": |
| 213 | + args[index] = res; |
| 214 | + break; |
| 215 | + } |
| 216 | + }); |
| 217 | + } |
| 218 | + const result = (instance as any)[handlerName](...args); |
| 219 | + // Support both sync and async handlers |
| 220 | + const awaited = result instanceof Promise ? await result : result; |
| 221 | + if (!res.headersSent && typeof awaited !== "undefined") { |
| 222 | + if (httpCode) res.status?.(httpCode); |
| 223 | + for (const [k, v] of Object.entries(headers)) res.set?.(k, v); |
| 224 | + res.json(awaited); |
| 225 | + } |
| 226 | + for (const fn of afterHooks) await fn(req, res, awaited); |
| 227 | + } catch (err) { |
| 228 | + next(err); |
| 229 | + } |
| 230 | + }; |
| 231 | + |
| 232 | + (router as any)[method](basePath + path, ...middlewares, handler); |
| 233 | + }); |
| 234 | + }); |
| 235 | +} |
0 commit comments