axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

Allow using route params

Open kierate opened this issue 5 years ago • 13 comments

Working with route params is more convenient than defining the regex in the matcher itself (avoids duplication of regexes for similar routes, allows using the same routes you have on the backend or in the API spec etc.)

This implementation:

  • allows configuring a set of known route params as the 3rd argument to the constructor
  • exposes the values for the params on matched routes under config.routeParams
  • allows using both colon (:userId) and curly braces ({userId}) for route params

Example using the colon notation:

const routeParams = {
  ':userId': '[0-9]{1,8}',
  ':filter': 'active|inactive|all',
}
const mock = new MockAdapter(axios, {}, routeParams);

mock.onGet('/users/:userId/posts/:filter').reply(function(config) {
  const { userId, filter } = config.routeParams;
  
  // userId === '123'
  // filter === 'active'

  return [200, {}];
});

axios.get('/users/123/posts/active');

Example using the curly braces notation:

const routeParams = {
  '{uuid}': '[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}',
  '{page}': '\\d?',
}
const mock = new MockAdapter(axios, {}, routeParams);

mock.onGet('/users/{uuid}/posts/{page}').reply(function(config) {
  const { uuid, page } = config.routeParams;
  
  // uuid === 'b67c0749-656c-4beb-9cd9-17e274a648d9'
  // page === '3'

  return [200, {}];
});

axios.get('/users/b67c0749-656c-4beb-9cd9-17e274a648d9/posts/3');

This addresses requests from https://github.com/ctimmerm/axios-mock-adapter/issues/199 and https://github.com/ctimmerm/axios-mock-adapter/issues/82

kierate avatar May 22 '19 16:05 kierate

great solution, what happened?

hernanif1 avatar Nov 01 '19 15:11 hernanif1

This is great feature, what happened which this pull request? Thanks you so much 😄

bahung1221 avatar Jan 05 '20 09:01 bahung1221

Has this PR been rejected silently?

yss14 avatar Feb 26 '20 13:02 yss14

Any news?

11joselu avatar Jun 09 '20 08:06 11joselu

Can this please be merged?

grbspltt avatar Oct 30 '20 17:10 grbspltt

I was looking for something exactly like this. There don't appear to be any showstoppers here from merging this, right?

dschreij avatar Nov 18 '20 09:11 dschreij

This should be merged. Keeping a PR open for this long is never a good Idea. Reject or (resolve conflicts and) merge, but please don't ignore, dear Axios authors.

thany avatar Jan 27 '21 16:01 thany

@ctimmerm ?

hernanif1 avatar Mar 17 '21 16:03 hernanif1

Such a needed and requested feature :disappointed:

Tofandel avatar May 03 '21 09:05 Tofandel

Still not merged after 2 years?

dguay avatar Jun 01 '21 19:06 dguay

It's possible that this project is dead, but only @ctimmerm knows, and @ctimmerm doesn't say 🤨

thany avatar Jun 04 '21 09:06 thany

https://github.com/pillarjs/path-to-regexp could be embed, it's used in express and react-router

bertho-zero avatar Oct 15 '21 16:10 bertho-zero

I used a different approach which doesn't require a 3rd argument in https://github.com/ctimmerm/axios-mock-adapter/pull/316/files.

bertho-zero avatar Oct 18 '21 10:10 bertho-zero