Skip to content

Commit 7b57d75

Browse files
committed
refactor: switch to HttpClient
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 dcfe515 commit 7b57d75

File tree

17 files changed

+119
-155
lines changed

17 files changed

+119
-155
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.3.6",
3131
"@angular/core": "~4.3.6",
3232
"@angular/forms": "~4.3.6",
33-
"@angular/http": "~4.3.6",
3433
"@angular/platform-browser": "~4.3.6",
3534
"core-js": "^2.4.1",
3635
"rxjs": "^5.0.1",
@@ -41,6 +40,7 @@
4140
},
4241
"devDependencies": {
4342
"@angular/compiler-cli": "~4.3.6",
43+
"@angular/http": "~4.3.6",
4444
"@angular/platform-browser-dynamic": "~4.3.6",
4545
"@angular/platform-server": "~4.3.6",
4646
"@angular/router": "~4.3.6",

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/router': 'node:@angular/router/bundles/router.umd.js',
2122
'@angular/animations': 'node:@angular/animations/bundles/animations.umd.js',

src/e2e-app/system-config.ts

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

1314
// Angular specific mappings.
1415
'@angular/core': 'node:@angular/core/bundles/core.umd.js',
1516
'@angular/common': 'node:@angular/common/bundles/common.umd.js',
17+
'@angular/common/http': 'node:@angular/common/bundles/common-http.umd.js',
1618
'@angular/compiler': 'node:@angular/compiler/bundles/compiler.umd.js',
17-
'@angular/http': 'node:@angular/http/bundles/http.umd.js',
1819
'@angular/forms': 'node:@angular/forms/bundles/forms.umd.js',
1920
'@angular/router': 'node:@angular/router/bundles/router.umd.js',
2021
'@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: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Injectable, SecurityContext, Optional, SkipSelf} from '@angular/core';
1010
import {SafeResourceUrl, DomSanitizer} from '@angular/platform-browser';
11-
import {Http} from '@angular/http';
11+
import {HttpClient} from '@angular/common/http';
1212
import {Observable} from 'rxjs/Observable';
1313
import {_throw as observableThrow} from 'rxjs/observable/throw';
1414
import {of as observableOf} from 'rxjs/observable/of';
@@ -27,12 +27,13 @@ export function getMdIconNameNotFoundError(iconName: string): Error {
2727

2828
/**
2929
* Returns an exception to be thrown when the consumer attempts to use
30-
* `<md-icon>` without including @angular/http.
30+
* `<md-icon>` without including @angular/common/http.
3131
* @docs-private
3232
*/
3333
export function getMdIconNoHttpProviderError(): Error {
34-
return Error('Could not find Http provider for use with Angular Material icons. ' +
35-
'Please include the HttpModule from @angular/http in your app imports.');
34+
return Error('Could not find the HttpClient provider for use with Angular Material icons. ' +
35+
'Please include the HttpClientModule from @angular/common/http in your ' +
36+
'app imports.');
3637
}
3738

3839

@@ -91,7 +92,7 @@ export class MdIconRegistry {
9192
*/
9293
private _defaultFontSetClass = 'material-icons';
9394

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

9697
/**
9798
* Registers an icon by URL in the default namespace.
@@ -272,7 +273,7 @@ export class MdIconRegistry {
272273

273274
// Not found in any cached icon sets. If there are icon sets with URLs that we haven't
274275
// fetched, fetch them now and look for iconName in the results.
275-
const iconSetFetchRequests: Observable<SVGElement>[] = iconSetConfigs
276+
const iconSetFetchRequests: Observable<SVGElement | null>[] = iconSetConfigs
276277
.filter(iconSetConfig => !iconSetConfig.svgElement)
277278
.map(iconSetConfig => {
278279
return RxChain.from(this._loadSvgIconSetFromConfig(iconSetConfig))
@@ -441,7 +442,7 @@ export class MdIconRegistry {
441442
* cached, so future calls with the same URL may not cause another HTTP request.
442443
*/
443444
private _fetchUrl(safeUrl: SafeResourceUrl): Observable<string> {
444-
if (!this._http) {
445+
if (!this._httpClient) {
445446
throw getMdIconNoHttpProviderError();
446447
}
447448

@@ -462,8 +463,7 @@ export class MdIconRegistry {
462463

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

476476
/** @docs-private */
477477
export function ICON_REGISTRY_PROVIDER_FACTORY(
478-
parentRegistry: MdIconRegistry, http: Http, sanitizer: DomSanitizer) {
479-
return parentRegistry || new MdIconRegistry(http, sanitizer);
478+
parentRegistry: MdIconRegistry, httpClient: HttpClient, sanitizer: DomSanitizer) {
479+
return parentRegistry || new MdIconRegistry(httpClient, sanitizer);
480480
}
481481

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

0 commit comments

Comments
 (0)