@seidor-cloud-produtos/orbit-backend-lib 2.0.92 → 2.0.93

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.
@@ -0,0 +1,10 @@
1
+ import { AuthorizerHeaders } from '../../infra/http/handle-authorizer-headers';
2
+ import LoggerGateway, { Trace } from '.';
3
+ import { LogOrbitParams } from '../../infra/logger/logger-orbit';
4
+ export declare abstract class BaseAuditLogger {
5
+ protected readonly gateway: LoggerGateway;
6
+ protected defaultExpirationHours: number;
7
+ constructor(gateway: LoggerGateway);
8
+ protected buildParams(type: string, authorizerHeaders: AuthorizerHeaders, trace: Trace): LogOrbitParams;
9
+ protected info(payload: any, params: LogOrbitParams): Promise<void>;
10
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseAuditLogger = void 0;
4
+ const _1 = require(".");
5
+ const FIVE_YEARS_IN_HOURS = 24 * 365 * 5;
6
+ class BaseAuditLogger {
7
+ gateway;
8
+ defaultExpirationHours = FIVE_YEARS_IN_HOURS;
9
+ constructor(gateway) {
10
+ this.gateway = gateway;
11
+ }
12
+ buildParams(type, authorizerHeaders, trace) {
13
+ trace?.ids.push(authorizerHeaders.traceId);
14
+ return {
15
+ type,
16
+ trace,
17
+ actor: authorizerHeaders.entityId,
18
+ tenantId: authorizerHeaders.tenantId,
19
+ actorType: _1.ActorEnum.USER,
20
+ category: _1.CategoryEnum.BUSINESS,
21
+ expirationHours: this.defaultExpirationHours,
22
+ };
23
+ }
24
+ async info(payload, params) {
25
+ return await this.gateway.info({ payload }, params);
26
+ }
27
+ }
28
+ exports.BaseAuditLogger = BaseAuditLogger;
@@ -1,5 +1,9 @@
1
1
  import { LOG_LEVEL } from '../../infra/environment/types';
2
2
  import { Cache } from '../cache/cache';
3
+ export interface Trace {
4
+ mainId?: string;
5
+ ids: string[];
6
+ }
3
7
  /**
4
8
  * Identifica o tipo de ator que originou o log.
5
9
  */
@@ -55,6 +59,11 @@ export interface Logger {
55
59
  error: (...input: any[]) => Promise<void> | void;
56
60
  debug: (...input: any[]) => Promise<void> | void;
57
61
  }
62
+ export type LoggerGatewayProps = {
63
+ cache?: Cache;
64
+ options?: LogOptions;
65
+ params?: LogParamsInput;
66
+ };
58
67
  /**
59
68
  * Classe base para loggers.
60
69
  *
@@ -64,10 +73,10 @@ export interface Logger {
64
73
  * - Delegar o envio para a implementação concreta (`register`).
65
74
  */
66
75
  export default abstract class LoggerGateway implements Logger {
67
- protected cache?: Cache | undefined;
68
- protected options?: LogOptions | undefined;
69
- protected params?: Partial<LogParams> | undefined;
70
- constructor(cache?: Cache | undefined, options?: LogOptions | undefined, params?: Partial<LogParams> | undefined);
76
+ protected cache?: Cache;
77
+ protected options?: LogOptions;
78
+ protected params?: LogParamsInput;
79
+ constructor(props: LoggerGatewayProps);
71
80
  private static cacheKey;
72
81
  /**
73
82
  * Loga com nível `info`.
@@ -30,10 +30,10 @@ class LoggerGateway {
30
30
  cache;
31
31
  options;
32
32
  params;
33
- constructor(cache, options, params) {
34
- this.cache = cache;
35
- this.options = options;
36
- this.params = params;
33
+ constructor(props) {
34
+ this.cache = props.cache;
35
+ this.options = props.options;
36
+ this.params = props.params;
37
37
  }
38
38
  static cacheKey = 'LOG_LEVEL';
39
39
  /**
@@ -6,4 +6,6 @@ export declare abstract class Entity<Props> {
6
6
  set id(id: UniqueEntityId);
7
7
  protected constructor(props: Props, id?: UniqueEntityId);
8
8
  equals(entity: Entity<any>): boolean;
9
+ private serializeValue;
10
+ toObject(): Record<string, unknown>;
9
11
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Entity = void 0;
4
4
  const unique_entity_id_1 = require("./unique-entity-id");
5
+ const watched_list_1 = require("./watched-list");
5
6
  class Entity {
6
7
  _id;
7
8
  props;
@@ -24,5 +25,32 @@ class Entity {
24
25
  }
25
26
  return false;
26
27
  }
28
+ serializeValue(value) {
29
+ if (value instanceof Entity) {
30
+ return value.toObject();
31
+ }
32
+ if (value instanceof Date) {
33
+ return value.toISOString();
34
+ }
35
+ if (value instanceof unique_entity_id_1.UniqueEntityId) {
36
+ return value.toString();
37
+ }
38
+ if (value instanceof watched_list_1.WatchedList) {
39
+ return value.toObject();
40
+ }
41
+ if (Array.isArray(value)) {
42
+ return value.map(item => this.serializeValue(item));
43
+ }
44
+ if (value && typeof value === 'object') {
45
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [
46
+ key,
47
+ this.serializeValue(item),
48
+ ]));
49
+ }
50
+ return value;
51
+ }
52
+ toObject() {
53
+ return this.serializeValue(this.props);
54
+ }
27
55
  }
28
56
  exports.Entity = Entity;
@@ -22,4 +22,5 @@ export declare abstract class WatchedList<T> {
22
22
  add(item: T): void;
23
23
  remove(item: T): void;
24
24
  update(items: T[]): void;
25
+ toObject(): (Record<string, unknown> | T)[];
25
26
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WatchedList = void 0;
4
+ const entity_1 = require("./entity");
4
5
  class WatchedList {
5
6
  currentItems;
6
7
  initial;
@@ -89,5 +90,13 @@ class WatchedList {
89
90
  this.removed = removedItems;
90
91
  this.updated = updatedItems;
91
92
  }
93
+ toObject() {
94
+ return this.currentItems.map(item => {
95
+ if (item instanceof entity_1.Entity) {
96
+ return item.toObject();
97
+ }
98
+ return item;
99
+ });
100
+ }
92
101
  }
93
102
  exports.WatchedList = WatchedList;
@@ -1,5 +1,6 @@
1
1
  import { Cache } from '../../../clean-arch/application/cache/cache';
2
- import LoggerGateway, { LogOptions, LogParamsInput, LogPropertiesInput } from '../../application/logger';
2
+ import LoggerGateway, { LogOptions, LogPropertiesInput } from '../../application/logger';
3
+ import { LogOrbitParamsInput } from './logger-orbit';
3
4
  export interface LoggerInMemoryOptions extends LogOptions {
4
5
  /**
5
6
  * Quando `true`, inclui as propriedades do log na saída do console.
@@ -7,6 +8,11 @@ export interface LoggerInMemoryOptions extends LogOptions {
7
8
  */
8
9
  showProps?: boolean;
9
10
  }
11
+ export type LoggerInMemoryProps = {
12
+ cache?: Cache;
13
+ options?: LoggerInMemoryOptions;
14
+ params?: LogOrbitParamsInput;
15
+ };
10
16
  /**
11
17
  * Logger simples que escreve no console.
12
18
  *
@@ -20,7 +26,7 @@ export declare class LoggerInMemory extends LoggerGateway {
20
26
  * @param options Opções de comportamento do logger.
21
27
  * @param params Parâmetros padrão de log (actor/type).
22
28
  */
23
- constructor(cache?: Cache, options?: LoggerInMemoryOptions, params?: LogParamsInput);
29
+ constructor(props: LoggerInMemoryProps);
24
30
  /**
25
31
  * Delegado para o fluxo comum de LoggerGateway.
26
32
  * Mantido como atalho para compatibilidade com outros loggers.
@@ -16,8 +16,8 @@ class LoggerInMemory extends logger_1.default {
16
16
  * @param options Opções de comportamento do logger.
17
17
  * @param params Parâmetros padrão de log (actor/type).
18
18
  */
19
- constructor(cache, options, params) {
20
- super(cache, options, params);
19
+ constructor(props) {
20
+ super(props);
21
21
  }
22
22
  /**
23
23
  * Delegado para o fluxo comum de LoggerGateway.
@@ -60,6 +60,11 @@ export type LogPayload = Omit<LogProperties, 'expirationHours'> & {
60
60
  expirationDate: Date;
61
61
  details: any;
62
62
  };
63
+ export type LoggerOrbitProps = {
64
+ cache?: Cache;
65
+ options?: LoggerOrbitOptions;
66
+ params?: LogOrbitParamsInput;
67
+ };
63
68
  /**
64
69
  * Evento de domínio enviado para o serviço de logs.
65
70
  */
@@ -80,7 +85,7 @@ export declare class LoggerOrbit extends LoggerGateway {
80
85
  private queue;
81
86
  protected options: LoggerOrbitOptions;
82
87
  protected params?: LogOrbitParamsInput;
83
- constructor(queue: Queue, cache?: Cache, options?: LoggerOrbitOptions, params?: LogOrbitParamsInput);
88
+ constructor(queue: Queue, props: LoggerOrbitProps);
84
89
  /**
85
90
  * Loga um evento (atalho para `super.log`).
86
91
  */
@@ -116,3 +121,9 @@ export declare class LoggerOrbit extends LoggerGateway {
116
121
  */
117
122
  protected buildDefaultProps(params?: LogOrbitParamsInput): LogOrbitProperties;
118
123
  }
124
+ export declare class BaseAuditOrbitLogger {
125
+ private static instance;
126
+ protected constructor();
127
+ static getInstance(queue: Queue, props?: LoggerOrbitProps): LoggerOrbit;
128
+ private static buildProps;
129
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LoggerOrbit = void 0;
3
+ exports.BaseAuditOrbitLogger = exports.LoggerOrbit = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const logger_1 = tslib_1.__importStar(require("../../application/logger"));
6
6
  const log_error_1 = tslib_1.__importDefault(require("../../domain/errors/log-error"));
@@ -25,8 +25,8 @@ exports.default = LogEvent;
25
25
  */
26
26
  class LoggerOrbit extends logger_1.default {
27
27
  queue;
28
- constructor(queue, cache, options, params) {
29
- super(cache, options, params);
28
+ constructor(queue, props) {
29
+ super(props);
30
30
  this.queue = queue;
31
31
  }
32
32
  /**
@@ -100,3 +100,21 @@ class LoggerOrbit extends logger_1.default {
100
100
  }
101
101
  }
102
102
  exports.LoggerOrbit = LoggerOrbit;
103
+ class BaseAuditOrbitLogger {
104
+ static instance;
105
+ constructor() { }
106
+ static getInstance(queue, props) {
107
+ if (!this.instance) {
108
+ this.instance = new LoggerOrbit(queue, this.buildProps(props));
109
+ }
110
+ return this.instance;
111
+ }
112
+ static buildProps(props = {}) {
113
+ props.options = props.options || {
114
+ throwOnError: false,
115
+ lastArgIsParams: true,
116
+ };
117
+ return props;
118
+ }
119
+ }
120
+ exports.BaseAuditOrbitLogger = BaseAuditOrbitLogger;
@@ -102,8 +102,8 @@ export declare const authorizerHeaders: z.ZodUnion<[z.ZodObject<z.objectUtil.ext
102
102
  }>>, {
103
103
  tenantname: z.ZodString;
104
104
  }>, "strip", z.ZodTypeAny, {
105
- username: string;
106
105
  "x-trace-id": string;
106
+ username: string;
107
107
  tenantid: string;
108
108
  useremail: string;
109
109
  tenantname: string;
@@ -148,9 +148,9 @@ export declare const authorizerHeadersWithFiscalIds: z.ZodUnion<[z.ZodObject<z.o
148
148
  }, {
149
149
  "x-trace-id": z.ZodDefault<z.ZodString>;
150
150
  }>>, "strip", z.ZodTypeAny, {
151
+ "x-trace-id": string;
151
152
  fiscalidsfromtenant: string;
152
153
  username: string;
153
- "x-trace-id": string;
154
154
  tenantid: string;
155
155
  useremail: string;
156
156
  userid?: string | undefined;
@@ -171,15 +171,15 @@ export declare const authorizerHeadersWithFiscalIds: z.ZodUnion<[z.ZodObject<z.o
171
171
  }, {
172
172
  "x-trace-id": z.ZodDefault<z.ZodString>;
173
173
  }>>, "strip", z.ZodTypeAny, {
174
- fiscalidsfromtenant: string;
175
174
  "app-id": string;
176
175
  "app-name": string;
177
176
  "x-trace-id": string;
177
+ fiscalidsfromtenant: string;
178
178
  tenantid: string;
179
179
  }, {
180
- fiscalidsfromtenant: string;
181
180
  "app-id": string;
182
181
  "app-name": string;
182
+ fiscalidsfromtenant: string;
183
183
  tenantid: string;
184
184
  "x-trace-id"?: string | undefined;
185
185
  }>]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seidor-cloud-produtos/orbit-backend-lib",
3
- "version": "2.0.92",
3
+ "version": "2.0.93",
4
4
  "description": "Internal lib for backend components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",