Skip to content

Commit 12b3a1a

Browse files
JeanMechepkozlowski-opensource
authored andcommitted
refactor(migrations): keep the http-xhr-backend migration idempotent
Our migrations should be idempotent in case they are run multiple times.
1 parent d1c1bc3 commit 12b3a1a

2 files changed

Lines changed: 8 additions & 41 deletions

File tree

packages/core/schematics/migrations/http-xhr-backend/http-xhr-backend.spec.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('http fetch backend migration', () => {
5959
expect(actual).toMatch(/import \{.*withXhr.*\}/);
6060
});
6161

62-
it('should update provideHttpClient to remove withFetch', async () => {
62+
it('should not update provideHttpClient to remove withFetch', async () => {
6363
const {fs} = await runTsurgeMigration(new XhrBackendMigration(), [
6464
{
6565
name: absoluteFrom('/index.ts'),
@@ -77,30 +77,9 @@ describe('http fetch backend migration', () => {
7777

7878
const actual = fs.readFile(absoluteFrom('/index.ts'));
7979
expect(actual).toContain(
80-
'provideHttpClient(withInterceptorsFromDi(), withXsrfConfiguration({}))',
80+
'provideHttpClient(withFetch(), withInterceptorsFromDi(), withXsrfConfiguration({}))',
8181
);
82-
expect(actual).not.toContain('withFetch');
83-
});
84-
85-
it('should update provideHttpClient to remove withFetch as only arg', async () => {
86-
const {fs} = await runTsurgeMigration(new XhrBackendMigration(), [
87-
{
88-
name: absoluteFrom('/index.ts'),
89-
isProgramRootFile: true,
90-
contents: `
91-
import {AppConfig} from '@angular/core';
92-
import {provideHttpClient, withFetch, withInterceptorsFromDi, withXsrfConfiguration} from '@angular/common/http';
93-
94-
const config: AppConfig = [
95-
provideHttpClient(withFetch()),
96-
]
97-
`,
98-
},
99-
]);
100-
101-
const actual = fs.readFile(absoluteFrom('/index.ts'));
102-
expect(actual).toContain('provideHttpClient()');
103-
expect(actual).not.toContain('withFetch');
82+
expect(actual).toContain('withFetch');
10483
});
10584

10685
it('should not update provideHttpClient if withXhr is already present', async () => {

packages/core/schematics/migrations/http-xhr-backend/migration.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ const provideHttpClient = 'provideHttpClient';
2626

2727
const WITH_FETCH = 'withFetch';
2828
const WITH_XHR = 'withXhr';
29-
const CORE_PACKAGE = '@angular/core';
3029
const HTTP_PACKAGE = '@angular/common/http';
31-
const PROVIDE_HTTP_CLIENT = 'provideHttpClient';
3230

3331
export interface CompilationUnitData {
3432
replacements: Replacement[];
@@ -41,8 +39,11 @@ export interface MigrationConfig {
4139
shouldMigrate?: (containingFile: ProjectFile) => boolean;
4240
}
4341

44-
const provideHttpClientIdentifier = ts.factory.createIdentifier('provideHttpClient');
45-
42+
/**
43+
* Prior to v22, provideHttpClient() had a Xhr backend by default. In v22, the default was switched to a Fetch backend.
44+
* This migration adds the withXhr() option to any provideHttpClient() calls that do not already have either withFetch() or withXhr() specified,
45+
* to preserve the Xhr backend behavior.
46+
*/
4647
export class XhrBackendMigration extends TsurgeFunnelMigration<
4748
CompilationUnitData,
4849
CompilationUnitData
@@ -107,19 +108,6 @@ export class XhrBackendMigration extends TsurgeFunnelMigration<
107108
exportSymbolName: WITH_XHR,
108109
requestedFile: sourceFile,
109110
});
110-
} else if (withFetchNode) {
111-
const isLastArg = node.arguments[node.arguments.length - 1] === withFetchNode;
112-
replacements.push(
113-
new Replacement(
114-
projectFile(sourceFile, info),
115-
new TextUpdate({
116-
position: withFetchNode.getStart(),
117-
end: isLastArg ? withFetchNode.getEnd() : withFetchNode.getEnd() + 2, // +2 to remove the comma and space, could be improved
118-
toInsert: '',
119-
}),
120-
),
121-
);
122-
importManager.removeImport(sourceFile, 'withFetch', HTTP_PACKAGE);
123111
}
124112
};
125113
sourceFile.forEachChild(walk);

0 commit comments

Comments
 (0)