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

112 lines
4.3 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google LLC 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`.
*
* `HttpRequest`
*
* 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()`.
*
* `next.handle(transformedReq)`
Merge remote-tracking branch 'en/master' into aio # Conflicts: # .travis.yml # aio/content/guide/aot-compiler.md # aio/content/guide/architecture.md # aio/content/guide/build.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/deployment.md # aio/content/guide/elements.md # aio/content/guide/file-structure.md # aio/content/guide/glossary.md # aio/content/guide/hierarchical-dependency-injection.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/pipes.md # aio/content/guide/router.md # aio/content/guide/rx-library.md # aio/content/guide/service-worker-communications.md # aio/content/guide/service-worker-config.md # aio/content/guide/singleton-services.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/guide/typescript-configuration.md # aio/content/guide/universal.md # aio/content/marketing/docs.md # aio/content/marketing/features.html # aio/content/tutorial/toh-pt0.md # aio/content/tutorial/toh-pt6.md # aio/package.json # aio/src/app/layout/footer/footer.component.html # aio/tools/transforms/templates/api/lib/memberHelpers.html # aio/yarn.lock # integration/cli-hello-world-ivy-minimal/src/polyfills.ts # modules/benchmarks/e2e_test/largetable_perf.ts # modules/benchmarks/e2e_test/largetable_spec.ts # packages/animations/src/animation_metadata.ts # packages/common/http/src/headers.ts # packages/common/http/src/interceptor.ts # packages/common/src/directives/ng_for_of.ts # packages/common/src/directives/ng_if.ts # packages/common/src/directives/ng_style.ts # packages/common/src/directives/ng_switch.ts # packages/core/src/change_detection/change_detection_util.ts # packages/core/src/di/injectable.ts # packages/core/src/interface/lifecycle_hooks.ts # packages/core/src/linker/template_ref.ts # packages/core/src/metadata/di.ts # packages/core/src/metadata/directives.ts # packages/core/src/metadata/ng_module.ts # packages/core/src/render/api.ts # packages/forms/src/form_providers.ts # packages/forms/src/model.ts # packages/router/src/config.ts # packages/router/src/directives/router_link.ts
2019-02-16 11:00:16 -05:00
* `next.handle()` RxJS
*
* 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.
*
Merge remote-tracking branch 'en/master' into aio # Conflicts: # .travis.yml # aio/content/guide/aot-compiler.md # aio/content/guide/architecture.md # aio/content/guide/build.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/deployment.md # aio/content/guide/elements.md # aio/content/guide/file-structure.md # aio/content/guide/glossary.md # aio/content/guide/hierarchical-dependency-injection.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/pipes.md # aio/content/guide/router.md # aio/content/guide/rx-library.md # aio/content/guide/service-worker-communications.md # aio/content/guide/service-worker-config.md # aio/content/guide/singleton-services.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/guide/typescript-configuration.md # aio/content/guide/universal.md # aio/content/marketing/docs.md # aio/content/marketing/features.html # aio/content/tutorial/toh-pt0.md # aio/content/tutorial/toh-pt6.md # aio/package.json # aio/src/app/layout/footer/footer.component.html # aio/tools/transforms/templates/api/lib/memberHelpers.html # aio/yarn.lock # integration/cli-hello-world-ivy-minimal/src/polyfills.ts # modules/benchmarks/e2e_test/largetable_perf.ts # modules/benchmarks/e2e_test/largetable_spec.ts # packages/animations/src/animation_metadata.ts # packages/common/http/src/headers.ts # packages/common/http/src/interceptor.ts # packages/common/src/directives/ng_for_of.ts # packages/common/src/directives/ng_if.ts # packages/common/src/directives/ng_style.ts # packages/common/src/directives/ng_switch.ts # packages/core/src/change_detection/change_detection_util.ts # packages/core/src/di/injectable.ts # packages/core/src/interface/lifecycle_hooks.ts # packages/core/src/linker/template_ref.ts # packages/core/src/metadata/di.ts # packages/core/src/metadata/directives.ts # packages/core/src/metadata/ng_module.ts # packages/core/src/render/api.ts # packages/forms/src/form_providers.ts # packages/forms/src/model.ts # packages/router/src/config.ts # packages/router/src/directives/router_link.ts
2019-02-16 11:00:16 -05:00
* `next.handle()`
*
*
* It is also rare but valid for an interceptor to return multiple responses on the
* event stream for a single request.
*
Merge remote-tracking branch 'en/master' into aio # Conflicts: # .travis.yml # aio/content/guide/aot-compiler.md # aio/content/guide/architecture.md # aio/content/guide/build.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/deployment.md # aio/content/guide/elements.md # aio/content/guide/file-structure.md # aio/content/guide/glossary.md # aio/content/guide/hierarchical-dependency-injection.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/pipes.md # aio/content/guide/router.md # aio/content/guide/rx-library.md # aio/content/guide/service-worker-communications.md # aio/content/guide/service-worker-config.md # aio/content/guide/singleton-services.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/guide/typescript-configuration.md # aio/content/guide/universal.md # aio/content/marketing/docs.md # aio/content/marketing/features.html # aio/content/tutorial/toh-pt0.md # aio/content/tutorial/toh-pt6.md # aio/package.json # aio/src/app/layout/footer/footer.component.html # aio/tools/transforms/templates/api/lib/memberHelpers.html # aio/yarn.lock # integration/cli-hello-world-ivy-minimal/src/polyfills.ts # modules/benchmarks/e2e_test/largetable_perf.ts # modules/benchmarks/e2e_test/largetable_spec.ts # packages/animations/src/animation_metadata.ts # packages/common/http/src/headers.ts # packages/common/http/src/interceptor.ts # packages/common/src/directives/ng_for_of.ts # packages/common/src/directives/ng_if.ts # packages/common/src/directives/ng_style.ts # packages/common/src/directives/ng_switch.ts # packages/core/src/change_detection/change_detection_util.ts # packages/core/src/di/injectable.ts # packages/core/src/interface/lifecycle_hooks.ts # packages/core/src/linker/template_ref.ts # packages/core/src/metadata/di.ts # packages/core/src/metadata/directives.ts # packages/core/src/metadata/ng_module.ts # packages/core/src/render/api.ts # packages/forms/src/form_providers.ts # packages/forms/src/model.ts # packages/router/src/config.ts # packages/router/src/directives/router_link.ts
2019-02-16 11:00:16 -05:00
*
*
* @publicApi
*
* @see [HTTP Guide](guide/http#intercepting-requests-and-responses)
*
Merge remote-tracking branch 'en/master' into aio # Conflicts: # .travis.yml # aio/content/guide/aot-compiler.md # aio/content/guide/architecture.md # aio/content/guide/build.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/deployment.md # aio/content/guide/elements.md # aio/content/guide/file-structure.md # aio/content/guide/glossary.md # aio/content/guide/hierarchical-dependency-injection.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/pipes.md # aio/content/guide/router.md # aio/content/guide/rx-library.md # aio/content/guide/service-worker-communications.md # aio/content/guide/service-worker-config.md # aio/content/guide/singleton-services.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/guide/typescript-configuration.md # aio/content/guide/universal.md # aio/content/marketing/docs.md # aio/content/marketing/features.html # aio/content/tutorial/toh-pt0.md # aio/content/tutorial/toh-pt6.md # aio/package.json # aio/src/app/layout/footer/footer.component.html # aio/tools/transforms/templates/api/lib/memberHelpers.html # aio/yarn.lock # integration/cli-hello-world-ivy-minimal/src/polyfills.ts # modules/benchmarks/e2e_test/largetable_perf.ts # modules/benchmarks/e2e_test/largetable_spec.ts # packages/animations/src/animation_metadata.ts # packages/common/http/src/headers.ts # packages/common/http/src/interceptor.ts # packages/common/src/directives/ng_for_of.ts # packages/common/src/directives/ng_if.ts # packages/common/src/directives/ng_style.ts # packages/common/src/directives/ng_switch.ts # packages/core/src/change_detection/change_detection_util.ts # packages/core/src/di/injectable.ts # packages/core/src/interface/lifecycle_hooks.ts # packages/core/src/linker/template_ref.ts # packages/core/src/metadata/di.ts # packages/core/src/metadata/directives.ts # packages/core/src/metadata/ng_module.ts # packages/core/src/render/api.ts # packages/forms/src/form_providers.ts # packages/forms/src/model.ts # packages/router/src/config.ts # packages/router/src/directives/router_link.ts
2019-02-16 11:00:16 -05:00
* [HTTP ](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.
*
Merge remote-tracking branch 'en/master' into aio # Conflicts: # .travis.yml # aio/content/guide/aot-compiler.md # aio/content/guide/architecture.md # aio/content/guide/build.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/deployment.md # aio/content/guide/elements.md # aio/content/guide/file-structure.md # aio/content/guide/glossary.md # aio/content/guide/hierarchical-dependency-injection.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/pipes.md # aio/content/guide/router.md # aio/content/guide/rx-library.md # aio/content/guide/service-worker-communications.md # aio/content/guide/service-worker-config.md # aio/content/guide/singleton-services.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/guide/typescript-configuration.md # aio/content/guide/universal.md # aio/content/marketing/docs.md # aio/content/marketing/features.html # aio/content/tutorial/toh-pt0.md # aio/content/tutorial/toh-pt6.md # aio/package.json # aio/src/app/layout/footer/footer.component.html # aio/tools/transforms/templates/api/lib/memberHelpers.html # aio/yarn.lock # integration/cli-hello-world-ivy-minimal/src/polyfills.ts # modules/benchmarks/e2e_test/largetable_perf.ts # modules/benchmarks/e2e_test/largetable_spec.ts # packages/animations/src/animation_metadata.ts # packages/common/http/src/headers.ts # packages/common/http/src/interceptor.ts # packages/common/src/directives/ng_for_of.ts # packages/common/src/directives/ng_if.ts # packages/common/src/directives/ng_style.ts # packages/common/src/directives/ng_switch.ts # packages/core/src/change_detection/change_detection_util.ts # packages/core/src/di/injectable.ts # packages/core/src/interface/lifecycle_hooks.ts # packages/core/src/linker/template_ref.ts # packages/core/src/metadata/di.ts # packages/core/src/metadata/directives.ts # packages/core/src/metadata/ng_module.ts # packages/core/src/render/api.ts # packages/forms/src/form_providers.ts # packages/forms/src/model.ts # packages/router/src/config.ts # packages/router/src/directives/router_link.ts
2019-02-16 11:00:16 -05:00
* 使 `HttpInterceptors` `AppModule` `HttpClientModule`
* `HttpClientModule` `HttpClientModule`
*
*/
export interface HttpInterceptor {
/**
* Identifies and handles a given HTTP request.
2021-02-04 06:45:54 -05:00
*
* HTTP
*
* @param req The outgoing request object to handle.
2021-02-04 06:45:54 -05: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
*
*
*
* @returns An observable of the event stream.
2021-02-04 06:45:54 -05:00
*
*
*/
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>;
}
/**
* `HttpHandler` which applies an `HttpInterceptor` to an `HttpRequest`.
*
* `HttpHandler` `HttpInterceptor` `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.
*
*
2021-02-04 06:45:54 -05:00
* multi-provider `HttpInterceptor`
*
* @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);
}
}