import { url } from 'crayon-kit' ; import { handlerFunc } from '../router' ; export type ParamMap = Record < string , string > ; export interface PatternMap { key : string ; params : Record < string , string > ; } export interface Route { params : ParamMap ; pattern : string ; patterns : PatternMap [ ] ; handlers : handlerFunc [ ] ; } export class RouteMap { middleware : handlerFunc [ ] = [ ] ; routes : Record < string , handlerFunc [ ] > = { } ; public add ( path , ... handlers ) { this . routes [ url . normalise ( path ) ] = handlers ; } public addMiddleware ( ... handlers ) { for ( const handler of handlers ) { this . middleware . push ( handler ) ; } } public findWithPathname ( pathname ) { let patterns = [ ] ; for ( let key in this . routes ) { const params = url . matchPath ( key , pathname ) ; if ( params !== undefined ) { patterns . push ( { key , params } ) ; } } if ( patterns . length === 0 ) { return ; } const wildcard = patterns . filter ( p => p . key . includes ( '**' ) ) ; const noWildcardPatterns = patterns . filter ( p => ! p . key . includes ( '**' ) ) ; let pattern ; let params ; for ( let item of noWildcardPatterns ) { pattern = item . key ; params = item . params ; if ( item . key === pathname ) { break ; } } if ( ! pattern || ! params && wildcard . length ) { const pattern = wildcard [ 0 ] . key ; const handlers = [ ... this . middleware , ... this . routes [ pattern ] ] ; return { params : wildcard [ 0 ] . params , pattern , patterns , handlers } ; } if ( ! pattern || ! params ) { return ; } const handlers = [ ... this . middleware , ... this . routes [ pattern ] ] ; return { params , pattern , patterns , handlers } ; } }