Skip to content

Commit 9f3dd6d

Browse files
dglsparsonsecklf
andauthored
Address routing issues with invalid dest (#102)
* Update example to latest runtime * Address routing issues with incorrect dest (dest was missing a leading `/`) --------- Co-authored-by: Florentin / 珞辰 <ecklf@icloud.com>
1 parent 511dc22 commit 9f3dd6d

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
gatherExtraFiles,
2525
runUserScripts,
2626
} from './lib/utils';
27-
import { CatchPriority, generateRoutes, parseRoute } from './lib/routes';
27+
import { generateRoutes, parseRoute } from './lib/routes';
2828

2929
type RustEnv = Record<'RUSTFLAGS' | 'PATH', string>;
3030

@@ -129,10 +129,7 @@ async function buildHandler(
129129
output: {
130130
[route.path]: lambda,
131131
},
132-
routes:
133-
route.catchType === CatchPriority.Static
134-
? undefined
135-
: [{ src: route.path, dest: route.dest }],
132+
routes: [{ src: route.src, dest: route.dest }],
136133
};
137134
}
138135

src/lib/routes.test.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import { generateRoutes } from './routes';
22

33
describe('generateRoutes', () => {
4-
it('should not generate static routes', () => {
4+
it('should generate static routes', () => {
55
const staticRoutes = ['api/foo.rs', 'api/bar/baz.rs'];
66

7-
expect(generateRoutes(staticRoutes)).toEqual([]);
7+
expect(generateRoutes(staticRoutes)).toMatchInlineSnapshot(`
8+
[
9+
{
10+
"dest": "/api/bar/baz",
11+
"path": "api/bar/baz",
12+
"src": "/api/bar/baz",
13+
},
14+
{
15+
"dest": "/api/foo",
16+
"path": "api/foo",
17+
"src": "/api/foo",
18+
},
19+
]
20+
`);
821
});
922

1023
it('should generate dynamic routes', () => {
@@ -16,12 +29,12 @@ describe('generateRoutes', () => {
1629
expect(generateRoutes(dynamicRoutes)).toMatchInlineSnapshot(`
1730
[
1831
{
19-
"dest": "api/post/[id]/comments/[commentId]?id=$id&commentId=$commentId",
32+
"dest": "/api/post/[id]/comments/[commentId]?id=$id&commentId=$commentId",
2033
"path": "api/post/[id]/comments/[commentId]",
2134
"src": "/api/post/(?<id>[^/]+)/comments/(?<commentId>[^/]+)",
2235
},
2336
{
24-
"dest": "api/post/[id]?id=$id",
37+
"dest": "/api/post/[id]?id=$id",
2538
"path": "api/post/[id]",
2639
"src": "/api/post/(?<id>[^/]+)",
2740
},
@@ -39,17 +52,17 @@ describe('generateRoutes', () => {
3952
expect(generateRoutes(catchAllRoutes)).toMatchInlineSnapshot(`
4053
[
4154
{
42-
"dest": "api/all/[...all]",
55+
"dest": "/api/all/[...all]",
4356
"path": "api/all/[...all]",
4457
"src": "/api/all/(\\S+)",
4558
},
4659
{
47-
"dest": "api/optional/[[...id]]",
60+
"dest": "/api/optional/[[...id]]",
4861
"path": "api/optional/[[...id]]",
4962
"src": "/api/optional/(/\\S+)?",
5063
},
5164
{
52-
"dest": "api/[...rootAll]",
65+
"dest": "/api/[...rootAll]",
5366
"path": "api/[...rootAll]",
5467
"src": "/api/(\\S+)",
5568
},
@@ -69,27 +82,27 @@ describe('generateRoutes', () => {
6982
expect(generateRoutes(allRoutes)).toMatchInlineSnapshot(`
7083
[
7184
{
72-
"dest": "api/post/[id]/comments/[commentId]?id=$id&commentId=$commentId",
85+
"dest": "/api/post/[id]/comments/[commentId]?id=$id&commentId=$commentId",
7386
"path": "api/post/[id]/comments/[commentId]",
7487
"src": "/api/post/(?<id>[^/]+)/comments/(?<commentId>[^/]+)",
7588
},
7689
{
77-
"dest": "api/post/[id]?id=$id",
90+
"dest": "/api/post/[id]?id=$id",
7891
"path": "api/post/[id]",
7992
"src": "/api/post/(?<id>[^/]+)",
8093
},
8194
{
82-
"dest": "api/all/[...all]",
95+
"dest": "/api/all/[...all]",
8396
"path": "api/all/[...all]",
8497
"src": "/api/all/(\\S+)",
8598
},
8699
{
87-
"dest": "api/optional/[[...id]]",
100+
"dest": "/api/optional/[[...id]]",
88101
"path": "api/optional/[[...id]]",
89102
"src": "/api/optional/(/\\S+)?",
90103
},
91104
{
92-
"dest": "api/[...rootAll]",
105+
"dest": "/api/[...rootAll]",
93106
"path": "api/[...rootAll]",
94107
"src": "/api/(\\S+)",
95108
},

src/lib/routes.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { orderBy } from 'lodash';
22

3-
export const CatchPriority = {
3+
const CatchPriority = {
44
Static: 0,
55
Dynamic: 1,
66
CatchAll: 2,
@@ -71,7 +71,7 @@ export function parseRoute(filepath: string): ParsedRoute {
7171

7272
return {
7373
src: `/${result.src.join('/')}`,
74-
dest: `${route}${queryString}`,
74+
dest: `/${route}${queryString}`,
7575
path: route,
7676
depth: segments.length,
7777
catchType: result.catchType,
@@ -89,11 +89,9 @@ export function generateRoutes(files: string[]): Route[] {
8989
['asc', 'desc'],
9090
);
9191

92-
return orderedRoutes
93-
.filter((r) => r.catchType !== CatchPriority.Static)
94-
.map<Route>((r) => ({
95-
src: r.src,
96-
dest: r.dest,
97-
path: r.path,
98-
}));
92+
return orderedRoutes.map<Route>((r) => ({
93+
src: r.src,
94+
dest: r.dest,
95+
path: r.path,
96+
}));
9997
}

0 commit comments

Comments
 (0)