Skip to content

Commit 6bb833f

Browse files
committed
fix: prevent JSON parsing when responseType is text
1 parent ad40de9 commit 6bb833f

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function create(defaults) {
109109
* @param {(...args: Args[]) => R} fn
110110
* @returns {(array: Args[]) => R}
111111
*/
112-
redaxios.spread = (fn) => /** @type {any} */ (fn.apply.bind(fn, fn));
112+
redaxios.spread = (fn) => /** @type {any} */(fn.apply.bind(fn, fn));
113113

114114
/**
115115
* @private
@@ -181,7 +181,7 @@ function create(defaults) {
181181
// @ts-ignore accessing match()[2] throws for no match, which is intentional
182182
document.cookie.match(RegExp('(^|; )' + options.xsrfCookieName + '=([^;]*)'))[2]
183183
);
184-
} catch (e) {}
184+
} catch (e) { }
185185

186186
if (options.baseURL) {
187187
url = url.replace(/^(?!.*\/\/)\/?/, options.baseURL + '/');
@@ -213,8 +213,16 @@ function create(defaults) {
213213
return res[options.responseType || 'text']()
214214
.then((data) => {
215215
response.data = data;
216-
// its okay if this fails: response.data will be the unparsed value:
217-
response.data = JSON.parse(data);
216+
217+
// Only attempt to parse if the user actually wants JSON
218+
// or if using the default 'json' type
219+
if (!options.responseType || options.responseType === 'json') {
220+
try {
221+
response.data = JSON.parse(data);
222+
} catch (e) {
223+
// do nothing and retur the no parsed data
224+
}
225+
}
218226
})
219227
.catch(Object)
220228
.then(() => {

0 commit comments

Comments
 (0)