@naturalcycles/backend-lib 5.10.0 → 5.12.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.
@@ -30,7 +30,6 @@ function getServerStatusData(projectDir = process.cwd(), extra) {
30
30
  // resourceUsage: process.resourceUsage?.(),
31
31
  versions,
32
32
  NODE_OPTIONS,
33
- fetch: typeof globalThis.fetch === 'function',
34
33
  ...extra,
35
34
  });
36
35
  }
@@ -11,11 +11,26 @@ export interface ReqValidationOptions<ERR extends Error> {
11
11
  * If true - `genericErrorHandler` will report it to errorReporter (aka Sentry).
12
12
  */
13
13
  report?: boolean | ((err: ERR) => boolean);
14
+ /**
15
+ * When set to true, the validated object will not be replaced with the Joi-converted value.
16
+ *
17
+ * The general default is `false`, with the excepction of `headers` validation, where the default is `true`.
18
+ */
19
+ keepOriginal?: boolean;
14
20
  }
15
21
  declare class ValidateRequest {
16
22
  body<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
17
23
  query<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
18
24
  params<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
25
+ /**
26
+ * Validates `req.headers` against the provided Joi schema.
27
+ *
28
+ * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
29
+ * i.e. schemas that cast values will not have any effect.
30
+ *
31
+ * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
32
+ * Keep in mind that this will also remove all values that are not in the schema.
33
+ */
19
34
  headers<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
20
35
  private validate;
21
36
  }
@@ -25,8 +25,21 @@ class ValidateRequest {
25
25
  params(req, schema, opt = {}) {
26
26
  return this.validate(req, 'params', schema, opt);
27
27
  }
28
+ /**
29
+ * Validates `req.headers` against the provided Joi schema.
30
+ *
31
+ * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
32
+ * i.e. schemas that cast values will not have any effect.
33
+ *
34
+ * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
35
+ * Keep in mind that this will also remove all values that are not in the schema.
36
+ */
28
37
  headers(req, schema, opt = {}) {
29
- return this.validate(req, 'headers', schema, opt);
38
+ const options = {
39
+ keepOriginal: true,
40
+ ...opt,
41
+ };
42
+ return this.validate(req, 'headers', schema, options);
30
43
  }
31
44
  validate(req, reqProperty, schema, opt = {}) {
32
45
  const { value, error } = (0, nodejs_lib_1.getValidationResult)(req[reqProperty], schema, `request ${reqProperty}`);
@@ -50,7 +63,9 @@ class ValidateRequest {
50
63
  });
51
64
  }
52
65
  // mutate req to replace the property with the value, converted by Joi
53
- req[reqProperty] = value;
66
+ if (!opt.keepOriginal) {
67
+ req[reqProperty] = value;
68
+ }
54
69
  return value;
55
70
  }
56
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "5.10.0",
3
+ "version": "5.12.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build",
@@ -34,7 +34,6 @@ export function getServerStatusData(
34
34
  // resourceUsage: process.resourceUsage?.(),
35
35
  versions,
36
36
  NODE_OPTIONS,
37
- fetch: typeof globalThis.fetch === 'function',
38
37
  ...extra,
39
38
  })
40
39
  }
@@ -16,6 +16,13 @@ export interface ReqValidationOptions<ERR extends Error> {
16
16
  * If true - `genericErrorHandler` will report it to errorReporter (aka Sentry).
17
17
  */
18
18
  report?: boolean | ((err: ERR) => boolean)
19
+
20
+ /**
21
+ * When set to true, the validated object will not be replaced with the Joi-converted value.
22
+ *
23
+ * The general default is `false`, with the excepction of `headers` validation, where the default is `true`.
24
+ */
25
+ keepOriginal?: boolean
19
26
  }
20
27
 
21
28
  /**
@@ -55,12 +62,25 @@ class ValidateRequest {
55
62
  return this.validate(req, 'params', schema, opt)
56
63
  }
57
64
 
65
+ /**
66
+ * Validates `req.headers` against the provided Joi schema.
67
+ *
68
+ * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
69
+ * i.e. schemas that cast values will not have any effect.
70
+ *
71
+ * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
72
+ * Keep in mind that this will also remove all values that are not in the schema.
73
+ */
58
74
  headers<T>(
59
75
  req: BackendRequest,
60
76
  schema: AnySchema<T>,
61
77
  opt: ReqValidationOptions<JoiValidationError> = {},
62
78
  ): T {
63
- return this.validate(req, 'headers', schema, opt)
79
+ const options: ReqValidationOptions<JoiValidationError> = {
80
+ keepOriginal: true,
81
+ ...opt,
82
+ }
83
+ return this.validate(req, 'headers', schema, options)
64
84
  }
65
85
 
66
86
  private validate<T>(
@@ -92,7 +112,10 @@ class ValidateRequest {
92
112
  }
93
113
 
94
114
  // mutate req to replace the property with the value, converted by Joi
95
- req[reqProperty] = value
115
+ if (!opt.keepOriginal) {
116
+ req[reqProperty] = value
117
+ }
118
+
96
119
  return value
97
120
  }
98
121
  }