Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
gatherExtraFiles,
runUserScripts,
} from './lib/utils';
import { CatchPriority, generateRoutes, parseRoute } from './lib/routes';
import { generateRoutes, parseRoute } from './lib/routes';

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

Expand Down Expand Up @@ -129,10 +129,7 @@ async function buildHandler(
output: {
[route.path]: lambda,
},
routes:
route.catchType === CatchPriority.Static
? undefined
: [{ src: route.path, dest: route.dest }],
routes: [{ src: route.src, dest: route.dest }],
};
}

Expand Down
37 changes: 25 additions & 12 deletions src/lib/routes.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { generateRoutes } from './routes';

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

expect(generateRoutes(staticRoutes)).toEqual([]);
expect(generateRoutes(staticRoutes)).toMatchInlineSnapshot(`
[
{
"dest": "/api/bar/baz",
"path": "api/bar/baz",
"src": "/api/bar/baz",
},
{
"dest": "/api/foo",
"path": "api/foo",
"src": "/api/foo",
},
]
`);
});

it('should generate dynamic routes', () => {
Expand All @@ -16,12 +29,12 @@ describe('generateRoutes', () => {
expect(generateRoutes(dynamicRoutes)).toMatchInlineSnapshot(`
[
{
"dest": "api/post/[id]/comments/[commentId]?id=$id&commentId=$commentId",
"dest": "/api/post/[id]/comments/[commentId]?id=$id&commentId=$commentId",
"path": "api/post/[id]/comments/[commentId]",
"src": "/api/post/(?<id>[^/]+)/comments/(?<commentId>[^/]+)",
},
{
"dest": "api/post/[id]?id=$id",
"dest": "/api/post/[id]?id=$id",
"path": "api/post/[id]",
"src": "/api/post/(?<id>[^/]+)",
},
Expand All @@ -39,17 +52,17 @@ describe('generateRoutes', () => {
expect(generateRoutes(catchAllRoutes)).toMatchInlineSnapshot(`
[
{
"dest": "api/all/[...all]",
"dest": "/api/all/[...all]",
"path": "api/all/[...all]",
"src": "/api/all/(\\S+)",
},
{
"dest": "api/optional/[[...id]]",
"dest": "/api/optional/[[...id]]",
"path": "api/optional/[[...id]]",
"src": "/api/optional/(/\\S+)?",
},
{
"dest": "api/[...rootAll]",
"dest": "/api/[...rootAll]",
"path": "api/[...rootAll]",
"src": "/api/(\\S+)",
},
Expand All @@ -69,27 +82,27 @@ describe('generateRoutes', () => {
expect(generateRoutes(allRoutes)).toMatchInlineSnapshot(`
[
{
"dest": "api/post/[id]/comments/[commentId]?id=$id&commentId=$commentId",
"dest": "/api/post/[id]/comments/[commentId]?id=$id&commentId=$commentId",
"path": "api/post/[id]/comments/[commentId]",
"src": "/api/post/(?<id>[^/]+)/comments/(?<commentId>[^/]+)",
},
{
"dest": "api/post/[id]?id=$id",
"dest": "/api/post/[id]?id=$id",
"path": "api/post/[id]",
"src": "/api/post/(?<id>[^/]+)",
},
{
"dest": "api/all/[...all]",
"dest": "/api/all/[...all]",
"path": "api/all/[...all]",
"src": "/api/all/(\\S+)",
},
{
"dest": "api/optional/[[...id]]",
"dest": "/api/optional/[[...id]]",
"path": "api/optional/[[...id]]",
"src": "/api/optional/(/\\S+)?",
},
{
"dest": "api/[...rootAll]",
"dest": "/api/[...rootAll]",
"path": "api/[...rootAll]",
"src": "/api/(\\S+)",
},
Expand Down
16 changes: 7 additions & 9 deletions src/lib/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { orderBy } from 'lodash';

export const CatchPriority = {
const CatchPriority = {
Static: 0,
Dynamic: 1,
CatchAll: 2,
Expand Down Expand Up @@ -71,7 +71,7 @@ export function parseRoute(filepath: string): ParsedRoute {

return {
src: `/${result.src.join('/')}`,
dest: `${route}${queryString}`,
dest: `/${route}${queryString}`,
path: route,
depth: segments.length,
catchType: result.catchType,
Expand All @@ -89,11 +89,9 @@ export function generateRoutes(files: string[]): Route[] {
['asc', 'desc'],
);

return orderedRoutes
.filter((r) => r.catchType !== CatchPriority.Static)
.map<Route>((r) => ({
src: r.src,
dest: r.dest,
path: r.path,
}));
return orderedRoutes.map<Route>((r) => ({
src: r.src,
dest: r.dest,
path: r.path,
}));
}