when to use interceptor in angular code example
Example 1: interceptor in angular 8
interface HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
}
Example 2: angularjs interceptor
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
'request': function(config) {
return config;
},
'requestError': function(rejection) {
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},
'response': function(response) {
return response;
},
'responseError': function(rejection) {
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('myHttpInterceptor');
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
return {
'request': function(config) {
},
'response': function(response) {
}
};
});