typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.62k stars 7k forks source link

Not finding any way to change a response for a request from the client side #1002

Open kshrestha99 opened 5 years ago

kshrestha99 commented 5 years ago

Here is the issue: I want to change the response to a request for a particular end point from the client side (for me, I am using webdriverIO). What I mean is that for a particualar test, I want to modify, delay, etc a response based on the critieria of that test for some end point. Other tests hitting that same endpoint can get the default response served up by the json-server but for some tests, I would want some custom responses. This is possible when using 'mockserver-node' with 'mockserver-client'. Does json-server also have some kind of functionality like that? @typicode

bmmpt commented 5 years ago

I also need similar functionality. I'm simulating a login request to an "account" route. I am filtering on email and password and when it matches a record it returns the full account record. But, the real Api will not return the full account, only the userGuid field inside the account. So if I could modify the response I'd extract that field.

It would be nice to be able to do: json-server db.json --response-middlewares res-mdw.js

bmmpt commented 5 years ago

I found a way to achieve what I need. In the middleware I pass in through --middlewares, I added the following:

// Make sure this override only applies for a specific route
if (condition) {
    var send = res.send;
    res.send = function(chunk) {
        chunk = JSON.parse(chunk)[0].id;
        send.apply(this, arguments);
    };
}

next();

But it would still be nice if we could do it through:

json-server db.json --response-middlewares res-mdw.js

calinafox commented 3 years ago

Have you found a solution on this?