Skip to content

Commit

Permalink
feat: support [any] as wildcard alias
Browse files Browse the repository at this point in the history
  • Loading branch information
anoack93 committed Jan 9, 2024
1 parent 3eed474 commit 9a65bec
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ mock-api/
├── v1
│   ├── user
│   │   ├── 1.json
│   │   └── 2.json
│   │   ├── 2.json
| | └── [any].json
│   ├── user.json
│   └── user.post.json
└── v2
Expand Down
3 changes: 0 additions & 3 deletions examples/mock-api/v1/*.get.json

This file was deleted.

3 changes: 3 additions & 0 deletions examples/mock-api/v1/[any].get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "this is the content of [any].get.json, files with wildcard should be registered at the end"
}
File renamed without changes.
5 changes: 5 additions & 0 deletions src/service/endpoint-registration.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const ajv = new Ajv()
// this is being used for directories which have the same name as a file like /test.jpg/medium
// you would name that file /test.jpg---medium.jpg
const SLASH_ALIAS = '---'
// Windows does not support the asterisk character '*' in file names, as a result,
// we need to support an alias for cross-platform support
// NOTE: if this is changed, make sure to change it in compareEndpointsBySpecificity too
const WILDCARD_ALIAS = '[any]'
const maxNumberOfLogEntriesInCompactMode = 2

// eslint-disable-next-line require-jsdoc
Expand Down Expand Up @@ -51,6 +55,7 @@ export class EndpointRegistrationService {
.replace(this.mockFileRoot, '')
.replace(SLASH_ALIAS, '/')
.replace(SLASH_ALIAS, '/')
.replace(WILDCARD_ALIAS, '*')
if (fileType.removeFileExtension === true) {
mapping = mapping.replace(fileType.extension, '')
}
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* @return {number}
*/
export function compareEndpointsBySpecificity (pathA, pathB) {
const wildcardsA = (pathA.match(/\*/g) || [])
const wildcardsB = (pathB.match(/\*/g) || [])
const wildcardsA = (pathA.match(/(\*|\[any\])/g) || [])
const wildcardsB = (pathB.match(/(\*|\[any\])/g) || [])

if (wildcardsA.length > 0 && wildcardsB.length > 0) {
// If the number of wildcards is the same, compare their positions
Expand Down

0 comments on commit 9a65bec

Please sign in to comment.