Background
We are using miragejs to mock server apis. Our server / backend, follows microservice architecture, and each microservice is versioned separately, and as a result and our api paths have
{base_version}
- server / release version, changes as per environment (dev / qa / uat / prod){microservice_version}
- different for each microservice on each environment
A typical api url looks like
example.com/
{base_version}
/microservice/{microservice_version}
/api_endpoint?parameter=value
Problem
While setting up routes for miragejs mocks,
- using wildcard (*) for both
{microservice_version}
, and{base_version}
fails, and results in a passthrough:
example.com/*/microservice/*/api_endpoint?parameter=value
but, all of the following work correctly:
- wildcard for
{base_version}
, and actual value for{microservice_version}
example.com/*/microservice/v2/api_endpoint?parameter=value
- wildcard for
{microservice_version}
, and actual value for{base_version}
example.com/v3/microservice/*/api_endpoint?parameter=value
- double wildcard (to escape
{microservice_version}
,{base_version}
, and anything in between)
example.com/**/api_endpoint?parameter=value
Either of the 3 working solutions above require us to hardcode one version, or ignore micro service name / path, which is not ideal, as we have multiple environments / releases, multiple micro service versions, and same api endpoints in different microservices (e.g. filters / attributes).
Is there a clean way to get miragejs to work with multiple wildcards in api url?
As Miragejs uses Pretender internally, the multiple wildcards in your sample url should work.
You can also use : for handling dynamic segments instead of *.
As for the issue you are facing, I suppose, you are trying to use the request.params to identify the baseversion and microservice version.
When using unnamed dynamic segments, the request.params will contain one value (the last segment matched).
This could be the reason you are hitting with passthrough when using multiple wildcards.
As @tmdesigned suggested you can use a named dynamic segments like :base or *base, if you need to use the version parameters.