Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 + '/');
Expand Down Expand Up @@ -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(() => {
Expand Down