I'm getting to know Angular, TypeScript and RxJS. I have an http request that returns a JSON. In this JSON there is data that I need to construct my defined object. Let's say, that my object is this:
export class RegularUser {
constructor(
public id: number,
public firstName: string,
public lastName: string,
public token: string
) {}
}
Now, I am sending a request to some API, which returns data in this format:
{
success: boolean,
uid: number,
first_name: string,
last_name: string,
cid: number,
rights: number[]
token: string
}
I have the HttpClient service, so I thought I would do:
this.httpClient.get(
'http://api.example.com/api/get_user'
).pipe(
tap((receivedData: Response) => console.log(receivedData)),
map((receivedData: Response) => {
return new RegularUser(
receivedData.uid,
receivedData.first_name,
receivedData.last_name,
receivedData.token);
})
);
But for TypeScript, the receivedData object doesn't have above-listed params. Do I have to create an interface for the API response and then map it to my RegularUser object?