@naturalcycles/backend-lib 9.49.2 → 9.51.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.
@@ -3,7 +3,7 @@ import { InMemoryDB } from '@naturalcycles/db-lib/inmemory';
3
3
  import { commonDBOptionsSchema, commonDBSaveOptionsSchema, dbQuerySchema, } from '@naturalcycles/db-lib/validation';
4
4
  import { j } from '@naturalcycles/nodejs-lib/ajv';
5
5
  import { getDefaultRouter } from '../express/getDefaultRouter.js';
6
- import { ajvValidateRequest } from '../validation/ajv/ajvValidateRequest.js';
6
+ import { validateRequest } from '../validation/ajv/validateRequest.js';
7
7
  const getByIdsInputSchema = j.object({
8
8
  table: j.string(),
9
9
  ids: j.array(j.string()),
@@ -49,24 +49,24 @@ export function httpDBRequestHandler(db) {
49
49
  // })
50
50
  // getByIds
51
51
  router.put('/getByIds', async (req, res) => {
52
- const { table, ids, opt } = ajvValidateRequest.body(req, getByIdsInputSchema);
52
+ const { table, ids, opt } = validateRequest.body(req, getByIdsInputSchema);
53
53
  res.json(await db.getByIds(table, ids, opt));
54
54
  });
55
55
  // runQuery
56
56
  router.put('/runQuery', async (req, res) => {
57
- const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
57
+ const { query, opt } = validateRequest.body(req, runQueryInputSchema);
58
58
  const q = DBQuery.fromPlainObject(query);
59
59
  res.json(await db.runQuery(q, opt));
60
60
  });
61
61
  // runQueryCount
62
62
  router.put('/runQueryCount', async (req, res) => {
63
- const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
63
+ const { query, opt } = validateRequest.body(req, runQueryInputSchema);
64
64
  const q = DBQuery.fromPlainObject(query);
65
65
  res.json(await db.runQueryCount(q, opt));
66
66
  });
67
67
  // saveBatch
68
68
  router.put('/saveBatch', async (req, res) => {
69
- const { table, rows, opt } = ajvValidateRequest.body(req, saveBatchInputSchema);
69
+ const { table, rows, opt } = validateRequest.body(req, saveBatchInputSchema);
70
70
  await db.saveBatch(table, rows, opt);
71
71
  res.end();
72
72
  });
@@ -77,7 +77,7 @@ export function httpDBRequestHandler(db) {
77
77
  // })
78
78
  // deleteByQuery
79
79
  router.put('/deleteByQuery', async (req, res) => {
80
- const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
80
+ const { query, opt } = validateRequest.body(req, runQueryInputSchema);
81
81
  const q = DBQuery.fromPlainObject(query);
82
82
  res.json(await db.deleteByQuery(q, opt));
83
83
  });
@@ -28,5 +28,5 @@ declare class AjvValidateRequest {
28
28
  headers<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
29
29
  private validate;
30
30
  }
31
- export declare const ajvValidateRequest: AjvValidateRequest;
31
+ export declare const validateRequest: AjvValidateRequest;
32
32
  export {};
@@ -60,4 +60,4 @@ class AjvValidateRequest {
60
60
  return output;
61
61
  }
62
62
  }
63
- export const ajvValidateRequest = new AjvValidateRequest();
63
+ export const validateRequest = new AjvValidateRequest();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.49.2",
4
+ "version": "9.51.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -45,8 +45,7 @@
45
45
  "./deploy": "./dist/deploy/index.js",
46
46
  "./deploy/*.js": "./dist/deploy/*.js",
47
47
  "./express/*.js": "./dist/express/*.js",
48
- "./ajvValidateRequest": "./dist/validation/ajv/ajvValidateRequest.js",
49
- "./joiValidateRequest": "./dist/validation/joi/joiValidateRequest.js",
48
+ "./ajvValidateRequest": "./dist/validation/ajv/validateRequest.js",
50
49
  "./zodValidateRequest": "./dist/validation/zod/zodValidateRequest.js",
51
50
  "./onFinished": "./dist/onFinished.js",
52
51
  "./testing": "./dist/testing/index.js"
@@ -10,7 +10,7 @@ import type { ObjectWithId } from '@naturalcycles/js-lib/types'
10
10
  import { j } from '@naturalcycles/nodejs-lib/ajv'
11
11
  import { getDefaultRouter } from '../express/getDefaultRouter.js'
12
12
  import type { BackendRouter } from '../server/server.model.js'
13
- import { ajvValidateRequest } from '../validation/ajv/ajvValidateRequest.js'
13
+ import { validateRequest } from '../validation/ajv/validateRequest.js'
14
14
 
15
15
  export interface GetByIdsInput {
16
16
  table: string
@@ -83,27 +83,27 @@ export function httpDBRequestHandler(db: CommonDB): BackendRouter {
83
83
 
84
84
  // getByIds
85
85
  router.put('/getByIds', async (req, res) => {
86
- const { table, ids, opt } = ajvValidateRequest.body(req, getByIdsInputSchema)
86
+ const { table, ids, opt } = validateRequest.body(req, getByIdsInputSchema)
87
87
  res.json(await db.getByIds(table, ids, opt))
88
88
  })
89
89
 
90
90
  // runQuery
91
91
  router.put('/runQuery', async (req, res) => {
92
- const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
92
+ const { query, opt } = validateRequest.body(req, runQueryInputSchema)
93
93
  const q = DBQuery.fromPlainObject(query)
94
94
  res.json(await db.runQuery(q, opt))
95
95
  })
96
96
 
97
97
  // runQueryCount
98
98
  router.put('/runQueryCount', async (req, res) => {
99
- const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
99
+ const { query, opt } = validateRequest.body(req, runQueryInputSchema)
100
100
  const q = DBQuery.fromPlainObject(query)
101
101
  res.json(await db.runQueryCount(q, opt))
102
102
  })
103
103
 
104
104
  // saveBatch
105
105
  router.put('/saveBatch', async (req, res) => {
106
- const { table, rows, opt } = ajvValidateRequest.body(req, saveBatchInputSchema)
106
+ const { table, rows, opt } = validateRequest.body(req, saveBatchInputSchema)
107
107
  await db.saveBatch(table, rows, opt)
108
108
  res.end()
109
109
  })
@@ -116,7 +116,7 @@ export function httpDBRequestHandler(db: CommonDB): BackendRouter {
116
116
 
117
117
  // deleteByQuery
118
118
  router.put('/deleteByQuery', async (req, res) => {
119
- const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
119
+ const { query, opt } = validateRequest.body(req, runQueryInputSchema)
120
120
  const q = DBQuery.fromPlainObject(query)
121
121
  res.json(await db.deleteByQuery(q, opt))
122
122
  })
@@ -104,4 +104,4 @@ class AjvValidateRequest {
104
104
  }
105
105
  }
106
106
 
107
- export const ajvValidateRequest = new AjvValidateRequest()
107
+ export const validateRequest = new AjvValidateRequest()
@@ -1,21 +0,0 @@
1
- import type { AnySchema, JoiValidationError } from '@naturalcycles/nodejs-lib/joi';
2
- import type { BackendRequest } from '../../server/server.model.js';
3
- import { type ReqValidationOptions } from '../validateRequest.util.js';
4
- declare class ValidateRequest {
5
- body<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
6
- query<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
7
- params<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
8
- /**
9
- * Validates `req.headers` against the provided Joi schema.
10
- *
11
- * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
12
- * i.e. schemas that cast values will not have any effect.
13
- *
14
- * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
15
- * Keep in mind that this will also remove all values that are not in the schema.
16
- */
17
- headers<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
18
- private validate;
19
- }
20
- export declare const validateRequest: ValidateRequest;
21
- export {};
@@ -1,41 +0,0 @@
1
- import { getValidationResult } from '@naturalcycles/nodejs-lib/joi';
2
- import { handleValidationError } from '../validateRequest.util.js';
3
- class ValidateRequest {
4
- body(req, schema, opt = {}) {
5
- return this.validate(req, 'body', schema, opt);
6
- }
7
- query(req, schema, opt = {}) {
8
- return this.validate(req, 'query', schema, opt);
9
- }
10
- params(req, schema, opt = {}) {
11
- return this.validate(req, 'params', schema, opt);
12
- }
13
- /**
14
- * Validates `req.headers` against the provided Joi schema.
15
- *
16
- * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
17
- * i.e. schemas that cast values will not have any effect.
18
- *
19
- * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
20
- * Keep in mind that this will also remove all values that are not in the schema.
21
- */
22
- headers(req, schema, opt = {}) {
23
- return this.validate(req, 'headers', schema, opt);
24
- }
25
- validate(req, reqProperty, schema, opt = {}) {
26
- const originalProperty = (req[reqProperty] || {});
27
- // Joi does not mutate the input
28
- const [error, value] = getValidationResult(originalProperty, schema, `request.${reqProperty}`);
29
- if (error) {
30
- if (opt.redactPaths) {
31
- error.data.joiValidationErrorItems.length = 0; // clears the array
32
- delete error.data.annotation;
33
- }
34
- handleValidationError(error, originalProperty, opt);
35
- }
36
- // Kirill: decide to not do it, let's see
37
- // req[reqProperty] = value
38
- return value;
39
- }
40
- }
41
- export const validateRequest = new ValidateRequest();
@@ -1,75 +0,0 @@
1
- import type { AnySchema, JoiValidationError } from '@naturalcycles/nodejs-lib/joi'
2
- import { getValidationResult } from '@naturalcycles/nodejs-lib/joi'
3
- import type { BackendRequest } from '../../server/server.model.js'
4
- import { handleValidationError, type ReqValidationOptions } from '../validateRequest.util.js'
5
-
6
- class ValidateRequest {
7
- body<T>(
8
- req: BackendRequest,
9
- schema: AnySchema<T>,
10
- opt: ReqValidationOptions<JoiValidationError> = {},
11
- ): T {
12
- return this.validate(req, 'body', schema, opt)
13
- }
14
-
15
- query<T>(
16
- req: BackendRequest,
17
- schema: AnySchema<T>,
18
- opt: ReqValidationOptions<JoiValidationError> = {},
19
- ): T {
20
- return this.validate(req, 'query', schema, opt)
21
- }
22
-
23
- params<T>(
24
- req: BackendRequest,
25
- schema: AnySchema<T>,
26
- opt: ReqValidationOptions<JoiValidationError> = {},
27
- ): T {
28
- return this.validate(req, 'params', schema, opt)
29
- }
30
-
31
- /**
32
- * Validates `req.headers` against the provided Joi schema.
33
- *
34
- * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
35
- * i.e. schemas that cast values will not have any effect.
36
- *
37
- * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
38
- * Keep in mind that this will also remove all values that are not in the schema.
39
- */
40
- headers<T>(
41
- req: BackendRequest,
42
- schema: AnySchema<T>,
43
- opt: ReqValidationOptions<JoiValidationError> = {},
44
- ): T {
45
- return this.validate(req, 'headers', schema, opt)
46
- }
47
-
48
- private validate<T>(
49
- req: BackendRequest,
50
- reqProperty: 'body' | 'params' | 'query' | 'headers',
51
- schema: AnySchema<T>,
52
- opt: ReqValidationOptions<JoiValidationError> = {},
53
- ): T {
54
- const originalProperty = (req[reqProperty] || {}) as T
55
-
56
- // Joi does not mutate the input
57
- const [error, value] = getValidationResult(originalProperty, schema, `request.${reqProperty}`)
58
-
59
- if (error) {
60
- if (opt.redactPaths) {
61
- error.data.joiValidationErrorItems.length = 0 // clears the array
62
- delete error.data.annotation
63
- }
64
-
65
- handleValidationError(error, originalProperty, opt)
66
- }
67
-
68
- // Kirill: decide to not do it, let's see
69
- // req[reqProperty] = value
70
-
71
- return value
72
- }
73
- }
74
-
75
- export const validateRequest = new ValidateRequest()