Skip to content

Commit 7f13173

Browse files
author
Egor
committed
add support for optional catch all routes
1 parent def5813 commit 7f13173

File tree

5 files changed

+254
-196
lines changed

5 files changed

+254
-196
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"@zeit/node-file-trace": "^0.8.2",
4040
"ansi-escapes": "^4.3.1",
4141
"archiver": "^5.0.0",
42-
"aws-sdk": "^2.724.0",
42+
"aws-sdk": "^2.725.0",
4343
"chalk": "^4.1.0",
4444
"execa": "^4.0.3",
4545
"figures": "^3.2.0",
@@ -73,13 +73,13 @@
7373
"@types/node": "^14.0.27",
7474
"@types/path-to-regexp": "^1.7.0",
7575
"@types/ramda": "^0.27.14",
76-
"@types/react": "^16.9.43",
76+
"@types/react": "^16.9.44",
7777
"@types/react-dom": "^16.9.8",
7878
"@types/strip-ansi": "^5.2.1",
7979
"@types/webpack": "^4.41.21",
8080
"@typescript-eslint/eslint-plugin": "^3.7.1",
8181
"@typescript-eslint/parser": "^3.7.1",
82-
"eslint": "^7.5.0",
82+
"eslint": "^7.6.0",
8383
"eslint-config-prettier": "^6.11.0",
8484
"eslint-plugin-prettier": "^3.1.4",
8585
"husky": "^4.2.5",

packages/aws-lambda-builder/src/index.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@ import {
1111
readFile,
1212
} from 'fs-extra';
1313
import { join, resolve, sep, extname, relative, basename } from 'path';
14-
import { pathToRegexp } from 'path-to-regexp';
1514

1615
import getAllFiles from './lib/getAllFilesInDirectory';
1716
import { getSortedRoutes } from './lib/sortedRoutes';
1817
import { OriginRequestHandlerManifest } from '../types';
19-
import expressifyDynamicRoute from './lib/expressifyDynamicRoute';
2018
import createServerlessConfig from './lib/createServerlessConfig';
19+
import {
20+
expressifyDynamicRoute,
21+
normalizeNodeModules,
22+
isDynamicRoute,
23+
pathToRegexStr,
24+
} from './utils';
2125

2226
export const REQUEST_LAMBDA_CODE_DIR = 'request-lambda';
2327

24-
const normalizeNodeModules = (path: string): string => path.substring(path.indexOf('node_modules'));
25-
// Identify /[param]/ in route string
26-
const isDynamicRoute = (route: string): boolean => /\/\[[^\/]+?\](?=\/|$)/.test(route);
27-
const pathToRegexStr = (path: string): string =>
28-
pathToRegexp(path)
29-
.toString()
30-
.replace(/\/(.*)\/\i/, '$1');
31-
3228
const defaultBuildOptions = {
3329
args: [],
3430
cwd: process.cwd(),
@@ -205,6 +201,7 @@ class Builder {
205201
if (isHtmlPage(pageFile)) {
206202
if (dynamicRoute) {
207203
const route = expressRoute as string;
204+
208205
htmlPages.dynamic[route] = {
209206
file: pageFile,
210207
regex: pathToRegexStr(route),

packages/aws-lambda-builder/src/lib/expressifyDynamicRoute.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { pathToRegexp } from 'path-to-regexp';
2+
3+
/**
4+
* Dynamic routes: /[param]/ -> /:param
5+
* Catch all routes: /[...param]/ -> /:param+
6+
* Optional catch all routes: /[...param]/ -> /:param*
7+
*
8+
* @param dynamicRoute - route to expressify.
9+
*/
10+
export const expressifyDynamicRoute = (dynamicRoute: string): string => {
11+
// replace any catch all group first
12+
let expressified = dynamicRoute.replace(/\[\.\.\.(.*)]$/, ':$1+');
13+
14+
// replace other dynamic route groups
15+
expressified = expressified.replace(/\[(.*?)]/g, ':$1');
16+
17+
// check if this is actually an optional catch all group
18+
if (expressified.includes('/::')) {
19+
expressified = expressified.replace('/::', '/:').replace(/\+$/, '*');
20+
}
21+
22+
return expressified;
23+
};
24+
export const normalizeNodeModules = (path: string): string =>
25+
path.substring(path.indexOf('node_modules'));
26+
// Identify /[param]/ in route string
27+
export const isDynamicRoute = (route: string): boolean => /\/\[[^\/]+?\](?=\/|$)/.test(route);
28+
export const pathToRegexStr = (path: string): string =>
29+
pathToRegexp(path)
30+
.toString()
31+
.replace(/\/(.*)\/\i/, '$1');

0 commit comments

Comments
 (0)