@naturalcycles/backend-lib 9.38.0 → 9.40.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.
@@ -2,9 +2,9 @@ import { type AjvValidationError, type SchemaHandledByAjv } from '@naturalcycles
2
2
  import type { BackendRequest } from '../../server/server.model.js';
3
3
  import { type ReqValidationOptions } from '../validateRequest.util.js';
4
4
  declare class AjvValidateRequest {
5
- body<T>(req: BackendRequest, schema: SchemaHandledByAjv<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
6
- query<T>(req: BackendRequest, schema: SchemaHandledByAjv<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
7
- params<T>(req: BackendRequest, schema: SchemaHandledByAjv<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
5
+ body<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
6
+ query<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
7
+ params<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
8
8
  /**
9
9
  * Does NOT mutate `req.headers`,
10
10
  * but returns validated/transformed headers.
@@ -13,7 +13,7 @@ declare class AjvValidateRequest {
13
13
  * We want to non-mutate the `req.headers`, because we anticipate that
14
14
  * there may be additional consumers for `req.headers` (e.g middlewares, etc).
15
15
  */
16
- headers<T>(req: BackendRequest, schema: SchemaHandledByAjv<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
16
+ headers<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
17
17
  private validate;
18
18
  }
19
19
  export declare const ajvValidateRequest: AjvValidateRequest;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.38.0",
4
+ "version": "9.40.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -8,27 +8,27 @@ import type { BackendRequest } from '../../server/server.model.js'
8
8
  import { handleValidationError, type ReqValidationOptions } from '../validateRequest.util.js'
9
9
 
10
10
  class AjvValidateRequest {
11
- body<T>(
11
+ body<IN, OUT>(
12
12
  req: BackendRequest,
13
- schema: SchemaHandledByAjv<T>,
13
+ schema: SchemaHandledByAjv<IN, OUT>,
14
14
  opt: ReqValidationOptions<AjvValidationError> = {},
15
- ): T {
15
+ ): OUT {
16
16
  return this.validate(req, 'body', schema, opt)
17
17
  }
18
18
 
19
- query<T>(
19
+ query<IN, OUT>(
20
20
  req: BackendRequest,
21
- schema: SchemaHandledByAjv<T>,
21
+ schema: SchemaHandledByAjv<IN, OUT>,
22
22
  opt: ReqValidationOptions<AjvValidationError> = {},
23
- ): T {
23
+ ): OUT {
24
24
  return this.validate(req, 'query', schema, opt)
25
25
  }
26
26
 
27
- params<T>(
27
+ params<IN, OUT>(
28
28
  req: BackendRequest,
29
- schema: SchemaHandledByAjv<T>,
29
+ schema: SchemaHandledByAjv<IN, OUT>,
30
30
  opt: ReqValidationOptions<AjvValidationError> = {},
31
- ): T {
31
+ ): OUT {
32
32
  return this.validate(req, 'params', schema, opt)
33
33
  }
34
34
 
@@ -40,24 +40,24 @@ class AjvValidateRequest {
40
40
  * We want to non-mutate the `req.headers`, because we anticipate that
41
41
  * there may be additional consumers for `req.headers` (e.g middlewares, etc).
42
42
  */
43
- headers<T>(
43
+ headers<IN, OUT>(
44
44
  req: BackendRequest,
45
- schema: SchemaHandledByAjv<T>,
45
+ schema: SchemaHandledByAjv<IN, OUT>,
46
46
  opt: ReqValidationOptions<AjvValidationError> = {},
47
- ): T {
47
+ ): OUT {
48
48
  const originalHeaders = _deepCopy(req.headers)
49
49
  const headers = this.validate(req, 'headers', schema, opt)
50
50
  req.headers = originalHeaders
51
51
  return headers
52
52
  }
53
53
 
54
- private validate<T>(
54
+ private validate<IN, OUT>(
55
55
  req: BackendRequest,
56
56
  reqProperty: 'body' | 'params' | 'query' | 'headers',
57
- schema: SchemaHandledByAjv<T>,
57
+ schema: SchemaHandledByAjv<IN, OUT>,
58
58
  opt: ReqValidationOptions<AjvValidationError> = {},
59
- ): T {
60
- const input: T = req[reqProperty] || {}
59
+ ): OUT {
60
+ const input: IN = req[reqProperty] || {}
61
61
  const ajvSchema = AjvSchema.create(schema)
62
62
 
63
63
  const [error, output] = ajvSchema.getValidationResult(input, {