@naturalcycles/backend-lib 9.37.0 → 9.39.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.
@@ -1,8 +1,8 @@
1
1
  import type { CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, DBQuery, RunQueryResult } from '@naturalcycles/db-lib';
2
2
  import { BaseCommonDB } from '@naturalcycles/db-lib';
3
3
  import type { FetcherOptions } from '@naturalcycles/js-lib/http';
4
- import type { JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
5
4
  import type { ObjectWithId } from '@naturalcycles/js-lib/types';
5
+ import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
6
6
  export interface HttpDBCfg extends FetcherOptions {
7
7
  baseUrl: string;
8
8
  }
@@ -17,7 +17,7 @@ export declare class HttpDB extends BaseCommonDB implements CommonDB {
17
17
  private fetcher;
18
18
  ping(): Promise<void>;
19
19
  getTables(): Promise<string[]>;
20
- getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
20
+ getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>>;
21
21
  resetCache(table?: string): Promise<void>;
22
22
  getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: CommonDBOptions): Promise<ROW[]>;
23
23
  runQuery<ROW extends ObjectWithId>(query: DBQuery<ROW>, opt?: CommonDBOptions): Promise<RunQueryResult<ROW>>;
@@ -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.37.0",
4
+ "version": "9.39.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -29,7 +29,7 @@
29
29
  "@sentry/node": "^10",
30
30
  "@types/ejs": "^3",
31
31
  "fastify": "^5",
32
- "@naturalcycles/dev-lib": "20.6.0"
32
+ "@naturalcycles/dev-lib": "18.4.2"
33
33
  },
34
34
  "exports": {
35
35
  ".": "./dist/index.js",
package/src/db/httpDB.ts CHANGED
@@ -9,8 +9,8 @@ import type {
9
9
  import { BaseCommonDB, commonDBFullSupport } from '@naturalcycles/db-lib'
10
10
  import type { Fetcher, FetcherOptions } from '@naturalcycles/js-lib/http'
11
11
  import { getFetcher } from '@naturalcycles/js-lib/http'
12
- import type { JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'
13
12
  import type { ObjectWithId } from '@naturalcycles/js-lib/types'
13
+ import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
14
14
 
15
15
  export interface HttpDBCfg extends FetcherOptions {
16
16
  baseUrl: string
@@ -51,9 +51,7 @@ export class HttpDB extends BaseCommonDB implements CommonDB {
51
51
  return await this.fetcher.get(`tables`)
52
52
  }
53
53
 
54
- override async getTableSchema<ROW extends ObjectWithId>(
55
- table: string,
56
- ): Promise<JsonSchemaRootObject<ROW>> {
54
+ override async getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>> {
57
55
  return await this.fetcher.get(`${table}/schema`)
58
56
  }
59
57
 
@@ -1,7 +1,6 @@
1
1
  import { _lazyValue } from '@naturalcycles/js-lib'
2
- import type { JsonSchema } from '@naturalcycles/js-lib/json-schema'
3
2
  import type { StringMap } from '@naturalcycles/js-lib/types'
4
- import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv'
3
+ import { AjvSchema, type JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
5
4
  import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
6
5
  import { yaml2 } from '@naturalcycles/nodejs-lib/yaml2'
7
6
  import { resourcesDir } from '../paths.cnst.js'
@@ -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, {