-
Notifications
You must be signed in to change notification settings - Fork 70
Description
I'm new to observables and this service seems to use them heavily. I'd like to transform the response so that it creates a collection (on GET resource/) of entities implementing an interface describing them, or a single entity on GET resource/id.
The response from my API is something like this:
{
data: [
{
id: 1,
name: "some name"
},
{
id: 2,
name: "some name"
},
],
meta: {
page: 1,
totalPages: 14
}
}
The method to receive this object is pretty basic:
@GET('/titles')
public getTitles(@Query('page') page: number): Observable<any>
{
return null;
}
In my component which injects this Rest client, I use:
this.client.getTitles(page).subscribe(res => {
console.log(res.json());
})
Now, I have two options:
-
the ugly solution: Pass the res.json() results to another service in the observer callback which transforms the data array of the response to an array of entities. The downside is that I'd have to copy-paste this everywhere I want to get the data from the client.
-
the nice solution: Somehow transform the response into entities in the client itself, then just pass the array of entities to the observer callback.
Any help is welcome.