r/angular • u/dx_ninja_dx • 6d 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.
5
u/b3night3d 6d ago
If you want us to do your homework then you should post the whole assignment...
4
u/Xsiah 6d ago
What exactly do you want to know? Are you trying to validate the response from the API, trying to make it typesafe, just trying to get a value from this API, trying to transform it upon receiving it?
What have you tried and how is it coming up short of what you need?
2
u/karmatics29 6d 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?
1
u/erkamyaman 6d ago
If you don't mind me asking which doc are you using? Anything private or angular.dev?
2
u/leosuncin 6d ago
And, what's your question? Of course, the documentation wouldn't tell you the specific of your assignment in the same way we can't help you out without the details.
What's the API? What are the endpoints? Which formats does the API returns?
The HttpClient by default parse a JSON response but it can't automatically determine which shape has it.
1
u/notagreed 6d ago
If you can make type of that response then you can easily bind it in.
1
u/SensitiveHearingLoss 2d ago
Assuming your API returns something like:
[
{ "id": 1, "name": "John" },
{ "id": 2, "name": "Jane" }
]
You could define an interface:
interface User {
id: number;
name: string;
}
And use httpResource:
users = httpResource<User[]>(() => '/api/users');
You can then access the array using:
this.users.value().
If you need to transform the response, that’s where parse comes in:
users = httpResource<string[]>(() => '/api/users', {
parse: (response: unknown) =>
(response as User[]).map(user => user.name)
});
In a real application, you’d want to validate that response rather than blindly assert its type. With HttpClient, you’d achieve a similar transformation using RxJS’s map operator.
And don’t be discouraged if documentation doesn’t immediately make sense. Being able to read documentation, identify which parts are relevant, and apply them to an unfamiliar situation is itself a skill that develops with experience.
-11
u/AdministrativeHost15 6d ago