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

passing matched regex groups in config

Open younes0 opened this issue 6 years ago • 0 comments

What about storing regex matches into the config object:

const getUser = (id) => [
   1: { name: 'Vladimir' },
   2: { name: 'Donald' },
](id)

mock
  .onGet(/\/api\/user\/(\d+)/) // path: '/api/user/1'
  .reply(config => {
     const id = config.matches[1]
     [200, getUser(id)] 
  })


mock
  .onGet(/\/api\/user\/(\d+)\/gun\/(\d+)/)  // path: '/api/user/1/gun/2'
  .reply(config => {
     const gunId = config.matches[2]
     [200, getUserGun(gunId)] 
  })

so we can do advanced mocking according to route parameters

younes0 avatar Apr 10 '19 14:04 younes0