@modular-rest/server 1.14.0 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -58,7 +58,7 @@ createRest({
58
58
  - Event system
59
59
  - Extensible architecture
60
60
 
61
- ## TypeScript Support
61
+ ## TypeScript Support
62
62
 
63
63
  This package includes full TypeScript type definitions for all API components. The entire codebase is written in TypeScript, providing:
64
64
 
@@ -1,3 +1,4 @@
1
+ import { DatabaseTriggerContext } from './database_trigger';
1
2
  /**
2
3
  * Type for CMS operations that can trigger a callback
3
4
  * @typedef {('update-one' | 'insert-one' | 'remove-one')} CmsOperation
@@ -7,16 +8,6 @@
7
8
  * - 'remove-one': Triggered when removing a document from the CMS
8
9
  */
9
10
  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
11
  /**
21
12
  * Defines a callback to be executed on specific CMS operations
22
13
  * @class CmsTrigger
@@ -26,7 +17,7 @@ export interface CmsTriggerContext {
26
17
  * ```typescript
27
18
  * const trigger = new CmsTrigger('insert-one', (context) => {
28
19
  * console.log('New CMS document inserted:', context.queryResult);
29
- * // Perform additional actions after CMS document insertion
20
+ * // Perform additional actions after CMS document insertion.
30
21
  * });
31
22
  *
32
23
  * // Use the trigger in RestOptions
@@ -38,7 +29,7 @@ export interface CmsTriggerContext {
38
29
  */
39
30
  export declare class CmsTrigger {
40
31
  operation: CmsOperation;
41
- callback: (context: CmsTriggerContext) => void;
32
+ callback: (context: DatabaseTriggerContext) => void;
42
33
  /**
43
34
  * Creates a new CmsTrigger instance
44
35
  * @param {CmsOperation} operation - The CMS operation to trigger on
@@ -56,6 +47,6 @@ export declare class CmsTrigger {
56
47
  * });
57
48
  * ```
58
49
  */
59
- constructor(operation: CmsOperation, callback?: (context: CmsTriggerContext) => void);
50
+ constructor(operation: CmsOperation, callback?: (context: DatabaseTriggerContext) => void);
60
51
  }
61
52
  export default CmsTrigger;
@@ -10,7 +10,7 @@ exports.CmsTrigger = void 0;
10
10
  * ```typescript
11
11
  * const trigger = new CmsTrigger('insert-one', (context) => {
12
12
  * console.log('New CMS document inserted:', context.queryResult);
13
- * // Perform additional actions after CMS document insertion
13
+ * // Perform additional actions after CMS document insertion.
14
14
  * });
15
15
  *
16
16
  * // Use the trigger in RestOptions
@@ -5,12 +5,18 @@ export type DatabaseOperation = 'find' | 'find-one' | 'count' | 'update-one' | '
5
5
  /**
6
6
  * Context interface for database trigger callbacks
7
7
  * @interface DatabaseTriggerContext
8
- * @property {Record<string, any>} query - The query parameters used in the database operation
9
- * @property {any | any[]} queryResult - The result of the database operation
8
+ * @property {Record<string, any>} doc - The document data for insert/update operations
9
+ * @property {Record<string, any>} query - The query data for find/find-one operations
10
+ * @property {Record<string, any>} update - The update data for update operations
11
+ * @property {Record<string, any>[]} pipelines - The aggregation pipelines for aggregate operations
12
+ * @property {Record<string, any> } queryResult - The result of the database operation
10
13
  */
11
14
  export interface DatabaseTriggerContext {
12
- query: Record<string, any>;
13
- queryResult: any | any[];
15
+ doc?: Record<string, any>;
16
+ query?: Record<string, any>;
17
+ update?: Record<string, any>;
18
+ pipelines?: Record<string, any>[];
19
+ queryResult: Record<string, any>;
14
20
  }
15
21
  /**
16
22
  * The callback function to be executed on specific database operations
@@ -227,6 +227,7 @@ dataProvider.post('/update-one', async (ctx) => {
227
227
  // Call trigger
228
228
  service.triggers.call('update-one', body.database, body.collection, {
229
229
  query: body.query,
230
+ update: body.update,
230
231
  queryResult: writeOpResult,
231
232
  });
232
233
  ctx.body = { data: writeOpResult };
@@ -261,7 +262,7 @@ dataProvider.post('/insert-one', async (ctx) => {
261
262
  .then(async (newDoc) => {
262
263
  // Call trigger
263
264
  service.triggers.call('insert-one', body.database, body.collection, {
264
- query: body.query,
265
+ doc: body.doc,
265
266
  queryResult: newDoc,
266
267
  });
267
268
  ctx.body = { data: newDoc };
@@ -329,7 +330,7 @@ dataProvider.post('/aggregate', async (ctx) => {
329
330
  .then(async (result) => {
330
331
  // Call trigger
331
332
  service.triggers.call('aggregate', body.database, body.collection, {
332
- query: body.query,
333
+ pipelines: body.pipelines,
333
334
  queryResult: result,
334
335
  });
335
336
  ctx.body = { data: result };
@@ -59,9 +59,10 @@ functionRouter.post('/run', middleware.auth, async (ctx) => {
59
59
  const { name, args } = ctx.request.body;
60
60
  try {
61
61
  const result = await service.runFunction(name, args, ctx.state.user);
62
- ctx.body = JSON.stringify((0, reply_1.create)('s', { data: result }));
62
+ ctx.body = (0, reply_1.create)('s', { data: result });
63
63
  }
64
64
  catch (e) {
65
- ctx.throw(400, JSON.stringify((0, reply_1.create)('e', { error: e.message })));
65
+ ctx.status = 400;
66
+ ctx.body = (0, reply_1.create)('e', { message: e.message });
66
67
  }
67
68
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-rest/server",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "description": "TypeScript version of a nodejs module based on KOAJS for developing Rest-APIs in a modular solution.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,3 +1,5 @@
1
+ import { DatabaseTriggerContext } from './database_trigger';
2
+
1
3
  /**
2
4
  * Type for CMS operations that can trigger a callback
3
5
  * @typedef {('update-one' | 'insert-one' | 'remove-one')} CmsOperation
@@ -8,17 +10,6 @@
8
10
  */
9
11
  export type CmsOperation = 'update-one' | 'insert-one' | 'remove-one';
10
12
 
11
- /**
12
- * Context interface for CMS trigger callbacks
13
- * @interface CmsTriggerContext
14
- * @property {Record<string, any>} query - The query parameters used in the CMS operation
15
- * @property {any} queryResult - The result of the CMS operation
16
- */
17
- export interface CmsTriggerContext {
18
- query: Record<string, any>;
19
- queryResult: any;
20
- }
21
-
22
13
  /**
23
14
  * Defines a callback to be executed on specific CMS operations
24
15
  * @class CmsTrigger
@@ -28,7 +19,7 @@ export interface CmsTriggerContext {
28
19
  * ```typescript
29
20
  * const trigger = new CmsTrigger('insert-one', (context) => {
30
21
  * console.log('New CMS document inserted:', context.queryResult);
31
- * // Perform additional actions after CMS document insertion
22
+ * // Perform additional actions after CMS document insertion.
32
23
  * });
33
24
  *
34
25
  * // Use the trigger in RestOptions
@@ -40,7 +31,7 @@ export interface CmsTriggerContext {
40
31
  */
41
32
  export class CmsTrigger {
42
33
  operation: CmsOperation;
43
- callback: (context: CmsTriggerContext) => void;
34
+ callback: (context: DatabaseTriggerContext) => void;
44
35
 
45
36
  /**
46
37
  * Creates a new CmsTrigger instance
@@ -59,7 +50,10 @@ export class CmsTrigger {
59
50
  * });
60
51
  * ```
61
52
  */
62
- constructor(operation: CmsOperation, callback: (context: CmsTriggerContext) => void = () => {}) {
53
+ constructor(
54
+ operation: CmsOperation,
55
+ callback: (context: DatabaseTriggerContext) => void = () => {}
56
+ ) {
63
57
  this.operation = operation;
64
58
  this.callback = callback;
65
59
  }
@@ -13,12 +13,18 @@ export type DatabaseOperation =
13
13
  /**
14
14
  * Context interface for database trigger callbacks
15
15
  * @interface DatabaseTriggerContext
16
- * @property {Record<string, any>} query - The query parameters used in the database operation
17
- * @property {any | any[]} queryResult - The result of the database operation
16
+ * @property {Record<string, any>} doc - The document data for insert/update operations
17
+ * @property {Record<string, any>} query - The query data for find/find-one operations
18
+ * @property {Record<string, any>} update - The update data for update operations
19
+ * @property {Record<string, any>[]} pipelines - The aggregation pipelines for aggregate operations
20
+ * @property {Record<string, any> } queryResult - The result of the database operation
18
21
  */
19
22
  export interface DatabaseTriggerContext {
20
- query: Record<string, any>;
21
- queryResult: any | any[];
23
+ doc?: Record<string, any>;
24
+ query?: Record<string, any>;
25
+ update?: Record<string, any>;
26
+ pipelines?: Record<string, any>[];
27
+ queryResult: Record<string, any>;
22
28
  }
23
29
 
24
30
  /**
@@ -244,6 +244,7 @@ dataProvider.post('/update-one', async (ctx: Context) => {
244
244
  // Call trigger
245
245
  service.triggers.call('update-one', body.database, body.collection, {
246
246
  query: body.query,
247
+ update: body.update,
247
248
  queryResult: writeOpResult,
248
249
  });
249
250
 
@@ -290,7 +291,7 @@ dataProvider.post('/insert-one', async (ctx: Context) => {
290
291
  .then(async newDoc => {
291
292
  // Call trigger
292
293
  service.triggers.call('insert-one', body.database, body.collection, {
293
- query: body.query,
294
+ doc: body.doc,
294
295
  queryResult: newDoc,
295
296
  });
296
297
 
@@ -381,7 +382,7 @@ dataProvider.post('/aggregate', async (ctx: Context) => {
381
382
  .then(async (result: any) => {
382
383
  // Call trigger
383
384
  service.triggers.call('aggregate', body.database, body.collection, {
384
- query: body.query,
385
+ pipelines: body.pipelines,
385
386
  queryResult: result,
386
387
  });
387
388
 
@@ -25,9 +25,10 @@ functionRouter.post('/run', middleware.auth, async (ctx: Context) => {
25
25
 
26
26
  try {
27
27
  const result = await service.runFunction(name, args, ctx.state.user);
28
- ctx.body = JSON.stringify(reply('s', { data: result }));
28
+ ctx.body = reply('s', { data: result });
29
29
  } catch (e) {
30
- ctx.throw(400, JSON.stringify(reply('e', { error: (e as Error).message })));
30
+ ctx.status = 400;
31
+ ctx.body = reply('e', { message: (e as Error).message });
31
32
  }
32
33
  });
33
34