Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using route params #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

kierate
Copy link

@kierate kierate commented May 22, 2019

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 #199 and #82

@hernanif1
Copy link

great solution, what happened?

@bahung1221
Copy link

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

@yss14
Copy link

yss14 commented Feb 26, 2020

Has this PR been rejected silently?

@11joselu
Copy link

11joselu commented Jun 9, 2020

Any news?

@grbspltt
Copy link

Can this please be merged?

@dschreij
Copy link

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

@thany
Copy link

thany commented Jan 27, 2021

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.

@hernanif1
Copy link

@ctimmerm ?

@Tofandel
Copy link

Tofandel commented May 3, 2021

Such a needed and requested feature 😞

@dguay
Copy link

dguay commented Jun 1, 2021

Still not merged after 2 years?

@thany
Copy link

thany commented Jun 4, 2021

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

@bertho-zero
Copy link

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

@bertho-zero
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.