Skip to content

Commit

Permalink
Merge pull request #574 from seznam/fix-router-looping
Browse files Browse the repository at this point in the history
Fixes router looping for specific request path and routes with more optional parameters
  • Loading branch information
Greld authored Aug 1, 2024
2 parents c70a7db + 351f9e2 commit 6d94496
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 19 deletions.
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

0 comments on commit 6d94496

Please sign in to comment.