From 586c104ed0573522191e72b120bb51adb4a87842 Mon Sep 17 00:00:00 2001 From: cedoor Date: Sun, 8 Dec 2019 16:36:15 +0100 Subject: [PATCH] [fix] set correct json http get output --- package-lock.json | 2 +- src/http/get.js | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2225c43..bdd6109 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@cedoor/utils", - "version": "0.1.6", + "version": "0.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/http/get.js b/src/http/get.js index a530efb..890815a 100644 --- a/src/http/get.js +++ b/src/http/get.js @@ -7,19 +7,20 @@ export function get (url, body) { return new Promise(function (resolve, reject) { const http = new window.XMLHttpRequest() + const json = function (string) { + try { + return JSON.parse(string) + } catch (error) { + return string + } + } http.onreadystatechange = function () { if (http.readyState === 4) { if (http.status === 200) { - resolve(http.responseText) + resolve(json(http.responseText)) } else { - try { - const json = JSON.parse(http.responseText) - - reject(json) - } catch (error) { - reject(http.responseText) - } + reject(json(http.responseText)) } } }