mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
JSON improvements: throw if JSON parsing failed; number, boolean can be passed directly as payload for encoding to JSON #2613, #61, #907 (#3688)
* Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option #2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts);
This commit is contained in:
+27
-6
@@ -2,6 +2,7 @@
|
||||
|
||||
var utils = require('./utils');
|
||||
var normalizeHeaderName = require('./helpers/normalizeHeaderName');
|
||||
var enhanceError = require('./core/enhanceError');
|
||||
|
||||
var DEFAULT_CONTENT_TYPE = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
@@ -26,11 +27,19 @@ function getDefaultAdapter() {
|
||||
}
|
||||
|
||||
var defaults = {
|
||||
|
||||
transitional: {
|
||||
silentJSONParsing: true,
|
||||
forcedJSONParsing: true,
|
||||
clarifyTimeoutError: false
|
||||
},
|
||||
|
||||
adapter: getDefaultAdapter(),
|
||||
|
||||
transformRequest: [function transformRequest(data, headers) {
|
||||
normalizeHeaderName(headers, 'Accept');
|
||||
normalizeHeaderName(headers, 'Content-Type');
|
||||
|
||||
if (utils.isFormData(data) ||
|
||||
utils.isArrayBuffer(data) ||
|
||||
utils.isBuffer(data) ||
|
||||
@@ -47,7 +56,7 @@ var defaults = {
|
||||
setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
|
||||
return data.toString();
|
||||
}
|
||||
if (utils.isObject(data)) {
|
||||
if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) {
|
||||
setContentTypeIfUnset(headers, 'application/json;charset=utf-8');
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
@@ -55,13 +64,25 @@ var defaults = {
|
||||
}],
|
||||
|
||||
transformResponse: [function transformResponse(data) {
|
||||
var result = data;
|
||||
if (utils.isString(result) && result.length) {
|
||||
var transitional = this.transitional;
|
||||
var silentJSONParsing = transitional && transitional.silentJSONParsing;
|
||||
var forcedJSONParsing = transitional && transitional.forcedJSONParsing;
|
||||
var strictJSONParsing = !silentJSONParsing && this.responseType === 'json';
|
||||
|
||||
if (strictJSONParsing || (forcedJSONParsing && utils.isString(data) && data.length)) {
|
||||
try {
|
||||
result = JSON.parse(result);
|
||||
} catch (e) { /* Ignore */ }
|
||||
return JSON.parse(data);
|
||||
} catch (e) {
|
||||
if (strictJSONParsing) {
|
||||
if (e.name === 'SyntaxError') {
|
||||
throw enhanceError(e, this, 'E_JSON_PARSE');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
return data;
|
||||
}],
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user