11import type { GenericLogger } from '@aws-lambda-powertools/commons/types' ;
2- import { isRecord } from '@aws-lambda-powertools/commons/typeutils' ;
32import {
43 getStringFromEnv ,
54 isDevMode ,
@@ -13,10 +12,15 @@ import type {
1312 RouteOptions ,
1413 RouterOptions ,
1514} from '../types/rest.js' ;
16- import { HttpVerbs } from './constatnts.js' ;
15+ import { HttpVerbs } from './constants.js' ;
16+ import { Route } from './Route.js' ;
17+ import { RouteHandlerRegistry } from './RouteHandlerRegistry.js' ;
1718
1819abstract class BaseRouter {
1920 protected context : Record < string , unknown > ;
21+
22+ protected routeRegistry : RouteHandlerRegistry ;
23+
2024 /**
2125 * A logger instance to be used for logging debug, warning, and error messages.
2226 *
@@ -39,6 +43,7 @@ abstract class BaseRouter {
3943 error : console . error ,
4044 warn : console . warn ,
4145 } ;
46+ this . routeRegistry = new RouteHandlerRegistry ( { logger : this . logger } ) ;
4247 this . isDev = isDevMode ( ) ;
4348 }
4449
@@ -48,126 +53,98 @@ abstract class BaseRouter {
4853 options ?: ResolveOptions
4954 ) : Promise < unknown > ;
5055
51- public abstract route ( handler : RouteHandler , options : RouteOptions ) : void ;
56+ public route ( handler : RouteHandler , options : RouteOptions ) : void {
57+ const { method, path } = options ;
58+ const methods = Array . isArray ( method ) ? method : [ method ] ;
59+
60+ for ( const method of methods ) {
61+ this . routeRegistry . register ( new Route ( method , path , handler ) ) ;
62+ }
63+ }
5264
5365 #handleHttpMethod(
5466 method : HttpMethod ,
5567 path : Path ,
56- handler ?: RouteHandler | RouteOptions ,
57- options ?: RouteOptions
68+ handler ?: RouteHandler
5869 ) : MethodDecorator | undefined {
5970 if ( handler && typeof handler === 'function' ) {
60- this . route ( handler , { ... ( options || { } ) , method, path } ) ;
71+ this . route ( handler , { method, path } ) ;
6172 return ;
6273 }
6374
6475 return ( _target , _propertyKey , descriptor : PropertyDescriptor ) => {
65- const routeOptions = isRecord ( handler ) ? handler : options ;
66- this . route ( descriptor . value , { ...( routeOptions || { } ) , method, path } ) ;
76+ this . route ( descriptor . value , { method, path } ) ;
6777 return descriptor ;
6878 } ;
6979 }
7080
71- public get ( path : string , handler : RouteHandler , options ?: RouteOptions ) : void ;
72- public get ( path : string , options ?: RouteOptions ) : MethodDecorator ;
73- public get (
74- path : Path ,
75- handler ?: RouteHandler | RouteOptions ,
76- options ?: RouteOptions
77- ) : MethodDecorator | undefined {
78- return this . #handleHttpMethod( HttpVerbs . GET , path , handler , options ) ;
81+ public get ( path : Path , handler : RouteHandler ) : void ;
82+ public get ( path : Path ) : MethodDecorator ;
83+ public get ( path : Path , handler ?: RouteHandler ) : MethodDecorator | undefined {
84+ return this . #handleHttpMethod( HttpVerbs . GET , path , handler ) ;
7985 }
8086
81- public post ( path : Path , handler : RouteHandler , options ?: RouteOptions ) : void ;
82- public post ( path : Path , options ?: RouteOptions ) : MethodDecorator ;
83- public post (
84- path : Path ,
85- handler ?: RouteHandler | RouteOptions ,
86- options ?: RouteOptions
87- ) : MethodDecorator | undefined {
88- return this . #handleHttpMethod( HttpVerbs . POST , path , handler , options ) ;
87+ public post ( path : Path , handler : RouteHandler ) : void ;
88+ public post ( path : Path ) : MethodDecorator ;
89+ public post ( path : Path , handler ?: RouteHandler ) : MethodDecorator | undefined {
90+ return this . #handleHttpMethod( HttpVerbs . POST , path , handler ) ;
8991 }
9092
91- public put ( path : Path , handler : RouteHandler , options ?: RouteOptions ) : void ;
92- public put ( path : Path , options ?: RouteOptions ) : MethodDecorator ;
93- public put (
94- path : Path ,
95- handler ?: RouteHandler | RouteOptions ,
96- options ?: RouteOptions
97- ) : MethodDecorator | undefined {
98- return this . #handleHttpMethod( HttpVerbs . PUT , path , handler , options ) ;
93+ public put ( path : Path , handler : RouteHandler ) : void ;
94+ public put ( path : Path ) : MethodDecorator ;
95+ public put ( path : Path , handler ?: RouteHandler ) : MethodDecorator | undefined {
96+ return this . #handleHttpMethod( HttpVerbs . PUT , path , handler ) ;
9997 }
10098
101- public patch ( path : Path , handler : RouteHandler , options ?: RouteOptions ) : void ;
102- public patch ( path : Path , options ?: RouteOptions ) : MethodDecorator ;
99+ public patch ( path : Path , handler : RouteHandler ) : void ;
100+ public patch ( path : Path ) : MethodDecorator ;
103101 public patch (
104102 path : Path ,
105- handler ?: RouteHandler | RouteOptions ,
106- options ?: RouteOptions
103+ handler ?: RouteHandler
107104 ) : MethodDecorator | undefined {
108- return this . #handleHttpMethod( HttpVerbs . PATCH , path , handler , options ) ;
105+ return this . #handleHttpMethod( HttpVerbs . PATCH , path , handler ) ;
109106 }
110107
108+ public delete ( path : Path , handler : RouteHandler ) : void ;
109+ public delete ( path : Path ) : MethodDecorator ;
111110 public delete (
112111 path : Path ,
113- handler : RouteHandler ,
114- options ?: RouteOptions
115- ) : void ;
116- public delete ( path : Path , options ?: RouteOptions ) : MethodDecorator ;
117- public delete (
118- path : Path ,
119- handler ?: RouteHandler | RouteOptions ,
120- options ?: RouteOptions
112+ handler ?: RouteHandler
121113 ) : MethodDecorator | undefined {
122- return this . #handleHttpMethod( HttpVerbs . DELETE , path , handler , options ) ;
114+ return this . #handleHttpMethod( HttpVerbs . DELETE , path , handler ) ;
123115 }
124116
125- public head ( path : Path , handler : RouteHandler , options ?: RouteOptions ) : void ;
126- public head ( path : Path , options ?: RouteOptions ) : MethodDecorator ;
127- public head (
128- path : Path ,
129- handler ?: RouteHandler | RouteOptions ,
130- options ?: RouteOptions
131- ) : MethodDecorator | undefined {
132- return this . #handleHttpMethod( HttpVerbs . HEAD , path , handler , options ) ;
117+ public head ( path : Path , handler : RouteHandler ) : void ;
118+ public head ( path : Path ) : MethodDecorator ;
119+ public head ( path : Path , handler ?: RouteHandler ) : MethodDecorator | undefined {
120+ return this . #handleHttpMethod( HttpVerbs . HEAD , path , handler ) ;
133121 }
134122
123+ public options ( path : Path , handler : RouteHandler ) : void ;
124+ public options ( path : Path ) : MethodDecorator ;
135125 public options (
136126 path : Path ,
137- handler : RouteHandler ,
138- options ?: RouteOptions
139- ) : void ;
140- public options ( path : Path , options ?: RouteOptions ) : MethodDecorator ;
141- public options (
142- path : Path ,
143- handler ?: RouteHandler | RouteOptions ,
144- options ?: RouteOptions
127+ handler ?: RouteHandler
145128 ) : MethodDecorator | undefined {
146- return this . #handleHttpMethod( HttpVerbs . OPTIONS , path , handler , options ) ;
129+ return this . #handleHttpMethod( HttpVerbs . OPTIONS , path , handler ) ;
147130 }
148131
132+ public connect ( path : Path , handler : RouteHandler ) : void ;
133+ public connect ( path : Path ) : MethodDecorator ;
149134 public connect (
150135 path : Path ,
151- handler : RouteHandler ,
152- options ?: RouteOptions
153- ) : void ;
154- public connect ( path : Path , options ?: RouteOptions ) : MethodDecorator ;
155- public connect (
156- path : Path ,
157- handler ?: RouteHandler | RouteOptions ,
158- options ?: RouteOptions
136+ handler ?: RouteHandler
159137 ) : MethodDecorator | undefined {
160- return this . #handleHttpMethod( HttpVerbs . CONNECT , path , handler , options ) ;
138+ return this . #handleHttpMethod( HttpVerbs . CONNECT , path , handler ) ;
161139 }
162140
163- public trace ( path : Path , handler : RouteHandler , options ?: RouteOptions ) : void ;
164- public trace ( path : Path , options ?: RouteOptions ) : MethodDecorator ;
141+ public trace ( path : Path , handler : RouteHandler ) : void ;
142+ public trace ( path : Path ) : MethodDecorator ;
165143 public trace (
166144 path : Path ,
167- handler ?: RouteHandler | RouteOptions ,
168- options ?: RouteOptions
145+ handler ?: RouteHandler
169146 ) : MethodDecorator | undefined {
170- return this . #handleHttpMethod( HttpVerbs . TRACE , path , handler , options ) ;
147+ return this . #handleHttpMethod( HttpVerbs . TRACE , path , handler ) ;
171148 }
172149}
173150
0 commit comments