mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(http): expose withXhr to prepare for fetch by default
This commit sets up the necessary changes that would allow us to safely migrate G3 before switch to the `FetchBackend` by default. For now the `HttpXhrBackend` is still the default backend for the `HttpClient`.
This commit is contained in:
parent
bdb6ae9dbc
commit
f30ed6bbf6
4 changed files with 42 additions and 3 deletions
|
|
@ -2680,7 +2680,9 @@ export enum HttpFeatureKind {
|
|||
// (undocumented)
|
||||
NoXsrfProtection = 3,
|
||||
// (undocumented)
|
||||
RequestsMadeViaParent = 5
|
||||
RequestsMadeViaParent = 5,
|
||||
// (undocumented)
|
||||
Xhr = 7
|
||||
}
|
||||
|
||||
// @public
|
||||
|
|
@ -3347,6 +3349,9 @@ export function withNoXsrfProtection(): HttpFeature<HttpFeatureKind.NoXsrfProtec
|
|||
// @public
|
||||
export function withRequestsMadeViaParent(): HttpFeature<HttpFeatureKind.RequestsMadeViaParent>;
|
||||
|
||||
// @public
|
||||
export function withXhr(): HttpFeature<HttpFeatureKind.Xhr>;
|
||||
|
||||
// @public
|
||||
export function withXsrfConfiguration({ cookieName, headerName, }: {
|
||||
cookieName?: string;
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ export {
|
|||
HttpFeatureKind,
|
||||
provideHttpClient,
|
||||
withFetch,
|
||||
withXhr,
|
||||
withInterceptors,
|
||||
withInterceptorsFromDi,
|
||||
withJsonpSupport,
|
||||
|
|
|
|||
15
packages/common/http/src/backend-default-value.ts
Normal file
15
packages/common/http/src/backend-default-value.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/**
|
||||
* @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 {HttpXhrBackend} from './xhr';
|
||||
|
||||
/**
|
||||
* A constant defining the default the default Http Backend.
|
||||
* Extracted to a separate file to facilitate G3 patches.
|
||||
*/
|
||||
export const NG_DEFAULT_HTTP_BACKEND = HttpXhrBackend;
|
||||
|
|
@ -15,6 +15,7 @@ import {
|
|||
} from '@angular/core';
|
||||
|
||||
import {HttpBackend, HttpHandler, HttpInterceptorHandler} from './backend';
|
||||
import {NG_DEFAULT_HTTP_BACKEND} from './backend-default-value';
|
||||
import {HttpClient} from './client';
|
||||
import {FETCH_BACKEND, FetchBackend} from './fetch';
|
||||
import {HTTP_INTERCEPTOR_FNS, HttpInterceptorFn, legacyInterceptorFnFactory} from './interceptor';
|
||||
|
|
@ -40,6 +41,7 @@ export enum HttpFeatureKind {
|
|||
JsonpSupport,
|
||||
RequestsMadeViaParent,
|
||||
Fetch,
|
||||
Xhr,
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -89,7 +91,7 @@ function makeHttpFeature<KindT extends HttpFeatureKind>(
|
|||
* @see {@link withNoXsrfProtection}
|
||||
* @see {@link withJsonpSupport}
|
||||
* @see {@link withRequestsMadeViaParent}
|
||||
* @see {@link withFetch}
|
||||
* @see {@link withXhr}
|
||||
*/
|
||||
export function provideHttpClient(
|
||||
...features: HttpFeature<HttpFeatureKind>[]
|
||||
|
|
@ -110,12 +112,13 @@ export function provideHttpClient(
|
|||
|
||||
const providers: Provider[] = [
|
||||
HttpClient,
|
||||
NG_DEFAULT_HTTP_BACKEND,
|
||||
HttpInterceptorHandler,
|
||||
{provide: HttpHandler, useExisting: HttpInterceptorHandler},
|
||||
{
|
||||
provide: HttpBackend,
|
||||
useFactory: () => {
|
||||
return inject(FETCH_BACKEND, {optional: true}) ?? inject(HttpXhrBackend);
|
||||
return inject(FETCH_BACKEND, {optional: true}) ?? inject(NG_DEFAULT_HTTP_BACKEND);
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -297,3 +300,18 @@ export function withFetch(): HttpFeature<HttpFeatureKind.Fetch> {
|
|||
{provide: HttpBackend, useExisting: FetchBackend},
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the current `HttpClient` instance to make requests using the Xhr API.
|
||||
*
|
||||
* Use this feature if you want to report progress on uploads as the Xhr API supports it.
|
||||
*
|
||||
* @see {@link provideHttpClient}
|
||||
* @publicApi
|
||||
*/
|
||||
export function withXhr(): HttpFeature<HttpFeatureKind.Xhr> {
|
||||
return makeHttpFeature(HttpFeatureKind.Xhr, [
|
||||
HttpXhrBackend,
|
||||
{provide: HttpBackend, useExisting: HttpXhrBackend},
|
||||
]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue