@seidor-cloud-produtos/orbit-backend-lib 2.0.11 → 2.0.13

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.
@@ -18,6 +18,10 @@ export type LogProperties = {
18
18
  dateTime?: Date;
19
19
  expirationHours?: number;
20
20
  };
21
+ export interface LogOptions {
22
+ throwOnError?: boolean;
23
+ retryStrategy?: (data: any, props?: LogProperties, options?: LogOptions) => Promise<void>;
24
+ }
21
25
  export default abstract class LoggerGateway {
22
- abstract log(data: any, props?: LogProperties): Promise<void>;
26
+ abstract log(data: any, props?: LogProperties, options?: LogOptions): Promise<void>;
23
27
  }
@@ -1,6 +1,6 @@
1
- import LoggerGateway, { LogProperties } from '../../application/logger';
1
+ import LoggerGateway, { LogOptions, LogProperties } from '../../application/logger';
2
2
  export declare class LoggerInMemory extends LoggerGateway {
3
- log(payload: any, props?: Partial<LogProperties>): Promise<void>;
3
+ log(payload: any, props?: Partial<LogProperties>, options?: LogOptions): Promise<void>;
4
4
  private info;
5
5
  private error;
6
6
  private warning;
@@ -6,7 +6,7 @@ const env_1 = require("../../infra/environment/env");
6
6
  const logger_1 = tslib_1.__importDefault(require("../../application/logger"));
7
7
  const types_1 = require("../environment/types");
8
8
  class LoggerInMemory extends logger_1.default {
9
- async log(payload, props) {
9
+ async log(payload, props, options) {
10
10
  switch (props?.level) {
11
11
  case types_1.LOG_LEVEL.error:
12
12
  return this.error(payload, props);
@@ -1,5 +1,5 @@
1
1
  import Queue from '../../application/queue/queue';
2
- import LoggerGateway, { LogProperties } from '../../application/logger';
2
+ import LoggerGateway, { LogOptions, LogProperties } from '../../application/logger';
3
3
  import DomainEvent from '../../domain/events/domain-event';
4
4
  export type LogPayload = Omit<LogProperties, 'expirationHours'> & {
5
5
  expirationDate: Date;
@@ -14,7 +14,7 @@ export default class LogEvent implements DomainEvent {
14
14
  export declare class LoggerOrbit extends LoggerGateway {
15
15
  private queue;
16
16
  constructor(queue: Queue);
17
- log(data: any, props?: LogProperties): Promise<void>;
17
+ log(data: any, props?: LogProperties, options?: LogOptions): Promise<void>;
18
18
  private buildPayload;
19
19
  private buildProps;
20
20
  protected buildDefaultProps(): LogProperties;
@@ -21,17 +21,22 @@ class LoggerOrbit extends logger_1.default {
21
21
  super();
22
22
  this.queue = queue;
23
23
  }
24
- async log(data, props) {
24
+ async log(data, props, options) {
25
25
  const payload = this.buildPayload(data, props);
26
26
  try {
27
27
  await this.queue.publish('logservice.common.direct', new LogEvent(payload));
28
28
  }
29
29
  catch (error) {
30
- await logger_in_memory_1.default.log({
30
+ logger_in_memory_1.default.log({
31
31
  message: 'SEND LOG ERROR: ',
32
32
  error,
33
33
  });
34
- throw new log_error_1.default();
34
+ if (options?.retryStrategy) {
35
+ await options.retryStrategy(data, props, options);
36
+ }
37
+ if (options?.throwOnError) {
38
+ throw new log_error_1.default();
39
+ }
35
40
  }
36
41
  }
37
42
  buildPayload(data, props) {
@@ -90,11 +90,17 @@ class AmqpQueue {
90
90
  }
91
91
  }
92
92
  async publish(exchangeName, domainEvent, configs) {
93
+ const buildedConfigs = configs || {};
94
+ const isValidExpiration = configs?.expiration === null || typeof configs?.expiration === 'number';
95
+ if (!isValidExpiration) {
96
+ const fourTeenDaysExpiration = 14 * 24 * 60 * 60 * 1000;
97
+ buildedConfigs.expiration = fourTeenDaysExpiration;
98
+ }
93
99
  try {
94
100
  const channel = await this.connection.createChannel();
95
101
  channel.publish(exchangeName, domainEvent.name || '', Buffer.from(JSON.stringify(domainEvent)), {
96
102
  persistent: true,
97
- ...(configs || {}),
103
+ ...buildedConfigs,
98
104
  });
99
105
  await channel.close();
100
106
  }
@@ -102,7 +108,7 @@ class AmqpQueue {
102
108
  this.messagesInMemory.push({
103
109
  exchangeName,
104
110
  domainEvent,
105
- configs,
111
+ configs: buildedConfigs,
106
112
  });
107
113
  console.log({
108
114
  message: `⛔ Error to publish messages to: ${domainEvent.name}. Publish in memory!`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seidor-cloud-produtos/orbit-backend-lib",
3
- "version": "2.0.11",
3
+ "version": "2.0.13",
4
4
  "description": "Internal lib for backend components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",