@naturalcycles/backend-lib 4.11.0 → 4.13.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.
package/dist/index.d.ts CHANGED
@@ -23,6 +23,7 @@ export * from './server/reqValidationMiddleware';
23
23
  export * from './server/simpleRequestLoggerMiddleware';
24
24
  export * from './server/serverStatusMiddleware';
25
25
  export * from './server/validateMiddleware';
26
+ export * from './server/zodValidateMiddleware';
26
27
  export * from './server/request.log.util';
27
28
  export * from './server/startServer';
28
29
  export * from './server/startServer.model';
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ tslib_1.__exportStar(require("./server/reqValidationMiddleware"), exports);
28
28
  tslib_1.__exportStar(require("./server/simpleRequestLoggerMiddleware"), exports);
29
29
  tslib_1.__exportStar(require("./server/serverStatusMiddleware"), exports);
30
30
  tslib_1.__exportStar(require("./server/validateMiddleware"), exports);
31
+ tslib_1.__exportStar(require("./server/zodValidateMiddleware"), exports);
31
32
  tslib_1.__exportStar(require("./server/request.log.util"), exports);
32
33
  tslib_1.__exportStar(require("./server/startServer"), exports);
33
34
  tslib_1.__exportStar(require("./server/startServer.model"), exports);
@@ -5,7 +5,7 @@ const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
6
6
  const deployInfo_util_1 = require("./deployInfo.util");
7
7
  const { versions } = process;
