Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/axios/lib/helpers/validator.js
1126 views
1
'use strict';
2
3
var VERSION = require('../env/data').version;
4
5
var validators = {};
6
7
// eslint-disable-next-line func-names
8
['object', 'boolean', 'number', 'function', 'string', 'symbol'].forEach(function(type, i) {
9
validators[type] = function validator(thing) {
10
return typeof thing === type || 'a' + (i < 1 ? 'n ' : ' ') + type;
11
};
12
});
13
14
var deprecatedWarnings = {};
15
16
/**
17
* Transitional option validator
18
* @param {function|boolean?} validator - set to false if the transitional option has been removed
19
* @param {string?} version - deprecated version / removed since version
20
* @param {string?} message - some message with additional info
21
* @returns {function}
22
*/
23
validators.transitional = function transitional(validator, version, message) {
24
function formatMessage(opt, desc) {
25
return '[Axios v' + VERSION + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
26
}
27
28
// eslint-disable-next-line func-names
29
return function(value, opt, opts) {
30
if (validator === false) {
31
throw new Error(formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')));
32
}
33
34
if (version && !deprecatedWarnings[opt]) {
35
deprecatedWarnings[opt] = true;
36
// eslint-disable-next-line no-console
37
console.warn(
38
formatMessage(
39
opt,
40
' has been deprecated since v' + version + ' and will be removed in the near future'
41
)
42
);
43
}
44
45
return validator ? validator(value, opt, opts) : true;
46
};
47
};
48
49
/**
50
* Assert object's properties type
51
* @param {object} options
52
* @param {object} schema
53
* @param {boolean?} allowUnknown
54
*/
55
56
function assertOptions(options, schema, allowUnknown) {
57
if (typeof options !== 'object') {
58
throw new TypeError('options must be an object');
59
}
60
var keys = Object.keys(options);
61
var i = keys.length;
62
while (i-- > 0) {
63
var opt = keys[i];
64
var validator = schema[opt];
65
if (validator) {
66
var value = options[opt];
67
var result = value === undefined || validator(value, opt, options);
68
if (result !== true) {
69
throw new TypeError('option ' + opt + ' must be ' + result);
70
}
71
continue;
72
}
73
if (allowUnknown !== true) {
74
throw Error('Unknown option ' + opt);
75
}
76
}
77
}
78
79
module.exports = {
80
assertOptions: assertOptions,
81
validators: validators
82
};
83
84