| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | import { VERSION } from '../env/data.js';
|
|---|
| 4 | import AxiosError from '../core/AxiosError.js';
|
|---|
| 5 |
|
|---|
| 6 | const validators = {};
|
|---|
| 7 |
|
|---|
| 8 | // eslint-disable-next-line func-names
|
|---|
| 9 | ['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 |
|
|---|
| 15 | const deprecatedWarnings = {};
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Transitional option validator
|
|---|
| 19 | *
|
|---|
| 20 | * @param {function|boolean?} validator - set to false if the transitional option has been removed
|
|---|
| 21 | * @param {string?} version - deprecated version / removed since version
|
|---|
| 22 | * @param {string?} message - some message with additional info
|
|---|
| 23 | *
|
|---|
| 24 | * @returns {function}
|
|---|
| 25 | */
|
|---|
| 26 | validators.transitional = function transitional(validator, version, message) {
|
|---|
| 27 | function formatMessage(opt, desc) {
|
|---|
| 28 | return (
|
|---|
| 29 | '[Axios v' +
|
|---|
| 30 | VERSION +
|
|---|
| 31 | "] Transitional option '" +
|
|---|
| 32 | opt +
|
|---|
| 33 | "'" +
|
|---|
| 34 | desc +
|
|---|
| 35 | (message ? '. ' + message : '')
|
|---|
| 36 | );
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | // eslint-disable-next-line func-names
|
|---|
| 40 | return (value, opt, opts) => {
|
|---|
| 41 | if (validator === false) {
|
|---|
| 42 | throw new AxiosError(
|
|---|
| 43 | formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')),
|
|---|
| 44 | AxiosError.ERR_DEPRECATED
|
|---|
| 45 | );
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | if (version && !deprecatedWarnings[opt]) {
|
|---|
| 49 | deprecatedWarnings[opt] = true;
|
|---|
| 50 | // eslint-disable-next-line no-console
|
|---|
| 51 | console.warn(
|
|---|
| 52 | formatMessage(
|
|---|
| 53 | opt,
|
|---|
| 54 | ' has been deprecated since v' + version + ' and will be removed in the near future'
|
|---|
| 55 | )
|
|---|
| 56 | );
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | return validator ? validator(value, opt, opts) : true;
|
|---|
| 60 | };
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | validators.spelling = function spelling(correctSpelling) {
|
|---|
| 64 | return (value, opt) => {
|
|---|
| 65 | // eslint-disable-next-line no-console
|
|---|
| 66 | console.warn(`${opt} is likely a misspelling of ${correctSpelling}`);
|
|---|
| 67 | return true;
|
|---|
| 68 | };
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * Assert object's properties type
|
|---|
| 73 | *
|
|---|
| 74 | * @param {object} options
|
|---|
| 75 | * @param {object} schema
|
|---|
| 76 | * @param {boolean?} allowUnknown
|
|---|
| 77 | *
|
|---|
| 78 | * @returns {object}
|
|---|
| 79 | */
|
|---|
| 80 |
|
|---|
| 81 | function assertOptions(options, schema, allowUnknown) {
|
|---|
| 82 | if (typeof options !== 'object') {
|
|---|
| 83 | throw new AxiosError('options must be an object', AxiosError.ERR_BAD_OPTION_VALUE);
|
|---|
| 84 | }
|
|---|
| 85 | const keys = Object.keys(options);
|
|---|
| 86 | let i = keys.length;
|
|---|
| 87 | while (i-- > 0) {
|
|---|
| 88 | const opt = keys[i];
|
|---|
| 89 | // Use hasOwnProperty so a polluted Object.prototype.<opt> cannot supply
|
|---|
| 90 | // a non-function validator and cause a TypeError.
|
|---|
| 91 | const validator = Object.prototype.hasOwnProperty.call(schema, opt) ? schema[opt] : undefined;
|
|---|
| 92 | if (validator) {
|
|---|
| 93 | const value = options[opt];
|
|---|
| 94 | const result = value === undefined || validator(value, opt, options);
|
|---|
| 95 | if (result !== true) {
|
|---|
| 96 | throw new AxiosError(
|
|---|
| 97 | 'option ' + opt + ' must be ' + result,
|
|---|
| 98 | AxiosError.ERR_BAD_OPTION_VALUE
|
|---|
| 99 | );
|
|---|
| 100 | }
|
|---|
| 101 | continue;
|
|---|
| 102 | }
|
|---|
| 103 | if (allowUnknown !== true) {
|
|---|
| 104 | throw new AxiosError('Unknown option ' + opt, AxiosError.ERR_BAD_OPTION);
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | export default {
|
|---|
| 110 | assertOptions,
|
|---|
| 111 | validators,
|
|---|
| 112 | };
|
|---|