Skip to content

Commit 0ea4370

Browse files
crisbetommalerba
authored andcommitted
refactor: switch to HttpClient (#6702)
Refactors all of the usages of the deprecated `Http` by replacing them with the `HttpClient`. BREAKING CHANGE: Material now requires `@angular/common` instead of `@angular/http` and uses `HttpClient` instead of `Http`.
1 parent 65d3630 commit 0ea4370

File tree

17 files changed

+121
-157
lines changed

17 files changed

+121
-157
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"@angular/compiler": "~4.4.4",
3131
"@angular/core": "~4.4.4",
3232
"@angular/forms": "~4.4.4",
33-
"@angular/http": "~4.4.4",
3433
"@angular/platform-browser": "~4.4.4",
3534
"core-js": "^2.4.1",
3635
"rxjs": "^5.0.1",
@@ -45,6 +44,7 @@
4544
"@angular/platform-server": "~4.4.4",
4645
"@angular/router": "~4.4.4",
4746
"@angular/tsc-wrapped": "~4.4.4",
47+
"@angular/http": "~4.4.4",
4848
"@google-cloud/storage": "^1.1.1",
4949
"@types/chalk": "^0.4.31",
5050
"@types/fs-extra": "^3.0.1",

scripts/closure-compiler/build-devapp-bundle.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ OPTS=(
4242
"--js_module_root=dist/releases/material-moment-adapter"
4343
"--js_module_root=node_modules/@angular/core"
4444
"--js_module_root=node_modules/@angular/common"
45+
"--js_module_root=node_modules/@angular/common/http"
4546
"--js_module_root=node_modules/@angular/compiler"
4647
"--js_module_root=node_modules/@angular/forms"
47-
"--js_module_root=node_modules/@angular/http"
4848
"--js_module_root=node_modules/@angular/router"
4949
"--js_module_root=node_modules/@angular/platform-browser"
5050
"--js_module_root=node_modules/@angular/platform-browser/animations"
@@ -65,9 +65,9 @@ OPTS=(
6565
# Include all Angular FESM bundles.
6666
node_modules/@angular/core/@angular/core.js
6767
node_modules/@angular/common/@angular/common.js
68+
node_modules/@angular/common/@angular/common/http.js
6869
node_modules/@angular/compiler/@angular/compiler.js
6970
node_modules/@angular/forms/@angular/forms.js
70-
node_modules/@angular/http/@angular/http.js
7171
node_modules/@angular/router/@angular/router.js
7272
node_modules/@angular/platform-browser/@angular/platform-browser.js
7373
node_modules/@angular/platform-browser/@angular/platform-browser/animations.js

src/demo-app/demo-app-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ApplicationRef, NgModule} from '@angular/core';
22
import {BrowserModule} from '@angular/platform-browser';
3-
import {HttpModule} from '@angular/http';
3+
import {HttpClientModule} from '@angular/common/http';
44
import {RouterModule} from '@angular/router';
55
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
66
import {ALL_ROUTES} from './demo-app/routes';
@@ -13,7 +13,7 @@ import {AccessibilityDemoModule} from './a11y/a11y-module';
1313
imports: [
1414
BrowserModule,
1515
BrowserAnimationsModule,
16-
HttpModule,
16+
HttpClientModule,
1717
DemoModule,
1818
AccessibilityDemoModule,
1919
RouterModule.forRoot(ALL_ROUTES),

src/demo-app/system-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ System.config({
99
map: {
1010
'rxjs': 'node:rxjs',
1111
'main': 'main.js',
12+
'tslib': 'node:tslib/tslib.js',
1213
'moment': 'node:moment/min/moment-with-locales.min.js',
1314

1415
// Angular specific mappings.
1516
'@angular/core': 'node:@angular/core/bundles/core.umd.js',
1617
'@angular/common': 'node:@angular/common/bundles/common.umd.js',
18+
'@angular/common/http': 'node:@angular/common/bundles/common-http.umd.js',
1719
'@angular/compiler': 'node:@angular/compiler/bundles/compiler.umd.js',
18-
'@angular/http': 'node:@angular/http/bundles/http.umd.js',
1920
'@angular/forms': 'node:@angular/forms/bundles/forms.umd.js',
2021
'@angular/animations': 'node:@angular/animations/bundles/animations.umd.js',
2122
'@angular/router': 'node:@angular/router/bundles/router.umd.js',

src/e2e-app/system-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ System.config({
1010
'rxjs': 'node:rxjs',
1111
'main': 'main.js',
1212
'moment': 'node:moment/min/moment-with-locales.min.js',
13+
'tslib': 'node:tslib/tslib.js',
1314

1415
// Angular specific mappings.
1516
'@angular/core': 'node:@angular/core/bundles/core.umd.js',
1617
'@angular/common': 'node:@angular/common/bundles/common.umd.js',
18+
'@angular/common/http': 'node:@angular/common/bundles/common-http.umd.js',
1719
'@angular/compiler': 'node:@angular/compiler/bundles/compiler.umd.js',
18-
'@angular/http': 'node:@angular/http/bundles/http.umd.js',
1920
'@angular/forms': 'node:@angular/forms/bundles/forms.umd.js',
2021
'@angular/router': 'node:@angular/router/bundles/router.umd.js',
2122
'@angular/animations': 'node:@angular/animations/bundles/animations.umd.js',

src/lib/icon/fake-svgs.ts

Lines changed: 34 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,41 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Response, ResponseOptions} from '@angular/http';
10-
119
/**
1210
* Fake URLs and associated SVG documents used by tests.
1311
* @docs-private
1412
*/
15-
const FAKE_SVGS = (() => {
16-
const svgs = new Map<string, string>();
17-
svgs.set('cat.svg',
18-
'<svg><path id="meow"></path></svg>');
19-
20-
svgs.set('dog.svg',
21-
'<svg><path id="woof"></path></svg>');
22-
23-
svgs.set('farm-set-1.svg', `
24-
<svg>
25-
<defs>
26-
<g id="pig"><path id="oink"></path></g>
27-
<g id="cow"><path id="moo"></path></g>
28-
</defs>
29-
</svg>
30-
`);
31-
32-
svgs.set('farm-set-2.svg', `
33-
<svg>
34-
<defs>
35-
<g id="cow"><path id="moo moo"></path></g>
36-
<g id="sheep"><path id="baa"></path></g>
37-
</defs>
38-
</svg>
39-
`);
40-
41-
svgs.set('farm-set-3.svg', `
42-
<svg>
43-
<symbol id="duck">
44-
<path id="quack"></path>
45-
</symbol>
46-
</svg>
47-
`);
48-
49-
svgs.set('arrow-set.svg', `
50-
<svg>
51-
<defs>
52-
<svg id="left-arrow"><path id="left"></path></svg>
53-
<svg id="right-arrow"><path id="right"></path></svg>
54-
</defs>
55-
</svg>
56-
`);
57-
58-
return svgs;
59-
})();
60-
61-
/**
62-
* Returns an HTTP response for a fake SVG URL.
63-
* @docs-private
64-
*/
65-
export function getFakeSvgHttpResponse(url: string) {
66-
if (FAKE_SVGS.has(url)) {
67-
return new Response(new ResponseOptions({
68-
status: 200,
69-
body: FAKE_SVGS.get(url),
70-
}));
71-
} else {
72-
return new Response(new ResponseOptions({status: 404}));
73-
}
74-
}
13+
export const FAKE_SVGS = {
14+
cat: '<svg><path id="meow"></path></svg>',
15+
dog: '<svg><path id="woof"></path></svg>',
16+
farmSet1: `
17+
<svg>
18+
<defs>
19+
<g id="pig"><path id="oink"></path></g>
20+
<g id="cow"><path id="moo"></path></g>
21+
</defs>
22+
</svg>
23+
`,
24+
farmSet2: `
25+
<svg>
26+
<defs>
27+
<g id="cow"><path id="moo moo"></path></g>
28+
<g id="sheep"><path id="baa"></path></g>
29+
</defs>
30+
</svg>
31+
`,
32+
farmSet3: `
33+
<svg>
34+
<symbol id="duck">
35+
<path id="quack"></path>
36+
</symbol>
37+
</svg>
38+
`,
39+
arrows: `
40+
<svg>
41+
<defs>
42+
<svg id="left-arrow"><path id="left"></path></svg>
43+
<svg id="right-arrow"><path id="right"></path></svg>
44+
</defs>
45+
</svg> `
46+
};

src/lib/icon/icon-registry.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {catchOperator, doOperator, finallyOperator, map, RxChain, share} from '@angular/cdk/rxjs';
1010
import {Injectable, Optional, SecurityContext, SkipSelf} from '@angular/core';
11-
import {Http} from '@angular/http';
11+
import {HttpClient} from '@angular/common/http';
1212
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
1313
import {Observable} from 'rxjs/Observable';
1414
import {forkJoin} from 'rxjs/observable/forkJoin';
@@ -32,8 +32,9 @@ export function getMatIconNameNotFoundError(iconName: string): Error {
3232
* @docs-private
3333
*/
3434
export function getMatIconNoHttpProviderError(): Error {
35-
return Error('Could not find Http provider for use with Angular Material icons. ' +
36-
'Please include the HttpModule from @angular/http in your app imports.');
35+
return Error('Could not find HttpClient provider for use with Angular Material icons. ' +
36+
'Please include the HttpClientModule from @angular/common/http in your ' +
37+
'app imports.');
3738
}
3839

3940

@@ -92,7 +93,7 @@ export class MatIconRegistry {
9293
*/
9394
private _defaultFontSetClass = 'material-icons';
9495

95-
constructor(@Optional() private _http: Http, private _sanitizer: DomSanitizer) {}
96+
constructor(@Optional() private _httpClient: HttpClient, private _sanitizer: DomSanitizer) {}
9697

9798
/**
9899
* Registers an icon by URL in the default namespace.
@@ -442,7 +443,7 @@ export class MatIconRegistry {
442443
* cached, so future calls with the same URL may not cause another HTTP request.
443444
*/
444445
private _fetchUrl(safeUrl: SafeResourceUrl): Observable<string> {
445-
if (!this._http) {
446+
if (!this._httpClient) {
446447
throw getMatIconNoHttpProviderError();
447448
}
448449

@@ -463,8 +464,7 @@ export class MatIconRegistry {
463464

464465
// TODO(jelbourn): for some reason, the `finally` operator "loses" the generic type on the
465466
// Observable. Figure out why and fix it.
466-
const req = RxChain.from(this._http.get(url))
467-
.call(map, response => response.text())
467+
const req = RxChain.from(this._httpClient.get(url, { responseType: 'text' }))
468468
.call(finallyOperator, () => this._inProgressUrlFetches.delete(url))
469469
.call(share)
470470
.result();
@@ -476,15 +476,19 @@ export class MatIconRegistry {
476476

477477
/** @docs-private */
478478
export function ICON_REGISTRY_PROVIDER_FACTORY(
479-
parentRegistry: MatIconRegistry, http: Http, sanitizer: DomSanitizer) {
480-
return parentRegistry || new MatIconRegistry(http, sanitizer);
479+
parentRegistry: MatIconRegistry, httpClient: HttpClient, sanitizer: DomSanitizer) {
480+
return parentRegistry || new MatIconRegistry(httpClient, sanitizer);
481481
}
482482

483483
/** @docs-private */
484484
export const ICON_REGISTRY_PROVIDER = {
485-
// If there is already an MatIconRegistry available, use that. Otherwise, provide a new one.
485+
// If there is already an MdIconRegistry available, use that. Otherwise, provide a new one.
486486
provide: MatIconRegistry,
487-
deps: [[new Optional(), new SkipSelf(), MatIconRegistry], [new Optional(), Http], DomSanitizer],
487+
deps: [
488+
[new Optional(), new SkipSelf(), MatIconRegistry],
489+
[new Optional(), HttpClient],
490+
DomSanitizer
491+
],
488492
useFactory: ICON_REGISTRY_PROVIDER_FACTORY
489493
};
490494

0 commit comments

Comments
 (0)