@naturalcycles/backend-lib 9.46.1 → 9.47.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.
@@ -20,8 +20,8 @@ export interface AdminInfo {
20
20
  * Base implementation based on Firebase Auth tokens passed as 'admin_token' cookie.
21
21
  */
22
22
  export declare class BaseAdminService {
23
- private firebaseAuth;
24
- constructor(firebaseAuth: FirebaseAdmin.auth.Auth, cfg: AdminServiceCfg);
23
+ private loadFirebaseAuth;
24
+ constructor(loadFirebaseAuth: () => Promise<FirebaseAdmin.auth.Auth>, cfg: AdminServiceCfg);
25
25
  cfg: Required<AdminServiceCfg>;
26
26
  adminInfoDisabled(): AdminInfo;
27
27
  /**
@@ -37,6 +37,7 @@ export declare class BaseAdminService {
37
37
  */
38
38
  protected onPermissionCheck(req: BackendRequest, email: string, reqPermissions: string[], required: boolean, granted: boolean, meta?: Record<string, any>): Promise<void>;
39
39
  getEmailByToken(req: BackendRequest, adminToken?: string): Promise<string | undefined>;
40
+ private getFirebaseAuth;
40
41
  /**
41
42
  * Current implementation is based on req=Request (from Express).
42
43
  * Override if needed.
@@ -1,3 +1,5 @@
1
+ import { __decorate } from "tslib";
2
+ import { _Memo } from '@naturalcycles/js-lib/decorators';
1
3
  import { _assert, AppError } from '@naturalcycles/js-lib/error';
2
4
  import { dimGrey, green, red } from '@naturalcycles/nodejs-lib/colors';
3
5
  const adminInfoDisabled = () => ({
@@ -8,9 +10,9 @@ const adminInfoDisabled = () => ({
8
10
  * Base implementation based on Firebase Auth tokens passed as 'admin_token' cookie.
9
11
  */
10
12
  export class BaseAdminService {
11
- firebaseAuth;
12
- constructor(firebaseAuth, cfg) {
13
- this.firebaseAuth = firebaseAuth;
13
+ loadFirebaseAuth;
14
+ constructor(loadFirebaseAuth, cfg) {
15
+ this.loadFirebaseAuth = loadFirebaseAuth;
14
16
  this.cfg = {
15
17
  adminTokenKey: 'admin_token',
16
18
  authEnabled: true,
@@ -47,7 +49,8 @@ export class BaseAdminService {
47
49
  if (!adminToken)
48
50
  return;
49
51
  try {
50
- const decodedToken = await this.firebaseAuth.verifyIdToken(adminToken);
52
+ const auth = await this.getFirebaseAuth();
53
+ const decodedToken = await auth.verifyIdToken(adminToken);
51
54
  const email = decodedToken?.email;
52
55
  req.log(`admin email: ${dimGrey(email)}`);
53
56
  return email;
@@ -63,6 +66,9 @@ export class BaseAdminService {
63
66
  req.error(`getEmailByToken error:`, err);
64
67
  }
65
68
  }
69
+ async getFirebaseAuth() {
70
+ return await this.loadFirebaseAuth();
71
+ }
66
72
  /**
67
73
  * Current implementation is based on req=Request (from Express).
68
74
  * Override if needed.
@@ -193,3 +199,6 @@ export class BaseAdminService {
193
199
  };
194
200
  }
195
201
  }
202
+ __decorate([
203
+ _Memo()
204
+ ], BaseAdminService.prototype, "getFirebaseAuth", null);
@@ -1,22 +1,22 @@
1
1
  import { DBQuery } from '@naturalcycles/db-lib';
2
2
  import { InMemoryDB } from '@naturalcycles/db-lib/inmemory';
3
3
  import { commonDBOptionsSchema, commonDBSaveOptionsSchema, dbQuerySchema, } from '@naturalcycles/db-lib/validation';
4
- import { anyObjectSchema, arraySchema, objectSchema, stringSchema, } from '@naturalcycles/nodejs-lib/joi';
4
+ import { j } from '@naturalcycles/nodejs-lib/ajv';
5
5
  import { getDefaultRouter } from '../express/getDefaultRouter.js';
6
- import { validateRequest } from '../validation/joi/joiValidateRequest.js';
7
- const getByIdsInputSchema = objectSchema({
8
- table: stringSchema,
9
- ids: arraySchema(stringSchema),
10
- opt: commonDBOptionsSchema.optional(),
6
+ import { ajvValidateRequest } from '../validation/ajv/ajvValidateRequest.js';
7
+ const getByIdsInputSchema = j.object({
8
+ table: j.string(),
9
+ ids: j.array(j.string()),
10
+ opt: commonDBOptionsSchema().optional(),
11
11
  });
12
- const runQueryInputSchema = objectSchema({
13
- query: dbQuerySchema,
14
- opt: commonDBOptionsSchema.optional(),
12
+ const runQueryInputSchema = j.object({
13
+ query: dbQuerySchema(),
14
+ opt: commonDBOptionsSchema().optional(),
15
15
  });
16
- const saveBatchInputSchema = objectSchema({
17
- table: stringSchema,
18
- rows: arraySchema(anyObjectSchema()),
19
- opt: commonDBSaveOptionsSchema.optional(),
16
+ const saveBatchInputSchema = j.object({
17
+ table: j.string(),
18
+ rows: j.array(j.object({ id: j.string() }).allowAdditionalProperties()),
19
+ opt: commonDBSaveOptionsSchema().optional(),
20
20
  });
21
21
  /**
22
22
  * Exposes CommonDB interface from provided CommonDB as HTTP endpoint (Express RequestHandler).
@@ -49,24 +49,24 @@ export function httpDBRequestHandler(db) {
49
49
  // })
50
50
  // getByIds
51
51
  router.put('/getByIds', async (req, res) => {
52
- const { table, ids, opt } = validateRequest.body(req, getByIdsInputSchema);
52
+ const { table, ids, opt } = ajvValidateRequest.body(req, getByIdsInputSchema);
53
53
  res.json(await db.getByIds(table, ids, opt));
54
54
  });
55
55
  // runQuery
56
56
  router.put('/runQuery', async (req, res) => {
57
- const { query, opt } = validateRequest.body(req, runQueryInputSchema);
57
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
58
58
  const q = DBQuery.fromPlainObject(query);
59
59
  res.json(await db.runQuery(q, opt));
60
60
  });
61
61
  // runQueryCount
62
62
  router.put('/runQueryCount', async (req, res) => {
63
- const { query, opt } = validateRequest.body(req, runQueryInputSchema);
63
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
64
64
  const q = DBQuery.fromPlainObject(query);
65
65
  res.json(await db.runQueryCount(q, opt));
66
66
  });
67
67
  // saveBatch
68
68
  router.put('/saveBatch', async (req, res) => {
69
- const { table, rows, opt } = validateRequest.body(req, saveBatchInputSchema);
69
+ const { table, rows, opt } = ajvValidateRequest.body(req, saveBatchInputSchema);
70
70
  await db.saveBatch(table, rows, opt);
71
71
  res.end();
72
72
  });
@@ -77,7 +77,7 @@ export function httpDBRequestHandler(db) {
77
77
  // })
78
78
  // deleteByQuery
79
79
  router.put('/deleteByQuery', async (req, res) => {
80
- const { query, opt } = validateRequest.body(req, runQueryInputSchema);
80
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
81
81
  const q = DBQuery.fromPlainObject(query);
82
82
  res.json(await db.deleteByQuery(q, opt));
83
83
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.46.1",
4
+ "version": "9.47.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -1,3 +1,4 @@
1
+ import { _Memo } from '@naturalcycles/js-lib/decorators'
1
2
  import { _assert, AppError } from '@naturalcycles/js-lib/error'
2
3
  import { dimGrey, green, red } from '@naturalcycles/nodejs-lib/colors'
3
4
  import type FirebaseAdmin from 'firebase-admin'
@@ -32,7 +33,7 @@ const adminInfoDisabled = (): AdminInfo => ({
32
33
  */
33
34
  export class BaseAdminService {
34
35
  constructor(
35
- private firebaseAuth: FirebaseAdmin.auth.Auth,
36
+ private loadFirebaseAuth: () => Promise<FirebaseAdmin.auth.Auth>,
36
37
  cfg: AdminServiceCfg,
37
38
  ) {
38
39
  this.cfg = {
@@ -91,7 +92,8 @@ export class BaseAdminService {
91
92
  if (!adminToken) return
92
93
 
93
94
  try {
94
- const decodedToken = await this.firebaseAuth.verifyIdToken(adminToken)
95
+ const auth = await this.getFirebaseAuth()
96
+ const decodedToken = await auth.verifyIdToken(adminToken)
95
97
  const email = decodedToken?.email
96
98
  req.log(`admin email: ${dimGrey(email)}`)
97
99
  return email
@@ -109,6 +111,11 @@ export class BaseAdminService {
109
111
  }
110
112
  }
111
113
 
114
+ @_Memo()
115
+ private async getFirebaseAuth(): Promise<FirebaseAdmin.auth.Auth> {
116
+ return await this.loadFirebaseAuth()
117
+ }
118
+
112
119
  /**
113
120
  * Current implementation is based on req=Request (from Express).
114
121
  * Override if needed.
@@ -7,15 +7,10 @@ import {
7
7
  dbQuerySchema,
8
8
  } from '@naturalcycles/db-lib/validation'
9
9
  import type { ObjectWithId } from '@naturalcycles/js-lib/types'
10
- import {
11
- anyObjectSchema,
12
- arraySchema,
13
- objectSchema,
14
- stringSchema,
15
- } from '@naturalcycles/nodejs-lib/joi'
10
+ import { j } from '@naturalcycles/nodejs-lib/ajv'
16
11
  import { getDefaultRouter } from '../express/getDefaultRouter.js'
17
12
  import type { BackendRouter } from '../server/server.model.js'
18
- import { validateRequest } from '../validation/joi/joiValidateRequest.js'
13
+ import { ajvValidateRequest } from '../validation/ajv/ajvValidateRequest.js'
19
14
 
20
15
  export interface GetByIdsInput {
21
16
  table: string
@@ -23,10 +18,10 @@ export interface GetByIdsInput {
23
18
  opt?: CommonDBOptions
24
19
  }
25
20
 
26
- const getByIdsInputSchema = objectSchema<GetByIdsInput>({
27
- table: stringSchema,
28
- ids: arraySchema(stringSchema),
29
- opt: commonDBOptionsSchema.optional(),
21
+ const getByIdsInputSchema = j.object<GetByIdsInput>({
22
+ table: j.string(),
23
+ ids: j.array(j.string()),
24
+ opt: commonDBOptionsSchema().optional(),
30
25
  })
31
26
 
32
27
  export interface RunQueryInput {
@@ -34,9 +29,9 @@ export interface RunQueryInput {
34
29
  opt?: CommonDBOptions
35
30
  }
36
31
 
37
- const runQueryInputSchema = objectSchema<RunQueryInput>({
38
- query: dbQuerySchema,
39
- opt: commonDBOptionsSchema.optional(),
32
+ const runQueryInputSchema = j.object<RunQueryInput>({
33
+ query: dbQuerySchema(),
34
+ opt: commonDBOptionsSchema().optional(),
40
35
  })
41
36
 
42
37
  export interface SaveBatchInput {
@@ -45,10 +40,10 @@ export interface SaveBatchInput {
45
40
  opt?: CommonDBSaveOptions<any>
46
41
  }
47
42
 
48
- const saveBatchInputSchema = objectSchema<SaveBatchInput>({
49
- table: stringSchema,
50
- rows: arraySchema(anyObjectSchema()),
51
- opt: commonDBSaveOptionsSchema.optional(),
43
+ const saveBatchInputSchema = j.object<SaveBatchInput>({
44
+ table: j.string(),
45
+ rows: j.array(j.object<ObjectWithId>({ id: j.string() }).allowAdditionalProperties()),
46
+ opt: commonDBSaveOptionsSchema().optional(),
52
47
  })
53
48
 
54
49
  /**
@@ -88,27 +83,27 @@ export function httpDBRequestHandler(db: CommonDB): BackendRouter {
88
83
 
89
84
  // getByIds
90
85
  router.put('/getByIds', async (req, res) => {
91
- const { table, ids, opt } = validateRequest.body(req, getByIdsInputSchema)
86
+ const { table, ids, opt } = ajvValidateRequest.body(req, getByIdsInputSchema)
92
87
  res.json(await db.getByIds(table, ids, opt))
93
88
  })
94
89
 
95
90
  // runQuery
96
91
  router.put('/runQuery', async (req, res) => {
97
- const { query, opt } = validateRequest.body(req, runQueryInputSchema)
92
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
98
93
  const q = DBQuery.fromPlainObject(query)
99
94
  res.json(await db.runQuery(q, opt))
100
95
  })
101
96
 
102
97
  // runQueryCount
103
98
  router.put('/runQueryCount', async (req, res) => {
104
- const { query, opt } = validateRequest.body(req, runQueryInputSchema)
99
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
105
100
  const q = DBQuery.fromPlainObject(query)
106
101
  res.json(await db.runQueryCount(q, opt))
107
102
  })
108
103
 
109
104
  // saveBatch
110
105
  router.put('/saveBatch', async (req, res) => {
111
- const { table, rows, opt } = validateRequest.body(req, saveBatchInputSchema)
106
+ const { table, rows, opt } = ajvValidateRequest.body(req, saveBatchInputSchema)
112
107
  await db.saveBatch(table, rows, opt)
113
108
  res.end()
114
109
  })
@@ -121,7 +116,7 @@ export function httpDBRequestHandler(db: CommonDB): BackendRouter {
121
116
 
122
117
  // deleteByQuery
123
118
  router.put('/deleteByQuery', async (req, res) => {
124
- const { query, opt } = validateRequest.body(req, runQueryInputSchema)
119
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
125
120
  const q = DBQuery.fromPlainObject(query)
126
121
  res.json(await db.deleteByQuery(q, opt))
127
122
  })