I am trying to transform Django response to Angular's User array. There are several reasons, such as different variable names (first_name vs firstName) and having some logic inside Angular User constructor.
Simply
Django User => Angular User
Example of server response:
[
{"id":2,"email":"[email protected]","first_name":"Name 1","client":{"id":1}}},
{"id":3,"email":"[email protected]","first_name":"Name 2","client":{"id":2}}}
]
What I want is to transform to this format:
export class User {
// contructor that transforms
id: number;
email: string;
firstName: string;
isClient: boolean = false;
}
I currently have this "solved", but I am wondering if there is a better solution for this.
Inside something.service.ts
public getClients(): Observable<User[]> {
return this.http.get(this.getClientsUrl)
.map(r => r.json() || []).map(x => x.map(y => new User(y)));
}
When I say better solution, while this works, it doesn't look very readable. Not a problem if you do this once, but when you have a lot of requests you start thinking of a better solution (one method to deal with this?). What would be a better way?
Something like having
return this.http.get(this.getClientsUrl)
.map(transformResponse);