Skip to content

Instantly share code, notes, and snippets.

@wKoza
Created September 18, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wKoza/990629c9e99aafbfb49d2bc9c74b5014 to your computer and use it in GitHub Desktop.
Save wKoza/990629c9e99aafbfb49d2bc9c74b5014 to your computer and use it in GitHub Desktop.
@Injectable()
export class HttpInterceptingHandler implements HttpHandler {
private chain: HttpHandler|null = null;
constructor(private backend: HttpBackend, private injector: Injector) {}
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
if (this.chain === null) {
const interceptors = this.injector.get(HTTP_INTERCEPTORS, []);
this.chain = interceptors.reduceRight(
(next, interceptor) => new HttpInterceptorHandler(next, interceptor), this.backend);
}
return this.chain.handle(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment