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:
@@ -0,0 +1,105 @@
|
||||
'use strict';
|
||||
|
||||
var pkg = require('./../../package.json');
|
||||
|
||||
var validators = {};
|
||||
|
||||
// eslint-disable-next-line func-names
|
||||
['object', 'boolean', 'number', 'function', 'string', 'symbol'].forEach(function(type, i) {
|
||||
validators[type] = function validator(thing) {
|
||||
return typeof thing === type || 'a' + (i < 1 ? 'n ' : ' ') + type;
|
||||
};
|
||||
});
|
||||
|
||||
var deprecatedWarnings = {};
|
||||
var currentVerArr = pkg.version.split('.');
|
||||
|
||||
/**
|
||||
* Compare package versions
|
||||
* @param {string} version
|
||||
* @param {string?} thanVersion
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function isOlderVersion(version, thanVersion) {
|
||||
var pkgVersionArr = thanVersion ? thanVersion.split('.') : currentVerArr;
|
||||
var destVer = version.split('.');
|
||||
for (var i = 0; i < 3; i++) {
|
||||
if (pkgVersionArr[i] > destVer[i]) {
|
||||
return true;
|
||||
} else if (pkgVersionArr[i] < destVer[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitional option validator
|
||||
* @param {function|boolean?} validator
|
||||
* @param {string?} version
|
||||
* @param {string} message
|
||||
* @returns {function}
|
||||
*/
|
||||
validators.transitional = function transitional(validator, version, message) {
|
||||
var isDeprecated = version && isOlderVersion(version);
|
||||
|
||||
function formatMessage(opt, desc) {
|
||||
return '[Axios v' + pkg.version + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
|
||||
}
|
||||
|
||||
// eslint-disable-next-line func-names
|
||||
return function(value, opt, opts) {
|
||||
if (validator === false) {
|
||||
throw new Error(formatMessage(opt, ' has been removed in ' + version));
|
||||
}
|
||||
|
||||
if (isDeprecated && !deprecatedWarnings[opt]) {
|
||||
deprecatedWarnings[opt] = true;
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(
|
||||
formatMessage(
|
||||
opt,
|
||||
' has been deprecated since v' + version + ' and will be removed in the near future'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return validator ? validator(value, opt, opts) : true;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Assert object's properties type
|
||||
* @param {object} options
|
||||
* @param {object} schema
|
||||
* @param {boolean?} allowUnknown
|
||||
*/
|
||||
|
||||
function assertOptions(options, schema, allowUnknown) {
|
||||
if (typeof options !== 'object') {
|
||||
throw new TypeError('options must be an object');
|
||||
}
|
||||
var keys = Object.keys(options);
|
||||
var i = keys.length;
|
||||
while (i-- > 0) {
|
||||
var opt = keys[i];
|
||||
var validator = schema[opt];
|
||||
if (validator) {
|
||||
var value = options[opt];
|
||||
var result = value === undefined || validator(value, opt, options);
|
||||
if (result !== true) {
|
||||
throw new TypeError('option ' + opt + ' must be ' + result);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (allowUnknown !== true) {
|
||||
throw Error('Unknown option ' + opt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isOlderVersion: isOlderVersion,
|
||||
assertOptions: assertOptions,
|
||||
validators: validators
|
||||
};
|
||||
Reference in New Issue
Block a user