@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.
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 -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
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ v21.5.0
package/.prettierrc.json CHANGED
@@ -0,0 +1,9 @@
1
+ {
2
+ "semi": true,
3
+ "tabWidth": 2,
4
+ "printWidth": 100,
5
+ "singleQuote": true,
6
+ "trailingComma": "es5",
7
+ "bracketSpacing": true,
8
+ "arrowParens": "avoid"
9
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "extends": "semantic-release-monorepo",
3
+ "branches": ["main"],
4
+ "tagFormat": "@modular-rest/server_v${version}",
5
+ "plugins": [
6
+ ["@semantic-release/commit-analyzer", {
7
+ "preset": "angular",
8
+ "releaseRules": [
9
+ {"type": "docs", "scope": "README", "release": "patch"},
10
+ {"type": "feat", "release": "minor"},
11
+ {"type": "fix", "release": "patch"},
12
+ {"type": "perf", "release": "patch"},
13
+ {"type": "revert", "release": "patch"},
14
+ {"type": "style", "release": "patch"},
15
+ {"type": "test", "release": "patch"},
16
+ {"type": "ci", "release": "patch"},
17
+ {"type": "chore", "release": "patch"}
18
+ ]
19
+ }],
20
+ "@semantic-release/release-notes-generator",
21
+ "@semantic-release/npm",
22
+ "@semantic-release/github"
23
+ ]
24
+ }
package/README.md CHANGED
@@ -1,114 +1,99 @@
1
- # Modular-Rest
2
- a nodejs module based on KOAJS for developing Rest-APIs in a modular solution.
3
- - modular-rest is only for developing rest api, and is not based on MVC.
4
- - each route would be a module in this system. you can define your base routes on difrent folders then the Modular-Rest would combine all routes to main app object.
1
+ # @modular-rest/server-ts
5
2
 
6
- this module has one method:
7
- - `createRest`: is the main functionality of the module.
3
+ TypeScript version of a Node.js module based on Koa.js for developing REST APIs in a modular solution.
8
4
 
9
- **Note**: active "javascript.implicitProjectConfig.checkJs": true in your vscode settings.
5
+ ## Version 2.0 Breaking Changes
10
6
 
11
- ## Install
7
+ The 2.0 release includes several breaking changes:
12
8
 
