diff --git a/src/index.js b/src/index.js index cca6534..aead3c8 100644 --- a/src/index.js +++ b/src/index.js @@ -109,7 +109,7 @@ function create(defaults) { * @param {(...args: Args[]) => R} fn * @returns {(array: Args[]) => R} */ - redaxios.spread = (fn) => /** @type {any} */ (fn.apply.bind(fn, fn)); + redaxios.spread = (fn) => /** @type {any} */(fn.apply.bind(fn, fn)); /** * @private @@ -181,7 +181,7 @@ function create(defaults) { // @ts-ignore accessing match()[2] throws for no match, which is intentional document.cookie.match(RegExp('(^|; )' + options.xsrfCookieName + '=([^;]*)'))[2] ); - } catch (e) {} + } catch (e) { } if (options.baseURL) { url = url.replace(/^(?!.*\/\/)\/?/, options.baseURL + '/'); @@ -213,8 +213,16 @@ function create(defaults) { return res[options.responseType || 'text']() .then((data) => { response.data = data; - // its okay if this fails: response.data will be the unparsed value: - response.data = JSON.parse(data); + + // Only attempt to parse if the user actually wants JSON + // or if using the default 'json' type + if (!options.responseType || options.responseType === 'json') { + try { + response.data = JSON.parse(data); + } catch (e) { + // do nothing and retur the no parsed data + } + } }) .catch(Object) .then(() => {