1
1
import type { GenericLogger } from '@aws-lambda-powertools/commons/types' ;
2
- import { isRecord } from '@aws-lambda-powertools/commons/typeutils' ;
3
2
import {
4
3
getStringFromEnv ,
5
4
isDevMode ,
@@ -13,10 +12,15 @@ import type {
13
12
RouteOptions ,
14
13
RouterOptions ,
15
14
} 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' ;
17
18
18
19
abstract class BaseRouter {
19
20
protected context : Record < string , unknown > ;
21
+
22
+ protected routeRegistry : RouteHandlerRegistry ;
23
+
20
24
/**
21
25
* A logger instance to be used for logging debug, warning, and error messages.
22
26
*
@@ -39,6 +43,7 @@ abstract class BaseRouter {
39
43
error : console . error ,
40
44
warn : console . warn ,
41
45
} ;
46
+ this . routeRegistry = new RouteHandlerRegistry ( { logger : this . logger } ) ;
42
47
this . isDev = isDevMode ( ) ;
43
48
}
44
49
@@ -48,126 +53,98 @@ abstract class BaseRouter {
48
53
options ?: ResolveOptions
49
54
) : Promise < unknown > ;
50
55
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
+ }
52
64
53
65
#handleHttpMethod(
54
66
method : HttpMethod ,
55
67
path : Path ,
56
- handler ?: RouteHandler | RouteOptions ,
57
- options ?: RouteOptions
68
+ handler ?: RouteHandler
58
69
) : MethodDecorator | undefined {
59
70
if ( handler && typeof handler === 'function' ) {
60
- this . route ( handler , { ... ( options || { } ) , method, path } ) ;
71
+ this . route ( handler , { method, path } ) ;
61
72
return ;
62
73
}
63
74
64
75
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 } ) ;
67
77
return descriptor ;
68
78
} ;
69
79
}
70
80
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 ) ;
79
85
}
80
86
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 ) ;
89
91
}
90
92
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 ) ;
99
97
}
100
98
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 ;
103
101
public patch (
104
102
path : Path ,
105
- handler ?: RouteHandler | RouteOptions ,
106
- options ?: RouteOptions
103
+ handler ?: RouteHandler
107
104
) : MethodDecorator | undefined {
108
- return this . #handleHttpMethod( HttpVerbs . PATCH , path , handler , options ) ;
105
+ return this . #handleHttpMethod( HttpVerbs . PATCH , path , handler ) ;
109
106
}
110
107
108
+ public delete ( path : Path , handler : RouteHandler ) : void ;
109
+ public delete ( path : Path ) : MethodDecorator ;
111
110
public delete (
112
111
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
121
113
) : MethodDecorator | undefined {
122
- return this . #handleHttpMethod( HttpVerbs . DELETE , path , handler , options ) ;
114
+ return this . #handleHttpMethod( HttpVerbs . DELETE , path , handler ) ;
123
115
}
124
116
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 ) ;
133
121
}
134
122
123
+ public options ( path : Path , handler : RouteHandler ) : void ;
124
+ public options ( path : Path ) : MethodDecorator ;
135
125
public options (
136
126
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
145
128
) : MethodDecorator | undefined {
146
- return this . #handleHttpMethod( HttpVerbs . OPTIONS , path , handler , options ) ;
129
+ return this . #handleHttpMethod( HttpVerbs . OPTIONS , path , handler ) ;
147
130
}
148
131
132
+ public connect ( path : Path , handler : RouteHandler ) : void ;
133
+ public connect ( path : Path ) : MethodDecorator ;
149
134
public connect (
150
135
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
159
137
) : MethodDecorator | undefined {
160
- return this . #handleHttpMethod( HttpVerbs . CONNECT , path , handler , options ) ;
138
+ return this . #handleHttpMethod( HttpVerbs . CONNECT , path , handler ) ;
161
139
}
162
140
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 ;
165
143
public trace (
166
144
path : Path ,
167
- handler ?: RouteHandler | RouteOptions ,
168
- options ?: RouteOptions
145
+ handler ?: RouteHandler
169
146
) : MethodDecorator | undefined {
170
- return this . #handleHttpMethod( HttpVerbs . TRACE , path , handler , options ) ;
147
+ return this . #handleHttpMethod( HttpVerbs . TRACE , path , handler ) ;
171
148
}
172
149
}
173
150
0 commit comments