|
1 | 1 | import "reflect-metadata"; |
2 | 2 | import type { Factory } from "hono/factory"; |
3 | 3 | import { createFactory } from "hono/factory"; |
4 | | -import type { HandlerInterface } from "hono/types"; |
| 4 | +import type { HandlerInterface, MiddlewareHandler } from "hono/types"; |
5 | 5 | import { InsufficientControllerMethodError, NotSupportedMethodError } from "App/Error/AppError.js"; |
6 | | -import { MetadataConstant } from "App/Types/ControllerConstant.js"; |
| 6 | +import { MetadataConstant, MetadataValidatorConstant } from "App/Types/ControllerConstant.js"; |
7 | 7 | import type { |
8 | 8 | AgniRoutingMetadata, |
9 | 9 | AgniSupportedMethod, |
@@ -33,16 +33,27 @@ export default class Controller { |
33 | 33 | if (!Reflect.hasMetadata(MetadataConstant, func)) return; |
34 | 34 |
|
35 | 35 | const metadata = Reflect.getMetadata(MetadataConstant, func) as AgniRoutingMetadata; |
| 36 | + const metadataKeys = Reflect.getMetadataKeys(func); |
36 | 37 | const honoApp = (this.app as Record<AgniSupportedMethod, any>)[metadata.method] as HandlerInterface | undefined; |
37 | 38 | if (typeof honoApp !== "function") { |
38 | 39 | throw new NotSupportedMethodError(); |
39 | 40 | } |
40 | 41 |
|
41 | 42 | /** |
42 | | - * TODO [2024-02-25]: Add support for middleware, multi handler/middleware |
43 | | - * and validator using Zod Validate |
| 43 | + * TODO [2024-02-27]: Add support for middleware, multi handler/middleware |
44 | 44 | */ |
45 | | - const handlers = this._honoFactory.createHandlers(func); |
| 45 | + const ctx: DefaultHonoFunctionContext[] = []; |
| 46 | + if (metadataKeys.includes(MetadataValidatorConstant)) { |
| 47 | + const middleware = Reflect.getMetadata(MetadataValidatorConstant, func) as MiddlewareHandler; |
| 48 | + ctx.push(this._honoFactory.createMiddleware(middleware)); |
| 49 | + } |
| 50 | + |
| 51 | + ctx.push(func); |
| 52 | + |
| 53 | + // TODO [2024-03-01]: How to bypass this? |
| 54 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 55 | + // @ts-expect-error |
| 56 | + const handlers = this._honoFactory.createHandlers(...ctx); |
46 | 57 | honoApp(metadata.path, ...handlers); |
47 | 58 | } |
48 | 59 | } |
|
0 commit comments