vue-mc icon indicating copy to clipboard operation
vue-mc copied to clipboard

Custom response payload format

Open JonasDoebertin opened this issue 6 years ago • 4 comments

I'm trying to use vue-mc (which is awesome, by the way) and have run into an issue:

The API I'm working with is responding with a data and an optional meta field for every request. As far as I read from the docs, vue-mc is expecting the actual data to be in the root node of the response data instead of in a separate data field, as in this example. The meta field includes additional cursor data, that I can handle myself.

{
    "data": [
        {
            "id": "9788dd4e-d38b-4982-b1b5-41b1eb4d5a04",
            "foo": "bar",
            "bar": "baz"
        },
        {
            "id": "e1d26b1f-8085-4e4f-af49-44ca039c65b1",
            "foo": "bar",
            "bar": "baz"
        }
    ],
    "meta": {
        "current": "b5c30e88-f43e-421b-95ef-f45b891bf1c1",
        "next": "7d46c5f1-709d-4945-995e-a141c085ae21"
    }
}

How would I manage to make vue-mc get the data from the data field? In another issue I saw that I can override the Collections getModelsFromResponse() method. This works only for collections though.

Much easier would be to override Response.getData() globally, but I have no idea how this could be done.

JonasDoebertin avatar Dec 04 '17 08:12 JonasDoebertin

There's definitely a need for an equivalent of getModelsFromResponse, maybe a getAttributesFromResponse? I'll leave this issue open to indicate a to-do on that front, but in the meantime you can override the update method.

update(data) {
    return super.update(data.data);
}

It's far from a nice solution and update isn't a great name for that method, but might tide you over in the meantime. Sorry about this.

rtheunissen avatar Dec 04 '17 09:12 rtheunissen

Hey @rtheunissen, let me know how you want this implemented, happy to write a pr. Having this problem atm.

sifex avatar Aug 18 '18 13:08 sifex

I came across the same issue, but overriding update as @rtheunissen said no longer seems to work.

Extending Collection and overriding getModelsFromResponse like this works for me in version 0.3.0.

getModelsFromResponse(response) {
    return response.getData().data // data is an array of models
}

I don't know when getModelsFromResponse was added, or if It's documented somewhere. I found It looking at the source code.

franciscotrillo avatar Sep 05 '18 09:09 franciscotrillo

I don't know when getModelsFromResponse was added, or if It's documented somewhere. I found It looking at the source code.

It was never intended to be public but that was short-sighted. We designed it so that vue-mc dictates the schema of the response, but not the other way around. This was because we developed it in-house and our response schema was already in place.

This requires a proper solution in v1.0, but we can do something in the meantime to get around it. I don't mind something like getAttributesFromResponse, but I'm thinking along the lines of a ModelResponse that has a getAttributes method. That way you can mock and override the response object, rather than an in-model method. It comes down to whether the model or the response itself is responsible for parsing the response.

rtheunissen avatar Sep 05 '18 17:09 rtheunissen