Skip to content

Commit

Permalink
matchPath - Improved matching with ending wildcard (#673)
Browse files Browse the repository at this point in the history
* matchPath - Improved matching with ending wildcard
  • Loading branch information
thim81 authored Nov 1, 2024
1 parent 19eb6fe commit c6e4fee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/utils/matchPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ describe('matchPath', () => {
const operationPath = '/messages/123/details'
expect(matchPath(targetPath, operationPath)).toBe(true)
})

it('should match when targetPath contains a wildcard suffix', () => {
const targetPath = '/licenses/{licenseKey}/modules*'
const operationPath = '/licenses/{licenseKey}/modules'
expect(matchPath(targetPath, operationPath)).toBe(true)
})

it('should handle paths with trailing slashes', () => {
const targetPath = '/messages/:id/'
const operationPath = '/messages/123/'
expect(matchPath(targetPath, operationPath)).toBe(true)
})
})
8 changes: 8 additions & 0 deletions src/utils/matchPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export const matchPath = (targetPath: string | RegExp, operationPath: string): b
return true
}

// If the last segment of targetPath contains a wildcard (*), allow any suffix
if (lastTargetSegment.includes('*')) {
const baseSegment = lastTargetSegment.replace(/\*/g, '')
if (lastOperationSegment.startsWith(baseSegment)) {
return true
}
}

// If the lengths of the paths don't match and there is no wildcard, return false
if (targetSegments.length !== operationSegments.length) {
return false
Expand Down

0 comments on commit c6e4fee

Please sign in to comment.