@modular-rest/server 1.11.12 → 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.
Files changed (181) hide show
  1. package/.nvmrc +1 -0
  2. package/.prettierrc.json +9 -0
  3. package/.releaserc.json +24 -0
  4. package/README.md +79 -94
  5. package/dist/application.d.ts +29 -0
  6. package/dist/application.js +217 -0
  7. package/dist/class/cms_trigger.d.ts +61 -0
  8. package/dist/class/cms_trigger.js +47 -0
  9. package/dist/class/collection_definition.d.ts +112 -0
  10. package/dist/class/collection_definition.js +87 -0
  11. package/dist/class/combinator.d.ts +43 -0
  12. package/dist/class/combinator.js +174 -0
  13. package/dist/class/database_trigger.d.ts +84 -0
  14. package/dist/class/database_trigger.js +64 -0
  15. package/dist/class/db_schemas.d.ts +25 -0
  16. package/dist/class/db_schemas.js +28 -0
  17. package/dist/class/directory.d.ts +20 -0
  18. package/dist/class/directory.js +87 -0
  19. package/dist/class/paginator.d.ts +31 -0
  20. package/dist/class/paginator.js +43 -0
  21. package/dist/class/reply.d.ts +29 -0
  22. package/dist/class/reply.js +44 -0
  23. package/dist/class/security.d.ts +186 -0
  24. package/dist/class/security.js +178 -0
  25. package/dist/class/trigger_operator.d.ts +92 -0
  26. package/dist/class/trigger_operator.js +99 -0
  27. package/dist/class/user.d.ts +81 -0
  28. package/dist/class/user.js +151 -0
  29. package/dist/class/validator.d.ts +19 -0
  30. package/dist/class/validator.js +101 -0
  31. package/dist/config.d.ts +112 -0
  32. package/dist/config.js +26 -0
  33. package/dist/defult-permissions.d.ts +2 -0
  34. package/dist/defult-permissions.js +31 -0
  35. package/dist/events.d.ts +23 -0
  36. package/dist/events.js +47 -0
  37. package/dist/helper/data_insertion.d.ts +38 -0
  38. package/dist/helper/data_insertion.js +110 -0
  39. package/dist/helper/presetup_services.d.ts +60 -0
  40. package/dist/helper/presetup_services.js +108 -0
  41. package/dist/index.d.ts +118 -0
  42. package/dist/index.js +79 -0
  43. package/dist/middlewares.d.ts +53 -0
  44. package/dist/middlewares.js +106 -0
  45. package/dist/play-test.d.ts +1 -0
  46. package/dist/play-test.js +9 -0
  47. package/dist/services/data_provider/router.d.ts +4 -0
  48. package/dist/services/data_provider/router.js +187 -0
  49. package/dist/services/data_provider/service.d.ts +131 -0
  50. package/dist/services/data_provider/service.js +252 -0
  51. package/dist/services/data_provider/typeCasters.d.ts +9 -0
  52. package/dist/services/data_provider/typeCasters.js +18 -0
  53. package/dist/services/file/db.d.ts +1 -0
  54. package/dist/services/file/db.js +31 -0
  55. package/dist/services/file/router.d.ts +4 -0
  56. package/dist/services/file/router.js +115 -0
  57. package/dist/services/file/service.d.ts +204 -0
  58. package/dist/services/file/service.js +341 -0
  59. package/dist/services/functions/router.d.ts +4 -0
  60. package/dist/services/functions/router.js +67 -0
  61. package/dist/services/functions/service.d.ts +132 -0
  62. package/dist/services/functions/service.js +159 -0
  63. package/dist/services/jwt/router.d.ts +4 -0
  64. package/dist/services/jwt/router.js +99 -0
  65. package/dist/services/jwt/service.d.ts +97 -0
  66. package/dist/services/jwt/service.js +135 -0
  67. package/dist/services/user_manager/db.d.ts +1 -0
  68. package/dist/services/user_manager/db.js +75 -0
  69. package/dist/services/user_manager/permissionManager.d.ts +19 -0
  70. package/dist/services/user_manager/permissionManager.js +42 -0
  71. package/dist/services/user_manager/router.d.ts +4 -0
  72. package/dist/services/user_manager/router.js +195 -0
  73. package/dist/services/user_manager/service.d.ts +317 -0
  74. package/dist/services/user_manager/service.js +628 -0
  75. package/docs/.keep +0 -0
  76. package/docs/system-access-type.md +26 -0
  77. package/package.json +58 -45
  78. package/src/application.ts +206 -0
  79. package/src/class/cms_trigger.ts +68 -0
  80. package/src/class/collection_definition.ts +134 -0
  81. package/src/class/combinator.ts +176 -0
  82. package/src/class/database_trigger.ts +99 -0
  83. package/src/class/db_schemas.ts +44 -0
  84. package/src/class/{directory.js → directory.ts} +40 -18
  85. package/src/class/paginator.ts +51 -0
  86. package/src/class/reply.ts +59 -0
  87. package/src/class/security.ts +250 -0
  88. package/src/class/trigger_operator.ts +142 -0
  89. package/src/class/user.ts +199 -0
  90. package/src/class/validator.ts +123 -0
  91. package/src/config.ts +121 -0
  92. package/src/defult-permissions.ts +31 -0
  93. package/src/events.ts +59 -0
  94. package/src/helper/data_insertion.ts +94 -0
  95. package/src/helper/presetup_services.ts +96 -0
  96. package/src/index.ts +146 -0
  97. package/src/middlewares.ts +75 -0
  98. package/src/play-test.ts +8 -0
  99. package/src/services/data_provider/router.ts +191 -0
  100. package/src/services/data_provider/service.ts +305 -0
  101. package/src/services/data_provider/typeCasters.ts +15 -0
  102. package/src/services/file/db.ts +29 -0
  103. package/src/services/file/router.ts +88 -0
  104. package/src/services/file/service.ts +387 -0
  105. package/src/services/functions/router.ts +34 -0
  106. package/src/services/functions/service.ts +203 -0
  107. package/src/services/jwt/router.ts +73 -0
  108. package/src/services/jwt/service.ts +139 -0
  109. package/src/services/user_manager/db.ts +87 -0
  110. package/src/services/user_manager/permissionManager.ts +49 -0
  111. package/src/services/user_manager/router.ts +193 -0
  112. package/src/services/user_manager/service.ts +698 -0
  113. package/tsconfig.json +16 -9
  114. package/typedoc.mjs +41 -0
  115. package/LICENSE +0 -21
  116. package/package-lock.json +0 -1373
  117. package/src/application.js +0 -239
  118. package/src/class/cms_trigger.js +0 -20
  119. package/src/class/collection_definition.js +0 -33
  120. package/src/class/combinator.js +0 -133
  121. package/src/class/database_trigger.js +0 -20
  122. package/src/class/db_schemas.js +0 -18
  123. package/src/class/paginator.js +0 -31
  124. package/src/class/reply.js +0 -37
  125. package/src/class/security.js +0 -141
  126. package/src/class/trigger_operator.js +0 -39
  127. package/src/class/user.js +0 -112
  128. package/src/class/validator.js +0 -91
  129. package/src/config.js +0 -67
  130. package/src/events.js +0 -15
  131. package/src/helper/data_insertion.js +0 -64
  132. package/src/helper/presetup_services.js +0 -31
  133. package/src/index.js +0 -66
  134. package/src/middlewares.js +0 -44
  135. package/src/services/data_provider/router.js +0 -552
  136. package/src/services/data_provider/service.js +0 -262
  137. package/src/services/data_provider/typeCasters.js +0 -10
  138. package/src/services/file/db.js +0 -29
  139. package/src/services/file/router.js +0 -92
  140. package/src/services/file/service.js +0 -231
  141. package/src/services/functions/router.js +0 -37
  142. package/src/services/functions/service.js +0 -74
  143. package/src/services/jwt/router.js +0 -70
  144. package/src/services/jwt/service.js +0 -37
  145. package/src/services/user_manager/db.js +0 -83
  146. package/src/services/user_manager/permissionManager.js +0 -43
  147. package/src/services/user_manager/router.js +0 -176
  148. package/src/services/user_manager/service.js +0 -377
  149. package/types/application.d.ts +0 -97
  150. package/types/class/cms_trigger.d.ts +0 -24
  151. package/types/class/collection_definition.d.ts +0 -36
  152. package/types/class/combinator.d.ts +0 -30
  153. package/types/class/database_trigger.d.ts +0 -28
  154. package/types/class/db_schemas.d.ts +0 -2
  155. package/types/class/directory.d.ts +0 -2
  156. package/types/class/paginator.d.ts +0 -8
  157. package/types/class/reply.d.ts +0 -8
  158. package/types/class/security.d.ts +0 -109
  159. package/types/class/trigger_operator.d.ts +0 -19
  160. package/types/class/user.d.ts +0 -24
  161. package/types/class/validator.d.ts +0 -9
  162. package/types/config.d.ts +0 -101
  163. package/types/events.d.ts +0 -7
  164. package/types/helper/data_insertion.d.ts +0 -4
  165. package/types/helper/presetup_services.d.ts +0 -5
  166. package/types/index.d.ts +0 -72
  167. package/types/middlewares.d.ts +0 -9
  168. package/types/services/data_provider/router.d.ts +0 -3
  169. package/types/services/data_provider/service.d.ts +0 -40
  170. package/types/services/data_provider/typeCasters.d.ts +0 -3
  171. package/types/services/file/db.d.ts +0 -3
  172. package/types/services/file/router.d.ts +0 -3
  173. package/types/services/file/service.d.ts +0 -81
  174. package/types/services/functions/router.d.ts +0 -3
  175. package/types/services/functions/service.d.ts +0 -23
  176. package/types/services/jwt/router.d.ts +0 -3
  177. package/types/services/jwt/service.d.ts +0 -10
  178. package/types/services/user_manager/db.d.ts +0 -3
  179. package/types/services/user_manager/permissionManager.d.ts +0 -3
  180. package/types/services/user_manager/router.d.ts +0 -3
  181. package/types/services/user_manager/service.d.ts +0 -131
