@naturalcycles/backend-lib 9.33.1 → 9.34.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.
@@ -100,7 +100,7 @@ export interface CloudRunEnv {
100
100
  /**
101
101
  * @experimental
102
102
  */
103
- declare class CloudRunService {
103
+ declare class CloudRunUtil {
104
104
  /**
105
105
  * Turns an object into a string representation where each
106
106
  * key-value pair is represented as `key1=value1,key2=value2,...`.
@@ -108,5 +108,5 @@ declare class CloudRunService {
108
108
  stringifyObject(obj: AnyObject): string;
109
109
  readonly defaultStartupProbeConfig: CloudRunStartupProbeConfig;
110
110
  }
111
- export declare const cloudRunService: CloudRunService;
111
+ export declare const cloudRunUtil: CloudRunUtil;
112
112
  export {};
@@ -2,7 +2,7 @@ import { _filterUndefinedValues } from '@naturalcycles/js-lib/object';
2
2
  /**
3
3
  * @experimental
4
4
  */
5
- class CloudRunService {
5
+ class CloudRunUtil {
6
6
  /**
7
7
  * Turns an object into a string representation where each
8
8
  * key-value pair is represented as `key1=value1,key2=value2,...`.
@@ -22,4 +22,4 @@ class CloudRunService {
22
22
  periodSeconds: 2,
23
23
  };
24
24
  }
25
- export const cloudRunService = new CloudRunService();
25
+ export const cloudRunUtil = new CloudRunUtil();
@@ -1,10 +1,11 @@
1
- import type { AjvSchema, AjvValidationError } from '@naturalcycles/nodejs-lib/ajv';
1
+ import type { ZodType } from '@naturalcycles/js-lib/zod';
2
+ import { AjvSchema, type AjvValidationError } from '@naturalcycles/nodejs-lib/ajv';
2
3
  import type { BackendRequest } from '../../server/server.model.js';
3
4
  import { type ReqValidationOptions } from '../validateRequest.util.js';
4
5
  declare class AjvValidateRequest {
5
- body<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
6
- query<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
7
- params<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
6
+ body<T>(req: BackendRequest, schema: AjvSchema<T> | ZodType<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
7
+ query<T>(req: BackendRequest, schema: AjvSchema<T> | ZodType<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
8
+ params<T>(req: BackendRequest, schema: AjvSchema<T> | ZodType<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
8
9
  /**
9
10
  * Does NOT mutate `req.headers`,
10
11
  * but returns validated/transformed headers.
@@ -1,4 +1,5 @@
1
1
  import { _deepCopy } from '@naturalcycles/js-lib/object';
2
+ import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv';
2
3
  import { handleValidationError } from '../validateRequest.util.js';
3
4
  class AjvValidateRequest {
4
5
  body(req, schema, opt = {}) {
@@ -26,7 +27,8 @@ class AjvValidateRequest {
26
27
  }
27
28
  validate(req, reqProperty, schema, opt = {}) {
28
29
  const input = req[reqProperty] || {};
29
- const [error, output] = schema.getValidationResult(input, {
30
+ const ajvSchema = AjvSchema.create(schema);
31
+ const [error, output] = ajvSchema.getValidationResult(input, {
30
32
  inputName: `request ${reqProperty}`,
31
33
  });
32
34
  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.33.1",
4
+ "version": "9.34.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -132,7 +132,7 @@ export interface CloudRunEnv {
132
132
  /**
133
133
  * @experimental
134
134
  */
135
- class CloudRunService {
135
+ class CloudRunUtil {
136
136
  /**
137
137
  * Turns an object into a string representation where each
138
138
  * key-value pair is represented as `key1=value1,key2=value2,...`.
@@ -154,4 +154,4 @@ class CloudRunService {
154
154
  }
155
155
  }
156
156
 
157
- export const cloudRunService = new CloudRunService()
157
+ export const cloudRunUtil = new CloudRunUtil()
@@ -1,12 +1,13 @@
1
1
  import { _deepCopy } from '@naturalcycles/js-lib/object'
2
- import type { AjvSchema, AjvValidationError } from '@naturalcycles/nodejs-lib/ajv'
2
+ import type { ZodType } from '@naturalcycles/js-lib/zod'
3
+ import { AjvSchema, type AjvValidationError } from '@naturalcycles/nodejs-lib/ajv'
3
4
  import type { BackendRequest } from '../../server/server.model.js'
4
5
  import { handleValidationError, type ReqValidationOptions } from '../validateRequest.util.js'
5
6
 
6
7
  class AjvValidateRequest {
7
8
  body<T>(
8
9
  req: BackendRequest,
9
- schema: AjvSchema<T>,
10
+ schema: AjvSchema<T> | ZodType<T>,
10
11
  opt: ReqValidationOptions<AjvValidationError> = {},
11
12
  ): T {
12
13
  return this.validate(req, 'body', schema, opt)
@@ -14,7 +15,7 @@ class AjvValidateRequest {
14
15
 
15
16
  query<T>(
16
17
  req: BackendRequest,
17
- schema: AjvSchema<T>,
18
+ schema: AjvSchema<T> | ZodType<T>,
18
19
  opt: ReqValidationOptions<AjvValidationError> = {},
19
20
  ): T {
20
21
  return this.validate(req, 'query', schema, opt)
@@ -22,7 +23,7 @@ class AjvValidateRequest {
22
23
 
23
24
  params<T>(
24
25
  req: BackendRequest,
25
- schema: AjvSchema<T>,
26
+ schema: AjvSchema<T> | ZodType<T>,
26
27
  opt: ReqValidationOptions<AjvValidationError> = {},
27
28
  ): T {
28
29
  return this.validate(req, 'params', schema, opt)
@@ -50,12 +51,13 @@ class AjvValidateRequest {
50
51
  private validate<T>(
51
52
  req: BackendRequest,
52
53
  reqProperty: 'body' | 'params' | 'query' | 'headers',
53
- schema: AjvSchema<T>,
54
+ schema: AjvSchema<T> | ZodType<T>,
54
55
  opt: ReqValidationOptions<AjvValidationError> = {},
55
56
  ): T {
56
57
  const input: T = req[reqProperty] || {}
58
+ const ajvSchema = AjvSchema.create(schema)
57
59
 
58
- const [error, output] = schema.getValidationResult(input, {
60
+ const [error, output] = ajvSchema.getValidationResult(input, {
59
61
  inputName: `request ${reqProperty}`,
60
62
  })
61
63