r/angular • • 24d 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.

4 Upvotes

19 comments sorted by

5

u/b3night3d 24d ago

If you want us to do your homework then you should post the whole assignment...

1

u/dx_ninja_dx 24d ago

I don’t want you guys to do the homework, I’m genuinely lost on how I am supposed to get something of value from the resources provided, I thought maybe I am just missing something.

1

u/b3night3d 23d ago

Yeah I'm just saying we need to see what you are working with to know how to answer it and at what level you need to understand it.

0

u/[deleted] 24d ago

[deleted] — view deleted comment

1

u/dx_ninja_dx 23d ago

That actually makes a lot of sense, teacher did mention in the assignment with http client we’d need to map the data I believe.

We did also have a JavaScript class, but it was back in the winter semester from January to April. So my JavaScript is a little rusty 😅

2

u/karmatics29 24d 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?

0

u/dx_ninja_dx 24d ago

I don’t think I need to transform it, I’m going to try what you said here, about writing it out once and logging it to see what I get and then from there trying to adjust might help get the content I need.

Do you know why the documentation just says parse: starWarsApi.parse (or something similar) is this supposed to be a url stored in a variable?

1

u/erkamyaman 24d ago

If you don't mind me asking which doc are you using? Anything private or angular.dev?

1

u/dx_ninja_dx 24d ago

Just angular.dev

1

u/dx_ninja_dx 24d ago

Also there is private documentation ??

1

u/erkamyaman 24d ago

sorry i meant a doc from your instructor

2

u/leosuncin 24d 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 24d ago

If you can make type of that response then you can easily bind it in.

1

u/SensitiveHearingLoss 20d 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 24d ago
getData(
): Observable<any[]> {
  let url = 
location
.origin + '/api/data?';
  const url1 = new URL(url);

  if (
location
.hostname === 'localhost') {
    url1.port = '3000';
  }
  url = url1.toString();
  return this.httpClient.get<any[]>(url);
}

Note: Change 'any' to the type that the API returns.

0

u/[deleted] 24d ago

[deleted] — view deleted comment

6

u/couldhaveebeen 24d ago

Funny thing is, it doesn't answer the question anyway

4

u/Responsible-Cold-627 24d ago

It's also dumb as hell manually adding the origin to the URL. Looks like he doesn't know how relative URLs work.

0

u/[deleted] 24d ago

[deleted] — view deleted comment

2

u/dx_ninja_dx 24d ago

Well I am seeing in my log that I am fetching my data, when I click into the hyperlink the data I’m looking for is there. It’s just that my information is wrapped for example x = [{},{},{}]

Obviously with actual information that’s just a skeleton or template, but I’m supposed to unwrap it I believe back into json to display using parse. It just seems like there isn’t much information on parse in the documentation and my slides don’t have it either. So I’m just wondering how I’m supposed to figure this out cause I want to learn how to find these things myself, because my college really doesn’t teach us how to find stuff like this or how to use documentation. It seems like we’re just expected to know and understand documentation.

0

u/[deleted] 24d ago

[deleted] — view deleted comment

2

u/dx_ninja_dx 24d ago edited 24d ago

Okay I understand that is fair, there is no mention of validating I believe. I can’t remember if validating responses was mentioned in my lectures.

You are right, realistically thinking, it is valid JSON for should be. I’m not getting marks for this assignment anyways it’s past due, I just need to understand how to do it. I’ve been told by my teacher that the api, themealdb, doesn’t return JSON and that I’d need to parse it.

Also I appreciate the information, that makes a lot more sense, so basically if I am struggling with understanding that’s a core concept programming in general than angular’s documentation won’t be of any value.

I guess I’m gonna go review previous core concepts of programming and hope for the best

1

u/drunky1337 24d ago

Check out this resource https://roadmap.sh/frontend?r=frontend-beginner

That gives you a learning path including basics