refactor: remove some facades (#12335)

This commit is contained in:
Victor Berchet
2016-10-19 13:42:39 -07:00
committed by Alex Rickabaugh
parent 0ecd9b2df0
commit 76dd026447
58 changed files with 281 additions and 429 deletions

View File

@ -13,7 +13,7 @@ import {Observer} from 'rxjs/Observer';
import {ResponseOptions} from '../base_response_options';
import {ContentType, ReadyState, RequestMethod, ResponseContentType, ResponseType} from '../enums';
import {isPresent, isString} from '../facade/lang';
import {isPresent} from '../facade/lang';
import {Headers} from '../headers';
import {getResponseURL, isSuccess} from '../http_utils';
import {Connection, ConnectionBackend, XSRFStrategy} from '../interfaces';
@ -57,7 +57,7 @@ export class XHRConnection implements Connection {
// by IE10)
let body = _xhr.response === undefined ? _xhr.responseText : _xhr.response;
// Implicitly strip a potential XSSI prefix.
if (isString(body)) body = body.replace(XSSI_PREFIX, '');
if (typeof body === 'string') body = body.replace(XSSI_PREFIX, '');
let headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders());
let url = getResponseURL(_xhr);

View File

@ -8,7 +8,7 @@
import {Injectable} from '@angular/core';
import {isPresent, isString} from '../src/facade/lang';
import {isPresent} from '../src/facade/lang';
import {RequestMethod, ResponseContentType} from './enums';
import {Headers} from './headers';
@ -82,7 +82,8 @@ export class RequestOptions {
this.body = isPresent(body) ? body : null;
this.url = isPresent(url) ? url : null;
this.search = isPresent(search) ?
(isString(search) ? new URLSearchParams(<string>(search)) : <URLSearchParams>(search)) :
(typeof search === 'string' ? new URLSearchParams(<string>(search)) :
<URLSearchParams>(search)) :
null;
this.withCredentials = isPresent(withCredentials) ? withCredentials : null;
this.responseType = isPresent(responseType) ? responseType : null;
@ -115,19 +116,18 @@ export class RequestOptions {
*/
merge(options?: RequestOptionsArgs): RequestOptions {
return new RequestOptions({
method: isPresent(options) && isPresent(options.method) ? options.method : this.method,
headers: isPresent(options) && isPresent(options.headers) ? options.headers : this.headers,
body: isPresent(options) && isPresent(options.body) ? options.body : this.body,
url: isPresent(options) && isPresent(options.url) ? options.url : this.url,
search: isPresent(options) && isPresent(options.search) ?
(isString(options.search) ? new URLSearchParams(<string>(options.search)) :
(<URLSearchParams>(options.search)).clone()) :
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) ?
(typeof options.search === 'string' ? new URLSearchParams(options.search) :
(<URLSearchParams>(options.search)).clone()) :
this.search,
withCredentials: isPresent(options) && isPresent(options.withCredentials) ?
options.withCredentials :
this.withCredentials,
responseType: isPresent(options) && isPresent(options.responseType) ? options.responseType :
this.responseType
withCredentials: options && isPresent(options.withCredentials) ? options.withCredentials :
this.withCredentials,
responseType: options && isPresent(options.responseType) ? options.responseType :
this.responseType
});
}
}

View File

@ -6,8 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Json, isString} from '../src/facade/lang';
import {isJsObject, stringToArrayBuffer} from './http_utils';
import {URLSearchParams} from './url_search_params';
@ -26,12 +24,12 @@ export abstract class Body {
* Attempts to return body as parsed `JSON` object, or raises an exception.
*/
json(): any {
if (isString(this._body)) {
return Json.parse(<string>this._body);
if (typeof this._body === 'string') {
return JSON.parse(<string>this._body);
}
if (this._body instanceof ArrayBuffer) {
return Json.parse(this.text());
return JSON.parse(this.text());
}
return this._body;
@ -54,7 +52,7 @@ export abstract class Body {
}
if (isJsObject(this._body)) {
return Json.stringify(this._body);
return JSON.stringify(this._body, null, 2);
}
return this._body.toString();

View File

@ -8,7 +8,7 @@
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {isPresent, isString} from '../src/facade/lang';
import {isPresent} from '../src/facade/lang';
import {BaseRequestOptions, RequestOptions} from './base_request_options';
import {RequestMethod} from './enums';
import {ConnectionBackend, RequestOptionsArgs} from './interfaces';
@ -114,7 +114,7 @@ export class Http {
*/
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
let responseObservable: any;
if (isString(url)) {
if (typeof url === 'string') {
responseObservable = httpRequest(
this._backend,
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url)));
@ -212,7 +212,7 @@ export class Jsonp extends Http {
*/
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
let responseObservable: any;
if (isString(url)) {
if (typeof url === 'string') {
url =
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url));
}

View File

@ -6,12 +6,11 @@
* found in the LICENSE file at https://angular.io/license
*/
import {isString} from '../src/facade/lang';
import {RequestMethod} from './enums';
export function normalizeMethodName(method: string | RequestMethod): RequestMethod {
if (!isString(method)) return method;
if (typeof method !== 'string') return method;
switch (method.toUpperCase()) {
case 'GET':
return RequestMethod.Get;

View File

@ -15,7 +15,6 @@ import {CookieXSRFStrategy, XHRBackend, XHRConnection} from '../../src/backends/
import {BaseRequestOptions, RequestOptions} from '../../src/base_request_options';
import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options';
import {ResponseContentType, ResponseType} from '../../src/enums';
import {Json} from '../../src/facade/lang';
import {Headers} from '../../src/headers';
import {XSRFStrategy} from '../../src/interfaces';
import {Request} from '../../src/static_request';
@ -257,7 +256,7 @@ export function main() {
var connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
connection.response.subscribe();
expect(sendSpy).toHaveBeenCalledWith(Json.stringify(body));
expect(sendSpy).toHaveBeenCalledWith(JSON.stringify(body, null, 2));
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'application/json');
});

View File

@ -14,7 +14,6 @@ import {Observable} from 'rxjs/Observable';
import {zip} from 'rxjs/observable/zip';
import {BaseRequestOptions, ConnectionBackend, Http, HttpModule, JSONPBackend, Jsonp, JsonpModule, Request, RequestMethod, RequestOptions, Response, ResponseContentType, ResponseOptions, URLSearchParams, XHRBackend} from '../index';
import {Json} from '../src/facade/lang';
import {stringToArrayBuffer} from '../src/http_utils';
import {MockBackend, MockConnection} from '../testing/mock_backend';
@ -460,7 +459,7 @@ export function main() {
c.mockRespond(new Response(new ResponseOptions({body: simpleObject}))));
http.get('https://www.google.com').subscribe((res: Response) => {
expect(res.arrayBuffer()).toBeAnInstanceOf(ArrayBuffer);
expect(res.text()).toEqual(Json.stringify(simpleObject));
expect(res.text()).toEqual(JSON.stringify(simpleObject, null, 2));
expect(res.json()).toBe(simpleObject);
async.done();
});
@ -500,11 +499,11 @@ export function main() {
let body = (): any => {
switch (c.request.responseType) {
case ResponseContentType.Text:
return Json.stringify(message);
return JSON.stringify(message, null, 2);
case ResponseContentType.Json:
return message;
case ResponseContentType.ArrayBuffer:
return stringToArrayBuffer(Json.stringify(message));
return stringToArrayBuffer(JSON.stringify(message, null, 2));
}
};
c.mockRespond(new Response(new ResponseOptions({body: body()})));