@naturalcycles/backend-lib 9.19.0 → 9.21.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.
@@ -3,7 +3,7 @@ import { _filterNullishValues } from '@naturalcycles/js-lib/object/object.util.j
3
3
  import { memoryUsageFull, processSharedUtil } from '@naturalcycles/nodejs-lib';
4
4
  import { getDeployInfo } from '../deploy/deployInfo.util.js';
5
5
  const { versions, arch, platform } = process;
6
- const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, GOOGLE_CLOUD_PROJECT, K_SERVICE, K_REVISION, APP_ENV, NODE_OPTIONS, DEPLOY_BUILD_TIME, } = process.env;
6
+ const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, GOOGLE_CLOUD_PROJECT, K_SERVICE, K_REVISION, APP_ENV, NODE_OPTIONS, DEPLOY_BUILD_TIME, BUILD_VERSION, } = process.env;
7
7
  export function serverStatusMiddleware(projectDir, extra) {
8
8
  return async (_req, res) => {
9
9
  res.json(getServerStatusData(projectDir, extra));
@@ -16,6 +16,7 @@ export function getServerStatusData(projectDir = process.cwd(), extra) {
16
16
  return _filterNullishValues({
17
17
  nodeProcessStarted: getStartedStr(),
18
18
  deployBuildTime,
19
+ BUILD_VERSION,
19
20
  APP_ENV,
20
21
  GOOGLE_CLOUD_PROJECT,
21
22
  GAE_APPLICATION,
@@ -26,13 +26,13 @@ class AjvValidateRequest {
26
26
  const originalProperty = req[reqProperty] || {};
27
27
  const item = mutate ? originalProperty : { ...originalProperty };
28
28
  // Ajv mutates the input
29
- const error = schema.getValidationError(item, {
29
+ const [error, output] = schema.getValidationResult(item, {
30
30
  objectName: `request ${reqProperty}`,
31
31
  });
32
32
  if (error) {
33
33
  handleValidationError(error, originalProperty, opt);
34
34
  }
35
- return item;
35
+ return output;
36
36
  }
37
37
  }
38
38
  export const ajvValidateRequest = new AjvValidateRequest();
@@ -26,7 +26,7 @@ class ValidateRequest {
26
26
  const { mutate } = opt;
27
27
  const originalProperty = req[reqProperty] || {};
28
28
  // Joi does not mutate the input
29
- const { error, value } = getValidationResult(originalProperty, schema, `request ${reqProperty}`);
29
+ const [error, value] = getValidationResult(originalProperty, schema, `request ${reqProperty}`);
30
30
  if (error) {
31
31
  if (opt.redactPaths) {
32
32
  error.data.joiValidationErrorItems.length = 0; // clears the array
@@ -26,7 +26,7 @@ class ZodValidateRequest {
26
26
  const { mutate } = opt;
27
27
  const originalProperty = req[reqProperty] || {};
28
28
  // Zod does not mutate the input
29
- const { error, data } = zSafeValidate(originalProperty, schema);
29
+ const [error, data] = zSafeValidate(originalProperty, schema);
30
30
  if (error) {
31
31
  handleValidationError(error, originalProperty, opt);
32
32
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.19.0",
4
+ "version": "9.21.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^9"
7
7
  },
@@ -29,7 +29,7 @@
29
29
  "@sentry/node": "^9",
30
30
  "@types/ejs": "^3",
31
31
  "fastify": "^5",
32
- "@naturalcycles/dev-lib": "18.4.2"
32
+ "@naturalcycles/dev-lib": "19.14.0"
33
33
  },
34
34
  "exports": {
35
35
  ".": "./dist/index.js",
@@ -15,6 +15,7 @@ const {
15
15
  APP_ENV,
16
16
  NODE_OPTIONS,
17
17
  DEPLOY_BUILD_TIME,
18
+ BUILD_VERSION,
18
19
  } = process.env
19
20
 
20
21
  export function serverStatusMiddleware(projectDir?: string, extra?: any): BackendRequestHandler {
@@ -34,6 +35,7 @@ export function getServerStatusData(
34
35
  return _filterNullishValues({
35
36
  nodeProcessStarted: getStartedStr(),
36
37
  deployBuildTime,
38
+ BUILD_VERSION,
37
39
  APP_ENV,
38
40
  GOOGLE_CLOUD_PROJECT,
39
41
  GAE_APPLICATION,
@@ -55,7 +55,7 @@ class AjvValidateRequest {
55
55
  const item: T = mutate ? originalProperty : { ...originalProperty }
56
56
 
57
57
  // Ajv mutates the input
58
- const error = schema.getValidationError(item, {
58
+ const [error, output] = schema.getValidationResult(item, {
59
59
  objectName: `request ${reqProperty}`,
60
60
  })
61
61
 
@@ -63,7 +63,7 @@ class AjvValidateRequest {
63
63
  handleValidationError(error, originalProperty, opt)
64
64
  }
65
65
 
66
- return item
66
+ return output
67
67
  }
68
68
  }
69
69
 
@@ -55,7 +55,7 @@ class ValidateRequest {
55
55
  const originalProperty = req[reqProperty] || {}
56
56
 
57
57
  // Joi does not mutate the input
58
- const { error, value } = getValidationResult(originalProperty, schema, `request ${reqProperty}`)
58
+ const [error, value] = getValidationResult(originalProperty, schema, `request ${reqProperty}`)
59
59
 
60
60
  if (error) {
61
61
  if (opt.redactPaths) {
@@ -54,7 +54,7 @@ class ZodValidateRequest {
54
54
  const originalProperty = req[reqProperty] || {}
55
55
 
56
56
  // Zod does not mutate the input
57
- const { error, data } = zSafeValidate(
57
+ const [error, data] = zSafeValidate(
58
58
  originalProperty,
59
59
  schema,
60
60
  // opt2?.itemName,
@@ -1,150 +0,0 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <title>Login</title>
5
-
6
- <meta charset="utf-8" />
7
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
8
-
9
- <!-- CSS only -->
10
- <link
11
- href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css"
12
- rel="stylesheet"
13
- crossorigin="anonymous"
14
- />
15
- </head>
16
- <body>
17
- <div id="app" style="padding: 40px 50px">
18
- <pre v-if="loading">Checking login...</pre>
19
- <div v-else>
20
- <div v-if="user">
21
- <p>Logged in as:</p>
22
- <pre><mark>{{ user.email }}</mark></pre>
23
- <pre><mark>{{ user.token }}</mark></pre>
24
- <button class="btn btn-primary btn-lg" @click="logout">Logout</button>
25
- </div>
26
- <div v-else>
27
- <button class="btn btn-primary btn-lg" @click="login" id="loginBtn">Login</button>
28
- </div>
29
- </div>
30
- </div>
31
-
32
- <script type="module">
33
- import { createApp } from 'https://cdn.jsdelivr.net/npm/vue@3.2.20/dist/vue.esm-browser.prod.js'
34
- import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.1.2/firebase-app.js'
35
- import {
36
- getAuth,
37
- GoogleAuthProvider,
38
- onAuthStateChanged,
39
- signInWithRedirect,
40
- } from 'https://www.gstatic.com/firebasejs/9.1.2/firebase-auth.js'
41
-
42
- const apiKey = '<%= firebaseApiKey %>'
43
- const authDomain = '<%= firebaseAuthDomain %>'
44
- // const authProvider = '<%= firebaseAuthProvider %>'
45
-
46
- if (!apiKey || !authDomain) {
47
- alert(`Error: 'apiKey' or 'authDomain' is missing!`)
48
- }
49
-
50
- // Initialize Firebase
51
- initializeApp({
52
- apiKey,
53
- authDomain,
54
- })
55
-
56
- const auth = getAuth()
57
- const provider = new GoogleAuthProvider()
58
-
59
- onAuthStateChanged(auth, user => {
60
- // console.log('onAuthStateChanged, user: ', JSON.stringify(user, null, 2))
61
- // console.log('onAuthStateChanged, user: ', user)
62
- onUser(user)
63
- })
64
-
65
- const qs = Object.fromEntries(new URLSearchParams(location.search))
66
- const { autoLogin, logout, returnUrl, adminTokenKey = 'admin_token' } = qs
67
- // console.log(qs)
68
-
69
- const app = createApp({
70
- data() {
71
- return {
72
- loading: 'Loading...',
73
- user: undefined,
74
- }
75
- },
76
- methods: {
77
- login: async function () {
78
- try {
79
- await signInWithRedirect(auth, provider)
80
- } catch (err) {
81
- logError(err)
82
- }
83
- },
84
-
85
- logout: async function () {
86
- try {
87
- await auth.signOut()
88
-
89
- await postToken('logout') // magic string
90
-
91
- if (logout && returnUrl) {
92
- alert('Logged out, redurecting back...')
93
- location.href = returnUrl
94
- }
95
- } catch (err) {
96
- logError(err)
97
- }
98
- },
99
- },
100
- }).mount('#app')
101
-
102
- if (logout) app.logout()
103
-
104
- ////
105
-
106
- async function onUser(user) {
107
- try {
108
- // alert('onUser')
109
- app.user = user
110
- app.loading = false
111
- if (!user) {
112
- if (autoLogin) app.login()
113
- } else {
114
- const token = await auth.currentUser.getIdToken()
115
-
116
- // alert('idToken')
117
- // console.log(idToken)
118
- app.user = Object.assign({}, app.user, {
119
- token,
120
- })
121
-
122
- await postToken(token)
123
-
124
- // Redirect if needed
125
- if (returnUrl) {
126
- // alert(`Logged in as ${app.user.email}, redirecting back...`)
127
- location.href = returnUrl
128
- }
129
- }
130
- } catch (err) {
131
- logError(err)
132
- }
133
- }
134
-
135
- function logError(err) {
136
- console.error(err)
137
- alert('Error\n ' + JSON.stringify(err, null, 2))
138
- }
139
-
140
- async function postToken(token) {
141
- await fetch(`/admin/login`, {
142
- method: 'post',
143
- headers: {
144
- Authentication: token,
145
- },
146
- })
147
- }
148
- </script>
149
- </body>
150
- </html>
@@ -1,7 +0,0 @@
1
- #
2
- # .gcloudignore
3
- #
4
- .gcloudignore
5
- .git
6
- .gitignore
7
- node_modules/