@naturalcycles/backend-lib 4.9.0 → 4.10.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.
@@ -29,6 +29,6 @@ function requireSecureHeaderOrAdmin(cfg, reqPermissions) {
29
29
  }));
30
30
  }
31
31
  // Forward to AdminMiddleware (try Admin)
32
- requireAdmin(req, res, next);
32
+ await requireAdmin(req, res, next);
33
33
  };
34
34
  }
@@ -15,7 +15,7 @@ let formatError;
15
15
  * Sends json payload as ErrorResponse, transformed via errorSharedUtil.
16
16
  */
17
17
  function genericErrorMiddleware(cfg = {}) {
18
- sentryService || (sentryService = cfg.sentryService);
18
+ sentryService ||= cfg.sentryService;
19
19
  reportOnly5xx = cfg.reportOnly5xx || false;
20
20
  formatError = cfg.formatError;
21
21
  return (err, req, res, _next) => {
@@ -35,7 +35,6 @@ function genericErrorMiddleware(cfg = {}) {
35
35
  }
36
36
  exports.genericErrorMiddleware = genericErrorMiddleware;
37
37
  function respondWithError(req, res, err) {
38
- var _a, _b;
39
38
  const { headersSent } = res;
40
39
  if (headersSent) {
41
40
  req.error(`after headersSent`, err);
@@ -54,9 +53,9 @@ function respondWithError(req, res, err) {
54
53
  return;
55
54
  const httpError = (0, js_lib_1._errorToErrorObject)(originalError, includeErrorStack);
56
55
  httpError.data.errorId = errorId;
57
- (_a = httpError.data).httpStatusCode || (_a.httpStatusCode = 500); // default to 500
56
+ httpError.data.httpStatusCode ||= 500; // default to 500
58
57
  httpError.data.headersSent = headersSent || undefined;
59
- (_b = httpError.data).report || (_b.report = undefined); // set to undefined if false
58
+ httpError.data.report ||= undefined; // set to undefined if false
60
59
  (0, js_lib_1._filterUndefinedValues)(httpError.data, true);
61
60
  formatError?.(httpError); // Mutates
62
61
  res.status(httpError.data.httpStatusCode).json({
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import { CommonLogFunction } from '@naturalcycles/js-lib';
3
- import { IRouter, Request, NextFunction, Response, Application } from 'express';
2
+ import type { CommonLogFunction, Promisable } from '@naturalcycles/js-lib';
3
+ import type { IRouter, Request, NextFunction, Response, Application } from 'express';
4
4
  /**
5
5
  * Use this interface instead of express.Request in cases when TypeScript gives an error, because it haven't "included" this very file.
6
6
  *
@@ -21,8 +21,8 @@ export interface BackendRequest extends Request {
21
21
  bodyParserTimeout?: NodeJS.Timeout;
22
22
  }
23
23
  export type BackendResponse = Response;
24
- export type BackendRequestHandler = (req: BackendRequest, res: BackendResponse, next: NextFunction) => void;
25
- export type BackendErrorRequestHandler = (err: any, req: BackendRequest, res: BackendResponse, next: NextFunction) => void;
24
+ export type BackendRequestHandler = (req: BackendRequest, res: BackendResponse, next: NextFunction) => Promisable<void>;
25
+ export type BackendErrorRequestHandler = (err: any, req: BackendRequest, res: BackendResponse, next: NextFunction) => Promisable<void>;
26
26
  export type BackendRouter = IRouter;
27
27
  export type BackendApplication = Application;
28
28
  declare module 'http' {
@@ -78,12 +78,12 @@ function serverStatsMiddleware() {
78
78
  const now = Date.now();
79
79
  const latency = now - started;
80
80
  const endpoint = (0, request_util_1.getRequestEndpoint)(req);
81
- serverStatsMap[endpoint] || (serverStatsMap[endpoint] = {
81
+ serverStatsMap[endpoint] ||= {
82
82
  stack: new js_lib_1.NumberStack(SIZE),
83
83
  '2xx': 0,
84
84
  '4xx': 0,
85
85
  '5xx': 0,
86
- });
86
+ };
87
87
  serverStatsMap[endpoint].stack.push(latency);
88
88
  if (res.statusCode) {
89
89
  serverStatsMap[endpoint][getStatusFamily(res.statusCode)]++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "4.9.0",
3
+ "version": "4.10.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install && patch-package",
6
6
  "serve": "APP_ENV=dev nodemon",
@@ -52,6 +52,6 @@ function requireSecureHeaderOrAdmin(
52
52
  }
53
53
 
54
54
  // Forward to AdminMiddleware (try Admin)
55
- requireAdmin(req, res, next)
55
+ await requireAdmin(req, res, next)
56
56
  }
57
57
  }
@@ -1,5 +1,5 @@
1
- import { CommonLogFunction } from '@naturalcycles/js-lib'
2
- import { IRouter, Request, NextFunction, Response, Application } from 'express'
1
+ import type { CommonLogFunction, Promisable } from '@naturalcycles/js-lib'
2
+ import type { IRouter, Request, NextFunction, Response, Application } from 'express'
3
3
 
4
4
  /**
5
5
  * Use this interface instead of express.Request in cases when TypeScript gives an error, because it haven't "included" this very file.
@@ -30,14 +30,14 @@ export type BackendRequestHandler = (
30
30
  req: BackendRequest,
31
31
  res: BackendResponse,
32
32
  next: NextFunction,
33
- ) => void
33
+ ) => Promisable<void>
34
34
 
35
35
  export type BackendErrorRequestHandler = (
36
36
  err: any,
37
37
  req: BackendRequest,
38
38
  res: BackendResponse,
39
39
  next: NextFunction,
40
- ) => void
40
+ ) => Promisable<void>
41
41
 
42
42
  export type BackendRouter = IRouter
43
43
  export type BackendApplication = Application