mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(http): convert NgModules to use provideHttpClient internally (#47502)
This commit converts `HttpClientModule` to use `provideHttpClient()` internally, with a particular configuration of features. Other NgModules related to configuring `HttpClient` are also converted to use the providers directly from various features, to ensure consistency of behavior. PR Close #47502
This commit is contained in:
parent
e47b129070
commit
fc69c8021c
4 changed files with 22 additions and 41 deletions
|
|
@ -1654,7 +1654,7 @@ export class HttpClientModule {
|
|||
// (undocumented)
|
||||
static ɵinj: i0.ɵɵInjectorDeclaration<HttpClientModule>;
|
||||
// (undocumented)
|
||||
static ɵmod: i0.ɵɵNgModuleDeclaration<HttpClientModule, never, [typeof HttpClientXsrfModule], never>;
|
||||
static ɵmod: i0.ɵɵNgModuleDeclaration<HttpClientModule, never, never, never>;
|
||||
}
|
||||
|
||||
// @public
|
||||
|
|
|
|||
|
|
@ -170,8 +170,6 @@ export function legacyInterceptorFnFactory(): HttpInterceptorFn {
|
|||
};
|
||||
}
|
||||
|
||||
export const LEGACY_INTERCEPTOR_FN = new InjectionToken<HttpInterceptorFn>('LEGACY_INTERCEPTOR_FN');
|
||||
|
||||
@Injectable()
|
||||
export class HttpInterceptorHandler extends HttpHandler {
|
||||
private chain: ChainedInterceptorFn<unknown>|null = null;
|
||||
|
|
|
|||
|
|
@ -8,12 +8,9 @@
|
|||
|
||||
import {ModuleWithProviders, NgModule} from '@angular/core';
|
||||
|
||||
import {HttpBackend, HttpHandler} from './backend';
|
||||
import {HttpClient} from './client';
|
||||
import {HTTP_INTERCEPTOR_FNS, HTTP_INTERCEPTORS, HttpInterceptorHandler, LEGACY_INTERCEPTOR_FN, legacyInterceptorFnFactory} from './interceptor';
|
||||
import {jsonpCallbackContext, JsonpCallbackContext, JsonpClientBackend, jsonpInterceptorFn} from './jsonp';
|
||||
import {HttpXhrBackend} from './xhr';
|
||||
import {HttpXsrfCookieExtractor, HttpXsrfInterceptor, HttpXsrfTokenExtractor, XSRF_COOKIE_NAME, XSRF_DEFAULT_COOKIE_NAME, XSRF_DEFAULT_HEADER_NAME, XSRF_ENABLED, XSRF_HEADER_NAME} from './xsrf';
|
||||
import {HTTP_INTERCEPTORS} from './interceptor';
|
||||
import {provideHttpClient, withJsonpSupport, withLegacyInterceptors, withNoXsrfProtection, withXsrfConfiguration} from './provider';
|
||||
import {HttpXsrfCookieExtractor, HttpXsrfInterceptor, HttpXsrfTokenExtractor, XSRF_DEFAULT_COOKIE_NAME, XSRF_DEFAULT_HEADER_NAME, XSRF_ENABLED} from './xsrf';
|
||||
|
||||
/**
|
||||
* Configures XSRF protection support for outgoing requests.
|
||||
|
|
@ -32,8 +29,10 @@ import {HttpXsrfCookieExtractor, HttpXsrfInterceptor, HttpXsrfTokenExtractor, XS
|
|||
HttpXsrfInterceptor,
|
||||
{provide: HTTP_INTERCEPTORS, useExisting: HttpXsrfInterceptor, multi: true},
|
||||
{provide: HttpXsrfTokenExtractor, useClass: HttpXsrfCookieExtractor},
|
||||
{provide: XSRF_COOKIE_NAME, useValue: XSRF_DEFAULT_COOKIE_NAME},
|
||||
{provide: XSRF_HEADER_NAME, useValue: XSRF_DEFAULT_HEADER_NAME},
|
||||
withXsrfConfiguration({
|
||||
cookieName: XSRF_DEFAULT_COOKIE_NAME,
|
||||
headerName: XSRF_DEFAULT_HEADER_NAME,
|
||||
}).ɵproviders,
|
||||
{provide: XSRF_ENABLED, useValue: true},
|
||||
],
|
||||
})
|
||||
|
|
@ -45,7 +44,7 @@ export class HttpClientXsrfModule {
|
|||
return {
|
||||
ngModule: HttpClientXsrfModule,
|
||||
providers: [
|
||||
{provide: XSRF_ENABLED, useValue: false},
|
||||
withNoXsrfProtection().ɵproviders,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
|
@ -64,10 +63,7 @@ export class HttpClientXsrfModule {
|
|||
} = {}): ModuleWithProviders<HttpClientXsrfModule> {
|
||||
return {
|
||||
ngModule: HttpClientXsrfModule,
|
||||
providers: [
|
||||
options.cookieName ? {provide: XSRF_COOKIE_NAME, useValue: options.cookieName} : [],
|
||||
options.headerName ? {provide: XSRF_HEADER_NAME, useValue: options.headerName} : [],
|
||||
],
|
||||
providers: withXsrfConfiguration(options).ɵproviders,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -82,31 +78,18 @@ export class HttpClientXsrfModule {
|
|||
* @publicApi
|
||||
*/
|
||||
@NgModule({
|
||||
/**
|
||||
* Optional configuration for XSRF protection.
|
||||
*/
|
||||
imports: [
|
||||
HttpClientXsrfModule.withOptions({
|
||||
cookieName: XSRF_DEFAULT_COOKIE_NAME,
|
||||
headerName: XSRF_DEFAULT_HEADER_NAME,
|
||||
}),
|
||||
],
|
||||
/**
|
||||
* Configures the [dependency injector](guide/glossary#injector) where it is imported
|
||||
* with supporting services for HTTP communications.
|
||||
*/
|
||||
providers: [
|
||||
HttpClient,
|
||||
HttpXhrBackend,
|
||||
HttpInterceptorHandler,
|
||||
{provide: HttpHandler, useExisting: HttpInterceptorHandler},
|
||||
{provide: HttpBackend, useExisting: HttpXhrBackend},
|
||||
{provide: LEGACY_INTERCEPTOR_FN, useFactory: legacyInterceptorFnFactory},
|
||||
{
|
||||
provide: HTTP_INTERCEPTOR_FNS,
|
||||
useExisting: LEGACY_INTERCEPTOR_FN,
|
||||
multi: true,
|
||||
},
|
||||
provideHttpClient(
|
||||
withLegacyInterceptors(),
|
||||
withXsrfConfiguration({
|
||||
cookieName: XSRF_DEFAULT_COOKIE_NAME,
|
||||
headerName: XSRF_DEFAULT_HEADER_NAME,
|
||||
}),
|
||||
),
|
||||
],
|
||||
})
|
||||
export class HttpClientModule {
|
||||
|
|
@ -125,9 +108,7 @@ export class HttpClientModule {
|
|||
*/
|
||||
@NgModule({
|
||||
providers: [
|
||||
JsonpClientBackend,
|
||||
{provide: JsonpCallbackContext, useFactory: jsonpCallbackContext},
|
||||
{provide: HTTP_INTERCEPTOR_FNS, useValue: jsonpInterceptorFn, multi: true},
|
||||
withJsonpSupport().ɵproviders,
|
||||
],
|
||||
})
|
||||
export class HttpClientJsonpModule {
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@
|
|||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Provider} from '@angular/core';
|
||||
import {InjectionToken, Provider} from '@angular/core';
|
||||
|
||||
import {HttpBackend, HttpHandler} from './backend';
|
||||
import {HttpClient} from './client';
|
||||
import {HTTP_INTERCEPTOR_FNS, HttpInterceptorHandler, LEGACY_INTERCEPTOR_FN, legacyInterceptorFnFactory} from './interceptor';
|
||||
import {HTTP_INTERCEPTOR_FNS, HttpInterceptorFn, HttpInterceptorHandler, legacyInterceptorFnFactory} from './interceptor';
|
||||
import {JsonpCallbackContext, jsonpCallbackContext, JsonpClientBackend, jsonpInterceptorFn} from './jsonp';
|
||||
import {HttpXhrBackend} from './xhr';
|
||||
import {HttpXsrfCookieExtractor, HttpXsrfTokenExtractor, XSRF_COOKIE_NAME, XSRF_ENABLED, XSRF_HEADER_NAME, xsrfInterceptorFn} from './xsrf';
|
||||
|
|
@ -70,6 +70,8 @@ export function provideHttpClient(...features: HttpFeature<HttpFeatureKind>[]):
|
|||
return providers;
|
||||
}
|
||||
|
||||
const LEGACY_INTERCEPTOR_FN = new InjectionToken<HttpInterceptorFn>('LEGACY_INTERCEPTOR_FN');
|
||||
|
||||
export function withLegacyInterceptors(): HttpFeature<HttpFeatureKind.LegacyInterceptors> {
|
||||
// Note: the legacy interceptor function is provided here via an intermediate token
|
||||
// (`LEGACY_INTERCEPTOR_FN`), using a pattern which guarantees that if these providers are
|
||||
|
|
|
|||
Loading…
Reference in a new issue