@naturalcycles/backend-lib 9.44.0 → 9.44.2

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.
@@ -8,7 +8,7 @@ import type { Application, IRouter, NextFunction, Request, Response } from 'expr
8
8
  * `BackendRequest` seems to not conflict with anything right now.
9
9
  * Previous name `ExpressRequest` was clashing with Sentry.
10
10
  */
11
- export interface BackendRequest extends Request {
11
+ export interface BackendRequest<BODY = unknown> extends Request {
12
12
  debug: CommonLogFunction;
13
13
  log: CommonLogFunction;
14
14
  warn: CommonLogFunction;
@@ -18,6 +18,11 @@ export interface BackendRequest extends Request {
18
18
  * Only used for request logging purposes.
19
19
  */
20
20
  userId?: string;
21
+ /**
22
+ * It defaults to unknown (instead of `any`) to prevent implicit use of any
23
+ * in unexpected places.
24
+ */
25
+ body: BODY;
21
26
  /**
22
27
  * Raw Buffer of the `req.body`, before it's stringified and json-parsed.
23
28
  * Useful for when something mutates `req.body` json (e.g j validation), and you
@@ -45,7 +45,7 @@ class AjvValidateRequest {
45
45
  });
46
46
  }
47
47
  validate(req, reqProperty, schema, getOriginalInput, opt = {}) {
48
- const input = req[reqProperty] || {};
48
+ const input = (req[reqProperty] || {});
49
49
  const { coerceTypes, mutateInput } = opt;
50
50
  const ajv = coerceTypes ? getCoercingAjv() : undefined;
51
51
  const ajvSchema = AjvSchema.create(schema, { ajv });
@@ -23,7 +23,7 @@ class ValidateRequest {
23
23
  return this.validate(req, 'headers', schema, opt);
24
24
  }
25
25
  validate(req, reqProperty, schema, opt = {}) {
26
- const originalProperty = req[reqProperty] || {};
26
+ const originalProperty = (req[reqProperty] || {});
27
27
  // Joi does not mutate the input
28
28
  const [error, value] = getValidationResult(originalProperty, schema, `request.${reqProperty}`);
29
29
  if (error) {
@@ -23,7 +23,7 @@ class ZodValidateRequest {
23
23
  return this.validate(req, 'headers', schema, opt);
24
24
  }
25
25
  validate(req, reqProperty, schema, opt = {}) {
26
- const originalProperty = req[reqProperty] || {};
26
+ const originalProperty = (req[reqProperty] || {});
27
27
  // Zod does not mutate the input
28
28
  const [error, data] = zSafeValidate(originalProperty, schema);
29
29
  if (error) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.44.0",
4
+ "version": "9.44.2",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -9,7 +9,7 @@ import type { Application, IRouter, NextFunction, Request, Response } from 'expr
9
9
  * `BackendRequest` seems to not conflict with anything right now.
10
10
  * Previous name `ExpressRequest` was clashing with Sentry.
11
11
  */
12
- export interface BackendRequest extends Request {
12
+ export interface BackendRequest<BODY = unknown> extends Request {
13
13
  debug: CommonLogFunction
14
14
  log: CommonLogFunction
15
15
  warn: CommonLogFunction
@@ -20,6 +20,13 @@ export interface BackendRequest extends Request {
20
20
  * Only used for request logging purposes.
21
21
  */
22
22
  userId?: string
23
+
24
+ /**
25
+ * It defaults to unknown (instead of `any`) to prevent implicit use of any
26
+ * in unexpected places.
27
+ */
28
+ body: BODY
29
+
23
30
  /**
24
31
  * Raw Buffer of the `req.body`, before it's stringified and json-parsed.
25
32
  * Useful for when something mutates `req.body` json (e.g j validation), and you
@@ -84,7 +84,7 @@ class AjvValidateRequest {
84
84
  getOriginalInput?: () => IN,
85
85
  opt: ReqValidationOptions<AjvValidationError> = {},
86
86
  ): OUT {
87
- const input: IN = req[reqProperty] || {}
87
+ const input = (req[reqProperty] || {}) as IN
88
88
 
89
89
  const { coerceTypes, mutateInput } = opt
90
90
  const ajv = coerceTypes ? getCoercingAjv() : undefined
@@ -51,7 +51,7 @@ class ValidateRequest {
51
51
  schema: AnySchema<T>,
52
52
  opt: ReqValidationOptions<JoiValidationError> = {},
53
53
  ): T {
54
- const originalProperty = req[reqProperty] || {}
54
+ const originalProperty = (req[reqProperty] || {}) as T
55
55
 
56
56
  // Joi does not mutate the input
57
57
  const [error, value] = getValidationResult(originalProperty, schema, `request.${reqProperty}`)
@@ -50,7 +50,7 @@ class ZodValidateRequest {
50
50
  schema: ZodType<T>,
51
51
  opt: ReqValidationOptions<ZodValidationError> = {},
52
52
  ): T {
53
- const originalProperty = req[reqProperty] || {}
53
+ const originalProperty = (req[reqProperty] || {}) as T
54
54
 
55
55
  // Zod does not mutate the input
56
56
  const [error, data] = zSafeValidate(