@modular-rest/server 1.3.3 → 1.4.1
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/package.json +1 -1
- package/src/application.js +7 -7
- package/src/class/paginator.js +20 -9
- package/src/class/reply.js +13 -11
- package/src/class/validator.js +32 -27
- package/src/index.js +27 -6
- package/src/services/data_provider/service.js +1 -1
package/package.json
CHANGED
package/src/application.js
CHANGED
|
@@ -12,8 +12,8 @@ let defaultServiceRoot = __dirname + '/services';
|
|
|
12
12
|
/**
|
|
13
13
|
* @param {{
|
|
14
14
|
* cors: any; // Options for @koa/cors middleware.
|
|
15
|
-
*
|
|
16
|
-
*
|
|
15
|
+
* modulesPath: string; // Root directory of your router.js/db.js files.
|
|
16
|
+
* staticPath: string; // Root directory for uploaded files.
|
|
17
17
|
* onBeforeInit: (koaApp) => void; // A callback called before initializing the Koa server.
|
|
18
18
|
* onAfterInit: (koaApp) => void; // A callback called after server initialization.
|
|
19
19
|
* port: number; // Server port.
|
|
@@ -71,9 +71,9 @@ module.exports = async function createRest(options) {
|
|
|
71
71
|
/**
|
|
72
72
|
* Plug In KoaStatic
|
|
73
73
|
*/
|
|
74
|
-
if (options.
|
|
74
|
+
if (options.staticPath)
|
|
75
75
|
app.use(koaStatic({
|
|
76
|
-
rootDir: options.
|
|
76
|
+
rootDir: options.staticPath,
|
|
77
77
|
rootPath: '/assets/',
|
|
78
78
|
|
|
79
79
|
}));
|
|
@@ -118,15 +118,15 @@ module.exports = async function createRest(options) {
|
|
|
118
118
|
*
|
|
119
119
|
* Plug in routes and database
|
|
120
120
|
*/
|
|
121
|
-
if (options.
|
|
121
|
+
if (options.modulesPath) {
|
|
122
122
|
|
|
123
123
|
// Plug in user routes
|
|
124
|
-
await Combination.combineRoutesByFilePath(options.
|
|
124
|
+
await Combination.combineRoutesByFilePath(options.modulesPath, app);
|
|
125
125
|
|
|
126
126
|
// Collect user CollectionDefinitions (db.js files)
|
|
127
127
|
let userDatabaseDetail = [];
|
|
128
128
|
userDatabaseDetail = await Combination.combineModulesByFilePath({
|
|
129
|
-
rootDirectory: options.
|
|
129
|
+
rootDirectory: options.modulesPath,
|
|
130
130
|
filename: {
|
|
131
131
|
name: 'db',
|
|
132
132
|
extension: '.js'
|
package/src/class/paginator.js
CHANGED
|
@@ -1,19 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Creates a pagination object based on the given parameters.
|
|
3
|
+
* @param {number} count - The total number of items to paginate.
|
|
4
|
+
* @param {number} perPage - The number of items to display per page.
|
|
5
|
+
* @param {number} page - The current page number.
|
|
6
|
+
* @returns {Object} - An object containing pagination information.
|
|
7
|
+
*/
|
|
8
|
+
function create(count, perPage, page) {
|
|
3
9
|
let totalPgaes = Math.ceil(count / perPage);
|
|
4
|
-
if(page > totalPgaes) page = 1;
|
|
10
|
+
if (page > totalPgaes) page = 1;
|
|
5
11
|
|
|
6
12
|
let from = 0;
|
|
7
|
-
if(perPage == 1) from = page-1;
|
|
13
|
+
if (perPage == 1) from = page - 1;
|
|
8
14
|
else from = (perPage * page) - perPage;
|
|
9
15
|
|
|
10
|
-
if(page <= 1) from = 0;
|
|
11
|
-
|
|
12
|
-
let result = {
|
|
13
|
-
|
|
16
|
+
if (page <= 1) from = 0;
|
|
17
|
+
|
|
18
|
+
let result = {
|
|
19
|
+
'pages': totalPgaes,
|
|
20
|
+
'page': page,
|
|
21
|
+
'from': from,
|
|
22
|
+
'to': perPage
|
|
23
|
+
};
|
|
24
|
+
|
|
14
25
|
return result;
|
|
15
26
|
};
|
|
16
27
|
|
|
17
28
|
module.exports = {
|
|
18
|
-
|
|
29
|
+
create,
|
|
19
30
|
}
|
package/src/class/reply.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* more detail
|
|
10
|
-
*/
|
|
1
|
+
/**
|
|
2
|
+
* Creates a response object with the given status and detail.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} status - The status of the response. Can be "s" for success, "f" for fail, or "e" for error.
|
|
5
|
+
* @param {Object} [detail={}] - The detail of the response. Can contain any additional information about the response.
|
|
6
|
+
* @returns {Object} - The response object with the given status and detail.
|
|
7
|
+
*/
|
|
8
|
+
function create(status, detail = {}) {
|
|
11
9
|
|
|
12
10
|
let result = detail || {};
|
|
13
11
|
|
|
@@ -20,7 +18,7 @@ module.exports.create = function(status, detail={})
|
|
|
20
18
|
case 'f':
|
|
21
19
|
result['status'] = 'fail';
|
|
22
20
|
break;
|
|
23
|
-
|
|
21
|
+
|
|
24
22
|
case 'e':
|
|
25
23
|
result['status'] = 'error';
|
|
26
24
|
break;
|
|
@@ -32,4 +30,8 @@ module.exports.create = function(status, detail={})
|
|
|
32
30
|
|
|
33
31
|
// return
|
|
34
32
|
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
create
|
|
35
37
|
}
|
package/src/class/validator.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Validates an object by checking if it contains all the required fields.
|
|
3
|
+
* @param {Object} obj - The object to be validated.
|
|
4
|
+
* @param {string|Object} requiredFields - The list of required fields. If it's a string, it should contain keys separated by spaces. If it's an object, it should contain key-value pairs where the key is the field name and the value is a boolean indicating whether the field is required or not.
|
|
5
|
+
* @returns {boolean} - Returns true if the object contains all the required fields, otherwise returns false.
|
|
6
|
+
* @throws {string} - Throws an error if the requiredFields parameter is not a string or an object.
|
|
7
|
+
*/
|
|
8
|
+
function validate(obj, requiredFields) {
|
|
3
9
|
/*
|
|
4
10
|
this method could validate an Object by given field's name list and return bool.
|
|
5
11
|
- requiredFields: is a string that contains keys being spareted by " ".
|
|
@@ -7,18 +13,19 @@ module.exports = function(obj, requiredFields)
|
|
|
7
13
|
let type = typeof requiredFields;
|
|
8
14
|
let result;
|
|
9
15
|
|
|
10
|
-
if(type == 'string')
|
|
16
|
+
if (type == 'string')
|
|
11
17
|
result = ckeckSimple(obj, requiredFields);
|
|
12
|
-
else if(type == 'object')
|
|
18
|
+
else if (type == 'object')
|
|
13
19
|
result = checkComplex(obj, requiredFields);
|
|
14
20
|
|
|
15
|
-
else throw('requiredFields has wrong form, it must be string or object');
|
|
21
|
+
else throw ('requiredFields has wrong form, it must be string or object');
|
|
16
22
|
|
|
17
23
|
return result;
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
module.exports = validate;
|
|
27
|
+
|
|
28
|
+
function ckeckSimple(obj, requiredFields = '') {
|
|
22
29
|
let isValide = false;
|
|
23
30
|
let requires = requiredFields.split(' ');
|
|
24
31
|
|
|
@@ -26,22 +33,20 @@ function ckeckSimple(obj, requiredFields='')
|
|
|
26
33
|
let notValidKeys = [];
|
|
27
34
|
|
|
28
35
|
// return if obj is null
|
|
29
|
-
if(obj == null) return _returnResult(isValide, requires);
|
|
36
|
+
if (obj == null) return _returnResult(isValide, requires);
|
|
30
37
|
|
|
31
|
-
requires.forEach(key =>
|
|
32
|
-
|
|
33
|
-
if (obj[key])
|
|
38
|
+
requires.forEach(key => {
|
|
39
|
+
if (obj[key])
|
|
34
40
|
validMembers++;
|
|
35
41
|
else notValidKeys.push(key);
|
|
36
42
|
});
|
|
37
|
-
|
|
43
|
+
|
|
38
44
|
// check validation
|
|
39
45
|
isValide = (requires.length == validMembers) ? true : false;
|
|
40
46
|
return _returnResult(isValide, notValidKeys);
|
|
41
47
|
}
|
|
42
48
|
|
|
43
|
-
function checkComplex(obj, requiredFields={})
|
|
44
|
-
{
|
|
49
|
+
function checkComplex(obj, requiredFields = {}) {
|
|
45
50
|
let isValide = false;
|
|
46
51
|
let requireKeys = Object.keys(requiredFields);
|
|
47
52
|
|
|
@@ -49,38 +54,38 @@ function checkComplex(obj, requiredFields={})
|
|
|
49
54
|
let notValidKeys = [];
|
|
50
55
|
|
|
51
56
|
// return if obj is null
|
|
52
|
-
if(obj == null) return _returnResult(isValide, requireKeys);
|
|
57
|
+
if (obj == null) return _returnResult(isValide, requireKeys);
|
|
53
58
|
|
|
54
|
-
for (let i = 0; i < requireKeys.length; i++)
|
|
55
|
-
{
|
|
59
|
+
for (let i = 0; i < requireKeys.length; i++) {
|
|
56
60
|
const key = requireKeys[i];
|
|
57
61
|
let isValidField = false;
|
|
58
62
|
|
|
59
63
|
// if field has specific values
|
|
60
|
-
if(requiredFields[key].length > 0)
|
|
61
|
-
{
|
|
64
|
+
if (requiredFields[key].length > 0) {
|
|
62
65
|
let expectedValues = requiredFields[key].split(' ');
|
|
63
66
|
|
|
64
|
-
if(typeof expectedValues != 'object')
|
|
65
|
-
throw(`${key} must be array of strings`);
|
|
67
|
+
if (typeof expectedValues != 'object')
|
|
68
|
+
throw (`${key} must be array of strings`);
|
|
66
69
|
|
|
67
70
|
expectedValues.forEach(value => {
|
|
68
|
-
if(obj[key] == value) isValidField = true;
|
|
71
|
+
if (obj[key] == value) isValidField = true;
|
|
69
72
|
})
|
|
70
73
|
}
|
|
71
74
|
// else does not has specific value
|
|
72
|
-
else if(obj[key] != null) isValidField = true;
|
|
75
|
+
else if (obj[key] != null) isValidField = true;
|
|
73
76
|
|
|
74
77
|
if (isValidField) validMembers++;
|
|
75
78
|
else notValidKeys.push(key);
|
|
76
79
|
}
|
|
77
|
-
|
|
80
|
+
|
|
78
81
|
// check validation
|
|
79
82
|
isValide = (requireKeys.length == validMembers) ? true : false;
|
|
80
83
|
return _returnResult(isValide, notValidKeys);
|
|
81
84
|
}
|
|
82
85
|
|
|
83
|
-
function _returnResult(isValide, notValidKeys)
|
|
84
|
-
{
|
|
85
|
-
|
|
86
|
+
function _returnResult(isValide, notValidKeys) {
|
|
87
|
+
return {
|
|
88
|
+
'isValid': isValide,
|
|
89
|
+
'requires': notValidKeys
|
|
90
|
+
};
|
|
86
91
|
}
|
package/src/index.js
CHANGED
|
@@ -6,7 +6,9 @@ const Schema = require('mongoose').Schema;
|
|
|
6
6
|
const paginator = require('./class/paginator');
|
|
7
7
|
const reply = require('./class/reply');
|
|
8
8
|
const validator = require('./class/validator');
|
|
9
|
-
const {
|
|
9
|
+
const {
|
|
10
|
+
getCollection
|
|
11
|
+
} = require('./services/data_provider/service');
|
|
10
12
|
const TypeCasters = require('./services/data_provider/typeCasters');
|
|
11
13
|
|
|
12
14
|
// Base class
|
|
@@ -20,11 +22,31 @@ const middleware = require('./middlewares');
|
|
|
20
22
|
module.exports = {
|
|
21
23
|
createRest,
|
|
22
24
|
|
|
25
|
+
//
|
|
23
26
|
// Utilities
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
//
|
|
28
|
+
reply,
|
|
29
|
+
TypeCasters,
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @type {import('./class/paginator').create}
|
|
33
|
+
*/
|
|
34
|
+
paginator,
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @type {import('./class/validator')}
|
|
38
|
+
*/
|
|
39
|
+
validator,
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @type {import('./services/data_provider/service').getCollection}
|
|
43
|
+
* @return {import('mongoose').Model} Mongoose model
|
|
44
|
+
*/
|
|
45
|
+
getCollection,
|
|
46
|
+
|
|
47
|
+
//
|
|
27
48
|
// Base class
|
|
49
|
+
//
|
|
28
50
|
CollectionDefinition,
|
|
29
51
|
Schemas,
|
|
30
52
|
Schema,
|
|
@@ -32,5 +54,4 @@ module.exports = {
|
|
|
32
54
|
|
|
33
55
|
...SecurityClass,
|
|
34
56
|
middleware
|
|
35
|
-
}
|
|
36
|
-
|
|
57
|
+
}
|
|
@@ -222,8 +222,8 @@ function performAdditionalOptionsToQueryObject(queryObj, options) {
|
|
|
222
222
|
|
|
223
223
|
module.exports = {
|
|
224
224
|
name,
|
|
225
|
-
addCollectionDefinitionByList,
|
|
226
225
|
getCollection,
|
|
226
|
+
addCollectionDefinitionByList,
|
|
227
227
|
checkAccess,
|
|
228
228
|
getAsID,
|
|
229
229
|
performPopulateToQueryObject,
|