From 13ba2f90b9551e7aa9f14e5a7e5feab3d47a9ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Dias?= Date: Tue, 15 Nov 2016 18:19:14 +0100 Subject: [PATCH] refactor(http): remove all facade methods from http module (#12870) --- .../http/src/backends/browser_jsonp.ts | 4 +- .../http/src/backends/jsonp_backend.ts | 12 ++---- .../@angular/http/src/base_request_options.ts | 40 +++++++++---------- .../http/src/base_response_options.ts | 27 ++++++------- modules/@angular/http/src/body.ts | 4 +- modules/@angular/http/src/facade | 1 - modules/@angular/http/src/http.ts | 10 ++--- modules/@angular/http/src/http_utils.ts | 2 - modules/@angular/http/src/static_request.ts | 5 +-- .../http/test/backends/jsonp_backend_spec.ts | 9 +---- 10 files changed, 44 insertions(+), 70 deletions(-) delete mode 120000 modules/@angular/http/src/facade diff --git a/modules/@angular/http/src/backends/browser_jsonp.ts b/modules/@angular/http/src/backends/browser_jsonp.ts index 7483bac0b7..2734836afc 100644 --- a/modules/@angular/http/src/backends/browser_jsonp.ts +++ b/modules/@angular/http/src/backends/browser_jsonp.ts @@ -7,15 +7,15 @@ */ import {Injectable} from '@angular/core'; -import {global} from '../facade/lang'; let _nextRequestId = 0; export const JSONP_HOME = '__ng_jsonp__'; let _jsonpConnections: {[key: string]: any} = null; function _getJsonpConnections(): {[key: string]: any} { + const w: {[key: string]: any} = typeof window == 'object' ? window : {}; if (_jsonpConnections === null) { - _jsonpConnections = (<{[key: string]: any}>global)[JSONP_HOME] = {}; + _jsonpConnections = w[JSONP_HOME] = {}; } return _jsonpConnections; } diff --git a/modules/@angular/http/src/backends/jsonp_backend.ts b/modules/@angular/http/src/backends/jsonp_backend.ts index 33a98aad67..a8eba54367 100644 --- a/modules/@angular/http/src/backends/jsonp_backend.ts +++ b/modules/@angular/http/src/backends/jsonp_backend.ts @@ -12,7 +12,6 @@ import {Observer} from 'rxjs/Observer'; import {ResponseOptions} from '../base_response_options'; import {ReadyState, RequestMethod, ResponseType} from '../enums'; -import {isPresent} from '../facade/lang'; import {Connection, ConnectionBackend} from '../interfaces'; import {Request} from '../static_request'; import {Response} from '../static_response'; @@ -89,7 +88,7 @@ export class JSONPConnection_ extends JSONPConnection { if (!this._finished) { let responseOptions = new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseType.Error, url}); - if (isPresent(baseResponseOptions)) { + if (baseResponseOptions) { responseOptions = baseResponseOptions.merge(responseOptions); } responseObserver.error(new Response(responseOptions)); @@ -97,7 +96,7 @@ export class JSONPConnection_ extends JSONPConnection { } let responseOptions = new ResponseOptions({body: this._responseData, url}); - if (isPresent(this.baseResponseOptions)) { + if (this.baseResponseOptions) { responseOptions = this.baseResponseOptions.merge(responseOptions); } @@ -110,7 +109,7 @@ export class JSONPConnection_ extends JSONPConnection { this.readyState = ReadyState.Done; _dom.cleanup(script); let responseOptions = new ResponseOptions({body: error.message, type: ResponseType.Error}); - if (isPresent(baseResponseOptions)) { + if (baseResponseOptions) { responseOptions = baseResponseOptions.merge(responseOptions); } responseObserver.error(new Response(responseOptions)); @@ -125,10 +124,7 @@ export class JSONPConnection_ extends JSONPConnection { this.readyState = ReadyState.Cancelled; script.removeEventListener('load', onLoad); script.removeEventListener('error', onError); - if (isPresent(script)) { - this._dom.cleanup(script); - } - + this._dom.cleanup(script); }; }); } diff --git a/modules/@angular/http/src/base_request_options.ts b/modules/@angular/http/src/base_request_options.ts index b196041394..5409a44d91 100644 --- a/modules/@angular/http/src/base_request_options.ts +++ b/modules/@angular/http/src/base_request_options.ts @@ -8,8 +8,6 @@ import {Injectable} from '@angular/core'; -import {isPresent} from '../src/facade/lang'; - import {RequestMethod, ResponseContentType} from './enums'; import {Headers} from './headers'; import {normalizeMethodName} from './http_utils'; @@ -77,16 +75,14 @@ export class RequestOptions { constructor( {method, headers, body, url, search, withCredentials, responseType}: RequestOptionsArgs = {}) { - this.method = isPresent(method) ? normalizeMethodName(method) : null; - this.headers = isPresent(headers) ? headers : null; - this.body = isPresent(body) ? body : null; - this.url = isPresent(url) ? url : null; - this.search = isPresent(search) ? - (typeof search === 'string' ? new URLSearchParams((search)) : - (search)) : - null; - this.withCredentials = isPresent(withCredentials) ? withCredentials : null; - this.responseType = isPresent(responseType) ? responseType : null; + this.method = method != null ? normalizeMethodName(method) : null; + this.headers = headers != null ? headers : null; + this.body = body != null ? body : null; + this.url = url != null ? url : null; + this.search = + search != null ? (typeof search === 'string' ? new URLSearchParams(search) : search) : null; + this.withCredentials = withCredentials != null ? withCredentials : null; + this.responseType = responseType != null ? responseType : null; } /** @@ -116,18 +112,18 @@ export class RequestOptions { */ merge(options?: RequestOptionsArgs): RequestOptions { return new RequestOptions({ - method: options && isPresent(options.method) ? options.method : this.method, - headers: options && isPresent(options.headers) ? options.headers : this.headers, - body: options && isPresent(options.body) ? options.body : this.body, - url: options && isPresent(options.url) ? options.url : this.url, - search: options && isPresent(options.search) ? + method: options && options.method != null ? options.method : this.method, + headers: options && options.headers != null ? options.headers : this.headers, + body: options && options.body != null ? options.body : this.body, + url: options && options.url != null ? options.url : this.url, + search: options && options.search != null ? (typeof options.search === 'string' ? new URLSearchParams(options.search) : - ((options.search)).clone()) : + options.search.clone()) : this.search, - withCredentials: options && isPresent(options.withCredentials) ? options.withCredentials : - this.withCredentials, - responseType: options && isPresent(options.responseType) ? options.responseType : - this.responseType + withCredentials: options && options.withCredentials != null ? options.withCredentials : + this.withCredentials, + responseType: options && options.responseType != null ? options.responseType : + this.responseType }); } } diff --git a/modules/@angular/http/src/base_response_options.ts b/modules/@angular/http/src/base_response_options.ts index 03c5a7e914..a9e56aebe3 100644 --- a/modules/@angular/http/src/base_response_options.ts +++ b/modules/@angular/http/src/base_response_options.ts @@ -8,8 +8,6 @@ import {Injectable} from '@angular/core'; -import {isPresent} from '../src/facade/lang'; - import {ResponseType} from './enums'; import {Headers} from './headers'; import {ResponseOptionsArgs} from './interfaces'; @@ -68,12 +66,12 @@ export class ResponseOptions { type: ResponseType; url: string; constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) { - this.body = isPresent(body) ? body : null; - this.status = isPresent(status) ? status : null; - this.headers = isPresent(headers) ? headers : null; - this.statusText = isPresent(statusText) ? statusText : null; - this.type = isPresent(type) ? type : null; - this.url = isPresent(url) ? url : null; + this.body = body != null ? body : null; + this.status = status != null ? status : null; + this.headers = headers != null ? headers : null; + this.statusText = statusText != null ? statusText : null; + this.type = type != null ? type : null; + this.url = url != null ? url : null; } /** @@ -103,13 +101,12 @@ export class ResponseOptions { */ merge(options?: ResponseOptionsArgs): ResponseOptions { return new ResponseOptions({ - body: isPresent(options) && isPresent(options.body) ? options.body : this.body, - status: isPresent(options) && isPresent(options.status) ? options.status : this.status, - headers: isPresent(options) && isPresent(options.headers) ? options.headers : this.headers, - statusText: isPresent(options) && isPresent(options.statusText) ? options.statusText : - this.statusText, - type: isPresent(options) && isPresent(options.type) ? options.type : this.type, - url: isPresent(options) && isPresent(options.url) ? options.url : this.url, + body: options && options.body != null ? options.body : this.body, + status: options && options.status != null ? options.status : this.status, + headers: options && options.headers != null ? options.headers : this.headers, + statusText: options && options.statusText != null ? options.statusText : this.statusText, + type: options && options.type != null ? options.type : this.type, + url: options && options.url != null ? options.url : this.url, }); } } diff --git a/modules/@angular/http/src/body.ts b/modules/@angular/http/src/body.ts index d4d60e9056..7e660a80e8 100644 --- a/modules/@angular/http/src/body.ts +++ b/modules/@angular/http/src/body.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {isJsObject, stringToArrayBuffer} from './http_utils'; +import {stringToArrayBuffer} from './http_utils'; import {URLSearchParams} from './url_search_params'; @@ -51,7 +51,7 @@ export abstract class Body { return ''; } - if (isJsObject(this._body)) { + if (typeof this._body === 'object') { return JSON.stringify(this._body, null, 2); } diff --git a/modules/@angular/http/src/facade b/modules/@angular/http/src/facade deleted file mode 120000 index e084c803c6..0000000000 --- a/modules/@angular/http/src/facade +++ /dev/null @@ -1 +0,0 @@ -../../facade/src \ No newline at end of file diff --git a/modules/@angular/http/src/http.ts b/modules/@angular/http/src/http.ts index 12833823e8..6776afc5db 100644 --- a/modules/@angular/http/src/http.ts +++ b/modules/@angular/http/src/http.ts @@ -8,7 +8,6 @@ import {Injectable} from '@angular/core'; import {Observable} from 'rxjs/Observable'; -import {isPresent} from '../src/facade/lang'; import {BaseRequestOptions, RequestOptions} from './base_request_options'; import {RequestMethod} from './enums'; import {ConnectionBackend, RequestOptionsArgs} from './interfaces'; @@ -23,7 +22,7 @@ function mergeOptions( defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod, url: string): RequestOptions { const newOptions = defaultOpts; - if (isPresent(providedOpts)) { + if (providedOpts) { // Hack so Dart can used named parameters return newOptions.merge(new RequestOptions({ method: providedOpts.method || method, @@ -35,11 +34,8 @@ function mergeOptions( responseType: providedOpts.responseType })); } - if (isPresent(method)) { - return newOptions.merge(new RequestOptions({method: method, url: url})); - } else { - return newOptions.merge(new RequestOptions({url: url})); - } + + return newOptions.merge(new RequestOptions({method, url})); } /** diff --git a/modules/@angular/http/src/http_utils.ts b/modules/@angular/http/src/http_utils.ts index 14f3f061b9..0344ee5649 100644 --- a/modules/@angular/http/src/http_utils.ts +++ b/modules/@angular/http/src/http_utils.ts @@ -49,5 +49,3 @@ export function stringToArrayBuffer(input: String): ArrayBuffer { } return view.buffer; } - -export {isJsObject} from '../src/facade/lang'; diff --git a/modules/@angular/http/src/static_request.ts b/modules/@angular/http/src/static_request.ts index dd0f311a96..9c756a9df8 100644 --- a/modules/@angular/http/src/static_request.ts +++ b/modules/@angular/http/src/static_request.ts @@ -6,8 +6,6 @@ * found in the LICENSE file at https://angular.io/license */ -import {isPresent} from '../src/facade/lang'; - import {Body} from './body'; import {ContentType, RequestMethod, ResponseContentType} from './enums'; import {Headers} from './headers'; @@ -78,7 +76,7 @@ export class Request extends Body { // TODO: assert that url is present const url = requestOptions.url; this.url = requestOptions.url; - if (isPresent(requestOptions.search)) { + if (requestOptions.search) { const search = requestOptions.search.toString(); if (search.length > 0) { let prefix = '?'; @@ -93,7 +91,6 @@ export class Request extends Body { this.method = normalizeMethodName(requestOptions.method); // TODO(jeffbcross): implement behavior // Defaults to 'omit', consistent with browser - // TODO(jeffbcross): implement behavior this.headers = new Headers(requestOptions.headers); this.contentType = this.detectContentType(); this.withCredentials = requestOptions.withCredentials; diff --git a/modules/@angular/http/test/backends/jsonp_backend_spec.ts b/modules/@angular/http/test/backends/jsonp_backend_spec.ts index bec39695ba..81b5ce6373 100644 --- a/modules/@angular/http/test/backends/jsonp_backend_spec.ts +++ b/modules/@angular/http/test/backends/jsonp_backend_spec.ts @@ -14,7 +14,6 @@ import {JSONPBackend, JSONPBackend_, JSONPConnection, JSONPConnection_} from '.. import {BaseRequestOptions, RequestOptions} from '../../src/base_request_options'; import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options'; import {ReadyState, RequestMethod, ResponseType} from '../../src/enums'; -import {isPresent} from '../../src/facade/lang'; import {Request} from '../../src/static_request'; let existingScripts: MockBrowserJsonp[] = []; @@ -22,18 +21,14 @@ let existingScripts: MockBrowserJsonp[] = []; class MockBrowserJsonp extends BrowserJsonp { src: string; callbacks = new Map any>(); - constructor() { super(); } addEventListener(type: string, cb: (data: any) => any) { this.callbacks.set(type, cb); } removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); } - dispatchEvent(type: string, argument?: any) { - if (!isPresent(argument)) { - argument = {}; - } + dispatchEvent(type: string, argument: any = {}) { const cb = this.callbacks.get(type); - if (isPresent(cb)) { + if (cb) { cb(argument); } }