Skip to content

Commit

Permalink
refactor: switch to HttpClient
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
crisbeto committed Sep 13, 2017
1 parent dcfe515 commit 7b57d75
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 155 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"@angular/compiler": "~4.3.6",
"@angular/core": "~4.3.6",
"@angular/forms": "~4.3.6",
"@angular/http": "~4.3.6",
"@angular/platform-browser": "~4.3.6",
"core-js": "^2.4.1",
"rxjs": "^5.0.1",
Expand All @@ -41,6 +40,7 @@
},
"devDependencies": {
"@angular/compiler-cli": "~4.3.6",
"@angular/http": "~4.3.6",
"@angular/platform-browser-dynamic": "~4.3.6",
"@angular/platform-server": "~4.3.6",
"@angular/router": "~4.3.6",
Expand Down
4 changes: 2 additions & 2 deletions scripts/closure-compiler/build-devapp-bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ OPTS=(
"--js_module_root=dist/releases/material-moment-adapter"
"--js_module_root=node_modules/@angular/core"
"--js_module_root=node_modules/@angular/common"
"--js_module_root=node_modules/@angular/common/http"
"--js_module_root=node_modules/@angular/compiler"
"--js_module_root=node_modules/@angular/forms"
"--js_module_root=node_modules/@angular/http"
"--js_module_root=node_modules/@angular/router"
"--js_module_root=node_modules/@angular/platform-browser"
"--js_module_root=node_modules/@angular/platform-browser/animations"
Expand All @@ -65,9 +65,9 @@ OPTS=(
# Include all Angular FESM bundles.
node_modules/@angular/core/@angular/core.js
node_modules/@angular/common/@angular/common.js
node_modules/@angular/common/@angular/common/http.js
node_modules/@angular/compiler/@angular/compiler.js
node_modules/@angular/forms/@angular/forms.js
node_modules/@angular/http/@angular/http.js
node_modules/@angular/router/@angular/router.js
node_modules/@angular/platform-browser/@angular/platform-browser.js
node_modules/@angular/platform-browser/@angular/platform-browser/animations.js
Expand Down
4 changes: 2 additions & 2 deletions src/demo-app/demo-app-module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ApplicationRef, NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpModule} from '@angular/http';
import {HttpClientModule} from '@angular/common/http';
import {RouterModule} from '@angular/router';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {ALL_ROUTES} from './demo-app/routes';
Expand All @@ -13,7 +13,7 @@ import {AccessibilityDemoModule} from './a11y/a11y-module';
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpModule,
HttpClientModule,
DemoModule,
AccessibilityDemoModule,
RouterModule.forRoot(ALL_ROUTES),
Expand Down
3 changes: 2 additions & 1 deletion src/demo-app/system-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ System.config({
map: {
'rxjs': 'node:rxjs',
'main': 'main.js',
'tslib': 'node:tslib/tslib.js',
'moment': 'node:moment/min/moment-with-locales.min.js',

// Angular specific mappings.
'@angular/core': 'node:@angular/core/bundles/core.umd.js',
'@angular/common': 'node:@angular/common/bundles/common.umd.js',
'@angular/common/http': 'node:@angular/common/bundles/common-http.umd.js',
'@angular/compiler': 'node:@angular/compiler/bundles/compiler.umd.js',
'@angular/http': 'node:@angular/http/bundles/http.umd.js',
'@angular/forms': 'node:@angular/forms/bundles/forms.umd.js',
'@angular/router': 'node:@angular/router/bundles/router.umd.js',
'@angular/animations': 'node:@angular/animations/bundles/animations.umd.js',
Expand Down
3 changes: 2 additions & 1 deletion src/e2e-app/system-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ System.config({
map: {
'rxjs': 'node:rxjs',
'main': 'main.js',
'tslib': 'node:tslib/tslib.js',

// Angular specific mappings.
'@angular/core': 'node:@angular/core/bundles/core.umd.js',
'@angular/common': 'node:@angular/common/bundles/common.umd.js',
'@angular/common/http': 'node:@angular/common/bundles/common-http.umd.js',
'@angular/compiler': 'node:@angular/compiler/bundles/compiler.umd.js',
'@angular/http': 'node:@angular/http/bundles/http.umd.js',
'@angular/forms': 'node:@angular/forms/bundles/forms.umd.js',
'@angular/router': 'node:@angular/router/bundles/router.umd.js',
'@angular/animations': 'node:@angular/animations/bundles/animations.umd.js',
Expand Down
96 changes: 34 additions & 62 deletions src/lib/icon/fake-svgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,41 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Response, ResponseOptions} from '@angular/http';

/**
* Fake URLs and associated SVG documents used by tests.
* @docs-private
*/
const FAKE_SVGS = (() => {
const svgs = new Map<string, string>();
svgs.set('cat.svg',
'<svg><path id="meow"></path></svg>');

svgs.set('dog.svg',
'<svg><path id="woof"></path></svg>');

svgs.set('farm-set-1.svg', `
<svg>
<defs>
<g id="pig"><path id="oink"></path></g>
<g id="cow"><path id="moo"></path></g>
</defs>
</svg>
`);

svgs.set('farm-set-2.svg', `
<svg>
<defs>
<g id="cow"><path id="moo moo"></path></g>
<g id="sheep"><path id="baa"></path></g>
</defs>
</svg>
`);

svgs.set('farm-set-3.svg', `
<svg>
<symbol id="duck">
<path id="quack"></path>
</symbol>
</svg>
`);

svgs.set('arrow-set.svg', `
<svg>
<defs>
<svg id="left-arrow"><path id="left"></path></svg>
<svg id="right-arrow"><path id="right"></path></svg>
</defs>
</svg>
`);

return svgs;
})();

/**
* Returns an HTTP response for a fake SVG URL.
* @docs-private
*/
export function getFakeSvgHttpResponse(url: string) {
if (FAKE_SVGS.has(url)) {
return new Response(new ResponseOptions({
status: 200,
body: FAKE_SVGS.get(url),
}));
} else {
return new Response(new ResponseOptions({status: 404}));
}
}
export const FAKE_SVGS = {
cat: '<svg><path id="meow"></path></svg>',
dog: '<svg><path id="woof"></path></svg>',
farmSet1: `
<svg>
<defs>
<g id="pig"><path id="oink"></path></g>
<g id="cow"><path id="moo"></path></g>
</defs>
</svg>
`,
farmSet2: `
<svg>
<defs>
<g id="cow"><path id="moo moo"></path></g>
<g id="sheep"><path id="baa"></path></g>
</defs>
</svg>
`,
farmSet3: `
<svg>
<symbol id="duck">
<path id="quack"></path>
</symbol>
</svg>
`,
arrows: `
<svg>
<defs>
<svg id="left-arrow"><path id="left"></path></svg>
<svg id="right-arrow"><path id="right"></path></svg>
</defs>
</svg> `
};
28 changes: 16 additions & 12 deletions src/lib/icon/icon-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

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


Expand Down Expand Up @@ -91,7 +92,7 @@ export class MdIconRegistry {
*/
private _defaultFontSetClass = 'material-icons';

constructor(@Optional() private _http: Http, private _sanitizer: DomSanitizer) {}
constructor(@Optional() private _httpClient: HttpClient, private _sanitizer: DomSanitizer) {}

/**
* Registers an icon by URL in the default namespace.
Expand Down Expand Up @@ -272,7 +273,7 @@ export class MdIconRegistry {

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

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

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

/** @docs-private */
export function ICON_REGISTRY_PROVIDER_FACTORY(
parentRegistry: MdIconRegistry, http: Http, sanitizer: DomSanitizer) {
return parentRegistry || new MdIconRegistry(http, sanitizer);
parentRegistry: MdIconRegistry, httpClient: HttpClient, sanitizer: DomSanitizer) {
return parentRegistry || new MdIconRegistry(httpClient, sanitizer);
}

/** @docs-private */
export const ICON_REGISTRY_PROVIDER = {
// If there is already an MdIconRegistry available, use that. Otherwise, provide a new one.
provide: MdIconRegistry,
deps: [[new Optional(), new SkipSelf(), MdIconRegistry], [new Optional(), Http], DomSanitizer],
deps: [
[new Optional(), new SkipSelf(), MdIconRegistry],
[new Optional(), HttpClient],
DomSanitizer
],
useFactory: ICON_REGISTRY_PROVIDER_FACTORY
};

Expand Down
Loading

0 comments on commit 7b57d75

Please sign in to comment.