refactor(http): remove all facade methods from http module (#12870)

This commit is contained in:
João Dias 2016-11-15 18:19:14 +01:00 committed by Victor Berchet
parent 75277cd94b
commit 13ba2f90b9
10 changed files with 44 additions and 70 deletions

View File

@ -7,15 +7,15 @@
*/ */
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {global} from '../facade/lang';
let _nextRequestId = 0; let _nextRequestId = 0;
export const JSONP_HOME = '__ng_jsonp__'; export const JSONP_HOME = '__ng_jsonp__';
let _jsonpConnections: {[key: string]: any} = null; let _jsonpConnections: {[key: string]: any} = null;
function _getJsonpConnections(): {[key: string]: any} { function _getJsonpConnections(): {[key: string]: any} {
const w: {[key: string]: any} = typeof window == 'object' ? window : {};
if (_jsonpConnections === null) { if (_jsonpConnections === null) {
_jsonpConnections = (<{[key: string]: any}>global)[JSONP_HOME] = {}; _jsonpConnections = w[JSONP_HOME] = {};
} }
return _jsonpConnections; return _jsonpConnections;
} }

View File

@ -12,7 +12,6 @@ import {Observer} from 'rxjs/Observer';
import {ResponseOptions} from '../base_response_options'; import {ResponseOptions} from '../base_response_options';
import {ReadyState, RequestMethod, ResponseType} from '../enums'; import {ReadyState, RequestMethod, ResponseType} from '../enums';
import {isPresent} from '../facade/lang';
import {Connection, ConnectionBackend} from '../interfaces'; import {Connection, ConnectionBackend} from '../interfaces';
import {Request} from '../static_request'; import {Request} from '../static_request';
import {Response} from '../static_response'; import {Response} from '../static_response';
@ -89,7 +88,7 @@ export class JSONPConnection_ extends JSONPConnection {
if (!this._finished) { if (!this._finished) {
let responseOptions = let responseOptions =
new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseType.Error, url}); new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseType.Error, url});
if (isPresent(baseResponseOptions)) { if (baseResponseOptions) {
responseOptions = baseResponseOptions.merge(responseOptions); responseOptions = baseResponseOptions.merge(responseOptions);
} }
responseObserver.error(new Response(responseOptions)); responseObserver.error(new Response(responseOptions));
@ -97,7 +96,7 @@ export class JSONPConnection_ extends JSONPConnection {
} }
let responseOptions = new ResponseOptions({body: this._responseData, url}); let responseOptions = new ResponseOptions({body: this._responseData, url});
if (isPresent(this.baseResponseOptions)) { if (this.baseResponseOptions) {
responseOptions = this.baseResponseOptions.merge(responseOptions); responseOptions = this.baseResponseOptions.merge(responseOptions);
} }
@ -110,7 +109,7 @@ export class JSONPConnection_ extends JSONPConnection {
this.readyState = ReadyState.Done; this.readyState = ReadyState.Done;
_dom.cleanup(script); _dom.cleanup(script);
let responseOptions = new ResponseOptions({body: error.message, type: ResponseType.Error}); let responseOptions = new ResponseOptions({body: error.message, type: ResponseType.Error});
if (isPresent(baseResponseOptions)) { if (baseResponseOptions) {
responseOptions = baseResponseOptions.merge(responseOptions); responseOptions = baseResponseOptions.merge(responseOptions);
} }
responseObserver.error(new Response(responseOptions)); responseObserver.error(new Response(responseOptions));
@ -125,10 +124,7 @@ export class JSONPConnection_ extends JSONPConnection {
this.readyState = ReadyState.Cancelled; this.readyState = ReadyState.Cancelled;
script.removeEventListener('load', onLoad); script.removeEventListener('load', onLoad);
script.removeEventListener('error', onError); script.removeEventListener('error', onError);
if (isPresent(script)) { this._dom.cleanup(script);
this._dom.cleanup(script);
}
}; };
}); });
} }

View File

