From 2cdd1f2fb33676d2c786da20404011f692086564 Mon Sep 17 00:00:00 2001 From: vakrilov Date: Thu, 21 Jul 2016 14:49:47 +0300 Subject: [PATCH] Add toString() method on response whtn type is text --- tns-core-modules/utils/types.d.ts | 7 ++++++ tns-core-modules/utils/types.ts | 7 ++++++ tns-core-modules/xhr/xhr.ts | 42 +++++++++++++++---------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/tns-core-modules/utils/types.d.ts b/tns-core-modules/utils/types.d.ts index 59640a2d6..d6d725bbf 100644 --- a/tns-core-modules/utils/types.d.ts +++ b/tns-core-modules/utils/types.d.ts @@ -19,6 +19,13 @@ * Returns true if value is a function. */ export function isFunction(value: any): boolean; + + /** + * A function that checks if something is an object. + * @param value The value which will be checked. + * Returns true if value is an object. + */ + export function isObject(value: any): boolean; /** * A function that checks if something is "undefined". diff --git a/tns-core-modules/utils/types.ts b/tns-core-modules/utils/types.ts index 7b52ba8cd..0c526e2a9 100644 --- a/tns-core-modules/utils/types.ts +++ b/tns-core-modules/utils/types.ts @@ -13,6 +13,13 @@ export function isFunction(value: any): boolean { return typeof value === "function"; } +export function isObject(value: any): boolean { + if (!value) { + return false; + } + return typeof value === "object"; +} + export function isUndefined(value: any): boolean { return typeof value === "undefined"; } diff --git a/tns-core-modules/xhr/xhr.ts b/tns-core-modules/xhr/xhr.ts index 5aaa32748..720b2abd2 100644 --- a/tns-core-modules/xhr/xhr.ts +++ b/tns-core-modules/xhr/xhr.ts @@ -81,7 +81,7 @@ export class XMLHttpRequest { this._options.content = (data).toString(); } - http.request(this._options).then(r=> { + http.request(this._options).then(r => { if (!this._errorFlag) { this._loadResponse(r); } @@ -104,29 +104,27 @@ export class XMLHttpRequest { this._setResponseType(); - if (this.responseType === XMLHttpRequestResponseType.json) { - this._prepareJsonResponse(r); + this._responseTextReader = () => r.content.toString(); + this._addToStringOnResponse(); - } else if (this.responseType === XMLHttpRequestResponseType.empty || - this.responseType === XMLHttpRequestResponseType.text) { - this._responseTextReader = () => r.content.toString(); + if (this.responseType === XMLHttpRequestResponseType.json) { + this._response = JSON.parse(this.responseText); } this._setReadyState(this.DONE); } - private _prepareJsonResponse(r) { - this._responseTextReader = () => r.content.toString(); - this._response = JSON.parse(this.responseText); - + private _addToStringOnResponse() { // Add toString() method to ease debugging and // make Angular2 response.text() method work properly. - Object.defineProperty(this._response, "toString", { - configurable: true, - enumerable: false, - writable: true, - value: () => this.responseText - }); + if (types.isObject(this.response)) { + Object.defineProperty(this._response, "toString", { + configurable: true, + enumerable: false, + writable: true, + value: () => this.responseText + }); + } } private _setResponseType() { @@ -180,9 +178,9 @@ export class XMLHttpRequest { return ""; } - var result = ""; + let result = ""; - for (var i in this._headers) { + for (let i in this._headers) { // Cookie headers are excluded if (i !== "set-cookie" && i !== "set-cookie2") { result += i + ": " + this._headers[i] + "\r\n"; @@ -197,7 +195,7 @@ export class XMLHttpRequest { && !this._errorFlag ) { header = header.toLowerCase(); - for (var i in this._headers) { + for (let i in this._headers) { if (i.toLowerCase() === header) { return this._headers[i]; } @@ -275,7 +273,7 @@ export class XMLHttpRequest { } } -var statuses = { +const statuses = { 100: "Continue", 101: "Switching Protocols", 200: "OK", @@ -330,9 +328,9 @@ export class FormData { } toString(): string { - var arr = new Array(); + let arr = new Array(); - this._data.forEach(function(value, name, map) { + this._data.forEach(function (value, name, map) { arr.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`); });