Skip to content

Commit

Permalink
fix(getters): route handling wildcard param
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Jul 7, 2022
1 parent 6dbaa3f commit 64d7a79
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/core/getters/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getRoute } from './route';
describe('getRoute getter', () => {
[
['/api/test/{id}', '/api/test/${id}'],
['/api/test/{path*}', '/api/test/${path}'],
['/api/test/{user_id}', '/api/test/${userId}'],
['/api/test/{locale}.js', '/api/test/${locale}.js'],
['/api/test/i18n-{locale}.js', '/api/test/i18n-${locale}.js'],
Expand Down
12 changes: 6 additions & 6 deletions src/core/getters/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { camel } from '../../utils/case';
import { sanitize } from '../../utils/string';

const hasParam = (path: string) => /[^{]*{[\w_-]*}.*/.test(path);
const hasParam = (path: string): boolean => /[^{]*{[\w*_-]*}.*/.test(path);

const getRoutePath = (path: string) => {
const matches = path.match(/([^{]*){?([\w_-]*)}?(.*)/);
const getRoutePath = (path: string): string => {
const matches = path.match(/([^{]*){?([\w*_-]*)}?(.*)/);
if (!matches?.length) return path; // impossible due to regexp grouping here, but for TS

const prev = matches[1];
const param = sanitize(camel(matches[2]), {
es5keyword: true,
underscore: true,
dash: true,
dot: true,
});
const next: string = hasParam(matches[3])
? getRoutePath(matches[3])
: matches[3];
const next = hasParam(matches[3]) ? getRoutePath(matches[3]) : matches[3];

if (hasParam(path)) {
return `${prev}\${${param}}${next}`;
} else {
Expand Down

1 comment on commit 64d7a79

@vercel
Copy link

@vercel vercel bot commented on 64d7a79 Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.