angular-cn/packages/common/http/src/interceptor.ts

83 lines
2.8 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Injectable, InjectionToken} from '@angular/core';
import {Observable} from 'rxjs';
import {HttpHandler} from './backend';
import {HttpRequest} from './request';
import {HttpEvent} from './response';
/**
* Intercepts and handles an `HttpRequest` or `HttpResponse`.
*
* Most interceptors transform the outgoing request before passing it to the
* next interceptor in the chain, by calling `next.handle(transformedReq)`.
* An interceptor may transform the
* response event stream as well, by applying additional RxJS operators on the stream
* returned by `next.handle()`.
*
* More rarely, an interceptor may handle the request entirely,
* and compose a new event stream instead of invoking `next.handle()`. This is an
* acceptable behavior, but keep in mind that further interceptors will be skipped entirely.
*
* It is also rare but valid for an interceptor to return multiple responses on the
* event stream for a single request.
*
* @publicApi
*
* @see [HTTP Guide](guide/http#intercepting-requests-and-responses)
*
* @usageNotes
*
* To use the same instance of `HttpInterceptors` for the entire app, import the `HttpClientModule`
* only in your `AppModule`, and add the interceptors to the root application injector .
* If you import `HttpClientModule` multiple times across different modules (for example, in lazy
* loading modules), each import creates a new copy of the `HttpClientModule`, which overwrites the
* interceptors provided in the root module.
*
*/
export interface HttpInterceptor {
/**
* Identifies and handles a given HTTP request.
* @param req The outgoing request object to handle.
* @param next The next interceptor in the chain, or the backend
* if no interceptors remain in the chain.
* @returns An observable of the event stream.
*/
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>;
}
/**
* `HttpHandler` which applies an `HttpInterceptor` to an `HttpRequest`.
*
*
*/
export class HttpInterceptorHandler implements HttpHandler {
constructor(private next: HttpHandler, private interceptor: HttpInterceptor) {}
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
return this.interceptor.intercept(req, this.next);
}
}
/**
* A multi-provider token that represents the array of registered
* `HttpInterceptor` objects.
*
* @publicApi
*/
export const HTTP_INTERCEPTORS = new InjectionToken<HttpInterceptor[]>('HTTP_INTERCEPTORS');
@Injectable()
export class NoopInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req);
}
}