Modify API response based on API request before passing it to ember data

Hello folks. I need to modify api response data based on the api request input parameters. How do I do this? The serializer doesn’t seem to have access to the original request data.

For instance, say I have an api endpoint like https://pet.api/pets/1 that returns the details for a pet. Part of the details is an optional include called vet_visits which is a list of past visits to the vet. If I include &include=vet_visits then the api will populate this list. If I don’t, then the api will return an empty list (not ideal, I would prefer that vet_visits is just not included in the response at all, but I can’t change this)

now, if at some point I make an api call with the &include=vet_visits it will populate the local ember-data store with the response. Then at some later point I make an api call without the include, it will wipe out previously loaded visits, when what I really want is to keep the previously loaded vet_visits. Right now, because the api returns an empty [] when it gets wiped out in the local store.

What I would like is to be able to tell if the include was present in the call, and if not then delete the property from the response before returning it up the chain and wiping out the previously loaded visits.

Off the top of my head you could probably extract some extra information (such as request data) in the handleResponse method of the adapter, put it in the payload, and then use it as you described in the serializer. This is generally where you’d do that sort of thing if you needed information from the API response headers so I think it fits well enough.

Thanks @dknutsen. I’m looking at the docs for the Adapter, and I don’t see any “handleResponse” method:

Is this a private method that is discouraged to use?

UPDATE: never mind, I see it in the RESTAdapter documentation

Ah yeah it can be a little tricky figuring out which class defines them. I usually start with the JSONAPIAdapter docs since that is the “leaf node” in the inheritance hierarchy, if you will, and work backwards from there.

So we tried this method, unfortunately the requestData available to us in handleResponse only contains the url:string and method:string. It doesn’t include the hash:object which includes the data for the includes.

Usually includes is serialized as a query parameter no? will require a bit of extra parsing but the options hash given to the store method isn’t preserved as far as i know.

Yeah, we do some crazy stuff, like overriding the ajax method. I think we figured it out though. Thanks for your help, it was really useful!

1 Like

this is in fact one of the two main reasons adapters and serializers are on the way out for EmberData