Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular HTTP Interceptors at Module Level

I just upgraded my Angular v4.2.5 app to Angular v4.3.6 primarily to use Interceptors. It is quite an awesome feature and provides a clean way to intercept HTTP calls.

However, it seems I cannot scope interceptors at modules level. For example, I have an AppModule, and two more modules AModule and BModule. Both AModule and BModule are getting included in AppModule.

Now, is there a way in Angular 4 where I can scope interceptors at module level such that an interceptor I use for HTTP requests in AModule shouldn't be used with HTTP requests in BModule. Presently, interceptors are getting shared across all HTTP calls which was not I expected. I know providers from all modules get merged into the parent module, but is there any way to restrict such thing?

Any help is highly appreciated.

like image 380
Imran Latif Avatar asked Aug 28 '17 15:08

Imran Latif


People also ask

What are interceptors in angular?

Interceptors are one of the built-in tools for specifically handling HTTP requests at a global application level. Often we want to enforce or apply behavior when receiving or sending HTTP requests within our application. Interceptors are a unique type of Angular Service that we can implement.

How to intercept HTTP requests in angular?

We can use Interceptors to intercept the incoming and outgoing HTTP requests and manipulate them using Angular HttpClient. One of the most use cases for adding an interceptor to our Angular application is adding the token to the header of outgoing HTTP requests. Let’s implement an interceptor in our Angular application!

What are interceptors in httpclient?

Interceptors allow us to intercept incoming or outgoing HTTP requests using the HttpClient. They can handle both HttpRequest as well as HttpResponse. By intercepting the Http request, we can modify or change the value of the request, and by intercepting the response, we can do some predefined actions on a particular status code or message.

Are httprequest and httpresponse immutable in angular?

From the Angular documentation: Although interceptors can modify requests and responses, the HttpRequest and HttpResponse instance properties are readonly, rendering them largely immutable.


1 Answers

Add a header or flag to the request from the different services? And then use this to delineate where it is from.

like image 196
Corrigan Avatar answered Oct 08 '22 06:10

Corrigan