Nested API's in Angular

Hi Friends, This is Lakshman here.

As part of the Series Angular Best Practices

Today, We will Nested API's in Angular.

Pre-requisites:

The pre-requisites are

Nested API's

It is a Concept where we may call multiple API at one time or Chaining the API to Call one by one with passing data.

The mainly used functions to implement Nested API from rxjs library,

  • forkJoin
  • mergeMap

forkJoin

It used to call multiple APIs to fetch all data as an array at one time.

For example,

 return forkJoin([
            this.helloService.getData(),
            this.helloService.getStatus()
        ]).pipe(
            map(allResponses => {
                return {
                    Data: allResponses[allResponses.length - 2],
                    Status: allResponses[allResponses.length - 1]
                };
            })
        );

mergeMap

It uses to chain the API by passing data from One API Response to another API.

For example,

return this.SearchUserByName(username).pipe(
      mergeMap((responses) =>
        zip(
          of(responses),
          this.FetchFollowersByUser(responses.followers_url))
      )
    ).pipe(
      map((allResponses) => {
        const responses = allResponses[allResponses.length - 1];
        responses['UserInfo'] = allResponses[allResponses.length - 2];

        return responses;
      })
    );

That wraps up the Nested API's in Angular

You may also access to source code on GitHub by Click Here

On-road head, we will discuss a lot of things.

Looking forward to your comments and sharing your feedback.

Until that, I am Lakshman, Signing Off.

Comments

Popular posts from this blog

Model Mapper in Angular

Implement Logging in Angular with Firebase

Dockerize Angular App for Dev