I have a generic API service in my app that makes requests like this to the backend:
post<T>(url: string, jsonObject: object): Observable<T> {
return this.http.post<T>(url, JSON.stringify(jsonObject));
}
I use that method like this from my components and try to cast the response as a specific model:
this.apiService.post<SomeSpecificModel>('some/path/here', this.usernameForm.value)
.subscribe(
res => {
console.warn(res);
},
() => {
console.warn("error");
}
);
My problem is that the API returns content like this:
{
code: some-string,
type: some-number,
data: []
}
And the data property is where the entity is that I want to cast with my model in Angular.
I would still like to have access to both code and data properties in my component, but I am not sure how to accomplish that.
Can I make a base model that mimics the response from the API and has a generic array property? Something like this:
export class BaseModel<T> {
code: string;
type: number;
data: Array<T>;
}
And then cast like this?
this.apiService.post<BaseModel<SomeSpecificModel>>(...
I tried that and I don't get any build errors, but the properties on SomeSpecificModel is not mapped.
BaseModelandSomeSpecificModelas interfaces and let me know if it makes a difference