storybook-addon-mock
storybook-addon-mock copied to clipboard
Support async response function
Hi
We could define a function when we want to return a custom response.
It should be great if it supports async response. Especially if we want to call a mock server.
It could be evolved like so:
setTimeout(async () => {
let payload = response;
if (typeof response === 'function') {
payload = response(request);
if (response.constructor.name === 'AsyncFunction' || payload instanceof Promise) {
payload = await payload;
}
}
resolve(new Response(url, status, payload));
}, +delay);
Thoughts?
I think this is a necessary addition. My use case is very simple - I want to read the JSON send by the client and create a response based on that. This is sadly not possible, as Request.json()
returns a promise. Any workarounds for this?