2017-03-22 20:13:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-03-22 20:13:24 -04:00
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
/**
|
2019-07-16 20:25:53 -04:00
|
|
|
|
* Intercepts and handles an `HttpRequest` or `HttpResponse`.
|
2017-03-22 20:13:24 -04:00
|
|
|
|
*
|
2018-09-04 01:33:50 -04:00
|
|
|
|
* 拦截 `HttpRequest` 并处理它们。
|
|
|
|
|
*
|
2018-12-10 13:53:40 -05:00
|
|
|
|
* Most interceptors transform the outgoing request before passing it to the
|
2017-03-22 20:13:24 -04:00
|
|
|
|
* next interceptor in the chain, by calling `next.handle(transformedReq)`.
|
2018-12-10 13:53:40 -05:00
|
|
|
|
* An interceptor may transform the
|
|
|
|
|
* response event stream as well, by applying additional RxJS operators on the stream
|
2018-10-29 12:35:33 -04:00
|
|
|
|
* returned by `next.handle()`.
|
2017-03-22 20:13:24 -04:00
|
|
|
|
*
|
2018-09-04 01:33:50 -04:00
|
|
|
|
* 大多数拦截器都会在外发的请求由 `next.handle(transformedReq)` 发给拦截器链中的下一个拦截器之前,对该请求进行转换。
|
2019-02-16 11:00:16 -05:00
|
|
|
|
* 拦截器还可以通过为 `next.handle()` 返回的流添加额外的 RxJS 操作符,来对响应事件流进行转换。
|
2018-09-04 01:33:50 -04:00
|
|
|
|
*
|
2018-12-10 13:53:40 -05:00
|
|
|
|
* More rarely, an interceptor may handle the request entirely,
|
2018-10-29 12:35:33 -04:00
|
|
|
|
* 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.
|
2017-03-22 20:13:24 -04:00
|
|
|
|
*
|
2019-02-16 11:00:16 -05:00
|
|
|
|
* 极少数情况下,拦截器也可以自己完全处理一个请求,并且组合出新的事件流来而不必调用 `next.handle()`。
|
|
|
|
|
* 这也是允许的,不过要时刻记住,这将会完全跳过所有后继拦截器。
|
|
|
|
|
*
|
2018-10-29 12:35:33 -04:00
|
|
|
|
* It is also rare but valid for an interceptor to return multiple responses on the
|
|
|
|
|
* event stream for a single request.
|
2017-03-22 20:13:24 -04:00
|
|
|
|
*
|
2019-02-16 11:00:16 -05:00
|
|
|
|
* 另一种同样罕见但是有用的拦截器,会为单个请求在事件流上给出多个响应对象。
|
2018-04-05 17:31:44 -04:00
|
|
|
|
*
|
2018-10-19 10:06:08 -04:00
|
|
|
|
* @publicApi
|
2018-10-29 12:35:33 -04:00
|
|
|
|
*
|
2018-12-10 13:53:40 -05:00
|
|
|
|
* @see [HTTP Guide](guide/http#intercepting-requests-and-responses)
|
|
|
|
|
*
|
2019-02-16 11:00:16 -05:00
|
|
|
|
* [HTTP 一章](guide/http#intercepting-requests-and-responses)
|
|
|
|
|
*
|
2018-12-10 13:53:40 -05:00
|
|
|
|
* @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
|
2020-04-13 19:40:21 -04:00
|
|
|
|
* loading modules), each import creates a new copy of the `HttpClientModule`, which overwrites the
|
|
|
|
|
* interceptors provided in the root module.
|
2018-12-10 13:53:40 -05:00
|
|
|
|
*
|
2019-02-16 11:00:16 -05:00
|
|
|
|
* 要想在整个应用中使用 `HttpInterceptors` 的同一个实例,就只能在 `AppModule` 模块中导入 `HttpClientModule`,并且把拦截器都添加到应用的根注入器中。
|
|
|
|
|
* 如果你在不同的模块中多次导入 `HttpClientModule`,则每次导入都会创建 `HttpClientModule` 的一个新复本,它将会覆盖根模块上提供的那些拦截器。
|
|
|
|
|
*
|
2017-03-22 20:13:24 -04:00
|
|
|
|
*/
|
|
|
|
|
export interface HttpInterceptor {
|
|
|
|
|
/**
|
2019-07-16 20:25:53 -04:00
|
|
|
|
* Identifies and handles a given HTTP request.
|
2021-02-04 06:45:54 -05:00
|
|
|
|
*
|
|
|
|
|
* 标识并处理给定的 HTTP 请求。
|
|
|
|
|
*
|
2019-07-16 20:25:53 -04:00
|
|
|
|
* @param req The outgoing request object to handle.
|
2021-02-04 06:45:54 -05:00
|
|
|
|
*
|
|
|
|
|
* 要处理的传出请求对象。
|
|
|
|
|
*
|
2019-07-16 20:25:53 -04:00
|
|
|
|
* @param next The next interceptor in the chain, or the backend
|
|
|
|
|
* if no interceptors remain in the chain.
|
2021-02-04 06:45:54 -05:00
|
|
|
|
*
|
|
|
|
|
* 链中的下一个拦截器,如果链中没有拦截器,则为其后端接口。
|
|
|
|
|
*
|
2019-07-16 20:25:53 -04:00
|
|
|
|
* @returns An observable of the event stream.
|
2021-02-04 06:45:54 -05:00
|
|
|
|
*
|
|
|
|
|
* 事件流的可观察值。
|
2017-03-22 20:13:24 -04:00
|
|
|
|
*/
|
|
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* `HttpHandler` which applies an `HttpInterceptor` to an `HttpRequest`.
|
|
|
|
|
*
|
2018-09-04 01:33:50 -04:00
|
|
|
|
* `HttpHandler` 会把 `HttpInterceptor` 应用到 `HttpRequest` 上。
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-16 20:25:53 -04:00
|
|
|
|
* A multi-provider token that represents the array of registered
|
|
|
|
|
* `HttpInterceptor` objects.
|
2017-03-22 20:13:24 -04:00
|
|
|
|
*
|
2018-04-05 17:31:44 -04:00
|
|
|
|
*
|
2021-02-04 06:45:54 -05:00
|
|
|
|
* 一个多重提供者(multi-provider)令牌,它代表所有已注册的 `HttpInterceptor` 构成的数组。
|
2018-09-04 01:33:50 -04:00
|
|
|
|
*
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|