@naturalcycles/backend-lib 9.67.0 → 9.69.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.
@@ -21,6 +21,7 @@ export function requireAdminPermissions(adminService, reqPermissions = [], cfg =
21
21
  return next();
22
22
  try {
23
23
  await adminService.requirePermissions(req, reqPermissions, {}, andComparison);
24
+ req.isAuthenticatedAdminRequest = true;
24
25
  return next();
25
26
  }
26
27
  catch (err) {
@@ -25,8 +25,14 @@ export function getRequest() {
25
25
  * @experimental
26
26
  */
27
27
  export function getRequestLogger() {
28
- return (storage().getStore()?.req ||
29
- (isGAE || isCloudRun ? gcpStructuredLogger : isCI ? ciLogger : devLogger));
28
+ const req = storage().getStore()?.req;
29
+ if (req)
30
+ return req;
31
+ if (isGAE || isCloudRun)
32
+ return gcpStructuredLogger;
33
+ if (isCI)
34
+ return ciLogger;
35
+ return devLogger;
30
36
  }
31
37
  /**
32
38
  * CommonLogger implementation that is Request-bound.
@@ -6,7 +6,7 @@ export interface GenericErrorMiddlewareCfg {
6
6
  * Generic hook that can be used to **mutate** errors before they are returned to client.
7
7
  * This function does not affect data sent to sentry.
8
8
  */
9
- formatError?: (err: ErrorObject) => void;
9
+ formatError?: (err: ErrorObject, req: BackendRequest) => void;
10
10
  }
11
11
  export interface ErrorReportingService {
12
12
  /**
@@ -58,7 +58,7 @@ export function respondWithError(req, res, err) {
58
58
  errorId,
59
59
  headersSent: headersSent || undefined,
60
60
  });
61
- formatError?.(httpError); // Mutates
61
+ formatError?.(httpError, req); // Mutates the error
62
62
  res.status(httpError.data.backendResponseStatusCode).json({
63
63
  error: httpError,
64
64
  });
@@ -38,6 +38,11 @@ export interface BackendRequest<BODY = unknown> extends Request {
38
38
  */
39
39
  requestTimeout?: NodeJS.Timeout;
40
40
  bodyParserTimeout?: NodeJS.Timeout;
41
+ /**
42
+ * When `true`, the request originated from an authenticated admin account.
43
+ * When not `true`, then we can make no claim either way.
44
+ */
45
+ isAuthenticatedAdminRequest?: true;
41
46
  }
42
47
  export type BackendResponse = Response;
43
48
  export type BackendRequestHandler = (req: BackendRequest, res: BackendResponse, next: NextFunction) => Promisable<any>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.67.0",
4
+ "version": "9.69.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/db-lib": "^10",
7
7
  "@naturalcycles/js-lib": "^15",
@@ -75,6 +75,7 @@ export function requireAdminPermissions(
75
75
 
76
76
  try {
77
77
  await adminService.requirePermissions(req, reqPermissions, {}, andComparison)
78
+ req.isAuthenticatedAdminRequest = true
78
79
  return next()
79
80
  } catch (err) {
80
81
  if (err instanceof AppError && err.data.adminAuthRequired) {
@@ -37,10 +37,11 @@ export function getRequest(): BackendRequest | undefined {
37
37
  * @experimental
38
38
  */
39
39
  export function getRequestLogger(): CommonLogger {
40
- return (
41
- storage().getStore()?.req ||
42
- (isGAE || isCloudRun ? gcpStructuredLogger : isCI ? ciLogger : devLogger)
43
- )
40
+ const req = storage().getStore()?.req
41
+ if (req) return req
42
+ if (isGAE || isCloudRun) return gcpStructuredLogger
43
+ if (isCI) return ciLogger
44
+ return devLogger
44
45
  }
45
46
 
46
47
  /**
@@ -10,7 +10,7 @@ export interface GenericErrorMiddlewareCfg {
10
10
  * Generic hook that can be used to **mutate** errors before they are returned to client.
11
11
  * This function does not affect data sent to sentry.
12
12
  */
13
- formatError?: (err: ErrorObject) => void
13
+ formatError?: (err: ErrorObject, req: BackendRequest) => void
14
14
  }
15
15
 
16
16
  export interface ErrorReportingService {
@@ -95,7 +95,7 @@ export function respondWithError(req: BackendRequest, res: BackendResponse, err:
95
95
  headersSent: headersSent || undefined,
96
96
  })
97
97
 
98
- formatError?.(httpError) // Mutates
98
+ formatError?.(httpError, req) // Mutates the error
99
99
 
100
100
  res.status(httpError.data.backendResponseStatusCode).json({
101
101
  error: httpError,
@@ -44,6 +44,12 @@ export interface BackendRequest<BODY = unknown> extends Request {
44
44
  requestTimeout?: NodeJS.Timeout
45
45
 
46
46
  bodyParserTimeout?: NodeJS.Timeout
47
+
48
+ /**
49
+ * When `true`, the request originated from an authenticated admin account.
50
+ * When not `true`, then we can make no claim either way.
51
+ */
52
+ isAuthenticatedAdminRequest?: true
47
53
  }
48
54
 
49
55
  export type BackendResponse = Response