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 '[Axios v' + VERSION + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
|
---|
29 | }
|
---|
30 |
|
---|
31 | // eslint-disable-next-line func-names
|
---|
32 | return (value, opt, opts) => {
|
---|
33 | if (validator === false) {
|
---|
34 | throw new AxiosError(
|
---|
35 | formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')),
|
---|
36 | AxiosError.ERR_DEPRECATED
|
---|
37 | );
|
---|
38 | }
|
---|
39 |
|
---|
40 | if (version && !deprecatedWarnings[opt]) {
|
---|
41 | deprecatedWarnings[opt] = true;
|
---|
42 | // eslint-disable-next-line no-console
|
---|
43 | 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 type
|
---|
57 | *
|
---|
58 | * @param {object} options
|
---|
59 | * @param {object} schema
|
---|
60 | * @param {boolean?} allowUnknown
|
---|
61 | *
|
---|
62 | * @returns {object}
|
---|
63 | */
|
---|
64 |
|
---|
65 | function 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 |
|
---|
88 | export default {
|
---|
89 | assertOptions,
|
---|
90 | validators
|
---|
91 | };
|
---|