binhend 2.1.18 → 2.1.20
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 +45 -0
- package/index.js +2 -2
- package/package.json +1 -1
- package/src/https.js +76 -79
- /package/src/{security.js → crypto.js} +0 -0
package/demo.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
const { HttpCodes, ConfigLoader, WebBuilder, HttpError, validation } = require('./index');
|
|
3
|
+
|
|
4
|
+
// HttpCodes.ACCEPTED;
|
|
5
|
+
|
|
6
|
+
// new ConfigLoader({}).cli;
|
|
7
|
+
|
|
8
|
+
// const builder = new WebBuilder('src', { output: 'build/bundle' });
|
|
9
|
+
|
|
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
|
@@ -21,7 +21,7 @@ const trycatch = require('./src/api/trycatch');
|
|
|
21
21
|
const cors = require('./src/middleware/cors');
|
|
22
22
|
|
|
23
23
|
const createCSD = require('./src/csd');
|
|
24
|
-
const
|
|
24
|
+
const crypto = require('./src/crypto');
|
|
25
25
|
const types = require('./src/utils/types');
|
|
26
26
|
const validation = require('./src/utils/validation');
|
|
27
27
|
const typedefs = require('./src/utils/typedefs');
|
|
@@ -45,7 +45,7 @@ module.exports = {
|
|
|
45
45
|
trycatch,
|
|
46
46
|
cors,
|
|
47
47
|
createCSD,
|
|
48
|
-
|
|
48
|
+
crypto,
|
|
49
49
|
types,
|
|
50
50
|
typedefs,
|
|
51
51
|
validation,
|
package/package.json
CHANGED
package/src/https.js
CHANGED
|
@@ -5,90 +5,87 @@ const https = require('https');
|
|
|
5
5
|
function HTTPS(url) {
|
|
6
6
|
this.url = url;
|
|
7
7
|
this.parameters = [];
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
var prototype = HTTPS.prototype;
|
|
11
|
-
|
|
12
|
-
prototype.get = function(callback) {
|
|
13
|
-
return this.request('GET', callback);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
prototype.post = function(callback) {
|
|
17
|
-
return this.request('POST', callback);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
prototype.request = function(method, callback) {
|
|
21
|
-
|
|
22
|
-
var URL = this.url + this.parameters.join('/') + (this.queries || '');
|
|
23
8
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
9
|
+
this.get = function(callback) {
|
|
10
|
+
return this.request('GET', callback);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
this.post = function(callback) {
|
|
14
|
+
return this.request('POST', callback);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.request = function(method, callback) {
|
|
18
|
+
var URL = this.url + this.parameters.join('/') + (this.queries || '');
|
|
19
|
+
|
|
20
|
+
var options = {
|
|
21
|
+
method,
|
|
22
|
+
headers: this.headers
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
var req = https.request(URL, options, function(res) {
|
|
26
|
+
res.setEncoding('utf8');
|
|
27
|
+
|
|
28
|
+
var data = '';
|
|
29
|
+
|
|
30
|
+
res.on('data', (chunk) => {
|
|
31
|
+
data += chunk;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
res.on('end', () => {
|
|
35
|
+
var response;
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
response = JSON.parse(data);
|
|
39
|
+
}
|
|
40
|
+
catch(error) {
|
|
41
|
+
response = data;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (callback instanceof Function) {
|
|
45
|
+
callback(response);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
36
48
|
});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
response = JSON.parse(data);
|
|
43
|
-
}
|
|
44
|
-
catch(error) {
|
|
45
|
-
response = data;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (callback instanceof Function) {
|
|
49
|
-
callback(response);
|
|
50
|
-
}
|
|
49
|
+
|
|
50
|
+
req.on("error", (err) => {
|
|
51
|
+
console.log("HTTPS request encounters error:" + err.message);
|
|
51
52
|
});
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
|
|
54
|
+
if (this.payload) {
|
|
55
|
+
req.write(JSON.stringify(this.payload));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
req.end();
|
|
59
|
+
|
|
60
|
+
return this;
|
|
60
61
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
prototype.header = function(map) {
|
|
68
|
-
this.headers = map;
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
prototype.body = function(map) {
|
|
73
|
-
this.payload = JSON.stringify(map);
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
prototype.parameter = function(array) {
|
|
78
|
-
if (array instanceof Array) {
|
|
79
|
-
var last_char = this.url[this.url.length - 1];
|
|
80
|
-
this.parameters = (last_char === '/' ? [] : ['']).concat(array);
|
|
62
|
+
|
|
63
|
+
this.header = function(map) {
|
|
64
|
+
this.headers = map;
|
|
65
|
+
return this;
|
|
81
66
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
var query_string = '';
|
|
87
|
-
for (var key in map) {
|
|
88
|
-
query_string += key + '=' + map[key] + '&';
|
|
67
|
+
|
|
68
|
+
this.body = function(map) {
|
|
69
|
+
this.payload = JSON.stringify(map);
|
|
70
|
+
return this;
|
|
89
71
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
72
|
+
|
|
73
|
+
this.parameter = function(array) {
|
|
74
|
+
if (array instanceof Array) {
|
|
75
|
+
var last_char = this.url[this.url.length - 1];
|
|
76
|
+
this.parameters = (last_char === '/' ? [] : ['']).concat(array);
|
|
77
|
+
}
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.query = function(map) {
|
|
82
|
+
var query_string = '';
|
|
83
|
+
for (var key in map) {
|
|
84
|
+
query_string += key + '=' + map[key] + '&';
|
|
85
|
+
}
|
|
86
|
+
this.queries = '?' + query_string;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
93
90
|
|
|
94
91
|
module.exports = { HTTPS };
|
|
File without changes
|