@modular-rest/server 1.11.13 → 1.11.15

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 +52 -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 +90 -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 +113 -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 +412 -0
  49. package/dist/services/data_provider/service.d.ts +132 -0
  50. package/dist/services/data_provider/service.js +253 -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 +68 -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 +632 -0
  75. package/docs/.keep +0 -0
  76. package/docs/system-access-type.md +26 -0
  77. package/package.json +59 -46
  78. package/src/application.ts +206 -0
  79. package/src/class/cms_trigger.ts +62 -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 +105 -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 +122 -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 +484 -0
  100. package/src/services/data_provider/service.ts +306 -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 +35 -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 +703 -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 -82
  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 -10
  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
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.walk = walk;
7
+ exports.find = find;
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
10
+ /**
11
+ * Walk through a directory and its subdirectories
12
+ * @param dir - Directory to walk
13
+ * @param settings - Settings for filtering files
14
+ * @param done - Callback function
15
+ */
16
+ function walk(dir, settings, done) {
17
+ let results = [];
18
+ // Read director file and folders
19
+ fs_1.default.readdir(dir, function (err, list) {
20
+ if (err)
21
+ return done(err, results);
22
+ let pending = list.length;
23
+ if (!pending)
24
+ return done(null, results);
25
+ list.forEach(function (file) {
26
+ file = path_1.default.join(dir, file);
27
+ fs_1.default.stat(file, function (err, stat) {
28
+ if (err) {
29
+ // Handle file stat error but continue with other files
30
+ console.error(`Error reading file stats for ${file}:`, err);
31
+ if (!--pending)
32
+ done(null, results);
33
+ return;
34
+ }
35
+ // If directory, execute a recursive call
36
+ if (stat && stat.isDirectory()) {
37
+ // Add directory to array [comment if you need to remove the directories from the array]
38
+ // results.push(file);
39
+ walk(file, settings, function (err, res) {
40
+ results = results.concat(res);
41
+ if (!--pending)
42
+ done(null, results);
43
+ });
44
+ }
45
+ else {
46
+ // file filter
47
+ const extension = path_1.default.extname(file);
48
+ const fileName = path_1.default.basename(file).split('.')[0];
49
+ let fileNameKey = true;
50
+ // name filter
51
+ if (settings.name && settings.name === fileName)
52
+ fileNameKey = true;
53
+ else
54
+ fileNameKey = false;
55
+ // extension filter
56
+ if (settings.filter && fileNameKey) {
57
+ settings.filter.forEach(function (element) {
58
+ if (element.toLowerCase() === extension.toLowerCase())
59
+ results.push(file);
60
+ });
61
+ }
62
+ // push any file if no option
63
+ else if (fileNameKey)
64
+ results.push(file);
65
+ if (!--pending)
66
+ done(null, results);
67
+ }
68
+ });
69
+ });
70
+ });
71
+ }
72
+ /**
73
+ * Find files in a directory with Promise API
74
+ * @param dir - Directory to search
75
+ * @param settings - Settings for filtering files
76
+ * @returns Promise resolving to an array of file paths
77
+ */
78
+ function find(dir, settings) {
79
+ return new Promise((resolve, reject) => {
80
+ walk(dir, settings, (err, result) => {
81
+ if (err)
82
+ reject(err);
83
+ else
84
+ resolve(result);
85
+ });
86
+ });
87
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Pagination result interface
3
+ */
4
+ export interface PaginationResult {
5
+ pages: number;
6
+ page: number;
7
+ from: number;
8
+ to: number;
9
+ }
10
+ /**
11
+ * Creates a pagination object based on the given parameters.
12
+ * @param count - The total number of items to paginate.
13
+ * @param perPage - The number of items to display per page.
14
+ * @param page - The current page number.
15
+ * @returns An object containing pagination information.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * import { paginator } from '@modular-rest/server';
20
+ *
21
+ * const pagination = paginator.create(100, 10, 1);
22
+ * // json response will be like this
23
+ * // {
24
+ * // pages: 10,
25
+ * // page: 1,
26
+ * // from: 0,
27
+ * // to: 10,
28
+ * // }
29
+ * ```
30
+ */
31
+ export declare function create(count: number, perPage: number, page: number): PaginationResult;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.create = create;
4
+ /**
5
+ * Creates a pagination object based on the given parameters.
6
+ * @param count - The total number of items to paginate.
7
+ * @param perPage - The number of items to display per page.
8
+ * @param page - The current page number.
9
+ * @returns An object containing pagination information.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { paginator } from '@modular-rest/server';
14
+ *
15
+ * const pagination = paginator.create(100, 10, 1);
16
+ * // json response will be like this
17
+ * // {
18
+ * // pages: 10,
19
+ * // page: 1,
20
+ * // from: 0,
21
+ * // to: 10,
22
+ * // }
23
+ * ```
24
+ */
25
+ function create(count, perPage, page) {
26
+ const totalPages = Math.ceil(count / perPage);
27
+ if (page > totalPages)
28
+ page = 1;
29
+ let from = 0;
30
+ if (perPage === 1)
31
+ from = page - 1;
32
+ else
33
+ from = perPage * page - perPage;
34
+ if (page <= 1)
35
+ from = 0;
36
+ const result = {
37
+ pages: totalPages,
38
+ page: page,
39
+ from: from,
40
+ to: perPage,
41
+ };
42
+ return result;
43
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Response status type
3
+ */
4
+ export type ResponseStatus = 's' | 'f' | 'e';
5
+ /**
6
+ * Response object interface
7
+ */
8
+ export interface ResponseObject {
9
+ status: 'success' | 'fail' | 'error';
10
+ [key: string]: any;
11
+ }
12
+ /**
13
+ * Creates a response object with the given status and detail.
14
+ *
15
+ * @param status - The status of the response. Can be "s" for success, "f" for fail, or "e" for error.
16
+ * @param detail - The detail of the response. Can contain any additional information about the response.
17
+ * @returns The response object with the given status and detail.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * import { reply } from '@modular-rest/server';
22
+ *
23
+ * // inside the router
24
+ * const response = reply.create("s", { message: "Hello, world!" });
25
+ * ctx.body = response;
26
+ * ctx.status = 200;
27
+ * ```
28
+ */
29
+ export declare function create(status: ResponseStatus, detail?: Record<string, any>): ResponseObject;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.create = create;
4
+ /**
5
+ * Creates a response object with the given status and detail.
6
+ *
7
+ * @param status - The status of the response. Can be "s" for success, "f" for fail, or "e" for error.
8
+ * @param detail - The detail of the response. Can contain any additional information about the response.
9
+ * @returns The response object with the given status and detail.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { reply } from '@modular-rest/server';
14
+ *
15
+ * // inside the router
16
+ * const response = reply.create("s", { message: "Hello, world!" });
17
+ * ctx.body = response;
18
+ * ctx.status = 200;
19
+ * ```
20
+ */
21
+ function create(status, detail = {}) {
22
+ // Initialize with a default status that will be overwritten
23
+ const result = {
24
+ status: 'success',
25
+ ...detail,
26
+ };
27
+ // define status
28
+ switch (status) {
29
+ case 's':
30
+ result.status = 'success';
31
+ break;
32
+ case 'f':
33
+ result.status = 'fail';
34
+ break;
35
+ case 'e':
36
+ result.status = 'error';
37
+ break;
38
+ default:
39
+ result.status = 'success';
40
+ break;
41
+ }
42
+ // return
43
+ return result;
44
+ }
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Permission type string literal type that defines various access levels and capabilities
3
+
4
+ * @inline
5
+ */
6
+ export type AccessType = 'god_access' | 'user_access' | 'upload_file_access' | 'remove_file_access' | 'anonymous_access' | 'advanced_settings' | string;
7
+ /**
8
+ * Defines access control for a specific database collection
9
+ *
10
+ * @internal
11
+ */
12
+ export declare class AccessDefinition {
13
+ /** @hidden */
14
+ database: string;
15
+ /** @hidden */
16
+ collection: string;
17
+ /** @hidden */
18
+ permissionList: Permission[];
19
+ /**
20
+ * Creates a new AccessDefinition instance
21
+ * @param {Object} options - Configuration options
22
+ * @param {string} options.database - The name of the database
23
+ * @param {string} options.collection - The name of the collection
24
+ * @param {Permission[]} options.permissionList - List of permissions
25
+ */
26
+ constructor({ database, collection, permissionList, }: {
27
+ database: string;
28
+ collection: string;
29
+ permissionList: Permission[];
30
+ });
31
+ }
32
+ /**
33
+ * Defines a permission for accessing data within the system. This class is a fundamental component used in both the {@link defineCollection} method and {@link CollectionDefinition} class
34
+ * by specifying which permission types can interact with them. The permission system matches a user's assigned permission types against
35
+ * the collection's permissions to determine access levels. For example, a collection can allow read access for 'user_access' while
36
+ * restricting writes to 'advanced_settings' permissions.
37
+ *
38
+ * @remark
39
+ * {@include ../../docs/system-access-type.md}
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * import { Permission } from '@modular-rest/server';
44
+ *
45
+ * const permission = new Permission({
46
+ * type: 'user_access',
47
+ * read: true,
48
+ * write: true,
49
+ * onlyOwnData: true,
50
+ * ownerIdField: 'userId'
51
+ * });
52
+ * ```
53
+ */
54
+ export declare class Permission {
55
+ /** @hidden */
56
+ accessType: AccessType;
57
+ /** @hidden */
58
+ read: boolean;
59
+ /** @hidden */
60
+ write: boolean;
61
+ /** @hidden */
62
+ onlyOwnData: boolean;
63
+ /** @hidden */
64
+ ownerIdField: string;
65
+ /**
66
+ * Creates a new Permission instance
67
+ * @param {Object} options - Configuration options
68
+ *
69
+ * @param {AccessType} options.type - The type of permission,system defined or custom. check the **Remarks section** for more information.
70
+ *
71
+ * @param {boolean} [options.read=false] - Whether read access is granted
72
+ * @param {boolean} [options.write=false] - Whether write access is granted
73
+ * @param {boolean} [options.onlyOwnData=false] - Whether access is limited to own data
74
+ * @param {string} [options.ownerIdField='refId'] - Field name for owner identification
75
+ */
76
+ constructor({ accessType: type, read, write, onlyOwnData, ownerIdField, }: {
77
+ accessType: AccessType;
78
+ read?: boolean;
79
+ write?: boolean;
80
+ onlyOwnData?: boolean;
81
+ ownerIdField?: string;
82
+ });
83
+ }
84
+ /**
85
+ * A comprehensive access control mechanism that manages user permissions through grouped access types.
86
+ *
87
+ * Permission groups are a fundamental security concept that define and enforce what actions users
88
+ * can perform within the system. They provide a flexible and maintainable way to handle authorization
89
+ * by grouping related access types together.
90
+ *
91
+ * These groups enable users to:
92
+ * 1. Read and write data from collections that match their access types, allowing granular control
93
+ * over database operations
94
+ * 2. Execute specific functions that require matching access types, ensuring that sensitive
95
+ * operations are only performed by authorized users
96
+ * 3. Perform custom developer-defined actions by validating against the user's access types,
97
+ * enabling extensible permission-based features
98
+ *
99
+ * Permission groups can be configured as default groups for new users or anonymous groups for
100
+ * unauthenticated access, providing a complete authorization framework.
101
+ *
102
+ * @class PermissionGroup
103
+ * @property {string} title - The title of the permission group
104
+ * @property {boolean} isDefault - This is a default group, on `true` this permission group will be given to any new user automatically.
105
+ * @property {boolean} isAnonymous - This is a anonymous group, on `true` will be used for anonymous users.
106
+ * @property {AccessType[]} allowedAccessTypes - List of valid access types.
107
+ * @example
108
+ * ```typescript
109
+ * const group = new PermissionGroup({
110
+ * title: 'Admin',
111
+ * isDefault: true,
112
+ * allowedAccessTypes: ['god_access', 'advanced_settings']
113
+ * });
114
+ * ```
115
+ */
116
+ export declare class PermissionGroup {
117
+ /** @hidden */
118
+ title: string;
119
+ /** @hidden */
120
+ isDefault: boolean;
121
+ /** @hidden */
122
+ isAnonymous: boolean;
123
+ /** @hidden */
124
+ allowedAccessTypes: AccessType[];
125
+ /**
126
+ * Creates a new PermissionGroup instance
127
+ * @param {Object} options - Configuration options
128
+ * @param {string} options.title - The title of the group
129
+ * @param {boolean} [options.isDefault=false] - Whether this is a default group
130
+ * @param {boolean} [options.isAnonymous=false] - Whether this group is for anonymous users
131
+ * @param {AccessType[]} [options.allowedAccessTypes=[]] - List of valid permission types
132
+ */
133
+ constructor({ title, isDefault, isAnonymous, allowedAccessTypes, }: {
134
+ title: string;
135
+ isDefault?: boolean;
136
+ isAnonymous?: boolean;
137
+ allowedAccessTypes?: AccessType[];
138
+ });
139
+ }
140
+ /**
141
+ * Provides static access to access type constants
142
+ * @class AccessTypes
143
+ */
144
+ export declare class AccessTypes {
145
+ /**
146
+ * Get the string representing read access type
147
+ * @returns {string} The read access type
148
+ */
149
+ static get read(): string;
150
+ /**
151
+ * Get the string representing write access type
152
+ * @returns {string} The write access type
153
+ */
154
+ static get write(): string;
155
+ }
156
+ /**
157
+ * Provides static access to permission type constants
158
+ * @class PermissionTypes
159
+ */
160
+ export declare class PermissionTypes {
161
+ /**
162
+ * Get the string representing god access permission type
163
+ * @returns {string} The god access permission type
164
+ */
165
+ static get god_access(): AccessType;
166
+ /**
167
+ * Get the string representing advanced settings permission type
168
+ * @returns {string} The advanced settings permission type
169
+ */
170
+ static get advanced_settings(): AccessType;
171
+ /**
172
+ * Get the string representing user access permission type
173
+ * @returns {string} The user access permission type
174
+ */
175
+ static get user_access(): AccessType;
176
+ /**
177
+ * Get the string representing upload file access permission type
178
+ * @returns {string} The upload file access permission type
179
+ */
180
+ static get upload_file_access(): AccessType;
181
+ /**
182
+ * Get the string representing remove file access permission type
183
+ * @returns {string} The remove file access permission type
184
+ */
185
+ static get remove_file_access(): AccessType;
186
+ }
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PermissionTypes = exports.AccessTypes = exports.PermissionGroup = exports.Permission = exports.AccessDefinition = void 0;
4
+ /**
5
+ * Defines access control for a specific database collection
6
+ *
7
+ * @internal
8
+ */
9
+ class AccessDefinition {
10
+ /**
11
+ * Creates a new AccessDefinition instance
12
+ * @param {Object} options - Configuration options
13
+ * @param {string} options.database - The name of the database
14
+ * @param {string} options.collection - The name of the collection
15
+ * @param {Permission[]} options.permissionList - List of permissions
16
+ */
17
+ constructor({ database, collection, permissionList, }) {
18
+ this.database = database;
19
+ this.collection = collection;
20
+ this.permissionList = permissionList;
21
+ }
22
+ }
23
+ exports.AccessDefinition = AccessDefinition;
24
+ /**
25
+ * Defines a permission for accessing data within the system. This class is a fundamental component used in both the {@link defineCollection} method and {@link CollectionDefinition} class
26
+ * by specifying which permission types can interact with them. The permission system matches a user's assigned permission types against
27
+ * the collection's permissions to determine access levels. For example, a collection can allow read access for 'user_access' while
28
+ * restricting writes to 'advanced_settings' permissions.
29
+ *
30
+ * @remark
31
+ * {@include ../../docs/system-access-type.md}
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * import { Permission } from '@modular-rest/server';
36
+ *
37
+ * const permission = new Permission({
38
+ * type: 'user_access',
39
+ * read: true,
40
+ * write: true,
41
+ * onlyOwnData: true,
42
+ * ownerIdField: 'userId'
43
+ * });
44
+ * ```
45
+ */
46
+ class Permission {
47
+ /**
48
+ * Creates a new Permission instance
49
+ * @param {Object} options - Configuration options
50
+ *
51
+ * @param {AccessType} options.type - The type of permission,system defined or custom. check the **Remarks section** for more information.
52
+ *
53
+ * @param {boolean} [options.read=false] - Whether read access is granted
54
+ * @param {boolean} [options.write=false] - Whether write access is granted
55
+ * @param {boolean} [options.onlyOwnData=false] - Whether access is limited to own data
56
+ * @param {string} [options.ownerIdField='refId'] - Field name for owner identification
57
+ */
58
+ constructor({ accessType: type, read = false, write = false, onlyOwnData = false, ownerIdField = 'refId', }) {
59
+ this.accessType = type;
60
+ this.read = read;
61
+ this.write = write;
62
+ this.onlyOwnData = onlyOwnData;
63
+ this.ownerIdField = ownerIdField;
64
+ }
65
+ }
66
+ exports.Permission = Permission;
67
+ /**
68
+ * A comprehensive access control mechanism that manages user permissions through grouped access types.
69
+ *
70
+ * Permission groups are a fundamental security concept that define and enforce what actions users
71
+ * can perform within the system. They provide a flexible and maintainable way to handle authorization
72
+ * by grouping related access types together.
73
+ *
74
+ * These groups enable users to:
75
+ * 1. Read and write data from collections that match their access types, allowing granular control
76
+ * over database operations
77
+ * 2. Execute specific functions that require matching access types, ensuring that sensitive
78
+ * operations are only performed by authorized users
79
+ * 3. Perform custom developer-defined actions by validating against the user's access types,
80
+ * enabling extensible permission-based features
81
+ *
82
+ * Permission groups can be configured as default groups for new users or anonymous groups for
83
+ * unauthenticated access, providing a complete authorization framework.
84
+ *
85
+ * @class PermissionGroup
86
+ * @property {string} title - The title of the permission group
87
+ * @property {boolean} isDefault - This is a default group, on `true` this permission group will be given to any new user automatically.
88
+ * @property {boolean} isAnonymous - This is a anonymous group, on `true` will be used for anonymous users.
89
+ * @property {AccessType[]} allowedAccessTypes - List of valid access types.
90
+ * @example
91
+ * ```typescript
92
+ * const group = new PermissionGroup({
93
+ * title: 'Admin',
94
+ * isDefault: true,
95
+ * allowedAccessTypes: ['god_access', 'advanced_settings']
96
+ * });
97
+ * ```
98
+ */
99
+ class PermissionGroup {
100
+ /**
101
+ * Creates a new PermissionGroup instance
102
+ * @param {Object} options - Configuration options
103
+ * @param {string} options.title - The title of the group
104
+ * @param {boolean} [options.isDefault=false] - Whether this is a default group
105
+ * @param {boolean} [options.isAnonymous=false] - Whether this group is for anonymous users
106
+ * @param {AccessType[]} [options.allowedAccessTypes=[]] - List of valid permission types
107
+ */
108
+ constructor({ title, isDefault = false, isAnonymous = false, allowedAccessTypes = [], }) {
109
+ this.title = title;
110
+ this.isDefault = isDefault;
111
+ this.isAnonymous = isAnonymous;
112
+ this.allowedAccessTypes = allowedAccessTypes;
113
+ }
114
+ }
115
+ exports.PermissionGroup = PermissionGroup;
116
+ /**
117
+ * Provides static access to access type constants
118
+ * @class AccessTypes
119
+ */
120
+ class AccessTypes {
121
+ /**
122
+ * Get the string representing read access type
123
+ * @returns {string} The read access type
124
+ */
125
+ static get read() {
126
+ return 'read';
127
+ }
128
+ /**
129
+ * Get the string representing write access type
130
+ * @returns {string} The write access type
131
+ */
132
+ static get write() {
133
+ return 'write';
134
+ }
135
+ }
136
+ exports.AccessTypes = AccessTypes;
137
+ /**
138
+ * Provides static access to permission type constants
139
+ * @class PermissionTypes
140
+ */
141
+ class PermissionTypes {
142
+ /**
143
+ * Get the string representing god access permission type
144
+ * @returns {string} The god access permission type
145
+ */
146
+ static get god_access() {
147
+ return 'god_access';
148
+ }
149
+ /**
150
+ * Get the string representing advanced settings permission type
151
+ * @returns {string} The advanced settings permission type
152
+ */
153
+ static get advanced_settings() {
154
+ return 'advanced_settings';
155
+ }
156
+ /**
157
+ * Get the string representing user access permission type
158
+ * @returns {string} The user access permission type
159
+ */
160
+ static get user_access() {
161
+ return 'user_access';
162
+ }
163
+ /**
164
+ * Get the string representing upload file access permission type
165
+ * @returns {string} The upload file access permission type
166
+ */
167
+ static get upload_file_access() {
168
+ return 'upload_file_access';
169
+ }
170
+ /**
171
+ * Get the string representing remove file access permission type
172
+ * @returns {string} The remove file access permission type
173
+ */
174
+ static get remove_file_access() {
175
+ return 'remove_file_access';
176
+ }
177
+ }
178
+ exports.PermissionTypes = PermissionTypes;
@@ -0,0 +1,92 @@
1
+ import { DatabaseOperation } from './database_trigger';
2
+ /**
3
+ * Interface defining a database trigger
4
+ * @interface Trigger
5
+ * @property {DatabaseOperation} operation - The database operation that triggers this callback
6
+ * @property {string} database - The database name to monitor
7
+ * @property {string} collection - The collection name to monitor
8
+ * @property {(data: any) => void} callback - Function to execute when trigger conditions are met
9
+ * @example
10
+ * ```typescript
11
+ * const trigger: Trigger = {
12
+ * operation: 'insert',
13
+ * database: 'myDB',
14
+ * collection: 'users',
15
+ * callback: (data) => {
16
+ * console.log('New user inserted:', data);
17
+ * }
18
+ * };
19
+ * ```
20
+ */
21
+ interface Trigger {
22
+ operation: DatabaseOperation;
23
+ database: string;
24
+ collection: string;
25
+ callback: (data: any) => void;
26
+ }
27
+ /**
28
+ * Singleton class for managing database triggers
29
+ * Provides functionality to add and execute triggers based on database operations
30
+ * @class TriggerOperator
31
+ * @example
32
+ * ```typescript
33
+ * // Add a trigger for user insertions
34
+ * TriggerOperator.instance.addTrigger({
35
+ * operation: 'insert',
36
+ * database: 'myDB',
37
+ * collection: 'users',
38
+ * callback: (data) => {
39
+ * console.log('New user inserted:', data);
40
+ * }
41
+ * });
42
+ * ```
43
+ */
44
+ declare class TriggerOperator {
45
+ private triggers;
46
+ private static _instance;
47
+ private constructor();
48
+ /**
49
+ * Gets the singleton instance of TriggerOperator
50
+ * @static
51
+ * @returns {TriggerOperator} The singleton instance
52
+ */
53
+ static get instance(): TriggerOperator;
54
+ /**
55
+ * Adds a new trigger to the registry
56
+ * @param {Trigger} trigger - The trigger configuration to add
57
+ * @throws {Error} If trigger is invalid or already exists
58
+ * @example
59
+ * ```typescript
60
+ * // Add a trigger for document updates
61
+ * TriggerOperator.instance.addTrigger({
62
+ * operation: 'update',
63
+ * database: 'myDB',
64
+ * collection: 'documents',
65
+ * callback: (data) => {
66
+ * console.log('Document updated:', data);
67
+ * }
68
+ * });
69
+ * ```
70
+ */
71
+ addTrigger(trigger: Trigger): void;
72
+ /**
73
+ * Executes all matching triggers for a given database operation
74
+ * @param {DatabaseOperation} operation - The database operation that occurred
75
+ * @param {string} database - The database where the operation occurred
76
+ * @param {string} collection - The collection where the operation occurred
77
+ * @param {any} data - The data associated with the operation
78
+ * @example
79
+ * ```typescript
80
+ * // This would typically be called by the database layer
81
+ * TriggerOperator.instance.call(
82
+ * 'insert',
83
+ * 'myDB',
84
+ * 'users',
85
+ * { id: 1, name: 'John' }
86
+ * );
87
+ * ```
88
+ */
89
+ call(operation: DatabaseOperation, database: string, collection: string, data: any): void;
90
+ }
91
+ declare const _default: TriggerOperator;
92
+ export = _default;