mirage.js multiple wildcard passthrough

1.7k Views Asked by At

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:

  1. wildcard for {base_version}, and actual value for {microservice_version}

example.com/*/microservice/v2/api_endpoint?parameter=value

  1. wildcard for {microservice_version}, and actual value for {base_version}

example.com/v3/microservice/*/api_endpoint?parameter=value

  1. 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?


1

There are 1 best solutions below

0
On

As Miragejs uses Pretender internally, the multiple wildcards in your sample url should work.

example.com/*/microservice/*/api_endpoint?parameter=value

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.