@ -8,8 +8,6 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {isPresent} from '../src/facade/lang';
import {RequestMethod, ResponseContentType} from './enums'; import {RequestMethod, ResponseContentType} from './enums';
import {Headers} from './headers'; import {Headers} from './headers';
import {normalizeMethodName} from './http_utils'; import {normalizeMethodName} from './http_utils';
@ -77,16 +75,14 @@ export class RequestOptions {
constructor( constructor(
{method, headers, body, url, search, withCredentials, {method, headers, body, url, search, withCredentials,
responseType}: RequestOptionsArgs = {}) { responseType}: RequestOptionsArgs = {}) {
this.method = isPresent(method) ? normalizeMethodName(method) : null; this.method = method != null ? normalizeMethodName(method) : null;
this.headers = isPresent(headers) ? headers : null; this.headers = headers != null ? headers : null;
this.body = isPresent(body) ? body : null; this.body = body != null ? body : null;
this.url = isPresent(url) ? url : null; this.url = url != null ? url : null;
this.search = isPresent(search) ? this.search =
(typeof search === 'string' ? new URLSearchParams(<string>(search)) : search != null ? (typeof search === 'string' ? new URLSearchParams(search) : search) : null;
<URLSearchParams>(search)) : this.withCredentials = withCredentials != null ? withCredentials : null;
null; this.responseType = responseType != null ? responseType : null;
this.withCredentials = isPresent(withCredentials) ? withCredentials : null;
this.responseType = isPresent(responseType) ? responseType : null;
} }
/** /**
@ -116,18 +112,18 @@ export class RequestOptions {
*/ */
merge(options?: RequestOptionsArgs): RequestOptions { merge(options?: RequestOptionsArgs): RequestOptions {
return new RequestOptions({ return new RequestOptions({
method: options && isPresent(options.method) ? options.method : this.method, method: options && options.method != null ? options.method : this.method,
headers: options && isPresent(options.headers) ? options.headers : this.headers, headers: options && options.headers != null ? options.headers : this.headers,
body: options && isPresent(options.body) ? options.body : this.body, body: options && options.body != null ? options.body : this.body,
url: options && isPresent(options.url) ? options.url : this.url, url: options && options.url != null ? options.url : this.url,
search: options && isPresent(options.search) ? search: options && options.search != null ?
(typeof options.search === 'string' ? new URLSearchParams(options.search) : (typeof options.search === 'string' ? new URLSearchParams(options.search) :
(<URLSearchParams>(options.search)).clone()) : options.search.clone()) :
this.search, this.search,
withCredentials: options && isPresent(options.withCredentials) ? options.withCredentials : withCredentials: options && options.withCredentials != null ? options.withCredentials :
this.withCredentials, this.withCredentials,
responseType: options && isPresent(options.responseType) ? options.responseType : responseType: options && options.responseType != null ? options.responseType :
this.responseType this.responseType
}); });
} }
} }

View File

@ -8,8 +8,6 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {isPresent} from '../src/facade/lang';
import {ResponseType} from './enums'; import {ResponseType} from './enums';
import {Headers} from './headers'; import {Headers} from './headers';
import {ResponseOptionsArgs} from './interfaces'; import {ResponseOptionsArgs} from './interfaces';
@ -68,12 +66,12 @@ export class ResponseOptions {
type: ResponseType; type: ResponseType;
url: string; url: string;
constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) { constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) {
this.body = isPresent(body) ? body : null; this.body = body != null ? body : null;
this.status = isPresent(status) ? status : null; this.status = status != null ? status : null;
this.headers = isPresent(headers) ? headers : null; this.headers = headers != null ? headers : null;
this.statusText = isPresent(statusText) ? statusText : null; this.statusText = statusText != null ? statusText : null;
this.type = isPresent(type) ? type : null; this.type = type != null ? type : null;
this.url = isPresent(url) ? url : null; this.url = url != null ? url : null;
} }
/** /**
@ -103,13 +101,12 @@ export class ResponseOptions {
*/ */
merge(options?: ResponseOptionsArgs): ResponseOptions { merge(options?: ResponseOptionsArgs): ResponseOptions {
return new ResponseOptions({ return new ResponseOptions({
body: isPresent(options) && isPresent(options.body) ? options.body : this.body, body: options && options.body != null ? options.body : this.body,
status: isPresent(options) && isPresent(options.status) ? options.status : this.status, status: options && options.status != null ? options.status : this.status,
headers: isPresent(options) && isPresent(options.headers) ? options.headers : this.headers, headers: options && options.headers != null ? options.headers : this.headers,
statusText: isPresent(options) && isPresent(options.statusText) ? options.statusText : statusText: options && options.statusText != null ? options.statusText : this.statusText,
this.statusText, type: options && options.type != null ? options.type : this.type,
type: isPresent(options) && isPresent(options.type) ? options.type : this.type, url: options && options.url != null ? options.url : this.url,
url: isPresent(options) && isPresent(options.url) ? options.url : this.url,
}); });
} }
} }

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * 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'; import {URLSearchParams} from './url_search_params';
@ -51,7 +51,7 @@ export abstract class Body {
return ''; return '';
} }
if (isJsObject(this._body)) { if (typeof this._body === 'object') {
return JSON.stringify(this._body, null, 2); return JSON.stringify(this._body, null, 2);
} }

View File

@ -1 +0,0 @@
../../facade/src

View File

