@modular-rest/server 1.11.13 → 1.12.0
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/.nvmrc +1 -0
- package/.prettierrc.json +9 -0
- package/.releaserc.json +24 -0
- package/README.md +79 -94
- package/dist/application.d.ts +29 -0
- package/dist/application.js +217 -0
- package/dist/class/cms_trigger.d.ts +61 -0
- package/dist/class/cms_trigger.js +47 -0
- package/dist/class/collection_definition.d.ts +112 -0
- package/dist/class/collection_definition.js +87 -0
- package/dist/class/combinator.d.ts +43 -0
- package/dist/class/combinator.js +174 -0
- package/dist/class/database_trigger.d.ts +84 -0
- package/dist/class/database_trigger.js +64 -0
- package/dist/class/db_schemas.d.ts +25 -0
- package/dist/class/db_schemas.js +28 -0
- package/dist/class/directory.d.ts +20 -0
- package/dist/class/directory.js +87 -0
- package/dist/class/paginator.d.ts +31 -0
- package/dist/class/paginator.js +43 -0
- package/dist/class/reply.d.ts +29 -0
- package/dist/class/reply.js +44 -0
- package/dist/class/security.d.ts +186 -0
- package/dist/class/security.js +178 -0
- package/dist/class/trigger_operator.d.ts +92 -0
- package/dist/class/trigger_operator.js +99 -0
- package/dist/class/user.d.ts +81 -0
- package/dist/class/user.js +151 -0
- package/dist/class/validator.d.ts +19 -0
- package/dist/class/validator.js +101 -0
- package/dist/config.d.ts +112 -0
- package/dist/config.js +26 -0
- package/dist/defult-permissions.d.ts +2 -0
- package/dist/defult-permissions.js +31 -0
- package/dist/events.d.ts +23 -0
- package/dist/events.js +47 -0
- package/dist/helper/data_insertion.d.ts +38 -0
- package/dist/helper/data_insertion.js +110 -0
- package/dist/helper/presetup_services.d.ts +60 -0
- package/dist/helper/presetup_services.js +108 -0
- package/dist/index.d.ts +118 -0
- package/dist/index.js +79 -0
- package/dist/middlewares.d.ts +53 -0
- package/dist/middlewares.js +106 -0
- package/dist/play-test.d.ts +1 -0
- package/dist/play-test.js +9 -0
- package/dist/services/data_provider/router.d.ts +4 -0
- package/dist/services/data_provider/router.js +187 -0
- package/dist/services/data_provider/service.d.ts +131 -0
- package/dist/services/data_provider/service.js +252 -0
- package/dist/services/data_provider/typeCasters.d.ts +9 -0
- package/dist/services/data_provider/typeCasters.js +18 -0
- package/dist/services/file/db.d.ts +1 -0
- package/dist/services/file/db.js +31 -0
- package/dist/services/file/router.d.ts +4 -0
- package/dist/services/file/router.js +115 -0
- package/dist/services/file/service.d.ts +204 -0
- package/dist/services/file/service.js +341 -0
- package/dist/services/functions/router.d.ts +4 -0
- package/dist/services/functions/router.js +67 -0
- package/dist/services/functions/service.d.ts +132 -0
- package/dist/services/functions/service.js +159 -0
- package/dist/services/jwt/router.d.ts +4 -0
- package/dist/services/jwt/router.js +99 -0
- package/dist/services/jwt/service.d.ts +97 -0
- package/dist/services/jwt/service.js +135 -0
- package/dist/services/user_manager/db.d.ts +1 -0
- package/dist/services/user_manager/db.js +75 -0
- package/dist/services/user_manager/permissionManager.d.ts +19 -0
- package/dist/services/user_manager/permissionManager.js +42 -0
- package/dist/services/user_manager/router.d.ts +4 -0
- package/dist/services/user_manager/router.js +195 -0
- package/dist/services/user_manager/service.d.ts +317 -0
- package/dist/services/user_manager/service.js +628 -0
- package/docs/.keep +0 -0
- package/docs/system-access-type.md +26 -0
- package/package.json +58 -45
- package/src/application.ts +206 -0
- package/src/class/cms_trigger.ts +68 -0
- package/src/class/collection_definition.ts +134 -0
- package/src/class/combinator.ts +176 -0
- package/src/class/database_trigger.ts +99 -0
- package/src/class/db_schemas.ts +44 -0
- package/src/class/{directory.js → directory.ts} +40 -18
- package/src/class/paginator.ts +51 -0
- package/src/class/reply.ts +59 -0
- package/src/class/security.ts +250 -0
- package/src/class/trigger_operator.ts +142 -0
- package/src/class/user.ts +199 -0
- package/src/class/validator.ts +123 -0
- package/src/config.ts +121 -0
- package/src/defult-permissions.ts +31 -0
- package/src/events.ts +59 -0
- package/src/helper/data_insertion.ts +94 -0
- package/src/helper/presetup_services.ts +96 -0
- package/src/index.ts +146 -0
- package/src/middlewares.ts +75 -0
- package/src/play-test.ts +8 -0
- package/src/services/data_provider/router.ts +191 -0
- package/src/services/data_provider/service.ts +305 -0
- package/src/services/data_provider/typeCasters.ts +15 -0
- package/src/services/file/db.ts +29 -0
- package/src/services/file/router.ts +88 -0
- package/src/services/file/service.ts +387 -0
- package/src/services/functions/router.ts +34 -0
- package/src/services/functions/service.ts +203 -0
- package/src/services/jwt/router.ts +73 -0
- package/src/services/jwt/service.ts +139 -0
- package/src/services/user_manager/db.ts +87 -0
- package/src/services/user_manager/permissionManager.ts +49 -0
- package/src/services/user_manager/router.ts +193 -0
- package/src/services/user_manager/service.ts +698 -0
- package/tsconfig.json +16 -9
- package/typedoc.mjs +41 -0
- package/LICENSE +0 -21
- package/package-lock.json +0 -1373
- package/src/application.js +0 -239
- package/src/class/cms_trigger.js +0 -20
- package/src/class/collection_definition.js +0 -33
- package/src/class/combinator.js +0 -133
- package/src/class/database_trigger.js +0 -20
- package/src/class/db_schemas.js +0 -18
- package/src/class/paginator.js +0 -31
- package/src/class/reply.js +0 -37
- package/src/class/security.js +0 -141
- package/src/class/trigger_operator.js +0 -39
- package/src/class/user.js +0 -112
- package/src/class/validator.js +0 -91
- package/src/config.js +0 -67
- package/src/events.js +0 -15
- package/src/helper/data_insertion.js +0 -64
- package/src/helper/presetup_services.js +0 -31
- package/src/index.js +0 -66
- package/src/middlewares.js +0 -44
- package/src/services/data_provider/router.js +0 -552
- package/src/services/data_provider/service.js +0 -262
- package/src/services/data_provider/typeCasters.js +0 -10
- package/src/services/file/db.js +0 -29
- package/src/services/file/router.js +0 -92
- package/src/services/file/service.js +0 -231
- package/src/services/functions/router.js +0 -37
- package/src/services/functions/service.js +0 -74
- package/src/services/jwt/router.js +0 -82
- package/src/services/jwt/service.js +0 -37
- package/src/services/user_manager/db.js +0 -83
- package/src/services/user_manager/permissionManager.js +0 -43
- package/src/services/user_manager/router.js +0 -176
- package/src/services/user_manager/service.js +0 -377
- package/types/application.d.ts +0 -97
- package/types/class/cms_trigger.d.ts +0 -24
- package/types/class/collection_definition.d.ts +0 -36
- package/types/class/combinator.d.ts +0 -30
- package/types/class/database_trigger.d.ts +0 -28
- package/types/class/db_schemas.d.ts +0 -2
- package/types/class/directory.d.ts +0 -2
- package/types/class/paginator.d.ts +0 -8
- package/types/class/reply.d.ts +0 -8
- package/types/class/security.d.ts +0 -109
- package/types/class/trigger_operator.d.ts +0 -19
- package/types/class/user.d.ts +0 -24
- package/types/class/validator.d.ts +0 -9
- package/types/config.d.ts +0 -101
- package/types/events.d.ts +0 -7
- package/types/helper/data_insertion.d.ts +0 -4
- package/types/helper/presetup_services.d.ts +0 -5
- package/types/index.d.ts +0 -72
- package/types/middlewares.d.ts +0 -10
- package/types/services/data_provider/router.d.ts +0 -3
- package/types/services/data_provider/service.d.ts +0 -40
- package/types/services/data_provider/typeCasters.d.ts +0 -3
- package/types/services/file/db.d.ts +0 -3
- package/types/services/file/router.d.ts +0 -3
- package/types/services/file/service.d.ts +0 -81
- package/types/services/functions/router.d.ts +0 -3
- package/types/services/functions/service.d.ts +0 -23
- package/types/services/jwt/router.d.ts +0 -3
- package/types/services/jwt/service.d.ts +0 -10
- package/types/services/user_manager/db.d.ts +0 -3
- package/types/services/user_manager/permissionManager.d.ts +0 -3
- package/types/services/user_manager/router.d.ts +0 -3
- package/types/services/user_manager/service.d.ts +0 -131
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Singleton class for managing database triggers
|
|
4
|
+
* Provides functionality to add and execute triggers based on database operations
|
|
5
|
+
* @class TriggerOperator
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* // Add a trigger for user insertions
|
|
9
|
+
* TriggerOperator.instance.addTrigger({
|
|
10
|
+
* operation: 'insert',
|
|
11
|
+
* database: 'myDB',
|
|
12
|
+
* collection: 'users',
|
|
13
|
+
* callback: (data) => {
|
|
14
|
+
* console.log('New user inserted:', data);
|
|
15
|
+
* }
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
class TriggerOperator {
|
|
20
|
+
constructor() {
|
|
21
|
+
this.triggers = [];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets the singleton instance of TriggerOperator
|
|
25
|
+
* @static
|
|
26
|
+
* @returns {TriggerOperator} The singleton instance
|
|
27
|
+
*/
|
|
28
|
+
static get instance() {
|
|
29
|
+
if (!TriggerOperator._instance) {
|
|
30
|
+
TriggerOperator._instance = new TriggerOperator();
|
|
31
|
+
}
|
|
32
|
+
return TriggerOperator._instance;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Adds a new trigger to the registry
|
|
36
|
+
* @param {Trigger} trigger - The trigger configuration to add
|
|
37
|
+
* @throws {Error} If trigger is invalid or already exists
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // Add a trigger for document updates
|
|
41
|
+
* TriggerOperator.instance.addTrigger({
|
|
42
|
+
* operation: 'update',
|
|
43
|
+
* database: 'myDB',
|
|
44
|
+
* collection: 'documents',
|
|
45
|
+
* callback: (data) => {
|
|
46
|
+
* console.log('Document updated:', data);
|
|
47
|
+
* }
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
addTrigger(trigger) {
|
|
52
|
+
// Validate trigger
|
|
53
|
+
if (!trigger.operation || !trigger.database || !trigger.collection || !trigger.callback) {
|
|
54
|
+
throw new Error('Invalid trigger configuration');
|
|
55
|
+
}
|
|
56
|
+
// Check for duplicate triggers
|
|
57
|
+
const exists = this.triggers.some(t => t.operation === trigger.operation &&
|
|
58
|
+
t.database === trigger.database &&
|
|
59
|
+
t.collection === trigger.collection);
|
|
60
|
+
if (exists) {
|
|
61
|
+
throw new Error(`Trigger already exists for operation ${trigger.operation} on ${trigger.database}.${trigger.collection}`);
|
|
62
|
+
}
|
|
63
|
+
this.triggers.push(trigger);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Executes all matching triggers for a given database operation
|
|
67
|
+
* @param {DatabaseOperation} operation - The database operation that occurred
|
|
68
|
+
* @param {string} database - The database where the operation occurred
|
|
69
|
+
* @param {string} collection - The collection where the operation occurred
|
|
70
|
+
* @param {any} data - The data associated with the operation
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* // This would typically be called by the database layer
|
|
74
|
+
* TriggerOperator.instance.call(
|
|
75
|
+
* 'insert',
|
|
76
|
+
* 'myDB',
|
|
77
|
+
* 'users',
|
|
78
|
+
* { id: 1, name: 'John' }
|
|
79
|
+
* );
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
call(operation, database, collection, data) {
|
|
83
|
+
this.triggers.forEach(trigger => {
|
|
84
|
+
if (operation === trigger.operation &&
|
|
85
|
+
database === trigger.database &&
|
|
86
|
+
collection === trigger.collection &&
|
|
87
|
+
trigger.callback) {
|
|
88
|
+
try {
|
|
89
|
+
trigger.callback(data);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.error(`Error executing trigger for ${operation} on ${database}.${collection}:`, error);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
TriggerOperator._instance = null;
|
|
99
|
+
module.exports = TriggerOperator.instance;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { PermissionGroup } from './security';
|
|
2
|
+
import { Model } from 'mongoose';
|
|
3
|
+
/**
|
|
4
|
+
* User detail interface
|
|
5
|
+
*/
|
|
6
|
+
interface UserDetail {
|
|
7
|
+
permissionGroup?: string | PermissionGroup;
|
|
8
|
+
phone?: string;
|
|
9
|
+
email?: string;
|
|
10
|
+
password?: string;
|
|
11
|
+
fullname?: string;
|
|
12
|
+
type?: string;
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* User class representing a user in the system
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export declare class User {
|
|
21
|
+
id: string;
|
|
22
|
+
permissionGroup: string;
|
|
23
|
+
phone: string;
|
|
24
|
+
email: string;
|
|
25
|
+
password: string;
|
|
26
|
+
type: string;
|
|
27
|
+
dbModel: any;
|
|
28
|
+
/**
|
|
29
|
+
* Create a user
|
|
30
|
+
* @param id - User ID
|
|
31
|
+
* @param permissionGroup - Permission group name
|
|
32
|
+
* @param phone - User phone
|
|
33
|
+
* @param email - User email
|
|
34
|
+
* @param password - User password
|
|
35
|
+
* @param type - User type
|
|
36
|
+
* @param model - Database model
|
|
37
|
+
*
|
|
38
|
+
* @hidden
|
|
39
|
+
*/
|
|
40
|
+
constructor(id: string, permissionGroup: string, phone: string, email: string, password: string, type: string, model: any);
|
|
41
|
+
/**
|
|
42
|
+
* Get brief user information
|
|
43
|
+
* @returns Brief user info object
|
|
44
|
+
*/
|
|
45
|
+
getBrief(): UserDetail;
|
|
46
|
+
/**
|
|
47
|
+
* Update user details
|
|
48
|
+
* @param detail - Object containing user details to update
|
|
49
|
+
*/
|
|
50
|
+
setNewDetail(detail: UserDetail): void;
|
|
51
|
+
/**
|
|
52
|
+
* Check if user has a specific permission
|
|
53
|
+
* @param accessType - Permission to check
|
|
54
|
+
* @returns True if user has permission, false otherwise
|
|
55
|
+
*/
|
|
56
|
+
hasPermission(accessType: string): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Save user to database
|
|
59
|
+
*/
|
|
60
|
+
save(): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Load user from database model
|
|
63
|
+
* @param model - Database model
|
|
64
|
+
* @returns Promise resolving to User instance
|
|
65
|
+
*/
|
|
66
|
+
static loadFromModel(model: any): Promise<User>;
|
|
67
|
+
/**
|
|
68
|
+
* Create user from model and details
|
|
69
|
+
* @param model - Mongoose model
|
|
70
|
+
* @param detail - User details
|
|
71
|
+
* @returns Promise resolving to User instance
|
|
72
|
+
*/
|
|
73
|
+
static createFromModel(model: Model<any>, detail: UserDetail): Promise<User>;
|
|
74
|
+
/**
|
|
75
|
+
* Create error for invalid user
|
|
76
|
+
* @param object - Invalid user object
|
|
77
|
+
* @returns Error message
|
|
78
|
+
*/
|
|
79
|
+
static notValid(object: any): string;
|
|
80
|
+
}
|
|
81
|
+
export default User;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.User = void 0;
|
|
4
|
+
const config_1 = require("../config");
|
|
5
|
+
const validator_1 = require("./validator");
|
|
6
|
+
/**
|
|
7
|
+
* User class representing a user in the system
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
class User {
|
|
12
|
+
/**
|
|
13
|
+
* Create a user
|
|
14
|
+
* @param id - User ID
|
|
15
|
+
* @param permissionGroup - Permission group name
|
|
16
|
+
* @param phone - User phone
|
|
17
|
+
* @param email - User email
|
|
18
|
+
* @param password - User password
|
|
19
|
+
* @param type - User type
|
|
20
|
+
* @param model - Database model
|
|
21
|
+
*
|
|
22
|
+
* @hidden
|
|
23
|
+
*/
|
|
24
|
+
constructor(id, permissionGroup, phone, email, password, type, model) {
|
|
25
|
+
this.id = id;
|
|
26
|
+
this.permissionGroup = permissionGroup;
|
|
27
|
+
this.email = email;
|
|
28
|
+
this.phone = phone;
|
|
29
|
+
this.password = password;
|
|
30
|
+
this.type = type;
|
|
31
|
+
this.dbModel = model;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get brief user information
|
|
35
|
+
* @returns Brief user info object
|
|
36
|
+
*/
|
|
37
|
+
getBrief() {
|
|
38
|
+
const permissionGroup = config_1.config.permissionGroups?.find(group => group.title === this.permissionGroup);
|
|
39
|
+
if (!permissionGroup) {
|
|
40
|
+
throw new Error('Permission group not found on user object');
|
|
41
|
+
}
|
|
42
|
+
const brief = {
|
|
43
|
+
id: this.id,
|
|
44
|
+
permissionGroup: permissionGroup,
|
|
45
|
+
phone: this.phone,
|
|
46
|
+
email: this.email,
|
|
47
|
+
type: this.type,
|
|
48
|
+
};
|
|
49
|
+
return brief;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Update user details
|
|
53
|
+
* @param detail - Object containing user details to update
|
|
54
|
+
*/
|
|
55
|
+
setNewDetail(detail) {
|
|
56
|
+
if (detail.phone)
|
|
57
|
+
this.phone = detail.phone;
|
|
58
|
+
if (detail.email)
|
|
59
|
+
this.email = detail.email;
|
|
60
|
+
if (detail.password)
|
|
61
|
+
this.password = detail.password;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check if user has a specific permission
|
|
65
|
+
* @param accessType - Permission to check
|
|
66
|
+
* @returns True if user has permission, false otherwise
|
|
67
|
+
*/
|
|
68
|
+
hasPermission(accessType) {
|
|
69
|
+
const permissionGroup = config_1.config.permissionGroups?.find(group => group.title === this.permissionGroup);
|
|
70
|
+
if (permissionGroup == null)
|
|
71
|
+
return false;
|
|
72
|
+
let key = false;
|
|
73
|
+
if (permissionGroup.allowedAccessTypes) {
|
|
74
|
+
for (let i = 0; i < permissionGroup.allowedAccessTypes.length; i++) {
|
|
75
|
+
const userPermissionType = permissionGroup.allowedAccessTypes[i];
|
|
76
|
+
if (userPermissionType === accessType) {
|
|
77
|
+
key = true;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return key;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Save user to database
|
|
86
|
+
*/
|
|
87
|
+
async save() {
|
|
88
|
+
if (!this.dbModel) {
|
|
89
|
+
throw new Error('User model is not initialized');
|
|
90
|
+
}
|
|
91
|
+
this.dbModel['permissionGroup'] = this.permissionGroup;
|
|
92
|
+
this.dbModel['phone'] = this.phone;
|
|
93
|
+
this.dbModel['email'] = this.email;
|
|
94
|
+
this.dbModel['password'] = this.password;
|
|
95
|
+
await this.dbModel.save();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Load user from database model
|
|
99
|
+
* @param model - Database model
|
|
100
|
+
* @returns Promise resolving to User instance
|
|
101
|
+
*/
|
|
102
|
+
static loadFromModel(model) {
|
|
103
|
+
return new Promise((done, reject) => {
|
|
104
|
+
// check required fields
|
|
105
|
+
const isValidData = (0, validator_1.validator)(model, 'fullname email password permission');
|
|
106
|
+
if (!isValidData.isValid) {
|
|
107
|
+
return reject(User.notValid(model));
|
|
108
|
+
}
|
|
109
|
+
const id = model.id;
|
|
110
|
+
const permissionGroup = model.permissionGroup;
|
|
111
|
+
const phone = model.phone;
|
|
112
|
+
const email = model.email;
|
|
113
|
+
const password = model.password;
|
|
114
|
+
const type = model.type;
|
|
115
|
+
//create user
|
|
116
|
+
const newUser = new User(id, permissionGroup, phone, email, password, type, model);
|
|
117
|
+
done(newUser);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Create user from model and details
|
|
122
|
+
* @param model - Mongoose model
|
|
123
|
+
* @param detail - User details
|
|
124
|
+
* @returns Promise resolving to User instance
|
|
125
|
+
*/
|
|
126
|
+
static createFromModel(model, detail) {
|
|
127
|
+
return new Promise(async (done, reject) => {
|
|
128
|
+
//create user
|
|
129
|
+
try {
|
|
130
|
+
const newUserDoc = await new model(detail).save();
|
|
131
|
+
const newUser = await User.loadFromModel(newUserDoc);
|
|
132
|
+
done(newUser);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
reject(error);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Create error for invalid user
|
|
141
|
+
* @param object - Invalid user object
|
|
142
|
+
* @returns Error message
|
|
143
|
+
*/
|
|
144
|
+
static notValid(object) {
|
|
145
|
+
const error = `user detail are not valid ${JSON.stringify(object)}`;
|
|
146
|
+
console.error(error);
|
|
147
|
+
return error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.User = User;
|
|
151
|
+
exports.default = User;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation result interface
|
|
3
|
+
*/
|
|
4
|
+
export interface ValidationResult {
|
|
5
|
+
isValid: boolean;
|
|
6
|
+
requires: string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Validates an object by checking if it contains all the required fields.
|
|
10
|
+
* @param obj - The object to be validated.
|
|
11
|
+
* @param 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.
|
|
12
|
+
* @returns Returns a ValidationResult object with validation status and missing fields.
|
|
13
|
+
* @throws Throws an error if the requiredFields parameter is not a string or an object.
|
|
14
|
+
*/
|
|
15
|
+
export declare function validator(obj: Record<string, any> | null, requiredFields: string | Record<string, string>): ValidationResult;
|
|
16
|
+
/**
|
|
17
|
+
* Return the validator function to maintain compatibility with the JavaScript version
|
|
18
|
+
*/
|
|
19
|
+
export declare const validateObject: typeof validator;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateObject = void 0;
|
|
4
|
+
exports.validator = validator;
|
|
5
|
+
/**
|
|
6
|
+
* Validates an object by checking if it contains all the required fields.
|
|
7
|
+
* @param obj - The object to be validated.
|
|
8
|
+
* @param 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.
|
|
9
|
+
* @returns Returns a ValidationResult object with validation status and missing fields.
|
|
10
|
+
* @throws Throws an error if the requiredFields parameter is not a string or an object.
|
|
11
|
+
*/
|
|
12
|
+
function validator(obj, requiredFields) {
|
|
13
|
+
/*
|
|
14
|
+
this method could validate an Object by given field's name list and return bool.
|
|
15
|
+
- requiredFields: is a string that contains keys being spared by " ".
|
|
16
|
+
*/
|
|
17
|
+
const type = typeof requiredFields;
|
|
18
|
+
let result;
|
|
19
|
+
if (type === 'string')
|
|
20
|
+
result = checkSimple(obj, requiredFields);
|
|
21
|
+
else if (type === 'object')
|
|
22
|
+
result = checkComplex(obj, requiredFields);
|
|
23
|
+
else
|
|
24
|
+
throw 'requiredFields has wrong form, it must be string or object';
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Check simple validation with space-separated string of keys
|
|
29
|
+
*/
|
|
30
|
+
function checkSimple(obj, requiredFields = '') {
|
|
31
|
+
let isValid = false;
|
|
32
|
+
const requires = requiredFields.split(' ');
|
|
33
|
+
const validMembers = [];
|
|
34
|
+
const notValidKeys = [];
|
|
35
|
+
// return if obj is null
|
|
36
|
+
if (obj == null)
|
|
37
|
+
return _returnResult(isValid, requires);
|
|
38
|
+
// Filter empty strings that might result from extra spaces
|
|
39
|
+
const requiredKeys = requires.filter(key => key !== '');
|
|
40
|
+
for (const key of requiredKeys) {
|
|
41
|
+
if (obj[key] !== undefined && obj[key] !== null)
|
|
42
|
+
validMembers.push(key);
|
|
43
|
+
else
|
|
44
|
+
notValidKeys.push(key);
|
|
45
|
+
}
|
|
46
|
+
// check validation
|
|
47
|
+
isValid = requiredKeys.length === validMembers.length;
|
|
48
|
+
return _returnResult(isValid, notValidKeys);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Check complex validation with object containing expected values
|
|
52
|
+
*/
|
|
53
|
+
function checkComplex(obj, requiredFields = {}) {
|
|
54
|
+
let isValid = false;
|
|
55
|
+
const requireKeys = Object.keys(requiredFields);
|
|
56
|
+
let validMembers = 0;
|
|
57
|
+
const notValidKeys = [];
|
|
58
|
+
// return if obj is null
|
|
59
|
+
if (obj == null)
|
|
60
|
+
return _returnResult(isValid, requireKeys);
|
|
61
|
+
for (let i = 0; i < requireKeys.length; i++) {
|
|
62
|
+
const key = requireKeys[i];
|
|
63
|
+
let isValidField = false;
|
|
64
|
+
// if field has specific values
|
|
65
|
+
if (requiredFields[key].length > 0) {
|
|
66
|
+
const expectedValues = requiredFields[key].split(' ');
|
|
67
|
+
if (typeof expectedValues !== 'object')
|
|
68
|
+
throw `${key} must be array of strings`;
|
|
69
|
+
for (const value of expectedValues) {
|
|
70
|
+
if (obj[key] === value) {
|
|
71
|
+
isValidField = true;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// else does not have specific value
|
|
77
|
+
else if (obj[key] != null) {
|
|
78
|
+
isValidField = true;
|
|
79
|
+
}
|
|
80
|
+
if (isValidField)
|
|
81
|
+
validMembers++;
|
|
82
|
+
else
|
|
83
|
+
notValidKeys.push(key);
|
|
84
|
+
}
|
|
85
|
+
// check validation
|
|
86
|
+
isValid = requireKeys.length === validMembers;
|
|
87
|
+
return _returnResult(isValid, notValidKeys);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create a validation result object
|
|
91
|
+
*/
|
|
92
|
+
function _returnResult(isValid, notValidKeys) {
|
|
93
|
+
return {
|
|
94
|
+
isValid: isValid,
|
|
95
|
+
requires: notValidKeys,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Return the validator function to maintain compatibility with the JavaScript version
|
|
100
|
+
*/
|
|
101
|
+
exports.validateObject = validator;
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import Koa from 'koa';
|
|
2
|
+
import { CollectionDefinition } from './class/collection_definition';
|
|
3
|
+
import { PermissionGroup } from './class/security';
|
|
4
|
+
import { CmsTrigger } from './class/cms_trigger';
|
|
5
|
+
import { DefinedFunction } from './services/functions/service';
|
|
6
|
+
import cors from '@koa/cors';
|
|
7
|
+
import { Options as KoaStaticOptionsBase } from 'koa-static';
|
|
8
|
+
export interface StaticPathOptions extends KoaStaticOptionsBase {
|
|
9
|
+
/**
|
|
10
|
+
* The actual path of the static files on your server
|
|
11
|
+
*/
|
|
12
|
+
actualPath: string;
|
|
13
|
+
/**
|
|
14
|
+
* The path you want to serve the static files from
|
|
15
|
+
*/
|
|
16
|
+
path: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* JWT keypair configuration
|
|
20
|
+
* @interface KeyPair
|
|
21
|
+
* @property {string} private - Private key for JWT signing
|
|
22
|
+
* @property {string} public - Public key for JWT verification
|
|
23
|
+
*/
|
|
24
|
+
interface KeyPair {
|
|
25
|
+
private: string;
|
|
26
|
+
public: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* MongoDB connection options
|
|
30
|
+
* @interface MongoOptions
|
|
31
|
+
* @property {string} dbPrefix - Prefix for database names
|
|
32
|
+
* @property {string} mongoBaseAddress - MongoDB connection URL
|
|
33
|
+
* @property {string} [addressMap] - Optional address mapping configuration
|
|
34
|
+
*/
|
|
35
|
+
interface MongoOptions {
|
|
36
|
+
dbPrefix: string;
|
|
37
|
+
mongoBaseAddress: string;
|
|
38
|
+
addressMap?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Admin user configuration
|
|
42
|
+
* @interface AdminUser
|
|
43
|
+
* @property {string} email - Admin user email
|
|
44
|
+
* @property {string} password - Admin user password
|
|
45
|
+
*/
|
|
46
|
+
interface AdminUser {
|
|
47
|
+
email: string;
|
|
48
|
+
password: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Configuration options for creating a REST API instance
|
|
52
|
+
* @interface RestOptions
|
|
53
|
+
* @property {cors.Options} [cors] - CORS configuration options
|
|
54
|
+
* @property {string} [modulesPath] - Path to custom modules directory
|
|
55
|
+
* @property {string} [uploadDirectory] - Directory for file uploads
|
|
56
|
+
* @property {any} [koaBodyOptions] - Options for koa-body middleware
|
|
57
|
+
* @property {StaticPathOptions} [staticPath] - Static file serving options
|
|
58
|
+
* @property {Function} [onBeforeInit] - Hook called before initialization
|
|
59
|
+
* @property {Function} [onAfterInit] - Hook called after initialization
|
|
60
|
+
* @property {number} [port] - Port to listen on
|
|
61
|
+
* @property {boolean} [dontListen] - Don't start the server
|
|
62
|
+
* @property {MongoOptions} [mongo] - MongoDB connection options
|
|
63
|
+
* @property {Object} [keypair] - JWT keypair for authentication
|
|
64
|
+
* @property {AdminUser} [adminUser] - Admin user configuration
|
|
65
|
+
* @property {Function} [verificationCodeGeneratorMethod] - Custom verification code generator
|
|
66
|
+
* @property {CollectionDefinition[]} [collectionDefinitions] - Custom collection definitions
|
|
67
|
+
* @property {PermissionGroup[]} [permissionGroups] - Custom permission groups
|
|
68
|
+
* @property {CmsTrigger[]} [authTriggers] - Authentication triggers
|
|
69
|
+
* @property {CmsTrigger[]} [fileTriggers] - File handling triggers
|
|
70
|
+
* @property {DefinedFunction[]} [functions] - Custom API functions
|
|
71
|
+
*/
|
|
72
|
+
export interface RestOptions {
|
|
73
|
+
cors?: cors.Options;
|
|
74
|
+
modulesPath?: string;
|
|
75
|
+
uploadDirectory?: string;
|
|
76
|
+
koaBodyOptions?: any;
|
|
77
|
+
staticPath?: StaticPathOptions;
|
|
78
|
+
onBeforeInit?: (koaApp: Koa) => void;
|
|
79
|
+
onAfterInit?: (koaApp: Koa) => void;
|
|
80
|
+
port?: number;
|
|
81
|
+
dontListen?: boolean;
|
|
82
|
+
mongo?: MongoOptions;
|
|
83
|
+
keypair?: KeyPair;
|
|
84
|
+
adminUser?: AdminUser;
|
|
85
|
+
verificationCodeGeneratorMethod?: () => string;
|
|
86
|
+
collectionDefinitions?: CollectionDefinition[];
|
|
87
|
+
permissionGroups?: PermissionGroup[];
|
|
88
|
+
authTriggers?: CmsTrigger[];
|
|
89
|
+
fileTriggers?: CmsTrigger[];
|
|
90
|
+
functions?: DefinedFunction[];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Global configuration object
|
|
94
|
+
* @type {RestOptions}
|
|
95
|
+
*/
|
|
96
|
+
export declare const config: RestOptions;
|
|
97
|
+
/**
|
|
98
|
+
* Updates the global configuration with new options
|
|
99
|
+
* @param {RestOptions} options - New configuration options to merge
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* setConfig({
|
|
103
|
+
* port: 3000,
|
|
104
|
+
* mongo: {
|
|
105
|
+
* mongoBaseAddress: 'mongodb://localhost:27017',
|
|
106
|
+
* dbPrefix: 'myapp_'
|
|
107
|
+
* }
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare function setConfig(options: RestOptions): void;
|
|
112
|
+
export {};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.config = void 0;
|
|
4
|
+
exports.setConfig = setConfig;
|
|
5
|
+
/**
|
|
6
|
+
* Global configuration object
|
|
7
|
+
* @type {RestOptions}
|
|
8
|
+
*/
|
|
9
|
+
exports.config = {};
|
|
10
|
+
/**
|
|
11
|
+
* Updates the global configuration with new options
|
|
12
|
+
* @param {RestOptions} options - New configuration options to merge
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* setConfig({
|
|
16
|
+
* port: 3000,
|
|
17
|
+
* mongo: {
|
|
18
|
+
* mongoBaseAddress: 'mongodb://localhost:27017',
|
|
19
|
+
* dbPrefix: 'myapp_'
|
|
20
|
+
* }
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function setConfig(options) {
|
|
25
|
+
Object.assign(exports.config, options);
|
|
26
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.permissionGroups = void 0;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
exports.permissionGroups = [
|
|
6
|
+
new index_1.PermissionGroup({
|
|
7
|
+
title: 'anonymous',
|
|
8
|
+
isAnonymous: true,
|
|
9
|
+
allowedAccessTypes: ['anonymous_access'],
|
|
10
|
+
}),
|
|
11
|
+
new index_1.PermissionGroup({
|
|
12
|
+
title: 'end-user',
|
|
13
|
+
isDefault: true,
|
|
14
|
+
allowedAccessTypes: [
|
|
15
|
+
'user_access',
|
|
16
|
+
'anonymous_access',
|
|
17
|
+
'upload_file_access',
|
|
18
|
+
'remove_file_access',
|
|
19
|
+
],
|
|
20
|
+
}),
|
|
21
|
+
new index_1.PermissionGroup({
|
|
22
|
+
title: 'administrator',
|
|
23
|
+
allowedAccessTypes: [
|
|
24
|
+
'user_access',
|
|
25
|
+
'anonymous_access',
|
|
26
|
+
'upload_file_access',
|
|
27
|
+
'remove_file_access',
|
|
28
|
+
'advanced_settings',
|
|
29
|
+
],
|
|
30
|
+
}),
|
|
31
|
+
];
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported event types:
|
|
3
|
+
* - onBeforeInit: (koaApp: Koa) => void; // A callback called before initializing the Koa server.
|
|
4
|
+
* - onAfterInit: (koaApp: Koa) => void; // A callback called after server initialization.
|
|
5
|
+
* - onNewUser: (user: any) => void; // A callback called when a new user is created.
|
|
6
|
+
*
|
|
7
|
+
* @param event - The event name to register
|
|
8
|
+
* @param callback - The callback function to be called when the event is triggered
|
|
9
|
+
* @throws Error if event is not a string or callback is not a function
|
|
10
|
+
*/
|
|
11
|
+
export declare function registerEventCallback(event: string, callback: (...args: any[]) => void): void;
|
|
12
|
+
/**
|
|
13
|
+
* Get all registered callbacks for a specific event
|
|
14
|
+
* @param event - The event name to get callbacks for
|
|
15
|
+
* @returns Array of callbacks registered for the event
|
|
16
|
+
*/
|
|
17
|
+
export declare function getEventCallbacks(event: string): ((...args: any[]) => void)[];
|
|
18
|
+
/**
|
|
19
|
+
* Trigger an event with arguments
|
|
20
|
+
* @param event - The event name to trigger
|
|
21
|
+
* @param args - Arguments to pass to the callback functions
|
|
22
|
+
*/
|
|
23
|
+
export declare function triggerEvent(event: string, ...args: any[]): void;
|