13
- Install using [npm](https://www.npmjs.com/package/modular-rest):
9
+ - Complete rewrite in TypeScript with full type safety and improved IDE support
10
+ - Auto-generated API documentation using TypeDoc
11
+ - Changed configuration structure for better TypeScript support
14
12
 
15
- ```sh
16
- npm i modular-rest --save
13
+ ## Migration Status
14
+
15
+ This is a TypeScript migration of the original [@modular-rest/server](https://github.com/modular-rest/modular-rest) package. While maintaining compatibility with the JavaScript version, it adds type safety and better developer experience.
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install @modular-rest/server-ts
21
+ # or
22
+ yarn add @modular-rest/server-ts
17
23
  ```
18
24
 
19
- ## use modular-rest `createRest`
20
-
21
- to work with modular-rest you need an `app.js` and a `routers` folder. then configuring `app.js` and put your each router as a subfolder into the `routers` folder.
22
-
23
-
24
- ### configuring `app.js`
25
- simple configuration of `app.js` with `koa-router` module.
26
-
27
- ```js
28
- const modularRest = require('modular-rest');
29
- let koaBody = require('koa-body');
30
-
31
- let option = {
32
- root: require('path').join(__dirname, 'routers'),
33
- onBeforInit: BeforInit, // befor anything
34
- onInit: Init, // after collecting routers
35
- onAfterInit: AfterInit, // affter launch server
36
- port: 80,
37
-
38
- // if it would be true, app doesn't listen to port,
39
- // and a raw app object with all routers will be returned.
40
- // this option is for virtual host middlewares
41
- dontlisten: false,
42
-
43
- // collecting other services from subfolders
44
- otherSrvice: [
45
- {
46
- filename: {name: 'fn', extension:'.js'},
47
- rootDirectory: require('path').join(__dirname, 'routers'),
48
- option: {
49
- // if this option woulde be true, the property of each service will be attached to rootObject
50
- // the `name` property will be rejected and only the main property of each service would be recognize.
51
- // it would be useful when you want to collect all mongoose models in one root object.
52
- combineWithRoot: false,
53
-
54
- // convert the rootObject to an array
55
- // the `name` property will be rejected and only the main property of each service would be recognize.
56
- convertToArray: false,
57
- }
58
- }
59
- ],
60
- };
61
-
62
- function BeforInit(app)
63
- {
64
- // use a body parser
65
- // it must be used befor init
66
- app.use(koaBody());
67
- }
68
-
69
- function Init(app, otherSrvice)
70
- {
71
- // use otherSrvice
72
- // all your other services will injected to `otherSrvice` object.
73
- // eahc service would be accessible by its filename
74
- global.services = otherSrvice['fn'];
75
- }
76
-
77
- function AfterInit(app, otherSrvice) {
78
- // do something
79
- }
80
-
81
- modularRest.createRest(option).then(app => {
82
- // do something
25
+ ## Usage
26
+
27
+ ```typescript
28
+ import { createRest } from '@modular-rest/server-ts';
29
+
30
+ // Create a Koa application with modular-rest
31
+ createRest({
32
+ port: 3000,
33
+ modulesPath: __dirname + '/modules',
34
+ uploadDirectory: __dirname + '/uploads',
35
+ staticPath: {
36
+ rootDir: __dirname + '/assets',
37
+ rootPath: '/assets'
38
+ },
39
+ mongo: {
40
+ mongoBaseAddress: 'mongodb://localhost:27017',
41
+ dbPrefix: 'myapp_'
42
+ },
43
+ adminUser: {
44
+ email: 'admin@example.com',
45
+ password: 'securePassword'
46
+ }
47
+ }).then(({ app, server }) => {
48
+ console.log('Server started successfully');
83
49
  });
84
50
  ```
85
51
 
86
- ### configuring a route
87
- 1. go to `routers` folder and create a subfolder called `search` folder.
88
- 2. go to `search` folder and create a `route.js` file, then put these lines into it.
52
+ ## Key Features
89
53
 
90
- ```js
91
- let Router = require('koa-router');
92
- let name = 'search';
54
+ - Modular REST API development with Koa.js
55
+ - Built-in MongoDB integration
56
+ - User authentication and authorization
57
+ - File upload and management
58
+ - Event system
59
+ - Extensible architecture
93
60
 
94
- let search = new Router();
95
- search.get('/', (ctx) => {
96
- ctx.body = 'send me a post request';
97
- });
61
+ ## TypeScript Support
62
+
63
+ This package includes full TypeScript type definitions for all API components. The entire codebase is written in TypeScript, providing:
64
+
65
+ - IntelliSense and autocompletion in modern IDEs
66
+ - Type safety for all API operations
67
+ - Better compile-time error detection
68
+ - Improved developer experience
69
+
70
+ ## Documentation
98
71
 
99
- search.post('/', (ctx) => {
100
- ctx.body = `Request Body: ${JSON.stringify(ctx.request.body)}`;
101
- })
72
+ The API documentation is automatically generated using TypeDoc. You can access the latest documentation:
102
73
 
103
- module.exports.name = name;
104
- module.exports.main = search;
74
+ - In the `docs` directory of this repository
75
+ - On the [modular-rest documentation site](https://modular-rest.github.io/docs)
76
+
77
+ ## Development
78
+
79
+ To build the package:
80
+
81
+ ```bash
82
+ yarn build
105
83
  ```
106
84
 
107
- ### Requesting
108
- your search web service is:
85
+ To run the development build with watch mode:
86
+
87
+ ```bash
88
+ yarn dev
109
89
  ```
110
- http://localhost:80/search
90
+
91
+ To generate documentation:
92
+
93
+ ```bash
94
+ yarn docs
111
95
  ```
112
96
 
97
+ ## License
113
98
 
114
- thank you for using Modular-Rest :)
99
+ MIT
@@ -0,0 +1,29 @@
1
+ import Koa from 'koa';
2
+ import { Server } from 'http';
3
+ import { RestOptions } from './config';
4
+ /**
5
+ * Create a modular REST instance with Koa and MongoDB support.
6
+ *
7
+ * @param {RestOptions} options - Options for the REST instance
8
+ * @expandType RestOptions
9
+ *
10
+ * @returns {Promise<{ app: Koa; server?: Server }>} - A promise that resolves to the Koa app and server
11
+ *
12
+ * @example
13
+ * import { createRest } from '@modular-rest/server';
14
+ *
15
+ * const app = createRest({
16
+ * port: '80',
17
+ * mongo: {
18
+ * mongoBaseAddress: 'mongodb://localhost:27017',
19
+ * dbPrefix: 'mrest_'
20
+ * },
21
+ * onBeforeInit: (koaApp) => {
22
+ * // do something before init with the koa app
23
+ * }
24
+ * })
25
+ */
26
+ export declare function createRest(options: RestOptions): Promise<{
27
+ app: Koa;
28
+ server?: Server;
29
+ }>;
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.createRest = createRest;
40
+ const koa_1 = __importDefault(require("koa"));
41
+ const cors_1 = __importDefault(require("@koa/cors"));
42
+ const koa_body_1 = __importDefault(require("koa-body"));
43
+ const koa_static_1 = __importDefault(require("koa-static"));
44
+ const koa_mount_1 = __importDefault(require("koa-mount"));
45
+ const path_1 = __importDefault(require("path"));
46
+ const combinator_1 = __importDefault(require("./class/combinator"));
47
+ const DataProvider = __importStar(require("./services/data_provider/service"));
48
+ const UserService = __importStar(require("./services/user_manager/service"));
49
+ const config_1 = require("./config");
50
+ const defult_permissions_1 = require("./defult-permissions");
51
+ const defaultServiceRoot = __dirname + '/services';
52
+ /**
53
+ * Create a modular REST instance with Koa and MongoDB support.
54
+ *
55
+ * @param {RestOptions} options - Options for the REST instance
56
+ * @expandType RestOptions
57
+ *
58
+ * @returns {Promise<{ app: Koa; server?: Server }>} - A promise that resolves to the Koa app and server
59
+ *
60
+ * @example
61
+ * import { createRest } from '@modular-rest/server';
62
+ *
63
+ * const app = createRest({
64
+ * port: '80',
65
+ * mongo: {
66
+ * mongoBaseAddress: 'mongodb://localhost:27017',
67
+ * dbPrefix: 'mrest_'
68
+ * },
69
+ * onBeforeInit: (koaApp) => {
70
+ * // do something before init with the koa app
71
+ * }
72
+ * })
73
+ */
74
+ async function createRest(options) {
75
+ (0, config_1.setConfig)({
76
+ port: 3000,
77
+ dontListen: false,
78
+ mongo: {
79
+ mongoBaseAddress: 'mongodb://localhost:27017',
80
+ dbPrefix: 'mrest_',
81
+ },
82
+ adminUser: {
83
+ email: 'admin@email.com',
84
+ password: '@dmin',
85
+ },
86
+ ...options,
87
+ });
88
+ if (config_1.config.permissionGroups === undefined) {
89
+ config_1.config.permissionGroups = defult_permissions_1.permissionGroups;
90
+ }
91
+ const app = new koa_1.default();
92
+ /**
93
+ * Plug in Cors
94
+ */
95
+ app.use((0, cors_1.default)(config_1.config.cors || {}));
96
+ /**
97
+ * Plug in BodyParser
98
+ */
99
+ const bodyParserOptions = {
100
+ multipart: true,
101
+ ...(config_1.config.koaBodyOptions || {}),
102
+ };
103
+ app.use((0, koa_body_1.default)(bodyParserOptions));
104
+ /**
105
+ * Plug In KoaStatic
106
+ */
107
+ if (config_1.config.staticPath) {
108
+ const defaultStaticPath = config_1.config.staticPath.actualPath || '';
109
+ const defaultStaticRootPath = config_1.config.staticPath.path || '/assets';
110
+ const staticOptions = { ...config_1.config.staticPath };
111
+ delete staticOptions.actualPath;
112
+ delete staticOptions.path;
113
+ app.use((0, koa_mount_1.default)(defaultStaticRootPath, (0, koa_static_1.default)(defaultStaticPath, staticOptions)));
114
+ }
115
+ /**
116
+ * Run before hook
117
+ */
118
+ if (config_1.config.onBeforeInit)
119
+ config_1.config.onBeforeInit(app);
120
+ /**
121
+ * Setup default services
122
+ *
123
+ * - Collect and plug in router.js/db.js of default services
124
+ * - Setting up default services
125
+ */
126
+ // 1. Plug in default routes
127
+ await combinator_1.default.combineRoutesByFilePath(path_1.default.join(defaultServiceRoot), app);
128
+ // Collect default databaseDefinitions
129
+ const defaultDatabaseDefinitionList = await combinator_1.default.combineModulesByFilePath({
130
+ rootDirectory: defaultServiceRoot,
131
+ filename: {
132
+ name: 'db',
133
+ extension: '.js',
134
+ },
135
+ combineWithRoot: true,
136
+ });
137
+ // 2. Plug in default databaseDefinitions
138
+ await DataProvider.addCollectionDefinitionByList({
139
+ list: defaultDatabaseDefinitionList,
140
+ mongoOption: config_1.config.mongo,
141
+ });
142
+ /**
143
+ * User Services
144
+ *
145
+ * 3. Plug in routes and database
146
+ */
147
+ if (config_1.config.modulesPath) {
148
+ // Plug in user routes
149
+ await combinator_1.default.combineRoutesByFilePath(config_1.config.modulesPath, app);
150
+ // Collect user CollectionDefinitions (db.js files)
151
+ let userDatabaseDetail = [];
152
+ userDatabaseDetail = await combinator_1.default.combineModulesByFilePath({
153
+ rootDirectory: config_1.config.modulesPath,
154
+ filename: {
155
+ name: 'db',
156
+ extension: '.js',
157
+ },
158
+ combineWithRoot: true,
159
+ });
160
+ // Combine additional CollectionDefinitions
161
+ if (config_1.config.collectionDefinitions) {
162
+ userDatabaseDetail = userDatabaseDetail.concat(config_1.config.collectionDefinitions);
163
+ }
164
+ // Plug in user CollectionDefinitions
165
+ await DataProvider.addCollectionDefinitionByList({
166
+ list: userDatabaseDetail || [],
167
+ mongoOption: config_1.config.mongo,
168
+ });
169
+ // Plug in Verification method
170
+ if (typeof config_1.config.verificationCodeGeneratorMethod === 'function') {
171
+ UserService.main.setCustomVerificationCodeGeneratorMethod(config_1.config.verificationCodeGeneratorMethod);
172
+ }
173
+ // 4. plug in modular functions
174
+ await combinator_1.default.combineFunctionsByFilePath({
175
+ rootDirectory: config_1.config.modulesPath,
176
+ filename: {
177
+ name: 'functions',
178
+ extension: '.js',
179
+ },
180
+ });
181
+ // 5. Plug in additional defined functions
182
+ if (config_1.config.functions) {
183
+ combinator_1.default.addFunctionsByArray(config_1.config.functions);
184
+ }
185
+ }
186
+ // 4. Setting up default services
187
+ try {
188
+ await require('./helper/presetup_services').setup(options);
189
+ }
190
+ catch (e) {
191
+ return Promise.reject(e);
192
+ }
193
+ /**
194
+ * Run the server
195
+ *
196
+ * return KOA app object
197
+ */
198
+ return new Promise((done, reject) => {
199
+ try {
200
+ let server;
201
+ if (!config_1.config.dontListen) {
202
+ server = app.listen(config_1.config.port);
203
+ console.log('\x1b[35m', `KOAS has been launched on: localhost:${config_1.config.port}`);
204
+ }
205
+ // on after init
206
+ if (config_1.config.onAfterInit)
207
+ config_1.config.onAfterInit(app);
208
+ done({
209
+ app,
210
+ server,
211
+ });
212
+ }
213
+ catch (err) {
214
+ reject(err);
215
+ }
216
+ });
217
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Type for CMS operations that can trigger a callback
3
+ * @typedef {('update-one' | 'insert-one' | 'remove-one')} CmsOperation
4
+ * @description Supported CMS operations:
5
+ * - 'update-one': Triggered when updating a single document in the CMS
6
+ * - 'insert-one': Triggered when inserting a new document in the CMS
7
+ * - 'remove-one': Triggered when removing a document from the CMS
8
+ */
9
+ export type CmsOperation = 'update-one' | 'insert-one' | 'remove-one';
10
+ /**
11
+ * Context interface for CMS trigger callbacks
12
+ * @interface CmsTriggerContext
13
+ * @property {Record<string, any>} query - The query parameters used in the CMS operation
14
+ * @property {any} queryResult - The result of the CMS operation
15
+ */
16
+ export interface CmsTriggerContext {
17
+ query: Record<string, any>;
18
+ queryResult: any;
19
+ }
20
+ /**
21
+ * Defines a callback to be executed on specific CMS operations
22
+ * @class CmsTrigger
23
+ * @property {CmsOperation} operation - The CMS operation that triggers the callback
24
+ * @property {Function} callback - The callback function to be executed
25
+ * @example
26
+ * ```typescript
27
+ * const trigger = new CmsTrigger('insert-one', (context) => {
28
+ * console.log('New CMS document inserted:', context.queryResult);
29
+ * // Perform additional actions after CMS document insertion
30
+ * });
31
+ *
32
+ * // Use the trigger in RestOptions
33
+ * const { app } = await createRest({
34
+ * authTriggers: [trigger],
35
+ * // ... other options
36
+ * });
37
+ * ```
38
+ */
39
+ export declare class CmsTrigger {
40
+ operation: CmsOperation;
41
+ callback: (context: CmsTriggerContext) => void;
42
+ /**
43
+ * Creates a new CmsTrigger instance
44
+ * @param {CmsOperation} operation - The CMS operation to trigger on
45
+ * @param {Function} [callback=() => {}] - The callback function to execute
46
+ * @example
47
+ * ```typescript
48
+ * // Log all CMS updates
49
+ * const updateTrigger = new CmsTrigger('update-one', (context) => {
50
+ * console.log('CMS document updated:', context.queryResult);
51
+ * });
52
+ *
53
+ * // Track CMS document removals
54
+ * const removeTrigger = new CmsTrigger('remove-one', (context) => {
55
+ * console.log('CMS document removed:', context.queryResult);
56
+ * });
57
+ * ```
58
+ */
59
+ constructor(operation: CmsOperation, callback?: (context: CmsTriggerContext) => void);
60
+ }
61
+ export default CmsTrigger;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CmsTrigger = void 0;
4
+ /**
5
+ * Defines a callback to be executed on specific CMS operations
6
+ * @class CmsTrigger
7
+ * @property {CmsOperation} operation - The CMS operation that triggers the callback
8
+ * @property {Function} callback - The callback function to be executed
9
+ * @example
10
+ * ```typescript
11
+ * const trigger = new CmsTrigger('insert-one', (context) => {
12
+ * console.log('New CMS document inserted:', context.queryResult);
13
+ * // Perform additional actions after CMS document insertion
14
+ * });
15
+ *
16
+ * // Use the trigger in RestOptions
17
+ * const { app } = await createRest({
18
+ * authTriggers: [trigger],
19
+ * // ... other options
20
+ * });
21
+ * ```
22
+ */
23
+ class CmsTrigger {
24
+ /**
25
+ * Creates a new CmsTrigger instance
26
+ * @param {CmsOperation} operation - The CMS operation to trigger on
27
+ * @param {Function} [callback=() => {}] - The callback function to execute
28
+ * @example
29
+ * ```typescript
30
+ * // Log all CMS updates
31
+ * const updateTrigger = new CmsTrigger('update-one', (context) => {
32
+ * console.log('CMS document updated:', context.queryResult);
33
+ * });
34
+ *
35
+ * // Track CMS document removals
36
+ * const removeTrigger = new CmsTrigger('remove-one', (context) => {
37
+ * console.log('CMS document removed:', context.queryResult);
38
+ * });
39
+ * ```
40
+ */
41
+ constructor(operation, callback = () => { }) {
42
+ this.operation = operation;
43
+ this.callback = callback;
44
+ }
45
+ }
46
+ exports.CmsTrigger = CmsTrigger;
47
+ exports.default = CmsTrigger;