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:
Matthieu Riegler 2026-02-23 22:09:33 +01:00 committed by Jessica Janiuk
parent bdb6ae9dbc
commit f30ed6bbf6
4 changed files with 42 additions and 3 deletions

View file

@ -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;

View file

@ -35,6 +35,7 @@ export {
HttpFeatureKind,
provideHttpClient,
withFetch,
withXhr,
withInterceptors,
withInterceptorsFromDi,
withJsonpSupport,

View 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;

View file

@ -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},
]);
}