r/angular • u/dx_ninja_dx • 4d ago
Angular API implementation
I’m a student that is working with angular for the first time, I have an assignment that requires implementing an api to our project. My teacher has no information on how to unwrap the response using parse for httpResource. There also is no information about mapping with httpclient & observables.
The documentation that is recommended seems completely irrelevant to my the api I have because my api is returning as an array.
How can I get value from the documentation or where should I look to figure this out. I feel like I’ve been having this problem with everything because the documentation never seems relevant. Even with certain concepts, there is things that I know but I am struggling with knowing what or how I can write things to do the task I need completed.
Any assistance would be appreciated.
2
u/karmatics29 4d ago
Since your API returns a plain array, the main thing is to type it as an array, for example httpResource<Item[]>(() => '/api/items'), and then read the result in the template with items.value(). The parse option is only needed if the raw JSON isn't already the shape you want, in which case it's just a function that takes the raw response and returns the transformed value, such as mapping each element to a smaller object. With HttpClient it's the same idea: http.get<Item[]>(url).pipe(map(items => items.map(...))), and you either subscribe or use toSignal. The docs often feel irrelevant because they use a single-object example, so I'd log the response first and write an interface that matches it. What does one element of your array look like, and do you need to transform it or just display it?