source: imaps-frontend/node_modules/axios/lib/helpers/validator.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.5 KB
Line 
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-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
15const 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 */
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-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
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 validators
91};
Note: See TracBrowser for help on using the repository browser.