-
Couldn't load subscription status.
- Fork 26.7k
feat(http): Use the Fetch backend by default #58212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5f93ec5
670ce2d
e07973a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 {FetchBackend} from './fetch'; | ||
|
|
||
| /** | ||
| * A constant defining the default the default Http Backend. | ||
| * Extracted to a separate file to facilitate G3 patches. | ||
| */ | ||
| export const NG_DEFAULT_HTTP_BACKEND = FetchBackend; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ import { | |
| XSRF_HEADER_NAME, | ||
| xsrfInterceptorFn, | ||
| } from './xsrf'; | ||
| import {NG_DEFAULT_HTTP_BACKEND} from './backend-default-value'; | ||
|
|
||
| /** | ||
| * Identifies a particular kind of `HttpFeature`. | ||
|
|
@@ -52,6 +53,7 @@ export enum HttpFeatureKind { | |
| JsonpSupport, | ||
| RequestsMadeViaParent, | ||
| Fetch, | ||
| Xhr, | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -101,7 +103,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>[] | ||
|
|
@@ -122,13 +124,13 @@ export function provideHttpClient( | |
|
|
||
| const providers: Provider[] = [ | ||
| HttpClient, | ||
| HttpXhrBackend, | ||
| 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); | ||
| }, | ||
| }, | ||
| { | ||
|
|
@@ -301,6 +303,7 @@ export function withRequestsMadeViaParent(): HttpFeature<HttpFeatureKind.Request | |
| * Note: The Fetch API doesn't support progress report on uploads. | ||
| * | ||
| * @publicApi | ||
| * @deprecated `withFetch` is not required anymore. `FetchBackend` is the default `HttpBackend`. | ||
| */ | ||
| export function withFetch(): HttpFeature<HttpFeatureKind.Fetch> { | ||
| return makeHttpFeature(HttpFeatureKind.Fetch, [ | ||
|
|
@@ -309,3 +312,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}, | ||
| ]); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| load("//tools:defaults.bzl", "ts_library") | ||
|
|
||
| package( | ||
| default_visibility = [ | ||
| "//packages/core/schematics:__pkg__", | ||
| "//packages/core/schematics/migrations/google3:__pkg__", | ||
| "//packages/core/schematics/test:__pkg__", | ||
| ], | ||
| ) | ||
|
|
||
| ts_library( | ||
| name = "http-xhr-backend", | ||
| srcs = glob(["**/*.ts"]), | ||
| tsconfig = "//packages/core/schematics:tsconfig.json", | ||
| deps = [ | ||
| "//packages/core/schematics/utils", | ||
| "@npm//@angular-devkit/schematics", | ||
| "@npm//@types/node", | ||
| "@npm//typescript", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * @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 {Rule, SchematicsException, Tree, UpdateRecorder} from '@angular-devkit/schematics'; | ||
| import {relative} from 'path'; | ||
| import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; | ||
| import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host'; | ||
| import {migrateFile} from './migration'; | ||
|
|
||
| export function migrate(): Rule { | ||
| return async (tree: Tree) => { | ||
| const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree); | ||
| const basePath = process.cwd(); | ||
| const allPaths = [...buildPaths, ...testPaths]; | ||
|
|
||
| if (!allPaths.length) { | ||
| throw new SchematicsException( | ||
| 'Could not find any tsconfig file. Cannot run the HttpBackend migration.', | ||
| ); | ||
| } | ||
|
|
||
| for (const tsconfigPath of allPaths) { | ||
| runMigration(tree, tsconfigPath, basePath); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function runMigration(tree: Tree, tsconfigPath: string, basePath: string) { | ||
| const program = createMigrationProgram(tree, tsconfigPath, basePath); | ||
| const sourceFiles = program | ||
| .getSourceFiles() | ||
| .filter((sourceFile) => canMigrateFile(basePath, sourceFile, program)); | ||
|
|
||
| for (const sourceFile of sourceFiles) { | ||
| let update: UpdateRecorder | null = null; | ||
|
|
||
| const rewriter = (startPos: number, width: number, text: string | null) => { | ||
| if (update === null) { | ||
| // Lazily initialize update, because most files will not require migration. | ||
| update = tree.beginUpdate(relative(basePath, sourceFile.fileName)); | ||
| } | ||
| update.remove(startPos, width); | ||
| if (text !== null) { | ||
| update.insertLeft(startPos, text); | ||
| } | ||
| }; | ||
| migrateFile(sourceFile, rewriter); | ||
|
|
||
| if (update !== null) { | ||
| tree.commitUpdate(update); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.