@ -8,7 +8,6 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {isPresent} from '../src/facade/lang';
import {BaseRequestOptions, RequestOptions} from './base_request_options'; import {BaseRequestOptions, RequestOptions} from './base_request_options';
import {RequestMethod} from './enums'; import {RequestMethod} from './enums';
import {ConnectionBackend, RequestOptionsArgs} from './interfaces'; import {ConnectionBackend, RequestOptionsArgs} from './interfaces';
@ -23,7 +22,7 @@ function mergeOptions(
defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod, defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod,
url: string): RequestOptions { url: string): RequestOptions {
const newOptions = defaultOpts; const newOptions = defaultOpts;
if (isPresent(providedOpts)) { if (providedOpts) {
// Hack so Dart can used named parameters // Hack so Dart can used named parameters
return newOptions.merge(new RequestOptions({ return newOptions.merge(new RequestOptions({
method: providedOpts.method || method, method: providedOpts.method || method,
@ -35,11 +34,8 @@ function mergeOptions(
responseType: providedOpts.responseType responseType: providedOpts.responseType
})); }));
} }
if (isPresent(method)) {
return newOptions.merge(new RequestOptions({method: method, url: url})); return newOptions.merge(new RequestOptions({method, url}));
} else {
return newOptions.merge(new RequestOptions({url: url}));
}
} }
/** /**

View File

@ -49,5 +49,3 @@ export function stringToArrayBuffer(input: String): ArrayBuffer {
} }
return view.buffer; return view.buffer;
} }
export {isJsObject} from '../src/facade/lang';

View File

@ -6,8 +6,6 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {isPresent} from '../src/facade/lang';
import {Body} from './body'; import {Body} from './body';
import {ContentType, RequestMethod, ResponseContentType} from './enums'; import {ContentType, RequestMethod, ResponseContentType} from './enums';
import {Headers} from './headers'; import {Headers} from './headers';
@ -78,7 +76,7 @@ export class Request extends Body {
// TODO: assert that url is present // TODO: assert that url is present
const url = requestOptions.url; const url = requestOptions.url;
this.url = requestOptions.url; this.url = requestOptions.url;
if (isPresent(requestOptions.search)) { if (requestOptions.search) {
const search = requestOptions.search.toString(); const search = requestOptions.search.toString();
if (search.length > 0) { if (search.length > 0) {
let prefix = '?'; let prefix = '?';
@ -93,7 +91,6 @@ export class Request extends Body {
this.method = normalizeMethodName(requestOptions.method); this.method = normalizeMethodName(requestOptions.method);
// TODO(jeffbcross): implement behavior // TODO(jeffbcross): implement behavior
// Defaults to 'omit', consistent with browser // Defaults to 'omit', consistent with browser
// TODO(jeffbcross): implement behavior
this.headers = new Headers(requestOptions.headers); this.headers = new Headers(requestOptions.headers);
this.contentType = this.detectContentType(); this.contentType = this.detectContentType();
this.withCredentials = requestOptions.withCredentials; this.withCredentials = requestOptions.withCredentials;

View File

@ -14,7 +14,6 @@ import {JSONPBackend, JSONPBackend_, JSONPConnection, JSONPConnection_} from '..
import {BaseRequestOptions, RequestOptions} from '../../src/base_request_options'; import {BaseRequestOptions, RequestOptions} from '../../src/base_request_options';
import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options'; import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options';
import {ReadyState, RequestMethod, ResponseType} from '../../src/enums'; import {ReadyState, RequestMethod, ResponseType} from '../../src/enums';
import {isPresent} from '../../src/facade/lang';
import {Request} from '../../src/static_request'; import {Request} from '../../src/static_request';
let existingScripts: MockBrowserJsonp[] = []; let existingScripts: MockBrowserJsonp[] = [];
@ -22,18 +21,14 @@ let existingScripts: MockBrowserJsonp[] = [];
class MockBrowserJsonp extends BrowserJsonp { class MockBrowserJsonp extends BrowserJsonp {
src: string; src: string;
callbacks = new Map<string, (data: any) => any>(); callbacks = new Map<string, (data: any) => any>();
constructor() { super(); }
addEventListener(type: string, cb: (data: any) => any) { this.callbacks.set(type, cb); } addEventListener(type: string, cb: (data: any) => any) { this.callbacks.set(type, cb); }
removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); } removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); }
dispatchEvent(type: string, argument?: any) { dispatchEvent(type: string, argument: any = {}) {
if (!isPresent(argument)) {
argument = {};
}
const cb = this.callbacks.get(type); const cb = this.callbacks.get(type);
if (isPresent(cb)) { if (cb) {
cb(argument); cb(argument);
} }
} }