@mondart/nestjs-common-module 2.6.5 → 2.6.6

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.
@@ -9,18 +9,23 @@ exports.IamContext = (0, common_1.createParamDecorator)((data, ctx) => {
9
9
  return (0, exports.getIamContext)(request);
10
10
  });
11
11
  const getIamContext = (request) => {
12
- const injectedPayload = request?.headers?.['injectedpayload'];
13
- const ip = request?.headers['x-forwarded-for'] ||
12
+ const headers = typeof request?.headers === 'string'
13
+ ? JSON.parse(request?.headers)
14
+ : request?.headers;
15
+ const injectedPayload = headers?.['injectedpayload'];
16
+ const ip = headers?.['x-forwarded-for'] ||
17
+ headers?.['host'] ||
18
+ headers?.address ||
14
19
  request?.socket?.remoteAddress ||
15
20
  null;
16
- const requestId = request?.headers['request-id'] ?? (0, crypto_1.randomUUID)();
17
- const timestamp = new Date(request?.headers?.['timestamp']);
21
+ const requestId = headers?.['request-id'] ?? (0, crypto_1.randomUUID)();
22
+ const timestamp = new Date(headers?.['timestamp'] || headers?.['time'] || Date.now());
18
23
  let isAgent = false;
19
24
  let user;
20
25
  let agent;
21
26
  if (injectedPayload) {
22
27
  const data = JSON.parse(injectedPayload);
23
- const token = request?.headers?.['authorization'];
28
+ const token = headers?.['authorization'];
24
29
  if (data) {
25
30
  if (!data?.is_agent && (data?.contact_id || data?.store_id)) {
26
31
  user = {
@@ -40,7 +45,7 @@ const getIamContext = (request) => {
40
45
  };
41
46
  }
42
47
  }
43
- delete request?.headers?.['authorization'];
48
+ delete headers?.['authorization'];
44
49
  }
45
50
  return new iam_context_dto_1.IamContextDto({
46
51
  ip,
@@ -29,9 +29,18 @@ let GlobalExceptionFilter = class GlobalExceptionFilter {
29
29
  Sentry.captureException(exception, exception?.response);
30
30
  const context = host.switchToHttp();
31
31
  const type = host?.getType();
32
- this.logger.error(`exceptionName: ${exception?.name}`, JSON.stringify(exception) ??
33
- exception?.message ??
34
- 'Unknown exception occurred.');
32
+ const message = typeof exception?.message === 'string'
33
+ ? exception?.message !== ''
34
+ ? exception?.message
35
+ : 'Unknown exception occurred.'
36
+ : (typeof exception?.message === 'object' &&
37
+ Object.keys(exception?.message)?.length !== 0) ||
38
+ (Array.isArray(exception?.message) &&
39
+ exception?.message?.length !== 0)
40
+ ? JSON.stringify(exception?.message)
41
+ : 'Unknown exception occurred.';
42
+ this.logger.error(`exceptionName: ${exception?.name} - ${message}`);
43
+ this.logger.debug(exception?.stack);
35
44
  if (type === 'rpc') {
36
45
  const exceptionsName = Object.values(exceptions).map((exception) => exception.name);
37
46
  if (exceptionsName.includes(exception.name)) {
@@ -33,15 +33,18 @@ let CaptchaGuard = class CaptchaGuard {
33
33
  const captchaSettings = this.getCaptchaDetails(context);
34
34
  const request = context.switchToHttp().getRequest();
35
35
  const iamContext = (0, decorators_1.getIamContext)(request);
36
- if (iamContext?.agentId || iamContext?.userId)
36
+ if (iamContext?.agentId || iamContext?.userId || !captchaSettings)
37
37
  return true;
38
38
  if (captchaSettings && captchaSettings.executeAfter) {
39
- const ip = request.headers['x-forwarded-for'] || request.socket.remoteAddress;
39
+ const ip = iamContext?.ip || null;
40
40
  const result = await this.incExecutionTimes(ip, captchaSettings.executeAfter);
41
41
  if (result)
42
42
  return true;
43
43
  }
44
- const text = request.headers['captcha-token'];
44
+ const headers = typeof request?.headers === 'string'
45
+ ? JSON.parse(request?.headers)
46
+ : request?.headers;
47
+ const text = headers?.['captcha-token'];
45
48
  if (!text)
46
49
  throw new captcha_exception_1.InvalidCaptcha();
47
50
  await this.isCaptchaValid(text);