2017-03-22 20:13:24 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2017-07-13 20:22:02 -04:00
|
|
|
import {Injectable, InjectionToken} from '@angular/core';
|
2018-02-27 17:06:06 -05:00
|
|
|
import {Observable} from 'rxjs';
|
2017-03-22 20:13:24 -04:00
|
|
|
|
|
|
|
import {HttpHandler} from './backend';
|
|
|
|
import {HttpRequest} from './request';
|
2017-07-14 06:55:45 -04:00
|
|
|
import {HttpEvent} from './response';
|
2017-03-22 20:13:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Intercepts `HttpRequest` and handles them.
|
|
|
|
*
|
|
|
|
* Most interceptors will transform the outgoing request before passing it to the
|
|
|
|
* next interceptor in the chain, by calling `next.handle(transformedReq)`.
|
|
|
|
*
|
|
|
|
* In rare cases, interceptors may wish to completely handle a request themselves,
|
|
|
|
* and not delegate to the remainder of the chain. This behavior is allowed.
|
|
|
|
*
|
2018-10-19 10:06:08 -04:00
|
|
|
* @publicApi
|
2017-03-22 20:13:24 -04:00
|
|
|
*/
|
|
|
|
export interface HttpInterceptor {
|
|
|
|
/**
|
|
|
|
* Intercept an outgoing `HttpRequest` and optionally transform it or the
|
|
|
|
* response.
|
|
|
|
*
|
|
|
|
* Typically an interceptor will transform the outgoing request before returning
|
|
|
|
* `next.handle(transformedReq)`. An interceptor may choose to transform the
|
|
|
|
* response event stream as well, by applying additional Rx operators on the stream
|
|
|
|
* returned by `next.handle()`.
|
|
|
|
*
|
|
|
|
* More rarely, an interceptor may choose to completely handle the request itself,
|
|
|
|
* and compose a new event stream instead of invoking `next.handle()`. This is
|
|
|
|
* acceptable behavior, but keep in mind 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.
|
|
|
|
*/
|
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `HttpHandler` which applies an `HttpInterceptor` to an `HttpRequest`.
|
|
|
|
*
|
2018-04-05 17:31:44 -04:00
|
|
|
*
|
2017-03-22 20:13:24 -04:00
|
|
|
*/
|
|
|
|
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 which represents the array of `HttpInterceptor`s that
|
|
|
|
* are registered.
|
|
|
|
*
|
2018-10-19 10:06:08 -04:00
|
|
|
* @publicApi
|
2017-03-22 20:13:24 -04:00
|
|
|
*/
|
|
|
|
export const HTTP_INTERCEPTORS = new InjectionToken<HttpInterceptor[]>('HTTP_INTERCEPTORS');
|
2017-07-13 20:22:02 -04:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NoopInterceptor implements HttpInterceptor {
|
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
|
|
return next.handle(req);
|
|
|
|
}
|
|
|
|
}
|