@modular-rest/server 1.11.14 → 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 (79) hide show
  1. package/dist/application.d.ts +29 -0
  2. package/dist/application.js +217 -0
  3. package/dist/class/cms_trigger.d.ts +52 -0
  4. package/dist/class/cms_trigger.js +47 -0
  5. package/dist/class/collection_definition.d.ts +112 -0
  6. package/dist/class/collection_definition.js +87 -0
  7. package/dist/class/combinator.d.ts +43 -0
  8. package/dist/class/combinator.js +174 -0
  9. package/dist/class/database_trigger.d.ts +90 -0
  10. package/dist/class/database_trigger.js +64 -0
  11. package/dist/class/db_schemas.d.ts +25 -0
  12. package/dist/class/db_schemas.js +28 -0
  13. package/dist/class/directory.d.ts +20 -0
  14. package/dist/class/directory.js +87 -0
  15. package/dist/class/paginator.d.ts +31 -0
  16. package/dist/class/paginator.js +43 -0
  17. package/dist/class/reply.d.ts +29 -0
  18. package/dist/class/reply.js +44 -0
  19. package/dist/class/security.d.ts +186 -0
  20. package/dist/class/security.js +178 -0
  21. package/dist/class/trigger_operator.d.ts +92 -0
  22. package/dist/class/trigger_operator.js +99 -0
  23. package/dist/class/user.d.ts +81 -0
  24. package/dist/class/user.js +151 -0
  25. package/dist/class/validator.d.ts +19 -0
  26. package/dist/class/validator.js +101 -0
  27. package/dist/config.d.ts +113 -0
  28. package/dist/config.js +26 -0
  29. package/dist/defult-permissions.d.ts +2 -0
  30. package/dist/defult-permissions.js +31 -0
  31. package/dist/events.d.ts +23 -0
  32. package/dist/events.js +47 -0
  33. package/dist/helper/data_insertion.d.ts +38 -0
  34. package/dist/helper/data_insertion.js +110 -0
  35. package/dist/helper/presetup_services.d.ts +60 -0
  36. package/dist/helper/presetup_services.js +108 -0
  37. package/dist/index.d.ts +118 -0
  38. package/dist/middlewares.d.ts +53 -0
  39. package/dist/middlewares.js +106 -0
  40. package/dist/play-test.d.ts +1 -0
  41. package/dist/play-test.js +9 -0
  42. package/dist/services/data_provider/router.d.ts +4 -0
  43. package/dist/services/data_provider/router.js +412 -0
  44. package/dist/services/data_provider/service.d.ts +132 -0
  45. package/dist/services/data_provider/service.js +253 -0
  46. package/dist/services/data_provider/typeCasters.d.ts +9 -0
  47. package/dist/services/data_provider/typeCasters.js +18 -0
  48. package/dist/services/file/db.d.ts +1 -0
  49. package/dist/services/file/db.js +31 -0
  50. package/dist/services/file/router.d.ts +4 -0
  51. package/dist/services/file/router.js +115 -0
  52. package/dist/services/file/service.d.ts +204 -0
  53. package/dist/services/file/service.js +341 -0
  54. package/dist/services/functions/router.d.ts +4 -0
  55. package/dist/services/functions/router.js +68 -0
  56. package/dist/services/functions/service.d.ts +132 -0
  57. package/dist/services/functions/service.js +159 -0
  58. package/dist/services/jwt/router.d.ts +4 -0
  59. package/dist/services/jwt/router.js +99 -0
  60. package/dist/services/jwt/service.d.ts +97 -0
  61. package/dist/services/jwt/service.js +135 -0
  62. package/dist/services/user_manager/db.d.ts +1 -0
  63. package/dist/services/user_manager/db.js +75 -0
  64. package/dist/services/user_manager/permissionManager.d.ts +19 -0
  65. package/dist/services/user_manager/permissionManager.js +42 -0
  66. package/dist/services/user_manager/router.d.ts +4 -0
  67. package/dist/services/user_manager/router.js +195 -0
  68. package/dist/services/user_manager/service.d.ts +317 -0
  69. package/dist/services/user_manager/service.js +632 -0
  70. package/package.json +3 -3
  71. package/src/application.ts +1 -1
  72. package/src/class/cms_trigger.ts +8 -14
  73. package/src/class/database_trigger.ts +10 -4
  74. package/src/class/user.ts +1 -1
  75. package/src/services/data_provider/router.ts +293 -0
  76. package/src/services/data_provider/service.ts +2 -1
  77. package/src/services/functions/router.ts +3 -2
  78. package/src/services/user_manager/db.ts +5 -5
  79. package/src/services/user_manager/service.ts +20 -15
