Home > Blockchain >  Implement maintenance logic - how to restrict further API calls in angular
Implement maintenance logic - how to restrict further API calls in angular

Time:10-13

we are working in angular 9 application where we have multiple components. some components are connected with parent-child relation and other components are independent. we are making one initial API call that will return the flag value true/false, based upon the value we need to execute further. i.e. if it is "true" we need to make further calls or else stop the execution.

homecomponent.html :

<div>
//header is child component
<app-header>

       </app-header>
.......
......
</div>


homecomponent.ts:

export class HomeComponent implements OnInit {

ngOnInit(): void {
this.getPageConent();
}

getPageConent() {
// below service will make the http call

    this.dataService.GetPovertyPageStaticContent('home').subscribe((result: any) => {
// based upon the flag execute further or stop execution
 });
  }
}


headercomponent.ts:

export class HeaderComponent implements OnInit {

ngOnInit(): void {
 this.getContents();
}
 getContents() {
  // Another API call to get page data
    this.dataService.GetPovertyPageStaticContent('pageheader').subscribe((result: any) => {

     //do some operation
    });
  }
}

Like that, we have multiple components are connected with one another. We want to restrict the other API calls in the application based upon the initial API call value.

CodePudding user response:

Would that be enough ?

this.dataService
  .GetPovertyPageStaticContent('home')
  .pipe(filter(v => !!v))
  .subscribe(...)

If the result is false, this will prevent the subscribe from executing.

CodePudding user response:

Following your explanation on my other answer, what you are looking for is called an interceptor.

You can find the documentation here

You will have to create a service that holds your condition at all times (so make it a singleton). When the condition is met, the interceptor can process the requests to the API. If the condition is not met, then the interceptor can simply return an EMPTY observable (an observable that completes without ever emitting).

Although, I would say that the best option is to do this logic on your API rather than your client app, I think this approach is the best solution to your question.

  • Related