I'm testing with a JSON data mock of this format:
[
{
"_id": "5ea1c3e2f376f7887032bd44",
"iso6393": "ara",
"name": "Arabic"
},
{
"_id": "5ea1c3e2f376f7887032bd41",
"iso6393": "eng",
"name": "English"
}
]
My Angular code injects it into the Language[] array. I can access the object and have intelligence about the object in my IDE and pre-transpiling failure:
getLanguages(): Promise<Language[]> {
const url = this.cfgSvc.composeURL(SettingsEnum.LANGS);
return this.http.get<Language[]>(url).toPromise();
}
Now, the webservice changed and wraps the JSON into a parent "languages" node:
{
"languages": [
{
"_id": "5ea1c3e2f376f7887032bd44",
"iso6393": "ara",
"name": "Arabic"
},
{
"_id": "5ea1c3e2f376f7887032bd41",
"iso6393": "eng",
"name": "English"
}
]
}
However, this wont work any more with my code. How can I strip the languages parent node and map the elements to Language[] array again?
I failed to implement it with pipe and map. And I don't want to do it with verbose code and constructors on the Language object.
.then(({ languages }) => languages)? How exactly did you fail to do this withpipeandmap? Have you updated the generic type of the response body to{ languages: Language[] }so TypeScript can help you out?http.getto reflect the actual shape of the response, the compiler can help to guide you from there.getLanguagesreturns a Promise of? My understanding was that you wanted to keep that the same, but the shape of the response was changing. A simplified example compiles just fine: typescriptlang.org/play/…. Otherwise give a minimal reproducible example.