@naturalcycles/backend-lib 5.9.0 → 5.10.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/dist/db/httpDB.js CHANGED
@@ -19,7 +19,8 @@ class HttpDB extends db_lib_1.BaseCommonDB {
19
19
  updateSaveMethod: false,
20
20
  insertSaveMethod: false,
21
21
  transactions: false,
22
- updateByQuery: false,
22
+ patchByQuery: false,
23
+ increment: false,
23
24
  };
24
25
  this.setCfg(cfg);
25
26
  }
@@ -5,3 +5,4 @@ import { ReqValidationOptions } from './validateRequest';
5
5
  export declare function validateBody(schema: JsonSchema | JsonSchemaBuilder | AjvSchema, opt?: ReqValidationOptions<AjvValidationError>): BackendRequestHandler;
6
6
  export declare function validateParams(schema: JsonSchema | JsonSchemaBuilder | AjvSchema, opt?: ReqValidationOptions<AjvValidationError>): BackendRequestHandler;
7
7
  export declare function validateQuery(schema: JsonSchema | JsonSchemaBuilder | AjvSchema, opt?: ReqValidationOptions<AjvValidationError>): BackendRequestHandler;
8
+ export declare function validateHeaders(schema: JsonSchema | JsonSchemaBuilder | AjvSchema, opt?: ReqValidationOptions<AjvValidationError>): BackendRequestHandler;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateBody = validateBody;
4
4
  exports.validateParams = validateParams;
5
5
  exports.validateQuery = validateQuery;
6
+ exports.validateHeaders = validateHeaders;
6
7
  const js_lib_1 = require("@naturalcycles/js-lib");
7
8
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
8
9
  const REDACTED = 'REDACTED';
@@ -15,6 +16,9 @@ function validateParams(schema, opt = {}) {
15
16
  function validateQuery(schema, opt = {}) {
16
17
  return validateObject('query', schema, opt);
17
18
  }
19
+ function validateHeaders(schema, opt = {}) {
20
+ return validateObject('headers', schema, opt);
21
+ }
18
22
  /**
19
23
  * Validates req property (body, params or query).
20
24
  * Supports Joi schema or AjvSchema (from nodejs-lib).
@@ -51,6 +55,6 @@ function redact(redactPaths, obj, error) {
51
55
  .map(path => (0, js_lib_1._get)(obj, path))
52
56
  .filter(Boolean)
53
57
  .forEach(secret => {
54
- error.message = error.message.replace(secret, REDACTED);
58
+ error.message = error.message.replaceAll(secret, REDACTED);
55
59
  });
56
60
  }
@@ -16,6 +16,7 @@ declare class ValidateRequest {
16
16
  body<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
17
17
  query<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
18
18
  params<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
19
+ headers<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
19
20
  private validate;
20
21
  }
21
22
  export declare const validateRequest: ValidateRequest;
@@ -12,7 +12,7 @@ function redact(redactPaths, obj, error) {
12
12
  .map(path => (0, js_lib_1._get)(obj, path))
13
13
  .filter(Boolean)
14
14
  .forEach(secret => {
15
- error.message = error.message.replace(secret, REDACTED);
15
+ error.message = error.message.replaceAll(secret, REDACTED);
16
16
  });
17
17
  }
18
18
  class ValidateRequest {
@@ -25,6 +25,9 @@ class ValidateRequest {
25
25
  params(req, schema, opt = {}) {
26
26
  return this.validate(req, 'params', schema, opt);
27
27
  }
28
+ headers(req, schema, opt = {}) {
29
+ return this.validate(req, 'headers', schema, opt);
30
+ }
28
31
  validate(req, reqProperty, schema, opt = {}) {
29
32
  const { value, error } = (0, nodejs_lib_1.getValidationResult)(req[reqProperty], schema, `request ${reqProperty}`);
30
33
  if (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "5.9.0",
3
+ "version": "5.10.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build",
package/src/db/httpDB.ts CHANGED
@@ -35,7 +35,8 @@ export class HttpDB extends BaseCommonDB implements CommonDB {
35
35
  updateSaveMethod: false,
36
36
  insertSaveMethod: false,
37
37
  transactions: false,
38
- updateByQuery: false,
38
+ patchByQuery: false,
39
+ increment: false,
39
40
  }
40
41
 
41
42
  constructor(public cfg: HttpDBCfg) {
@@ -26,6 +26,13 @@ export function validateQuery(
26
26
  return validateObject('query', schema, opt)
27
27
  }
28
28
 
29
+ export function validateHeaders(
30
+ schema: JsonSchema | JsonSchemaBuilder | AjvSchema,
31
+ opt: ReqValidationOptions<AjvValidationError> = {},
32
+ ): BackendRequestHandler {
33
+ return validateObject('headers', schema, opt)
34
+ }
35
+
29
36
  /**
30
37
  * Validates req property (body, params or query).
31
38
  * Supports Joi schema or AjvSchema (from nodejs-lib).
@@ -33,7 +40,7 @@ export function validateQuery(
33
40
  * Throws http 400 on error.
34
41
  */
35
42
  function validateObject(
36
- prop: 'body' | 'params' | 'query',
43
+ prop: 'body' | 'params' | 'query' | 'headers',
37
44
  schema: JsonSchema | JsonSchemaBuilder | AjvSchema,
38
45
  opt: ReqValidationOptions<AjvValidationError> = {},
39
46
  ): BackendRequestHandler {
@@ -75,6 +82,6 @@ function redact(redactPaths: string[], obj: any, error: Error): void {
75
82
  .map(path => _get(obj, path) as string)
76
83
  .filter(Boolean)
77
84
  .forEach(secret => {
78
- error.message = error.message.replace(secret, REDACTED)
85
+ error.message = error.message.replaceAll(secret, REDACTED)
79
86
  })
80
87
  }
@@ -26,7 +26,7 @@ function redact(redactPaths: string[], obj: any, error: Error): void {
26
26
  .map(path => _get(obj, path) as string)
27
27
  .filter(Boolean)
28
28
  .forEach(secret => {
29
- error.message = error.message.replace(secret, REDACTED)
29
+ error.message = error.message.replaceAll(secret, REDACTED)
30
30
  })
31
31
  }
32
32
 
@@ -55,9 +55,17 @@ class ValidateRequest {
55
55
  return this.validate(req, 'params', schema, opt)
56
56
  }
57
57
 
58
+ headers<T>(
59
+ req: BackendRequest,
60
+ schema: AnySchema<T>,
61
+ opt: ReqValidationOptions<JoiValidationError> = {},
62
+ ): T {
63
+ return this.validate(req, 'headers', schema, opt)
64
+ }
65
+
58
66
  private validate<T>(
59
67
  req: BackendRequest,
60
- reqProperty: 'body' | 'params' | 'query',
68
+ reqProperty: 'body' | 'params' | 'query' | 'headers',
61
69
  schema: AnySchema<T>,
62
70
  opt: ReqValidationOptions<JoiValidationError> = {},
63
71
  ): T {