@@ -12,6 +12,7 @@ import type {
1212 RouteHandler ,
1313} from '../types'
1414import type { Router } from './router'
15+ import { matchPath } from '../utils'
1516
1617// ============================================================================
1718// Types
@@ -177,8 +178,10 @@ export class FluentRouter {
177178 handler : resolvedHandler ,
178179 middleware,
179180 domain : this . currentDomain || undefined ,
181+ pattern : this . createPattern ( fullPath ) ,
180182 }
181- this . router . addRoute ( route )
183+ // Use direct push to routes array to avoid conflict with http-methods.ts addRoute
184+ this . router . routes . push ( route )
182185 }
183186
184187 return new FluentRouteBuilder ( this . router , fullPath , methods [ 0 ] )
@@ -191,11 +194,11 @@ export class FluentRouter {
191194 /**
192195 * Register a resource controller
193196 */
194- resource ( name : string , controller : ControllerClass , options : ResourceOptions = { } ) : this {
197+ resource ( name : string , Controller : ControllerClass , options : ResourceOptions = { } ) : this {
195198 const methods = this . getResourceMethods ( options )
196199 const paramName = options . parameters ?. [ name ] || 'id'
197200
198- const controllerInstance = new controller ( )
201+ const controllerInstance = new Controller ( )
199202
200203 for ( const method of methods ) {
201204 const { httpMethod, path, handlerName } = this . getResourceRoute ( name , method , paramName )
@@ -212,8 +215,13 @@ export class FluentRouter {
212215 middleware : this . buildMiddleware ( options . middleware ) ,
213216 name : this . currentNamePrefix + routeName ,
214217 domain : this . currentDomain || undefined ,
218+ pattern : this . createPattern ( fullPath ) ,
219+ }
220+ // Use direct push to routes array to avoid conflict with http-methods.ts addRoute
221+ this . router . routes . push ( route )
222+ if ( route . name ) {
223+ this . router . namedRoutes . set ( route . name , route )
215224 }
216- this . router . addRoute ( route )
217225 }
218226 }
219227
@@ -235,9 +243,9 @@ export class FluentRouter {
235243 /**
236244 * Register a singleton resource (no index, create, store, destroy)
237245 */
238- singleton ( name : string , controller : ControllerClass , options : ResourceOptions = { } ) : this {
246+ singleton ( name : string , Controller : ControllerClass , options : ResourceOptions = { } ) : this {
239247 const singletonMethods : SingletonMethod [ ] = [ 'show' , 'edit' , 'update' ]
240- const controllerInstance = new controller ( )
248+ const controllerInstance = new Controller ( )
241249
242250 for ( const method of singletonMethods ) {
243251 if ( options . except ?. includes ( method ) )
@@ -259,8 +267,13 @@ export class FluentRouter {
259267 middleware : this . buildMiddleware ( options . middleware ) ,
260268 name : this . currentNamePrefix + routeName ,
261269 domain : this . currentDomain || undefined ,
270+ pattern : this . createPattern ( fullPath ) ,
271+ }
272+ // Use direct push to routes array to avoid conflict with http-methods.ts addRoute
273+ this . router . routes . push ( route )
274+ if ( route . name ) {
275+ this . router . namedRoutes . set ( route . name , route )
262276 }
263- this . router . addRoute ( route )
264277 }
265278 }
266279
@@ -410,12 +423,39 @@ export class FluentRouter {
410423 handler : resolvedHandler ,
411424 middleware,
412425 domain : this . currentDomain || undefined ,
426+ pattern : this . createPattern ( fullPath ) ,
413427 }
414428
415- this . router . addRoute ( route )
429+ // Use direct push to routes array to avoid conflict with http-methods.ts addRoute
430+ this . router . routes . push ( route )
431+ if ( route . name ) {
432+ this . router . namedRoutes . set ( route . name , route )
433+ }
416434 return new FluentRouteBuilder ( this . router , fullPath , method )
417435 }
418436
437+ /**
438+ * Create a pattern object for route matching
439+ */
440+ private createPattern ( routePath : string ) : { exec : ( url : URL ) => { pathname : { groups : Record < string , string > } } | null } {
441+ return {
442+ exec : ( url : URL ) : { pathname : { groups : Record < string , string > } } | null => {
443+ const params : Record < string , string > = { }
444+ const isMatch = matchPath ( routePath , url . pathname , params )
445+
446+ if ( ! isMatch ) {
447+ return null
448+ }
449+
450+ return {
451+ pathname : {
452+ groups : params ,
453+ } ,
454+ }
455+ } ,
456+ }
457+ }
458+
419459 private buildPath ( path : string ) : string {
420460 if ( this . currentPrefix ) {
421461 return `${ this . currentPrefix } ${ path . startsWith ( '/' ) ? path : `/${ path } ` } `
@@ -433,7 +473,8 @@ export class FluentRouter {
433473 private resolveHandler ( handler : RouteHandler | string ) : ActionHandler {
434474 if ( typeof handler === 'string' ) {
435475 if ( this . currentController ) {
436- const controllerInstance = new this . currentController ( )
476+ const ControllerClass = this . currentController
477+ const controllerInstance = new ControllerClass ( )
437478 const method = controllerInstance [ handler ] as RouteHandler | undefined
438479 if ( method ) {
439480 return method . bind ( controllerInstance )
0 commit comments