From f30ed6bbf6706790437a6e4dbfffed3fa8708f57 Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Mon, 23 Feb 2026 22:09:33 +0100 Subject: [PATCH] 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`. --- goldens/public-api/common/http/index.api.md | 7 +++++- packages/common/http/public_api.ts | 1 + .../common/http/src/backend-default-value.ts | 15 +++++++++++++ packages/common/http/src/provider.ts | 22 +++++++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 packages/common/http/src/backend-default-value.ts diff --git a/goldens/public-api/common/http/index.api.md b/goldens/public-api/common/http/index.api.md index 1dd9586fe65..2ab59d6e07b 100644 --- a/goldens/public-api/common/http/index.api.md +++ b/goldens/public-api/common/http/index.api.md @@ -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; +// @public +export function withXhr(): HttpFeature; + // @public export function withXsrfConfiguration({ cookieName, headerName, }: { cookieName?: string; diff --git a/packages/common/http/public_api.ts b/packages/common/http/public_api.ts index fd38fda6382..62a421579a0 100644 --- a/packages/common/http/public_api.ts +++ b/packages/common/http/public_api.ts @@ -35,6 +35,7 @@ export { HttpFeatureKind, provideHttpClient, withFetch, + withXhr, withInterceptors, withInterceptorsFromDi, withJsonpSupport, diff --git a/packages/common/http/src/backend-default-value.ts b/packages/common/http/src/backend-default-value.ts new file mode 100644 index 00000000000..edfee871a71 --- /dev/null +++ b/packages/common/http/src/backend-default-value.ts @@ -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; diff --git a/packages/common/http/src/provider.ts b/packages/common/http/src/provider.ts index dfee321420d..234a0232b5b 100644 --- a/packages/common/http/src/provider.ts +++ b/packages/common/http/src/provider.ts @@ -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( * @see {@link withNoXsrfProtection} * @see {@link withJsonpSupport} * @see {@link withRequestsMadeViaParent} - * @see {@link withFetch} + * @see {@link withXhr} */ export function provideHttpClient( ...features: HttpFeature[] @@ -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 { {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 { + return makeHttpFeature(HttpFeatureKind.Xhr, [ + HttpXhrBackend, + {provide: HttpBackend, useExisting: HttpXhrBackend}, + ]); +}