Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes router looping for specific request path and routes with more optional parameters #574

Merged
merged 4 commits into from
Aug 1, 2024
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
5 changes: 5 additions & 0 deletions .changeset/heavy-readers-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ima/core": patch
---

Fixes router looping for specific request path and routes with more optional parameters
20 changes: 3 additions & 17 deletions packages/core/src/router/StaticRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,23 +317,9 @@ export class StaticRoute extends AbstractRoute<string> {
* @return RegExp pattern.
*/
_replaceOptionalParametersInPath(path: string, optionalParams: string[]) {
const pattern = optionalParams.reduce((path, paramExpr, idx, matches) => {
const lastIdx = matches.length - 1;
const hasSlash = paramExpr.substr(0, 2) === '\\/';

let separator = '';

if (idx === 0) {
separator = '(?:' + (hasSlash ? '/' : '');
} else {
separator = hasSlash ? '/?' : '';
}

let regExpr = separator + `([^/?]+)?(?=/|$)?`;

if (idx === lastIdx) {
regExpr += ')?';
}
const pattern = optionalParams.reduce((path, paramExpr) => {
const separator = paramExpr.startsWith('\\/') ? '/' : '';
const regExpr = '(?:' + separator + `([^/?]+)(?=/|$))?`;

return path.replace(paramExpr, regExpr);
}, path);
Expand Down
Loading
Loading