@@ -1,39 +0,0 @@
1
- class TriggerOperator {
2
- constructor() {
3
- this.triggers = [];
4
- }
5
-
6
- /**
7
- * add a collection trigger
8
- * @param {object} trigger DatabaseTrigger object
9
- */
10
- addTrigger(trigger) {
11
- this.triggers.push(trigger);
12
- }
13
-
14
- /**
15
- * Call a trigger
16
- * @param {'find' | 'find-one' | 'count' | 'update-one' | 'insert-one' | 'remove-one' | 'aggregate'} operation operation name
17
- * @param {string} database database name
18
- * @param {string} collection collection name
19
- * @param {string} data
20
- */
21
- call(operation, database, collection, data) {
22
- this.triggers.forEach((trigger) => {
23
- if (
24
- operation == trigger.operation &&
25
- database == trigger.database &&
26
- collection == trigger.collection &&
27
- trigger.callback
28
- )
29
- trigger.callback(data);
30
- });
31
- }
32
-
33
- static get instance() {
34
- return instance;
35
- }
36
- }
37
-
38
- const instance = new TriggerOperator();
39
- module.exports = TriggerOperator.instance;
package/src/class/user.js DELETED
@@ -1,112 +0,0 @@
1
- const { config } = require("../config");
2
- let validateObject = require("./validator");
3
-
4
- module.exports = class User {
5
- constructor(id, permissionGroup, phone, email, password, type, model) {
6
- this.id = id;
7
- this.permissionGroup = permissionGroup;
8
- this.email = email;
9
- this.phone = phone;
10
- this.password = password;
11
- this.type = type;
12
- this.dbModel = model;
13
- }
14
-
15
- getBrief() {
16
- const permissionGroup = config.permissionGroups.find(
17
- (group) => group.title == this.permissionGroup
18
- );
19
-
20
- const brief = {
21
- id: this.id,
22
- permissionGroup: permissionGroup,
23
- phone: this.phone,
24
- email: this.email,
25
- type: this.type,
26
- };
27
-
28
- return brief;
29
- }
30
-
31
- setNewDetail(detail) {
32
- if (detail.phone) this.phone = detail.phone;
33
- if (detail.email) this.email = detail.email;
34
- if (detail.password) this.password = detail.password;
35
- }
36
-
37
- hasPermission(permissionField) {
38
- const permissionGroup = config.permissionGroups.find(
39
- (group) => group.title == this.permissionGroup
40
- );
41
-
42
- if (permissionGroup == null) return false;
43
-
44
- let key = false;
45
-
46
- for (let i = 0; i < permissionGroup.validPermissionTypes.length; i++) {
47
- const userPermissionType = permissionGroup.validPermissionTypes[i];
48
-
49
- if (userPermissionType == permissionField) {
50
- key = true;
51
- break;
52
- }
53
- }
54
-
55
- return key;
56
- }
57
-
58
- async save() {
59
- this.mode["permissionGroup"] = this.permissionGroup;
60
- this.mode["phone"] = this.phone;
61
- this.mode["email"] = this.email;
62
- this.mode["password"] = this.password;
63
-
64
- await mode.save();
65
- }
66
-
67
- static loadFromModel(model) {
68
- return new Promise((done, reject) => {
69
- // check required fields
70
- let isValidData = validateObject(
71
- model,
72
- "fullname email password permission"
73
- );
74
- if (!isValidData) reject(User.notValid(detail));
75
-
76
- let id = model.id;
77
- let permissionGroup = model.permissionGroup;
78
- let phone = model.phone;
79
- let email = model.email;
80
- let password = model.password;
81
- let type = model.type;
82
-
83
- //create user
84
- let newUser = new User(
85
- id,
86
- permissionGroup,
87
- phone,
88
- email,
89
- password,
90
- type,
91
- model
92
- );
93
- done(newUser);
94
- });
95
- }
96
-
97
- static createFromModel(model, detail) {
98
- return new Promise(async (done, reject) => {
99
- //create user
100
- await new model(detail)
101
- .save()
102
- .then((newUser) => done(User.loadFromModel(newUser)))
103
- .catch(reject);
104
- });
105
- }
106
-
107
- static notValid(object) {
108
- let error = `user detail are not valid ${object}`;
109
- console.error(error);
110
- return error;
111
- }
112
- };
@@ -1,91 +0,0 @@
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) {
9
- /*
10
- this method could validate an Object by given field's name list and return bool.
11
- - requiredFields: is a string that contains keys being spared by " ".
12
- */
13
- let type = typeof requiredFields;
14
- let result;
15
-
16
- if (type == 'string')
17
- result = checkSimple(obj, requiredFields);
18
- else if (type == 'object')
19
- result = checkComplex(obj, requiredFields);
20
-
21
- else throw ('requiredFields has wrong form, it must be string or object');
22
-
23
- return result;
24
- }
25
-
26
- module.exports = validate;
27
-
28
- function checkSimple(obj, requiredFields = '') {
29
- let isValide = false;
30
- let requires = requiredFields.split(' ');
31
-
32
- let validMembers = 0;
33
- let notValidKeys = [];
34
-
35
- // return if obj is null
36
- if (obj == null) return _returnResult(isValide, requires);
37
-
38
- requires.forEach(key => {
39
- if (obj[key])
40
- validMembers++;
41
- else notValidKeys.push(key);
42
- });
43
-
44
- // check validation
45
- isValide = (requires.length == validMembers) ? true : false;
46
- return _returnResult(isValide, notValidKeys);
47
- }
48
-
49
- function checkComplex(obj, requiredFields = {}) {
50
- let isValide = false;
51
- let requireKeys = Object.keys(requiredFields);
52
-
53
- let validMembers = 0;
54
- let notValidKeys = [];
55
-
56
- // return if obj is null
57
- if (obj == null) return _returnResult(isValide, requireKeys);
58
-
59
- for (let i = 0; i < requireKeys.length; i++) {
60
- const key = requireKeys[i];
61
- let isValidField = false;
62
-
63
- // if field has specific values
64
- if (requiredFields[key].length > 0) {
65
- let expectedValues = requiredFields[key].split(' ');
66
-
67
- if (typeof expectedValues != 'object')
68
- throw (`${key} must be array of strings`);
69
-
70
- expectedValues.forEach(value => {
71
- if (obj[key] == value) isValidField = true;
72
- })
73
- }
74
- // else does not has specific value
75
- else if (obj[key] != null) isValidField = true;
76
-
77
- if (isValidField) validMembers++;
78
- else notValidKeys.push(key);
79
- }
80
-
81
- // check validation
82
- isValide = (requireKeys.length == validMembers) ? true : false;
83
- return _returnResult(isValide, notValidKeys);
84
- }
85
-
86
- function _returnResult(isValide, notValidKeys) {
87
- return {
88
- 'isValid': isValide,
89
- 'requires': notValidKeys
90
- };
91
- }
package/src/config.js DELETED
@@ -1,67 +0,0 @@
1
- /**
2
- * @typedef {import('koa')} Koa
3
- * @typedef {import('@koa/cors').Options} Cors
4
- * @typedef {import('./class/collection_definition.js')} CollectionDefinition
5
- * @typedef {import('./class/security.js').PermissionGroup} PermissionGroup
6
- * @typedef {import('./class/cms_trigger.js')} CmsTrigger
7
- */
8
-
9
- /**
10
- * @typedef {{
11
- * cors?: Cors; // CORS options.
12
- * modulesPath?: string; // Root directory of your router.js/db.js files.
13
- * koaBodyOptions?: object; // Options for koa-body.
14
- * staticPath?: {
15
- * rootDir: string; // Root directory of your static files.
16
- * rootPath: string; // Root path of your static files, defaults to '/assets'.
17
- * maxage?: number; // Browser cache max-age in milliseconds. Defaults to 0.
18
- * hidden?: boolean; // Allow transfer of hidden files. Defaults to false.
19
- * index?: string; // Default file name. Defaults to 'index.html'.
20
- * defer?: boolean; // If true, serves after return next(), allowing any downstream middleware to respond first. Defaults to false.
21
- * gzip?: boolean; // Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. Defaults to true.
22
- * br?: boolean; // Try to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists. Note that brotli is only accepted over https. Defaults to false.
23
- * setHeaders?: Function; // Function to set custom headers on response.
24
- * extensions?: boolean|Array; // Try to match extensions from passed array to search for file when no extension is suffixed in URL. First found is served. Defaults to false.
25
- * };
26
- * onBeforeInit?: (koaApp:Koa) => void; // A callback called before initializing the Koa server.
27
- * onAfterInit?: (koaApp:Koa) => void; // A callback called after server initialization.
28
- * port?: number; // Server port.
29
- * dontListen?: boolean; // If true, the server will not run and will only return the Koa app object.
30
- * mongo?: {
31
- * dbPrefix: string; // A prefix for your database name.
32
- * mongoBaseAddress: string; // The address of your MongoDB server without any database specification.
33
- * addressMap?: string; // Specific addresses for each database.
34
- * };
35
- * keypair?: {
36
- * private: string; // Private key for RSA authentication.
37
- * public: string; // Public key for RSA authentication.
38
- * };
39
- * adminUser?: {
40
- * email: string; // Admin user email.
41
- * password: string; // Admin user password.
42
- * };
43
- * verificationCodeGeneratorMethod: () => string; // A method to return a verification code when registering a new user.
44
- * collectionDefinitions?: CollectionDefinition[]; // An array of additional collection definitions.
45
- * permissionGroups?: PermissionGroup[]; // An array of additional permission groups.
46
- * authTriggers?: DatabaseTrigger[]; // An array of additional database triggers for the auth collection.
47
- * fileTriggers?: CmsTrigger[]; // An array of additional database triggers for the auth collection.
48
- * }} Config
49
- * @exports Config
50
- */
51
-
52
- /**
53
- * @type {Config}
54
- */
55
- const config = {};
56
-
57
- /**
58
- * @param {Config} options
59
- */
60
- function setConfig(options) {
61
- Object.assign(config, options);
62
- }
63
-
64
- module.exports = {
65
- setConfig,
66
- config,
67
- };
package/src/events.js DELETED
@@ -1,15 +0,0 @@
1
- const eventCallbacks = [];
2
- /**
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:
6
- */
7
-
8
- function registerEventCallback(event, callback) {
9
- if (typeof event !== "string") throw new Error("Event must be a string");
10
-
11
- if (typeof callback !== "function")
12
- throw new Error("Callback must be a function");
13
-
14
- eventCallbacks.push({ event, callback });
15
- }
@@ -1,64 +0,0 @@
1
- const DataProvider = require("../services/data_provider/service");
2
- const {
3
- getDefaultAnonymousPermissionGroup,
4
- getDefaultAdministratorPermissionGroup,
5
- } = require("../services/user_manager/permissionManager");
6
-
7
- const userManager = require("../services/user_manager/service");
8
-
9
- async function createAdminUser({ email, password }) {
10
- let authModel = DataProvider.getCollection("cms", "auth");
11
-
12
- try {
13
- const isAnonymousExisted = await authModel
14
- .countDocuments({ type: "anonymous" })
15
- .exec();
16
-
17
- const isAdministratorExisted = await authModel
18
- .countDocuments({ type: "user", email: email })
19
- .exec();
20
-
21
- if (isAnonymousExisted == 0) {
22
- await userManager.main.registerUser({
23
- permissionGroup: getDefaultAnonymousPermissionGroup().title,
24
- email: "",
25
- phone: "",
26
- password: "",
27
- type: "anonymous",
28
- });
29
- // await new authModel({
30
- // permission: getDefaultAnonymousPermissionGroup().title,
31
- // email: "",
32
- // phone: "",
33
- // password: "",
34
- // type: "anonymous",
35
- // }).save();
36
- }
37
-
38
- if (isAdministratorExisted == 0) {
39
- if (!email || !password) {
40
- return Promise.reject("Invalid email or password for admin user.");
41
- }
42
-
43
- await userManager.main.registerUser({
44
- permissionGroup: getDefaultAdministratorPermissionGroup().title,
45
- email: email,
46
- password: password,
47
- type: "user",
48
- });
49
-
50
- // await new authModel({
51
- // permission: getDefaultAdministratorPermissionGroup().title,
52
- // email: email,
53
- // password: password,
54
- // type: "user",
55
- // }).save();
56
- }
57
- } catch (e) {
58
- return Promise.reject(e);
59
- }
60
- }
61
-
62
- module.exports = {
63
- createAdminUser,
64
- };
@@ -1,31 +0,0 @@
1
- let DataInsertion = require("./data_insertion");
2
- let JWT = require("../services/jwt/service");
3
- let FileService = require("../services/file/service");
4
-
5
- module.exports.setup = async ({ keypair, adminUser, uploadDirectory }) => {
6
- /**
7
- * Json web Token
8
- *
9
- * Setup private and public keys for JWT module
10
- */
11
- if (!keypair) {
12
- // generate new keypair
13
- const generateKeypair = require("keypair");
14
- keypair = generateKeypair();
15
- }
16
-
17
- JWT.main.setKies(keypair.private, keypair.public);
18
-
19
- /**
20
- * Data Insertion
21
- *
22
- * Insert admin user
23
- * for the first time
24
- */
25
- await DataInsertion.createAdminUser(adminUser);
26
-
27
- /**
28
- * File Service
29
- */
30
- FileService.setUploadDirectory(uploadDirectory);
31
- };
package/src/index.js DELETED
@@ -1,66 +0,0 @@
1
- // Application
2
- const createRest = require("./application");
3
- const Schema = require("mongoose").Schema;
4
-
5
- // Utilities
6
- const paginator = require("./class/paginator");
7
- const reply = require("./class/reply");
8
- const validator = require("./class/validator");
9
- const { getCollection } = require("./services/data_provider/service");
10
- const { defineFunction } = require("./services/functions/service");
11
- const TypeCasters = require("./services/data_provider/typeCasters");
12
- const userManager = require("./services/user_manager/service");
13
- const {
14
- getFile,
15
- getFileLink,
16
- getFilePath,
17
- removeFile,
18
- storeFile,
19
- } = require("./services/file/service");
20
-
21
- // Base class
22
- const CollectionDefinition = require("./class/collection_definition");
23
- const Schemas = require("./class/db_schemas");
24
- const DatabaseTrigger = require("./class/database_trigger");
25
- const CmsTrigger = require("./class/cms_trigger");
26
- const SecurityClass = require("./class/security");
27
- const middleware = require("./middlewares");
28
-
29
- module.exports = {
30
- createRest,
31
-
32
- // Database
33
- CollectionDefinition,
34
- Schemas,
35
- Schema,
36
- DatabaseTrigger,
37
- CmsTrigger,
38
- ...SecurityClass,
39
-
40
- // Function
41
- defineFunction,
42
-
43
- // Private utilities
44
- TypeCasters,
45
- validator,
46
-
47
- // Route utilities
48
- reply,
49
- paginator,
50
-
51
- // Database utilities
52
- getCollection,
53
-
54
- // File Utilities
55
- getFile,
56
- getFileLink,
57
- getFilePath,
58
- removeFile,
59
- storeFile,
60
-
61
- // Middleware utilities
62
- middleware,
63
-
64
- // User utilities
65
- userManager: userManager.main,
66
- };
@@ -1,44 +0,0 @@
1
- /**
2
- * Validator module
3
- * @module class/validator
4
- */
5
- let validateObject = require("./class/validator");
6
-
7
- /**
8
- * User manager service
9
- * @module services/user_manager/service
10
- */
11
- const userManager = require("./services/user_manager/service");
12
-
13
- /**
14
- * Authentication middleware
15
- * It checks if incoming request has a valid token in header.authorization
16
- * Then attaches the user object to ctx.state.user
17
- *
18
- * @param {Object} ctx - Koa context
19
- * @param {Function} next - Koa next function
20
- * @returns {Promise<void>}
21
- */
22
- async function auth(ctx, next) {
23
- let headers = ctx.header;
24
- let headersValidated = validateObject(headers, "authorization");
25
-
26
- if (!headersValidated.isValid) ctx.throw(401, "authentication is required");
27
-
28
- let token = headers.authorization;
29
-
30
- await userManager.main
31
- .getUserByToken(token)
32
- .then(async (user) => {
33
- ctx.state.user = user;
34
- await next();
35
- })
36
- .catch((err) => {
37
- console.log(err);
38
- ctx.throw(err.status || 412, err.message);
39
- });
40
- }
41
-
42
- module.exports = {
43
- auth,
44
- };