My REST API is designed to return json data structured with the actual content contained within a "data" node. This setup allows for additional meta information to be included in the request, such as pagination details.
{
"data": [
{
"id": 1,
"title": "List 1"
},
{
"id": 2,
"title": "List 2"
},
]
}
To retrieve this data using Angular's http Client, I use the following method:
getLists (): Observable<List[]> {
return this._httpClient.get<List[]>('http://localhost/api/v1/lists').pipe(
tap((response) => {
this._lists.next(response)
}),
)
}
An issue arises because the response does not directly match the expected array of elements of type "List", since the elements are nested inside the "data" node. Accessing this content within the data node from the response object poses a challenge due to the predetermined generic/expected return type.
Is there a way to maintain the use of generics in the get() method and effectively retrieve the content stored within the "data" array, resulting in an array of List objects?