1
- import { isRecord , isString } from '@aws-lambda-powertools/commons/typeutils' ;
1
+ import {
2
+ isRecord ,
3
+ isRegExp ,
4
+ isString ,
5
+ } from '@aws-lambda-powertools/commons/typeutils' ;
2
6
import type { APIGatewayProxyEvent , APIGatewayProxyResult } from 'aws-lambda' ;
3
7
import type {
4
8
CompiledRoute ,
@@ -15,13 +19,21 @@ import {
15
19
UNSAFE_CHARS ,
16
20
} from './constants.js' ;
17
21
22
+ export function getPathString ( path : Path ) : string {
23
+ return isString ( path ) ? path : path . source . replace ( / \\ \/ / g, '/' ) ;
24
+ }
25
+
18
26
export function compilePath ( path : Path ) : CompiledRoute {
19
27
const paramNames : string [ ] = [ ] ;
20
28
21
- const regexPattern = path . replace ( PARAM_PATTERN , ( _match , paramName ) => {
22
- paramNames . push ( paramName ) ;
23
- return `(?<${ paramName } >[${ SAFE_CHARS } ${ UNSAFE_CHARS } \\w]+)` ;
24
- } ) ;
29
+ const pathString = getPathString ( path ) ;
30
+ const regexPattern = pathString . replace (
31
+ PARAM_PATTERN ,
32
+ ( _match , paramName ) => {
33
+ paramNames . push ( paramName ) ;
34
+ return `(?<${ paramName } >[${ SAFE_CHARS } ${ UNSAFE_CHARS } \\w]+)` ;
35
+ }
36
+ ) ;
25
37
26
38
const finalPattern = `^${ regexPattern } $` ;
27
39
@@ -36,9 +48,10 @@ export function compilePath(path: Path): CompiledRoute {
36
48
export function validatePathPattern ( path : Path ) : ValidationResult {
37
49
const issues : string [ ] = [ ] ;
38
50
39
- const matches = [ ...path . matchAll ( PARAM_PATTERN ) ] ;
40
- if ( path . includes ( ':' ) ) {
41
- const expectedParams = path . split ( ':' ) . length ;
51
+ const pathString = getPathString ( path ) ;
52
+ const matches = [ ...pathString . matchAll ( PARAM_PATTERN ) ] ;
53
+ if ( pathString . includes ( ':' ) ) {
54
+ const expectedParams = pathString . split ( ':' ) . length ;
42
55
if ( matches . length !== expectedParams - 1 ) {
43
56
issues . push ( 'Malformed parameter syntax. Use :paramName format.' ) ;
44
57
}
@@ -200,13 +213,22 @@ export const composeMiddleware = (middleware: Middleware[]): Middleware => {
200
213
/**
201
214
* Resolves a prefixed path by combining the provided path and prefix.
202
215
*
216
+ * The function returns a RegExp if any of the path or prefix is a RegExp.
217
+ * Otherwise, it returns a `/${string}` type value.
218
+ *
203
219
* @param path - The path to resolve
204
220
* @param prefix - The prefix to prepend to the path
205
221
*/
206
222
export const resolvePrefixedPath = ( path : Path , prefix ?: Path ) : Path => {
207
- if ( prefix ) {
208
- if ( ! path . startsWith ( '/' ) ) return `${ prefix } /${ path } ` ;
209
- return path === '/' ? prefix : `${ prefix } ${ path } ` ;
223
+ if ( ! prefix ) return path ;
224
+ if ( isRegExp ( prefix ) ) {
225
+ if ( isRegExp ( path ) ) {
226
+ return new RegExp ( `${ getPathString ( prefix ) } /${ getPathString ( path ) } ` ) ;
227
+ }
228
+ return new RegExp ( `${ getPathString ( prefix ) } ${ path } ` ) ;
229
+ }
230
+ if ( isRegExp ( path ) ) {
231
+ return new RegExp ( `${ prefix } /${ getPathString ( path ) } ` ) ;
210
232
}
211
- return path ;
233
+ return ` ${ prefix } ${ path } ` . replace ( / \/ $ / , '' ) as Path ;
212
234
} ;
0 commit comments