@naturalcycles/backend-lib 8.0.1 → 8.0.3

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
@@ -8,7 +8,6 @@ export * from './sentry/sentry.shared.service';
8
8
  export * from './server/asyncLocalStorageMiddleware';
9
9
  export * from './server/basicAuthMiddleware';
10
10
  export * from './server/bodyParserTimeoutMiddleware';
11
- export * from './server/catchWrapper';
12
11
  export * from './server/createDefaultApp';
13
12
  export * from './server/createDefaultApp.model';
14
13
  export * from './server/deployInfo.util';
package/dist/index.js CHANGED
@@ -13,7 +13,6 @@ tslib_1.__exportStar(require("./sentry/sentry.shared.service"), exports);
13
13
  tslib_1.__exportStar(require("./server/asyncLocalStorageMiddleware"), exports);
14
14
  tslib_1.__exportStar(require("./server/basicAuthMiddleware"), exports);
15
15
  tslib_1.__exportStar(require("./server/bodyParserTimeoutMiddleware"), exports);
16
- tslib_1.__exportStar(require("./server/catchWrapper"), exports);
17
16
  tslib_1.__exportStar(require("./server/createDefaultApp"), exports);
18
17
  tslib_1.__exportStar(require("./server/createDefaultApp.model"), exports);
19
18
  tslib_1.__exportStar(require("./server/deployInfo.util"), exports);
@@ -31,7 +31,7 @@ function validateObject(prop, schema, opt = {}) {
31
31
  });
32
32
  const reportPredicate = typeof opt.report === 'function' ? opt.report : () => opt.report;
33
33
  return (req, _res, next) => {
34
- const error = ajvSchema.getValidationError(req[prop]);
34
+ const error = ajvSchema.getValidationError(req[prop] || {});
35
35
  if (error) {
36
36
  const report = reportPredicate(error);
37
37
  if (opt.redactPaths) {
@@ -42,7 +42,7 @@ class ValidateRequest {
42
42
  return this.validate(req, 'headers', schema, options);
43
43
  }
44
44
  validate(req, reqProperty, schema, opt = {}) {
45
- const { value, error } = (0, nodejs_lib_1.getValidationResult)(req[reqProperty], schema, `request ${reqProperty}`);
45
+ const { value, error } = (0, nodejs_lib_1.getValidationResult)(req[reqProperty] || {}, schema, `request ${reqProperty}`);
46
46
  if (error) {
47
47
  let report;
48
48
  if (typeof opt.report === 'boolean') {
@@ -12,7 +12,7 @@ const REDACTED = 'REDACTED';
12
12
  function zodReqValidate(prop, schema, opt = {}) {
13
13
  const reportPredicate = typeof opt.report === 'function' ? opt.report : () => opt.report;
14
14
  return (req, _res, next) => {
15
- const { error } = (0, js_lib_1.zSafeValidate)(req[prop], schema);
15
+ const { error } = (0, js_lib_1.zSafeValidate)(req[prop] || {}, schema);
16
16
  if (!error) {
17
17
  return next();
18
18
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "8.0.1",
3
+ "version": "8.0.3",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build",
@@ -8,7 +8,7 @@
8
8
  "lint": "dev-lib lint",
9
9
  "bt": "dev-lib bt",
10
10
  "lbt": "dev-lib lbt",
11
- "dev": "APP_ENV=dev node -r ts-node/register --watch ./src/test/server/server.ts",
11
+ "dev": "APP_ENV=dev tsx watch src/test/server/server.ts",
12
12
  "docs-serve": "NODE_OPTIONS=--openssl-legacy-provider vuepress dev docs",
13
13
  "docs-build": "NODE_OPTIONS=--openssl-legacy-provider vuepress build docs",
14
14
  "deploy-gae": "yarn tsx ./src/bin/deploy-gae.ts",
package/src/index.ts CHANGED
@@ -8,7 +8,6 @@ export * from './sentry/sentry.shared.service'
8
8
  export * from './server/asyncLocalStorageMiddleware'
9
9
  export * from './server/basicAuthMiddleware'
10
10
  export * from './server/bodyParserTimeoutMiddleware'
11
- export * from './server/catchWrapper'
12
11
  export * from './server/createDefaultApp'
13
12
  export * from './server/createDefaultApp.model'
14
13
  export * from './server/deployInfo.util'
@@ -52,7 +52,7 @@ function validateObject(
52
52
  typeof opt.report === 'function' ? opt.report : () => opt.report as boolean | undefined
53
53
 
54
54
  return (req, _res, next) => {
55
- const error = ajvSchema.getValidationError(req[prop])
55
+ const error = ajvSchema.getValidationError(req[prop] || {})
56
56
  if (error) {
57
57
  const report = reportPredicate(error)
58
58
 
@@ -89,7 +89,11 @@ class ValidateRequest {
89
89
  schema: AnySchema<T>,
90
90
  opt: ReqValidationOptions<JoiValidationError> = {},
91
91
  ): T {
92
- const { value, error } = getValidationResult(req[reqProperty], schema, `request ${reqProperty}`)
92
+ const { value, error } = getValidationResult(
93
+ req[reqProperty] || {},
94
+ schema,
95
+ `request ${reqProperty}`,
96
+ )
93
97
  if (error) {
94
98
  let report: boolean | undefined
95
99
  if (typeof opt.report === 'boolean') {
@@ -19,7 +19,7 @@ export function zodReqValidate(
19
19
  typeof opt.report === 'function' ? opt.report : () => opt.report as boolean | undefined
20
20
 
21
21
  return (req, _res, next) => {
22
- const { error } = zSafeValidate(req[prop], schema)
22
+ const { error } = zSafeValidate(req[prop] || {}, schema)
23
23
  if (!error) {
24
24
  return next()
25
25
  }
@@ -1,2 +0,0 @@
1
- import { BackendRequestHandler } from './server.model';
2
- export declare const catchWrapper: (fn: BackendRequestHandler) => BackendRequestHandler;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.catchWrapper = void 0;
4
- // https://strongloop.com/strongblog/async-error-handling-expressjs-es7-promises-generators/
5
- // https://stackoverflow.com/a/43564267/4919972
6
- const catchWrapper = (fn) => async (req, res, next) => {
7
- try {
8
- await fn(req, res, next);
9
- }
10
- catch (err) {
11
- next(err);
12
- }
13
- };
14
- exports.catchWrapper = catchWrapper;
@@ -1,13 +0,0 @@
1
- import { BackendRequestHandler } from './server.model'
2
-
3
- // https://strongloop.com/strongblog/async-error-handling-expressjs-es7-promises-generators/
4
- // https://stackoverflow.com/a/43564267/4919972
5
- export const catchWrapper =
6
- (fn: BackendRequestHandler): BackendRequestHandler =>
7
- async (req, res, next) => {
8
- try {
9
- await fn(req, res, next)
10
- } catch (err) {
11
- next(err)
12
- }
13
- }