codekingpro/portable-devtools
114k
1'use strict';2 3import {VERSION} from '../env/data.js';4import AxiosError from '../core/AxiosError.js';5 6const validators = {};7 8// eslint-disable-next-line func-names9['object', 'boolean', 'number', 'function', 'string', 'symbol'].forEach((type, i) => {10 validators[type] = function validator(thing) {11 return typeof thing === type || 'a' + (i < 1 ? 'n ' : ' ') + type;12 };13});14 15const deprecatedWarnings = {};16 17/**18 * Transitional option validator19 *20 * @param {function|boolean?} validator - set to false if the transitional option has been removed21 * @param {string?} version - deprecated version / removed since version22 * @param {string?} message - some message with additional info23 *24 * @returns {function}25 */26validators.transitional = function transitional(validator, version, message) {27 function formatMessage(opt, desc) {28 return '[Axios v' + VERSION + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');29 }30 31 // eslint-disable-next-line func-names32 return (value, opt, opts) => {33 if (validator === false) {34 throw new AxiosError(35 formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')),36 AxiosError.ERR_DEPRECATED37 );38 }39 40 if (version && !deprecatedWarnings[opt]) {41 deprecatedWarnings[opt] = true;42 // eslint-disable-next-line no-console43 console.warn(44 formatMessage(45 opt,46 ' has been deprecated since v' + version + ' and will be removed in the near future'47 )48 );49 }50 51 return validator ? validator(value, opt, opts) : true;52 };53};54 55/**56 * Assert object's properties type57 *58 * @param {object} options59 * @param {object} schema60 * @param {boolean?} allowUnknown61 *62 * @returns {object}63 */64 65function assertOptions(options, schema, allowUnknown) {66 if (typeof options !== 'object') {67 throw new AxiosError('options must be an object', AxiosError.ERR_BAD_OPTION_VALUE);68 }69 const keys = Object.keys(options);70 let i = keys.length;71 while (i-- > 0) {72 const opt = keys[i];73 const validator = schema[opt];74 if (validator) {75 const value = options[opt];76 const result = value === undefined || validator(value, opt, options);77 if (result !== true) {78 throw new AxiosError('option ' + opt + ' must be ' + result, AxiosError.ERR_BAD_OPTION_VALUE);79 }80 continue;81 }82 if (allowUnknown !== true) {83 throw new AxiosError('Unknown option ' + opt, AxiosError.ERR_BAD_OPTION);84 }85 }86}87 88export default {89 assertOptions,90 validators91};92 