diff --git a/README.md b/README.md index f3bfeb4..a6f32a6 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ mock-api/ ├── v1 │   ├── user │   │   ├── 1.json -│   │   └── 2.json +│   │   ├── 2.json +| | └── [any].json │   ├── user.json │   └── user.post.json └── v2 diff --git a/examples/mock-api/v1/*.get.json b/examples/mock-api/v1/*.get.json deleted file mode 100644 index bbda069..0000000 --- a/examples/mock-api/v1/*.get.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "message": "this is the content of *.get.json, files with wildcard should be registered at the end" -} diff --git a/examples/mock-api/v1/[any].get.json b/examples/mock-api/v1/[any].get.json new file mode 100644 index 0000000..deb9eaf --- /dev/null +++ b/examples/mock-api/v1/[any].get.json @@ -0,0 +1,3 @@ +{ + "message": "this is the content of [any].get.json, files with wildcard should be registered at the end" +} diff --git a/examples/mock-api/v1/*/company.json b/examples/mock-api/v1/[any]/company.json similarity index 100% rename from examples/mock-api/v1/*/company.json rename to examples/mock-api/v1/[any]/company.json diff --git a/src/service/endpoint-registration.service.js b/src/service/endpoint-registration.service.js index 557b73e..b0ffe71 100644 --- a/src/service/endpoint-registration.service.js +++ b/src/service/endpoint-registration.service.js @@ -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 @@ -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, '') } diff --git a/src/utilities/sorting.js b/src/utilities/sorting.js index 01d380f..f8f0a82 100644 --- a/src/utilities/sorting.js +++ b/src/utilities/sorting.js @@ -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