binhend 2.1.15 → 2.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/demo.js +39 -4
- package/index.js +5 -3
- package/jsconfig.json +1 -0
- package/package.json +1 -1
- package/src/api/Router.js +1 -1
- package/src/api/routes.js +1 -1
- package/src/configuration.js +1 -1
- package/src/csd/controller.js +1 -1
- package/src/middleware/cors.js +1 -1
- package/src/middleware/parseBasicAuthToken.js +1 -1
- package/src/utils/{typeOf.js → types.js} +17 -1
- package/src/utils/validation.js +233 -0
- package/src/web/component.method.js +1 -1
- package/src/web/index.js +1 -1
- /package/src/utils/{bromise.js → Bromise.js} +0 -0
package/demo.js
CHANGED
|
@@ -1,10 +1,45 @@
|
|
|
1
1
|
|
|
2
|
-
const { HttpCodes, ConfigLoader, WebBuilder } = require('./index');
|
|
2
|
+
const { HttpCodes, ConfigLoader, WebBuilder, HttpError, validation } = require('./index');
|
|
3
3
|
|
|
4
|
-
HttpCodes.ACCEPTED;
|
|
4
|
+
// HttpCodes.ACCEPTED;
|
|
5
5
|
|
|
6
|
-
new ConfigLoader({}).cli;
|
|
6
|
+
// new ConfigLoader({}).cli;
|
|
7
7
|
|
|
8
8
|
// const builder = new WebBuilder('src', { output: 'build/bundle' });
|
|
9
9
|
|
|
10
|
-
// builder.bundle();
|
|
10
|
+
// builder.bundle();
|
|
11
|
+
|
|
12
|
+
// throw new HttpError(HttpCodes.BAD_REQUEST, '[BINHEND][VALIDATION] Must be a string.');
|
|
13
|
+
// throw new HttpError(HttpCodes.BAD_REQUEST, '[BINHEND][VALIDATION] MUST BE A STRING.');
|
|
14
|
+
// var typeName = `array`;
|
|
15
|
+
// throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: value not passed custom validator.`);
|
|
16
|
+
// throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: unknown reason.`);
|
|
17
|
+
// throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: value must be type of \`${typeName}\`.`);
|
|
18
|
+
// throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: Field "title" must be type of \`${typeName}\`.`);
|
|
19
|
+
|
|
20
|
+
// validation.String('a', {});
|
|
21
|
+
|
|
22
|
+
validation.String('a');
|
|
23
|
+
// validation.String(null);
|
|
24
|
+
// validation.String(1, { name: 'myNum', message: 'Require string for this field' });
|
|
25
|
+
|
|
26
|
+
// validation.Validator('this is me', (input) => {
|
|
27
|
+
// if (!input.startsWith('It')) validation.throwError(`"title" must start with 'it' or 'It'`)
|
|
28
|
+
// return input.length < 5;
|
|
29
|
+
// }, { message: '"title" must not exceed max length of 5' })
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
validation.NotNull(1);
|
|
33
|
+
// validation.NotNullish(null, { required: true, default: null });
|
|
34
|
+
// validation.String(null, { required: true, default: 1 });
|
|
35
|
+
|
|
36
|
+
validation.DateLike('2022-10-29a', { default: new Date().getTime() });
|
|
37
|
+
validation.Date(new Date());
|
|
38
|
+
|
|
39
|
+
function Abc(a) {
|
|
40
|
+
this.a = a;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
console.log(JSON.stringify(new Abc(123)));
|
|
44
|
+
|
|
45
|
+
validation.Object(new Abc(123));
|
package/index.js
CHANGED
|
@@ -22,8 +22,9 @@ const cors = require('./src/middleware/cors');
|
|
|
22
22
|
|
|
23
23
|
const createCSD = require('./src/csd');
|
|
24
24
|
const security = require('./src/security');
|
|
25
|
-
const
|
|
26
|
-
const
|
|
25
|
+
const types = require('./src/utils/types');
|
|
26
|
+
const validation = require('./src/utils/validation');
|
|
27
|
+
const Bromise = require('./src/utils/Bromise.js');
|
|
27
28
|
|
|
28
29
|
/** CLIENT - Frontend */
|
|
29
30
|
const { WebBuilder } = require('./src/web');
|
|
@@ -44,7 +45,8 @@ module.exports = {
|
|
|
44
45
|
cors,
|
|
45
46
|
createCSD,
|
|
46
47
|
security,
|
|
47
|
-
|
|
48
|
+
types,
|
|
49
|
+
validation,
|
|
48
50
|
Bromise,
|
|
49
51
|
WebBuilder,
|
|
50
52
|
binh
|
package/jsconfig.json
CHANGED
package/package.json
CHANGED
package/src/api/Router.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
const { ServerResponse } = require('http');
|
|
3
|
-
const { isFunction } = require('../utils/
|
|
3
|
+
const { isFunction } = require('../utils/types');
|
|
4
4
|
const responseErrorByDefault = require('./responseErrorByDefault');
|
|
5
5
|
const trycatch = require('./trycatch');
|
|
6
6
|
const express = require('express');
|
package/src/api/routes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const { readdir, stat } = require('fs/promises');
|
|
2
2
|
const { join, parse, resolve } = require('path');
|
|
3
|
-
const { isUndefined } = require('../utils/
|
|
3
|
+
const { isUndefined } = require('../utils/types');
|
|
4
4
|
const { server } = require('../server');
|
|
5
5
|
const cors = require('../middleware/cors');
|
|
6
6
|
const express = require('express');
|
package/src/configuration.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const { isEmptyArray, isArray, isObject } = require('./utils/
|
|
3
|
+
const { isEmptyArray, isArray, isObject } = require('./utils/types');
|
|
4
4
|
|
|
5
5
|
// @ts-ignore
|
|
6
6
|
function ConfigLoader(configObject, { module } = {}) {
|
package/src/csd/controller.js
CHANGED
package/src/middleware/cors.js
CHANGED
|
@@ -45,6 +45,15 @@ function mustNumber(input, defaultValue) {
|
|
|
45
45
|
return isNumber(input) ? input : isNumber(defaultValue) ? defaultValue : 0;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
function isDate(input) {
|
|
49
|
+
return typeOf(input) === 'Date' && !isNaN(input.getTime());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isDateLike(value) {
|
|
53
|
+
const date = new Date(value);
|
|
54
|
+
return !isNaN(date.getTime());
|
|
55
|
+
}
|
|
56
|
+
|
|
48
57
|
function isBigInt(input) {
|
|
49
58
|
return typeof input === 'bigint';
|
|
50
59
|
}
|
|
@@ -69,6 +78,10 @@ function isNullOrUndefined(input) {
|
|
|
69
78
|
return isNull(input) || isUndefined(input);
|
|
70
79
|
}
|
|
71
80
|
|
|
81
|
+
function isNullish(input) {
|
|
82
|
+
return input == null;
|
|
83
|
+
}
|
|
84
|
+
|
|
72
85
|
module.exports = {
|
|
73
86
|
typeOf,
|
|
74
87
|
isObject,
|
|
@@ -80,6 +93,7 @@ module.exports = {
|
|
|
80
93
|
isNull,
|
|
81
94
|
isUndefined,
|
|
82
95
|
isNullOrUndefined,
|
|
96
|
+
isNullish,
|
|
83
97
|
isBoolean,
|
|
84
98
|
isNumber,
|
|
85
99
|
mustNumber,
|
|
@@ -87,5 +101,7 @@ module.exports = {
|
|
|
87
101
|
isEmptyString,
|
|
88
102
|
mustString,
|
|
89
103
|
isSymbol,
|
|
90
|
-
isBigInt
|
|
104
|
+
isBigInt,
|
|
105
|
+
isDate,
|
|
106
|
+
isDateLike
|
|
91
107
|
};
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
const HttpError = require('../api/HttpError');
|
|
2
|
+
const HttpCodes = require('../api/HttpCodes');
|
|
3
|
+
const types = require('./types');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} Options
|
|
7
|
+
* @property {string=} message - Any custom error message
|
|
8
|
+
* @property {string=} type - Expected type name of value, e.g. string, number, array, etc.
|
|
9
|
+
* @property {string=} name - The variable/property name holding value
|
|
10
|
+
* @property {boolean=} required - The variable/property name holding value
|
|
11
|
+
* @property {*=} default - The variable/property name holding value
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Throw error by default for invalid input.
|
|
16
|
+
* A `HttpError` is thrown for `router` sending response to client.
|
|
17
|
+
*
|
|
18
|
+
* @param {string=} message - Optional error message.
|
|
19
|
+
* @throws {HttpError}
|
|
20
|
+
*/
|
|
21
|
+
function throwError(message) {
|
|
22
|
+
message = types.isString(message) ? message : `unknown reason`;
|
|
23
|
+
throw new HttpError(HttpCodes.BAD_REQUEST, `Invalid input: ${message}.`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Throw a type error with formatted message.
|
|
28
|
+
*
|
|
29
|
+
* @param {string=} typeName - The expected type name
|
|
30
|
+
* @param {string=} variableName - The name of the variable/property
|
|
31
|
+
* @throws {HttpError}
|
|
32
|
+
*/
|
|
33
|
+
function throwTypeError(typeName, variableName) {
|
|
34
|
+
typeName = types.isString(typeName) ? typeName : '?';
|
|
35
|
+
variableName = types.isString(variableName) ? `"${variableName}"` : `value`;
|
|
36
|
+
throwError(`${variableName} must be type of \`${typeName}\``);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Throw a validator error with formatted message.
|
|
41
|
+
*
|
|
42
|
+
* @param {string=} variableName - The name of the variable/property
|
|
43
|
+
* @throws {HttpError}
|
|
44
|
+
*/
|
|
45
|
+
function throwValidatorError(variableName) {
|
|
46
|
+
variableName = types.isString(variableName) ? `"${variableName}"` : `value`;
|
|
47
|
+
throwError(`${variableName} not passed custom validator`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Validate an input being string
|
|
52
|
+
*
|
|
53
|
+
* @param {*} input - Input value needs to be validated.
|
|
54
|
+
* @param {Options} options - Additional options for validation.
|
|
55
|
+
* @returns {string} - The validated input
|
|
56
|
+
* @throws {HttpError} - If validation fails
|
|
57
|
+
*/
|
|
58
|
+
function String(input, options = {}) {
|
|
59
|
+
return Validator(input, types.isString, { ...options, type: 'string' });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Validate an input being number
|
|
64
|
+
*
|
|
65
|
+
* @param {*} input - Input value needs to be validated.
|
|
66
|
+
* @param {Options} options - Additional options for validation.
|
|
67
|
+
* @returns {number} - The validated input
|
|
68
|
+
* @throws {HttpError} - If validation fails
|
|
69
|
+
*/
|
|
70
|
+
function Number(input, options = {}) {
|
|
71
|
+
return Validator(input, types.isNumber, { ...options, type: 'number' });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Validate an input being boolean
|
|
76
|
+
*
|
|
77
|
+
* @param {*} input - Input value needs to be validated.
|
|
78
|
+
* @param {Options} options - Additional options for validation.
|
|
79
|
+
* @returns {boolean} - The validated input
|
|
80
|
+
* @throws {HttpError} - If validation fails
|
|
81
|
+
*/
|
|
82
|
+
function Boolean(input, options = {}) {
|
|
83
|
+
return Validator(input, types.isBoolean, { ...options, type: 'boolean' });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Validate an input being an array
|
|
88
|
+
*
|
|
89
|
+
* @param {*} input - Input value needs to be validated.
|
|
90
|
+
* @param {Options} options - Additional options for validation.
|
|
91
|
+
* @returns {Array} - The validated input
|
|
92
|
+
* @throws {HttpError} - If validation fails
|
|
93
|
+
*/
|
|
94
|
+
function Array(input, options = {}) {
|
|
95
|
+
return Validator(input, types.isArray, { ...options, type: 'array' });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Validate an input being an object (non-array)
|
|
100
|
+
*
|
|
101
|
+
* @param {*} input - Input value needs to be validated.
|
|
102
|
+
* @param {Options} options - Additional options for validation.
|
|
103
|
+
* @returns {Object} - The validated input
|
|
104
|
+
* @throws {HttpError} - If validation fails
|
|
105
|
+
*/
|
|
106
|
+
function Object(input, options = {}) {
|
|
107
|
+
return Validator(input, types.isObject, { ...options, type: 'object' });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Validate an input being a function
|
|
112
|
+
*
|
|
113
|
+
* @param {*} input - Input value needs to be validated.
|
|
114
|
+
* @param {Options} options - Additional options for validation.
|
|
115
|
+
* @returns {Function} - The validated input
|
|
116
|
+
* @throws {HttpError} - If validation fails
|
|
117
|
+
*/
|
|
118
|
+
function Function(input, options = {}) {
|
|
119
|
+
return Validator(input, types.isFunction, { ...options, type: 'function' });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Validate an input being a date
|
|
124
|
+
*
|
|
125
|
+
* @param {*} input - Input value needs to be validated.
|
|
126
|
+
* @param {Options} options - Additional options for validation.
|
|
127
|
+
* @returns {Date} - The validated input
|
|
128
|
+
* @throws {HttpError} - If validation fails
|
|
129
|
+
*/
|
|
130
|
+
function Date(input, options = {}) {
|
|
131
|
+
return Validator(input, types.isDate, { ...options, type: 'date' });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Validate an input being a date format
|
|
136
|
+
*
|
|
137
|
+
* @param {*} input - Input value needs to be validated.
|
|
138
|
+
* @param {Options} options - Additional options for validation.
|
|
139
|
+
* @returns {*} - The validated input
|
|
140
|
+
* @throws {HttpError} - If validation fails
|
|
141
|
+
*/
|
|
142
|
+
function DateLike(input, options = {}) {
|
|
143
|
+
return Validator(input, types.isDateLike, { ...options, type: 'date-like' });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Validate an input being defined (not undefined)
|
|
148
|
+
*
|
|
149
|
+
* @param {*} input - Input value needs to be validated.
|
|
150
|
+
* @param {Options} options - Additional options for validation.
|
|
151
|
+
* @returns {*} - The validated input
|
|
152
|
+
* @throws {HttpError} - If validation fails
|
|
153
|
+
*/
|
|
154
|
+
function Defined(input, options = {}) {
|
|
155
|
+
return Validator(input, (input) => !types.isUndefined(input), { ...options, type: 'defined' });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Validate an input not being null (but accept undefined)
|
|
160
|
+
*
|
|
161
|
+
* @param {*} input - Input value needs to be validated.
|
|
162
|
+
* @param {Options} options - Additional options for validation.
|
|
163
|
+
* @returns {*} - The validated input
|
|
164
|
+
* @throws {HttpError} - If validation fails
|
|
165
|
+
*/
|
|
166
|
+
function NotNull(input, options = {}) {
|
|
167
|
+
return Validator(input, (input) => !types.isNull(input), { ...options, type: 'not null', required: true });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Validate an input not being null or undefined
|
|
172
|
+
*
|
|
173
|
+
* @param {*} input - Input value needs to be validated.
|
|
174
|
+
* @param {Options} options - Additional options for validation.
|
|
175
|
+
* @returns {*} - The validated input
|
|
176
|
+
* @throws {HttpError} - If validation fails
|
|
177
|
+
*/
|
|
178
|
+
function NotNullish(input, options = {}) {
|
|
179
|
+
return Validator(input, (input) => !types.isNullish(input), { ...options, type: 'not nullish', required: true });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Generic validator that accepts a validation function
|
|
184
|
+
*
|
|
185
|
+
* @param {*} input - Input value needs to be validated.
|
|
186
|
+
* @param {Function} validate - Validation function that returns boolean
|
|
187
|
+
* @param {Options} options - Additional options for validation.
|
|
188
|
+
* @returns {*} - The validated input
|
|
189
|
+
* @throws {HttpError} - If validation fails
|
|
190
|
+
*/
|
|
191
|
+
function Validator(input, validate, options = {}) {
|
|
192
|
+
if (validate(input)) {
|
|
193
|
+
return input;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// use default value (options.default - must also be valid) if has, when input is invalid
|
|
197
|
+
if (options.hasOwnProperty('default') && validate(options.default)) {
|
|
198
|
+
return options.default;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// allow null or undefined by default, set `options.required=true` for not allowing
|
|
202
|
+
if (!options.required && types.isNullish(input)) {
|
|
203
|
+
return input;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
var { message, type, name } = options;
|
|
207
|
+
|
|
208
|
+
if (message) {
|
|
209
|
+
throwError(message);
|
|
210
|
+
}
|
|
211
|
+
else if (type) {
|
|
212
|
+
throwTypeError(type, name);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
throwValidatorError(name)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
module.exports = {
|
|
220
|
+
String,
|
|
221
|
+
Number,
|
|
222
|
+
Boolean,
|
|
223
|
+
Array,
|
|
224
|
+
Object,
|
|
225
|
+
Function,
|
|
226
|
+
Date,
|
|
227
|
+
DateLike,
|
|
228
|
+
Defined,
|
|
229
|
+
NotNull,
|
|
230
|
+
NotNullish,
|
|
231
|
+
Validator,
|
|
232
|
+
throwError
|
|
233
|
+
};
|
|
@@ -4,7 +4,7 @@ const path = require('path');
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const UglifyCSS = require('uglifycss');
|
|
6
6
|
const Component = require('./component');
|
|
7
|
-
const { isString } = require('../utils/
|
|
7
|
+
const { isString } = require('../utils/types');
|
|
8
8
|
|
|
9
9
|
const binh = {};
|
|
10
10
|
|
package/src/web/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
|
|
3
3
|
const path = require('path');
|
|
4
|
-
const { isEmptyString, isString, mustString } = require('../utils/
|
|
4
|
+
const { isEmptyString, isString, mustString } = require('../utils/types');
|
|
5
5
|
const ComponentFormat = require('./component.format');
|
|
6
6
|
const ComponentBuild = require('./component.build');
|
|
7
7
|
const Component = require('./component');
|
|
File without changes
|