express-toolkit 1.0.6 → 1.0.8

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/index.d.ts ADDED
@@ -0,0 +1,62 @@
1
+ declare module 'express-toolkit' {
2
+ import { NextFunction, Request, Response, Router } from 'express';
3
+ import { Document, Model } from 'mongoose';
4
+
5
+ export function buildRouter(config: {
6
+ controller: Controller;
7
+ endpoints?: { [key: string]: boolean };
8
+ id?: string;
9
+ options?: object;
10
+ }): Router;
11
+
12
+ export class Controller {
13
+ constructor(config: {
14
+ name: string;
15
+ model: Model<Document>;
16
+ id?: string;
17
+ defaultSkipValue?: number;
18
+ defaultLimitValue?: number;
19
+ useUpdateOne?: boolean;
20
+ });
21
+
22
+ find(query: object): Promise<any[]>;
23
+ findOne(query: object): Promise<any>;
24
+ findById(id: string | number, query?: object): Promise<any>;
25
+ create(data: object | object[]): Promise<any>;
26
+ bulkCreate(data: object[]): Promise<any[]>;
27
+ bulkUpdate(documents: object[]): Promise<any>;
28
+ updateByQuery(query: object, update: object): Promise<any>;
29
+ updateById(id: string | number, update: object, query?: object): Promise<any>;
30
+ replaceById(id: string | number, replacement: object, query?: object): Promise<any>;
31
+ deleteByQuery(query: object): Promise<any>;
32
+ deleteById(id: string | number, query?: object): Promise<any>;
33
+ count(query: object): Promise<number>;
34
+ patchById(id: string | number, operations: object[], query?: object): Promise<any>;
35
+ registerHook(eventName: string, handler: (req: Request, res: Response, next: NextFunction) => void): void;
36
+ getHooks(eventName: string): ((req: Request, res: Response, next: NextFunction) => void)[];
37
+ }
38
+
39
+ export class Resource {
40
+ constructor(config: {
41
+ name: string;
42
+ model: Model<Document>;
43
+ endpoints?: { [key: string]: boolean };
44
+ id?: string;
45
+ defaultSkipValue?: number;
46
+ defaultLimitValue?: number;
47
+ });
48
+
49
+ getRouter(): Router;
50
+ rebuildRouter(): Router;
51
+ mount(path: string, app: any): void;
52
+ registerHook(eventName: string, handler: (req: Request, res: Response, next: NextFunction) => void): void;
53
+ readonly router: Router;
54
+ }
55
+
56
+ export const utils: {
57
+ asyncMiddleware(fn: (req: Request, res: Response, next: NextFunction) => Promise<void>): (req: Request, res: Response, next: NextFunction) => void;
58
+ isJSON(str: string): boolean;
59
+ getSorting(query: object): object;
60
+ getProjection(query: object): object | null;
61
+ };
62
+ }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "express-toolkit",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "files": [
5
5
  "index.js",
6
6
  "src/",
7
- "types/"
7
+ "types/",
8
+ "index.d.ts"
8
9
  ],
9
10
  "description": "Handy tools for building expressjs based http microservices",
10
11
  "main": "index.js",
package/src/router.js CHANGED
@@ -90,7 +90,7 @@ function buildRouter (config) {
90
90
  })
91
91
 
92
92
  const deleteByIdMiddleware = asyncMiddleware(async (req, res, next) => {
93
- await config.controller.deleteById(req.params[config.id])
93
+ await config.controller.deleteById(req.params[config.id], req.query)
94
94
  req.toSend = null
95
95
  return next()
96
96
  })
@@ -116,13 +116,13 @@ function buildRouter (config) {
116
116
  })
117
117
 
118
118
  const patchByIdMiddleware = asyncMiddleware(async (req, res, next) => {
119
- const resource = await config.controller.patchById(req.params[config.id], req.body)
119
+ const resource = await config.controller.patchById(req.params[config.id], req.body, req.query)
120
120
  req.toSend = resource
121
121
  return next()
122
122
  })
123
123
 
124
124
  const replaceByIdMiddleware = asyncMiddleware(async (req, res, next) => {
125
- const resource = await config.controller.replaceById(req.params[config.id], req.body)
125
+ const resource = await config.controller.replaceById(req.params[config.id], req.body, req.query)
126
126
  req.toSend = resource
127
127
  return next()
128
128
  })
@@ -181,7 +181,7 @@ function buildRouter (config) {
181
181
 
182
182
  }
183
183
 
184
- for (let endpointName in endpoints) {
184
+ for (const endpointName in endpoints) {
185
185
  /**
186
186
  * This is for reto-compatibility reasons, some hooks (due to a bug or a bad design decision)
187
187
  * did not match the middleware name, for example the pre:update hook is run before the updateByQuery