@@ -3,7 +3,7 @@ import { H3Event, Middleware, MiddlewareOptions, type H3 } from 'h3'
33import { Application , Container , Kernel } from '@h3ravel/core'
44import { Request , Response } from '@h3ravel/http'
55import { singularize } from '@h3ravel/support'
6- import { HttpContext , type EventHandler , type IController , type IMiddleware , type IRouter , type RouterEnd } from '@h3ravel/shared'
6+ import { HttpContext , RouteEventHandler , type EventHandler , type IController , type IMiddleware , type IRouter , type RouterEnd } from '@h3ravel/shared'
77
88interface RouteDefinition {
99 method : string
@@ -96,7 +96,7 @@ export class Router implements IRouter {
9696 /**
9797 * Checks if the handler is a function (either a plain function or a class constructor)
9898 */
99- if ( typeof handler === 'function' ) {
99+ if ( typeof handler === 'function' && typeof ( handler as any ) . prototype !== 'undefined' ) {
100100 return ( _ctx ) => {
101101 let controller : IController
102102
@@ -126,18 +126,14 @@ export class Router implements IRouter {
126126 }
127127
128128 /**
129- * Get param types for the method
129+ * Get param types for the controller method
130130 */
131- const paramTypes = Reflect . getMetadata (
132- 'design:paramtypes' ,
133- controller ,
134- action
135- ) || [ ] ;
131+ const paramTypes : [ ] = Reflect . getMetadata ( 'design:paramtypes' , controller , action ) || [ ] ;
136132
137133 /**
138134 * Resolve the bound dependencies
139135 */
140- const args : [ ] = paramTypes . map ( ( paramType : any ) => {
136+ let args : any [ ] = paramTypes . map ( ( paramType : any ) => {
141137 switch ( paramType ?. name ) {
142138 case 'Application' :
143139 return this . app
@@ -152,6 +148,13 @@ export class Router implements IRouter {
152148 }
153149 } ) ;
154150
151+ /**
152+ * Ensure that the HttpContext is always available
153+ */
154+ if ( args . length < 1 ) {
155+ args = [ _ctx ]
156+ }
157+
155158 /**
156159 * Call the controller method, passing all resolved dependencies
157160 */
@@ -174,7 +177,7 @@ export class Router implements IRouter {
174177 */
175178 get (
176179 path : string ,
177- definition : EventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
180+ definition : RouteEventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
178181 name ?: string ,
179182 middleware : IMiddleware [ ] = [ ]
180183 ) : Omit < this, RouterEnd > {
@@ -197,7 +200,7 @@ export class Router implements IRouter {
197200 */
198201 post (
199202 path : string ,
200- definition : EventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
203+ definition : RouteEventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
201204 name ?: string ,
202205 middleware : IMiddleware [ ] = [ ]
203206 ) : Omit < this, RouterEnd > {
@@ -219,7 +222,7 @@ export class Router implements IRouter {
219222 */
220223 put (
221224 path : string ,
222- definition : EventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
225+ definition : RouteEventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
223226 name ?: string ,
224227 middleware : IMiddleware [ ] = [ ]
225228 ) : Omit < this, RouterEnd > {
@@ -241,7 +244,7 @@ export class Router implements IRouter {
241244 */
242245 patch (
243246 path : string ,
244- definition : EventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
247+ definition : RouteEventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
245248 name ?: string ,
246249 middleware : IMiddleware [ ] = [ ]
247250 ) : Omit < this, RouterEnd > {
@@ -263,7 +266,7 @@ export class Router implements IRouter {
263266 */
264267 delete (
265268 path : string ,
266- definition : EventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
269+ definition : RouteEventHandler | [ ( new ( ...args : any [ ] ) => Record < string , any > ) , methodName : string ] ,
267270 name ?: string ,
268271 middleware : IMiddleware [ ] = [ ]
269272 ) : Omit < this, RouterEnd > {
@@ -290,14 +293,13 @@ export class Router implements IRouter {
290293 const name = basePath . substring ( basePath . lastIndexOf ( '/' ) + 1 ) . replaceAll ( / \/ | : / g, '' ) || '' ;
291294 const param = singularize ( name )
292295
293- const controller = new Controller ( this . app )
296+ this . get ( basePath , [ Controller , 'index' ] , `${ name } .index` , middleware )
297+ this . post ( basePath , [ Controller , 'store' ] , `${ name } .store` , middleware )
298+ this . get ( `${ basePath } /:${ param } ` , [ Controller , 'show' ] , `${ name } .show` , middleware )
299+ this . put ( `${ basePath } /:${ param } ` , [ Controller , 'update' ] , `${ name } .update` , middleware )
300+ this . patch ( `${ basePath } /:${ param } ` , [ Controller , 'update' ] , `${ name } .update` , middleware )
301+ this . delete ( `${ basePath } /:${ param } ` , [ Controller , 'destroy' ] , `${ name } .destroy` , middleware )
294302
295- this . addRoute ( 'get' , basePath , controller . index . bind ( controller ) , `${ name } .index` , middleware )
296- this . addRoute ( 'post' , basePath , controller . store . bind ( controller ) , `${ name } .store` , middleware )
297- this . addRoute ( 'get' , `${ basePath } /:${ param } ` , controller . show . bind ( controller ) , `${ name } .show` , middleware )
298- this . addRoute ( 'put' , `${ basePath } /:${ param } ` , controller . update . bind ( controller ) , `${ name } .update` , middleware )
299- this . addRoute ( 'patch' , `${ basePath } /:${ param } ` , controller . update . bind ( controller ) , `${ name } .update` , middleware )
300- this . addRoute ( 'delete' , `${ basePath } /:${ param } ` , controller . destroy . bind ( controller ) , `${ name } .destroy` , middleware )
301303 return this
302304 }
303305
@@ -325,14 +327,14 @@ export class Router implements IRouter {
325327 * @param options
326328 * @param callback
327329 */
328- group ( options : { prefix ?: string ; middleware ?: EventHandler [ ] } , callback : ( ) => void ) {
330+ group ( options : { prefix ?: string ; middleware ?: EventHandler [ ] } , callback : ( _e : this ) => void ) {
329331 const prevPrefix = this . groupPrefix
330332 const prevMiddleware = [ ...this . groupMiddleware ]
331333
332334 this . groupPrefix += options . prefix || ''
333335 this . groupMiddleware . push ( ...( options . middleware || [ ] ) )
334336
335- callback ( )
337+ callback ( this )
336338
337339 /**
338340 * Restore state after group
0 commit comments