@@ -0,0 +1,195 @@
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.main = exports.name = void 0;
40
+ const koa_router_1 = __importDefault(require("koa-router"));
41
+ const validator_1 = require("../../class/validator");
42
+ const reply_1 = require("../../class/reply");
43
+ const service = __importStar(require("./service"));
44
+ const name = 'user';
45
+ exports.name = name;
46
+ const userManager = new koa_router_1.default();
47
+ exports.main = userManager;
48
+ userManager.post('/register_id', async (ctx) => {
49
+ const body = ctx.request.body;
50
+ const validateOption = {
51
+ id: '',
52
+ idType: 'phone email',
53
+ };
54
+ // validate result
55
+ const bodyValidate = (0, validator_1.validateObject)(body, validateOption);
56
+ // fields validation
57
+ if (!bodyValidate.isValid) {
58
+ ctx.status = 412;
59
+ ctx.body = (0, reply_1.create)('e', { e: bodyValidate.requires });
60
+ return;
61
+ }
62
+ const serial = service.main.generateVerificationCode(body.id, body.idType);
63
+ if (serial) {
64
+ service.main.registerTemporaryID(body.id, body.idType, serial);
65
+ ctx.body = (0, reply_1.create)('s');
66
+ }
67
+ else {
68
+ ctx.status = 412;
69
+ ctx.body = (0, reply_1.create)('e', { e: 'Could not generate verification code.' });
70
+ }
71
+ });
72
+ userManager.post('/validateCode', async (ctx) => {
73
+ const body = ctx.request.body;
74
+ // validate result
75
+ const bodyValidate = (0, validator_1.validateObject)(body, 'id code');
76
+ // fields validation
77
+ if (!bodyValidate.isValid) {
78
+ ctx.status = 412;
79
+ ctx.body = (0, reply_1.create)('e', { e: bodyValidate.requires });
80
+ return;
81
+ }
82
+ const isValid = service.main.isCodeValid(body.id, body.code);
83
+ if (!isValid) {
84
+ ctx.status = 412;
85
+ ctx.body = (0, reply_1.create)('e', {
86
+ e: 'Verification code is wrong',
87
+ isValid: isValid,
88
+ });
89
+ return;
90
+ }
91
+ ctx.body = (0, reply_1.create)('s', { isValid: isValid });
92
+ });
93
+ userManager.post('/submit_password', async (ctx) => {
94
+ const body = ctx.request.body;
95
+ // validate result
96
+ const bodyValidate = (0, validator_1.validateObject)(body, 'id password code');
97
+ // fields validation
98
+ if (!bodyValidate.isValid) {
99
+ ctx.status = 412;
100
+ ctx.body = (0, reply_1.create)('e', { e: bodyValidate.requires });
101
+ return;
102
+ }
103
+ try {
104
+ const userId = await service.main.submitPasswordForTemporaryID(body.id, body.password, body.code);
105
+ if (userId) {
106
+ ctx.body = (0, reply_1.create)('s');
107
+ }
108
+ else {
109
+ ctx.status = 412;
110
+ ctx.body = (0, reply_1.create)('f');
111
+ }
112
+ }
113
+ catch (error) {
114
+ ctx.status = 412;
115
+ ctx.body = (0, reply_1.create)('f');
116
+ }
117
+ });
118
+ userManager.post('/change_password', async (ctx) => {
119
+ const body = ctx.request.body;
120
+ // validate result
121
+ const bodyValidate = (0, validator_1.validateObject)(body, 'id password code');
122
+ // fields validation
123
+ if (!bodyValidate.isValid) {
124
+ ctx.status = 412;
125
+ ctx.body = (0, reply_1.create)('e', { e: bodyValidate.requires });
126
+ return;
127
+ }
128
+ try {
129
+ const userId = await service.main.changePasswordForTemporaryID(body.id, body.password, body.code);
130
+ if (userId) {
131
+ ctx.body = (0, reply_1.create)('s');
132
+ }
133
+ else {
134
+ ctx.status = 412;
135
+ ctx.body = (0, reply_1.create)('f');
136
+ }
137
+ }
138
+ catch (error) {
139
+ ctx.status = 412;
140
+ ctx.body = (0, reply_1.create)('f');
141
+ }
142
+ });
143
+ userManager.post('/login', async (ctx) => {
144
+ const body = ctx.request.body;
145
+ const validateOption = {
146
+ id: '',
147
+ password: '',
148
+ idType: 'phone email',
149
+ };
150
+ // validate result
151
+ const bodyValidate = (0, validator_1.validateObject)(body, validateOption);
152
+ // fields validation
153
+ if (!bodyValidate.isValid) {
154
+ ctx.status = 412;
155
+ ctx.body = (0, reply_1.create)('e', { e: bodyValidate.requires });
156
+ return;
157
+ }
158
+ await service.main
159
+ .loginUser(body.id, body.idType, body.password)
160
+ .then(token => (ctx.body = (0, reply_1.create)('s', { token: token })))
161
+ .catch(err => {
162
+ ctx.status = 412;
163
+ ctx.body = (0, reply_1.create)('e', { e: err });
164
+ });
165
+ });
166
+ userManager.get('/loginAnonymous', async (ctx) => {
167
+ await service.main
168
+ .loginAnonymous()
169
+ .then(token => (ctx.body = (0, reply_1.create)('s', { token: token })))
170
+ .catch(err => {
171
+ ctx.status = 412;
172
+ ctx.body = (0, reply_1.create)('e', { e: err });
173
+ });
174
+ });
175
+ userManager.post('/getPermission', async (ctx) => {
176
+ const body = ctx.request.body;
177
+ // validate result
178
+ const bodyValidate = (0, validator_1.validateObject)(body, 'id');
179
+ // fields validation
180
+ if (!bodyValidate.isValid) {
181
+ ctx.status = 412;
182
+ ctx.body = (0, reply_1.create)('e', { e: bodyValidate.requires });
183
+ return;
184
+ }
185
+ const query = { _id: body.id };
186
+ const dataProvider = global.services.dataProvider;
187
+ const permission = await dataProvider
188
+ .getCollection('cms', 'permission')
189
+ .findOne(query)
190
+ .catch((err) => {
191
+ ctx.status = 412;
192
+ ctx.body = (0, reply_1.create)('e', { e: err });
193
+ });
194
+ ctx.body = (0, reply_1.create)('s', { permission: permission });
195
+ });
@@ -0,0 +1,317 @@
1
+ import { User } from '../../class/user';
2
+ /**
3
+ * Service name constant
4
+ * @constant {string}
5
+ */
6
+ export declare const name = "userManager";
7
+ /**
8
+ * User registration details
9
+ * @interface UserRegistrationDetail
10
+ * @property {string} [permissionGroup] - User's permission group
11
+ * @property {string} [phone] - User's phone number
12
+ * @property {string} [email] - User's email address
13
+ * @property {string} [password] - User's password (base64 encoded)
14
+ * @property {'user' | 'anonymous'} [type='user'] - User type
15
+ * @property {any} [key: string] - Additional user properties
16
+ */
17
+ interface UserRegistrationDetail {
18
+ permissionGroup?: string;
19
+ phone?: string;
20
+ email?: string;
21
+ password?: string;
22
+ type?: 'user' | 'anonymous';
23
+ [key: string]: any;
24
+ }
25
+ /**
26
+ * User manager class for handling user operations
27
+
28
+ * This service provides functionality for managing users, including:
29
+ * - User registration and authentication
30
+ * - Password management
31
+ * - Token generation and verification
32
+ * - Temporary ID handling for password reset and verification
33
+ *
34
+ */
35
+ declare class UserManager {
36
+ /**
37
+ * @hidden
38
+ */
39
+ private tempIds;
40
+ /**
41
+ * @hidden
42
+ */
43
+ private verificationCodeGeneratorMethod?;
44
+ /**
45
+ * @hidden
46
+ */
47
+ constructor();
48
+ /**
49
+ * Sets a custom method for generating verification codes
50
+ * @param {Function} generatorMethod - Function that generates verification codes
51
+ * @example
52
+ * ```typescript
53
+ * import { userManager } from '@modular-rest/server';
54
+ *
55
+ * userManager.setCustomVerificationCodeGeneratorMethod((id, type) => {
56
+ * return Math.random().toString(36).substring(2, 8).toUpperCase();
57
+ * });
58
+ * ```
59
+ */
60
+ setCustomVerificationCodeGeneratorMethod(generatorMethod: (id: string, idType: string) => string): void;
61
+ /**
62
+ * Generates a verification code for a user
63
+ * @param {string} id - User ID or identifier
64
+ * @param {string} idType - Type of ID (email, phone)
65
+ * @returns {string} Verification code
66
+ * @example
67
+ * ```typescript
68
+ * import { userManager } from '@modular-rest/server';
69
+ *
70
+ * const code = userManager.generateVerificationCode('user@example.com', 'email');
71
+ * // Returns: '123' (default) or custom generated code
72
+ * ```
73
+ */
74
+ generateVerificationCode(id: string, idType: string): string;
75
+ /**
76
+ * Gets a user by their ID
77
+ * @param {string} id - The ID of the user
78
+ * @returns {Promise<User>} Promise resolving to the user
79
+ * @throws {Error} If user model is not found or user is not found
80
+ * @example
81
+ * ```typescript
82
+ * import { userManager } from '@modular-rest/server';
83
+ *
84
+ * try {
85
+ * const user = await userManager.getUserById('user123');
86
+ * console.log('User details:', user);
87
+ * } catch (error) {
88
+ * console.error('Failed to get user:', error);
89
+ * }
90
+ * ```
91
+ */
92
+ getUserById(id: string): Promise<User>;
93
+ /**
94
+ * Gets a user by their identity (email or phone)
95
+ * @param {string} id - The identity of the user
96
+ * @param {string} idType - The type of the identity (phone or email)
97
+ * @returns {Promise<User>} Promise resolving to the user
98
+ * @throws {Error} If user model is not found or user is not found
99
+ * @example
100
+ * ```typescript
101
+ * import { userManager } from '@modular-rest/server';
102
+ *
103
+ * // Get user by email
104
+ * const user = await userManager.getUserByIdentity('user@example.com', 'email');
105
+ *
106
+ * // Get user by phone
107
+ * const user = await userManager.getUserByIdentity('+1234567890', 'phone');
108
+ * ```
109
+ */
110
+ getUserByIdentity(id: string, idType: string): Promise<User>;
111
+ /**
112
+ * Gets a user by their JWT token
113
+ * @param {string} token - The JWT token of the user
114
+ * @returns {Promise<User>} Promise resolving to the user
115
+ * @throws {Error} If token is invalid or user is not found
116
+ * @example
117
+ * ```typescript
118
+ * import { userManager } from '@modular-rest/server';
119
+ *
120
+ * try {
121
+ * const user = await userManager.getUserByToken('jwt.token.here');
122
+ * console.log('Authenticated user:', user);
123
+ * } catch (error) {
124
+ * console.error('Invalid token:', error);
125
+ * }
126
+ * ```
127
+ */
128
+ getUserByToken(token: string): Promise<User>;
129
+ /**
130
+ * Checks if a verification code is valid
131
+ * @param {string} id - The ID of the user
132
+ * @param {string} code - The verification code
133
+ * @returns {boolean} Whether the verification code is valid
134
+ * @example
135
+ * ```typescript
136
+ * import { userManager } from '@modular-rest/server';
137
+ *
138
+ * const isValid = userManager.isCodeValid('user123', '123');
139
+ * if (isValid) {
140
+ * // Proceed with verification
141
+ * }
142
+ * ```
143
+ */
144
+ isCodeValid(id: string, code: string): boolean;
145
+ /**
146
+ * Logs in a user and returns their JWT token
147
+ * @param {string} [id=''] - The ID of the user (email or phone)
148
+ * @param {string} [idType=''] - The type of the ID (phone or email)
149
+ * @param {string} [password=''] - The password of the user
150
+ * @returns {Promise<string>} Promise resolving to the JWT token
151
+ * @throws {Error} If user is not found or credentials are invalid
152
+ * @example
153
+ * ```typescript
154
+ * import { userManager } from '@modular-rest/server';
155
+ *
156
+ * try {
157
+ * // Login with email
158
+ * const token = await userManager.loginUser('user@example.com', 'email', 'password123');
159
+ *
160
+ * // Login with phone
161
+ * const token = await userManager.loginUser('+1234567890', 'phone', 'password123');
162
+ * } catch (error) {
163
+ * console.error('Login failed:', error);
164
+ * }
165
+ * ```
166
+ */
167
+ loginUser(id?: string, idType?: string, password?: string): Promise<string>;
168
+ /**
169
+ * Issues a JWT token for a user by email
170
+ * @param {string} email - The email of the user
171
+ * @returns {Promise<string>} Promise resolving to the JWT token
172
+ * @throws {Error} If user is not found
173
+ * @example
174
+ * ```typescript
175
+ * import { userManager } from '@modular-rest/server';
176
+ *
177
+ * try {
178
+ * const token = await userManager.issueTokenForUser('user@example.com');
179
+ * console.log('Issued token:', token);
180
+ * } catch (error) {
181
+ * console.error('Failed to issue token:', error);
182
+ * }
183
+ * ```
184
+ */
185
+ issueTokenForUser(email: string): Promise<string>;
186
+ /**
187
+ * Logs in an anonymous user and returns their JWT token
188
+ * @returns {Promise<string>} Promise resolving to the JWT token
189
+ * @example
190
+ * ```typescript
191
+ * import { userManager } from '@modular-rest/server';
192
+ *
193
+ * const token = await userManager.loginAnonymous();
194
+ * console.log('Anonymous token:', token);
195
+ * ```
196
+ */
197
+ loginAnonymous(): Promise<string>;
198
+ /**
199
+ * Registers a temporary ID for verification or password reset
200
+ * @param {string} id - The ID to register
201
+ * @param {string} type - The type of temporary ID
202
+ * @param {string} code - The verification code
203
+ * @returns {string} The registered ID
204
+ * @example
205
+ * ```typescript
206
+ * import { userManager } from '@modular-rest/server';
207
+ *
208
+ * const tempId = userManager.registerTemporaryID('user@example.com', 'password_reset', '123456');
209
+ * ```
210
+ */
211
+ registerTemporaryID(id: string, type: string, code: string): string;
212
+ /**
213
+ * Submits a password for a temporary ID
214
+ * @param {string} id - The temporary ID
215
+ * @param {string} password - The new password
216
+ * @param {string} code - The verification code
217
+ * @returns {Promise<string>} Promise resolving to the JWT token
218
+ * @throws {Error} If verification code is invalid or user is not found
219
+ * @example
220
+ * ```typescript
221
+ * import { userManager } from '@modular-rest/server';
222
+ *
223
+ * try {
224
+ * const token = await userManager.submitPasswordForTemporaryID(
225
+ * 'user@example.com',
226
+ * 'newpassword123',
227
+ * '123456'
228
+ * );
229
+ * console.log('Password set successfully');
230
+ * } catch (error) {
231
+ * console.error('Failed to set password:', error);
232
+ * }
233
+ * ```
234
+ */
235
+ submitPasswordForTemporaryID(id: string, password: string, code: string): Promise<string>;
236
+ /**
237
+ * Changes password for a temporary ID
238
+ * @param {string} id - The temporary ID
239
+ * @param {string} password - The new password
240
+ * @param {string} code - The verification code
241
+ * @returns {Promise<string>} Promise resolving to the JWT token
242
+ * @throws {Error} If verification code is invalid or user is not found
243
+ * @example
244
+ * ```typescript
245
+ * import { userManager } from '@modular-rest/server';
246
+ *
247
+ * try {
248
+ * const token = await userManager.changePasswordForTemporaryID(
249
+ * 'user@example.com',
250
+ * 'newpassword123',
251
+ * '123456'
252
+ * );
253
+ * console.log('Password changed successfully');
254
+ * } catch (error) {
255
+ * console.error('Failed to change password:', error);
256
+ * }
257
+ * ```
258
+ */
259
+ changePasswordForTemporaryID(id: string, password: string, code: string): Promise<string>;
260
+ /**
261
+ * Registers a new user
262
+ * @param {UserRegistrationDetail} detail - User registration details
263
+ * @returns {Promise<string>} Promise resolving to the JWT token
264
+ * @throws {Error} If user model is not found or registration fails
265
+ * @example
266
+ * ```typescript
267
+ * import { userManager } from '@modular-rest/server';
268
+ *
269
+ * try {
270
+ * const token = await userManager.registerUser({
271
+ * email: 'user@example.com',
272
+ * password: 'secure123',
273
+ * permissionGroup: 'user',
274
+ * phone: '+1234567890'
275
+ * });
276
+ * console.log('User registered successfully');
277
+ * } catch (error) {
278
+ * console.error('Registration failed:', error);
279
+ * }
280
+ * ```
281
+ */
282
+ registerUser(detail: UserRegistrationDetail): Promise<string>;
283
+ /**
284
+ * Changes a user's password
285
+ * @param {Record<string, any>} query - Query to find the user
286
+ * @param {string} newPass - The new password
287
+ * @returns {Promise<void>} Promise resolving when password is changed
288
+ * @throws {Error} If user is not found or password change fails
289
+ * @example
290
+ * ```typescript
291
+ * import { userManager } from '@modular-rest/server';
292
+ *
293
+ * try {
294
+ * await userManager.changePassword(
295
+ * { email: 'user@example.com' },
296
+ * 'newpassword123'
297
+ * );
298
+ * console.log('Password changed successfully');
299
+ * } catch (error) {
300
+ * console.error('Failed to change password:', error);
301
+ * }
302
+ * ```
303
+ */
304
+ changePassword(query: Record<string, any>, newPass: string): Promise<void>;
305
+ /**
306
+ * Gets the singleton instance of UserManager
307
+ * @returns {UserManager} The UserManager instance
308
+ * @hidden
309
+ */
310
+ static get instance(): UserManager;
311
+ }
312
+ /**
313
+ * Main user manager instance
314
+ * @constant {UserManager}
315
+ */
316
+ export declare const main: UserManager;
317
+ export {};