|
|
'use strict'; |
|
|
|
|
|
var pkg = require('./../../package.json'); |
|
|
|
|
|
var validators = {}; |
|
|
|
|
|
|
|
|
['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('.'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 : ''); |
|
|
} |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
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; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
}; |
|
|
|