@ewb/mongoose-query-express 1.0.1 → 1.0.3

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.
@@ -50,9 +50,9 @@ export declare class QueryExpress<R extends Request, T extends mongoose.Document
50
50
  query(req: R): Query<T, object, import("@ewb/mongoose-query").IMongooseQueryOptions>;
51
51
  pageLinkHeader(req: R, data: ISearchPaginate<any>): string;
52
52
  private getDoc;
53
- private validateBody;
54
53
  private getFindOpts;
55
54
  private validateReq;
56
55
  private validateIdsInBody;
56
+ private skipHandlerAction;
57
57
  }
58
58
  export {};
@@ -27,10 +27,9 @@ export class QueryExpress {
27
27
  }
28
28
  async post(req, res, next) {
29
29
  try {
30
- if (req.ewb.queryOptions.skipHandlerAction === 'post') {
30
+ if (this.skipHandlerAction(req, 'post')) {
31
31
  return next();
32
32
  }
33
- this.validateBody(req);
34
33
  this.init(req);
35
34
  if (req.ewb.queryPostBody) {
36
35
  for (const key in req.ewb.queryPostBody) {
@@ -48,7 +47,8 @@ export class QueryExpress {
48
47
  }
49
48
  async findOne(req, _res, next) {
50
49
  try {
51
- if (req.ewb.queryOptions.skipHandlerAction === 'findOne') {
50
+ if (this.skipHandlerAction(req, 'findOne')) {
51
+ req.ewb.queryOptions.skipHandlerAction = undefined;
52
52
  return next();
53
53
  }
54
54
  this.init(req);
@@ -66,7 +66,7 @@ export class QueryExpress {
66
66
  }
67
67
  async search(req, res, next) {
68
68
  try {
69
- if (req.ewb.queryOptions.skipHandlerAction === 'search') {
69
+ if (this.skipHandlerAction(req, 'search')) {
70
70
  return next();
71
71
  }
72
72
  const data = await this._search(req, res);
@@ -81,7 +81,7 @@ export class QueryExpress {
81
81
  }
82
82
  async searchNext(req, res, next) {
83
83
  try {
84
- if (req.ewb.queryOptions.skipHandlerAction === 'search') {
84
+ if (this.skipHandlerAction(req, 'search')) {
85
85
  return next();
86
86
  }
87
87
  req.ewb.querySearchJson = await this._search(req, res);
@@ -101,7 +101,7 @@ export class QueryExpress {
101
101
  }
102
102
  async export(req, res, next) {
103
103
  try {
104
- if (req.ewb.queryOptions.skipHandlerAction === 'export') {
104
+ if (this.skipHandlerAction(req, 'export')) {
105
105
  return next();
106
106
  }
107
107
  this.init(req);
@@ -118,11 +118,10 @@ export class QueryExpress {
118
118
  }
119
119
  async patch(req, _res, next) {
120
120
  try {
121
- if (req.ewb.queryOptions.skipHandlerAction === 'patch') {
121
+ if (this.skipHandlerAction(req, 'patch')) {
122
122
  return next();
123
123
  }
124
124
  this.validateReq(req);
125
- this.validateBody(req);
126
125
  this.init(req);
127
126
  const query = this.query(req);
128
127
  req[this.param] = (await this.handler.findOneAndUpdate(query, { _id: this.getDoc(req)._id }, { $set: req.body }));
@@ -136,7 +135,7 @@ export class QueryExpress {
136
135
  }
137
136
  async archive(req, _res, next) {
138
137
  try {
139
- if (req.ewb.queryOptions.skipHandlerAction === 'archive') {
138
+ if (this.skipHandlerAction(req, 'archive')) {
140
139
  return next();
141
140
  }
142
141
  this.validateReq(req);
@@ -151,7 +150,7 @@ export class QueryExpress {
151
150
  }
152
151
  async delete(req, _res, next) {
153
152
  try {
154
- if (req.ewb.queryOptions.skipHandlerAction === 'delete') {
153
+ if (this.skipHandlerAction(req, 'delete')) {
155
154
  return next();
156
155
  }
157
156
  this.validateReq(req);
@@ -185,7 +184,6 @@ export class QueryExpress {
185
184
  try {
186
185
  const ids = this.validateIdsInBody(req);
187
186
  delete req.body.ids;
188
- this.handler.validateBody(req.body);
189
187
  await this.handler.updateMany(this.query(req).root || {}, ids, req.body);
190
188
  this.query(req).addRoot({ _id: { $in: ids } });
191
189
  this.query(req).setSkipAndLimit(0, ids.length);
@@ -359,18 +357,6 @@ export class QueryExpress {
359
357
  getDoc(req, param = this.param) {
360
358
  return req[param];
361
359
  }
362
- validateBody(req) {
363
- const invalidFields = this.handler.validateBody(req.body);
364
- if (invalidFields) {
365
- throw new QueryError(httpStatus.BAD_REQUEST, 'Invalid body', {
366
- detail: 'Body contained invalid keys, remove keys located in source.body to post or patch',
367
- errors: invalidFields.map((x) => ({
368
- message: 'Invalid field',
369
- detail: x,
370
- })),
371
- });
372
- }
373
- }
374
360
  getFindOpts(_req) {
375
361
  return {
376
362
  select: false,
@@ -392,4 +378,11 @@ export class QueryExpress {
392
378
  }
393
379
  return req.body.ids.map((x) => new mongoose.Types.ObjectId(x));
394
380
  }
381
+ skipHandlerAction(req, action) {
382
+ if (req.ewb.queryOptions.skipHandlerAction === action) {
383
+ req.ewb.queryOptions.skipHandlerAction = undefined;
384
+ return true;
385
+ }
386
+ return false;
387
+ }
395
388
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ewb/mongoose-query-express",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "description": "Routes and controllers for express to handle mongoose queries",
6
6
  "main": "./lib/index.js",
@@ -17,7 +17,7 @@
17
17
  "preversion": "yarn build"
18
18
  },
19
19
  "dependencies": {
20
- "@ewb/mongoose-query": "^1.1.1",
20
+ "@ewb/mongoose-query": "*",
21
21
  "http-status": "^1.6.2"
22
22
  },
23
23
  "devDependencies": {