8
- const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, APP_ENV } = process.env;
8
+ const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, APP_ENV, NODE_OPTIONS } = process.env;
9
9
  function serverStatusMiddleware(projectDir, extra) {
10
10
  return async (req, res) => {
11
11
  res.json(getServerStatusData(projectDir, extra));
@@ -17,7 +17,7 @@ function getServerStatusData(projectDir = process.cwd(), extra) {
17
17
  const t = (0, js_lib_1.localTime)(ts);
18
18
  const deployBuildTime = t.toPretty();
19
19
  const buildInfo = [t.toStringCompact(), gitBranch, gitRev].filter(Boolean).join('_');
20
- return (0, js_lib_1._filterFalsyValues)({
20
+ return (0, js_lib_1._filterNullishValues)({
21
21
  started: getStartedStr(),
22
22
  deployBuildTime,
23
23
  APP_ENV,
@@ -30,6 +30,8 @@ function getServerStatusData(projectDir = process.cwd(), extra) {
30
30
  cpuAvg: nodejs_lib_1.processSharedUtil.cpuAvg(),
31
31
  // resourceUsage: process.resourceUsage?.(),
32
32
  versions,
33
+ NODE_OPTIONS,
34
+ fetch: typeof globalThis.fetch === 'function',
33
35
  ...extra,
34
36
  });
35
37
  }
@@ -0,0 +1,10 @@
1
+ import { ZodSchema, ZodValidationError } from '@naturalcycles/js-lib';
2
+ import { BackendRequestHandler } from './server.model';
3
+ import { ReqValidationOptions } from './reqValidationMiddleware';
4
+ /**
5
+ * Validates req property (body, params or query).
6
+ * Supports Joi schema or AjvSchema (from nodejs-lib).
7
+ * Able to redact sensitive data from the error message.
8
+ * Throws http 400 on error.
9
+ */
10
+ export declare function zodReqValidate(prop: 'body' | 'params' | 'query', schema: ZodSchema, opt?: ReqValidationOptions<ZodValidationError<any>>): BackendRequestHandler;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zodReqValidate = void 0;
4
+ const js_lib_1 = require("@naturalcycles/js-lib");
5
+ const REDACTED = 'REDACTED';
6
+ /**
7
+ * Validates req property (body, params or query).
8
+ * Supports Joi schema or AjvSchema (from nodejs-lib).
9
+ * Able to redact sensitive data from the error message.
10
+ * Throws http 400 on error.
11
+ */
12
+ function zodReqValidate(prop, schema, opt = {}) {
13
+ const reportPredicate = typeof opt.report === 'function' ? opt.report : () => !!opt.report;
14
+ return (req, res, next) => {
15
+ const { error } = (0, js_lib_1.zSafeValidate)(req[prop], schema);
16
+ if (!error) {
17
+ return next();
18
+ }
19
+ const report = reportPredicate(error);
20
+ if (opt.redactPaths) {
21
+ redact(opt.redactPaths, req[prop], error);
22
+ }
23
+ return next(new js_lib_1.HttpError(error.message, {
24
+ httpStatusCode: 400,
25
+ report,
26
+ }));
27
+ };
28
+ }
29
+ exports.zodReqValidate = zodReqValidate;
30
+ /**
31
+ * Mutates error
32
+ */
33
+ function redact(redactPaths, obj, error) {
34
+ redactPaths
35
+ .map(path => (0, js_lib_1._get)(obj, path))
36
+ .filter(Boolean)
37
+ .forEach(secret => {
38
+ error.message = error.message.replace(secret, REDACTED);
39
+ });
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "4.11.0",
3
+ "version": "4.13.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install && patch-package",
6
6
  "serve": "APP_ENV=dev nodemon",
package/src/index.ts CHANGED
@@ -23,6 +23,7 @@ export * from './server/reqValidationMiddleware'
23
23
  export * from './server/simpleRequestLoggerMiddleware'
24
24
  export * from './server/serverStatusMiddleware'
25
25
  export * from './server/validateMiddleware'
26
+ export * from './server/zodValidateMiddleware'
26
27
  export * from './server/request.log.util'
27
28
  export * from './server/startServer'
28
29
  export * from './server/startServer.model'
@@ -1,10 +1,10 @@
1
- import { _filterFalsyValues, localTime } from '@naturalcycles/js-lib'
1
+ import { _filterNullishValues, localTime } from '@naturalcycles/js-lib'
2
2
  import { memoryUsageFull, processSharedUtil } from '@naturalcycles/nodejs-lib'
3
3
  import { getDeployInfo } from './deployInfo.util'
4
4
  import { BackendRequestHandler } from './server.model'
5
5
 
6
6
  const { versions } = process
7
- const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, APP_ENV } = process.env
7
+ const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, APP_ENV, NODE_OPTIONS } = process.env
8
8
 
9
9
  export function serverStatusMiddleware(projectDir?: string, extra?: any): BackendRequestHandler {
10
10
  return async (req, res) => {
@@ -21,7 +21,7 @@ export function getServerStatusData(
21
21
  const deployBuildTime = t.toPretty()
22
22
  const buildInfo = [t.toStringCompact(), gitBranch, gitRev].filter(Boolean).join('_')
23
23
 
24
- return _filterFalsyValues({
24
+ return _filterNullishValues({
25
25
  started: getStartedStr(),
26
26
  deployBuildTime,
27
27
  APP_ENV,
@@ -34,6 +34,8 @@ export function getServerStatusData(
34
34
  cpuAvg: processSharedUtil.cpuAvg(),
35
35
  // resourceUsage: process.resourceUsage?.(),
36
36
  versions,
37
+ NODE_OPTIONS,
38
+ fetch: typeof globalThis.fetch === 'function',
37
39
  ...extra,
38
40
  })
39
41
  }
@@ -0,0 +1,57 @@
1
+ import {
2
+ HttpError,
3
+ _get,
4
+ ZodSchema,
5
+ ZodValidationError,
6
+ zSafeValidate,
7
+ } from '@naturalcycles/js-lib'
8
+ import { BackendRequestHandler } from './server.model'
9
+ import { ReqValidationOptions } from './reqValidationMiddleware'
10
+
11
+ const REDACTED = 'REDACTED'
12
+
13
+ /**
14
+ * Validates req property (body, params or query).
15
+ * Supports Joi schema or AjvSchema (from nodejs-lib).
16
+ * Able to redact sensitive data from the error message.
17
+ * Throws http 400 on error.
18
+ */
19
+ export function zodReqValidate(
20
+ prop: 'body' | 'params' | 'query',
21
+ schema: ZodSchema,
22
+ opt: ReqValidationOptions<ZodValidationError<any>> = {},
23
+ ): BackendRequestHandler {
24
+ const reportPredicate = typeof opt.report === 'function' ? opt.report : () => !!opt.report
25
+
26
+ return (req, res, next) => {
27
+ const { error } = zSafeValidate(req[prop], schema)
28
+ if (!error) {
29
+ return next()
30
+ }
31
+
32
+ const report = reportPredicate(error)
33
+
34
+ if (opt.redactPaths) {
35
+ redact(opt.redactPaths, req[prop], error)
36
+ }
37
+
38
+ return next(
39
+ new HttpError(error.message, {
40
+ httpStatusCode: 400,
41
+ report,
42
+ }),
43
+ )
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Mutates error
49
+ */
50
+ function redact(redactPaths: string[], obj: any, error: Error): void {
51
+ redactPaths
52
+ .map(path => _get(obj, path) as string)
53
+ .filter(Boolean)
54
+ .forEach(secret => {
55
+ error.message = error.message.replace(secret, REDACTED)
56
+ })
57
+ }