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

Is there a way to inspect body/query params and respond with dynamic data? #454

Closed
gcdurastanti opened this issue Oct 3, 2019 · 1 comment
Closed

Comments

@gcdurastanti
Copy link

@gcdurastanti gcdurastanti commented Oct 3, 2019

Looking through the documentation I can't seem to find anything that would solve this issue so I figured I'd come here for some clarification. My use case is simple.

Given:

fetch('/artifact/:id', { params: { id: 1 } })

where 1 represents an Identifier but can change based on the interaction point, is there a way to setup a fetch mock that would be able to see that query param and respond with different data based on what that is set to.

i.e.

const responseMap = {
  0: 'foo',
  1: 'bar',
  2: 'baz',
};

fetchMock.get('express:/artifact/:id', (url, options) => {
  return responseMap[options.params.id]; 
});

yields: 'bar'
@wheresrhys

This comment has been minimized.

Copy link
Owner

@wheresrhys wheresrhys commented Oct 5, 2019

No there isn't. I would accept a PR to implement it, though I'm not sure what the API should look like. Overloading the options object with a params property feels ok I guess

You could implement something for yourself by using https://github.com/pillarjs/path-to-regexp and applying it to url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.