@navservice/core 1.113.0 → 1.115.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.
@@ -0,0 +1,28 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ type Level = "info" | "warn" | "error";
3
+ type LogEntry = {
4
+ level: Level;
5
+ fn: string;
6
+ message: string;
7
+ results: any;
8
+ timestamp?: number;
9
+ };
10
+ type RequestStore = {
11
+ requestId: string;
12
+ logs: LogEntry[];
13
+ startedAt: number;
14
+ };
15
+ declare class RequestLoggerClass {
16
+ storage: AsyncLocalStorage<RequestStore>;
17
+ run<T>(callback: () => Promise<T>): Promise<T>;
18
+ add(entry: Omit<LogEntry, "timestamp">): void;
19
+ build(): {
20
+ requestId: string;
21
+ durationMs: number;
22
+ level: Level;
23
+ logs: LogEntry[];
24
+ } | null;
25
+ resolveLevel(logs: LogEntry[]): Level;
26
+ }
27
+ declare const _logger: RequestLoggerClass;
28
+ export default _logger;
@@ -135,7 +135,7 @@ declare const set_response: {
135
135
  ACTION_REQUIRED({ message, results }: ResponseErrorOmmit): void;
136
136
  WARNING({ message, results }: ResponseErrorOmmit): void;
137
137
  AUTHORIZATION_ERROR({ message, results }: ResponseErrorOmmit): void;
138
- DATABASE_ERROR({ message, results, erro }: ResponseErrorOmmit & {
138
+ DATABASE_ERROR({ message, erro }: ResponseErrorOmmit & {
139
139
  erro: any;
140
140
  }): void;
141
141
  SCHEMA_VALIDATION({ results }: ResponseErrorOmmit): void;
@@ -164,44 +164,54 @@ declare const helpers: {
164
164
  ACTION_REQUIRED({ message, results }: {
165
165
  message?: string | undefined;
166
166
  results?: unknown;
167
+ fn?: string | undefined;
167
168
  }): void;
168
169
  WARNING({ message, results }: {
169
170
  message?: string | undefined;
170
171
  results?: unknown;
172
+ fn?: string | undefined;
171
173
  }): void;
172
174
  AUTHORIZATION_ERROR({ message, results }: {
173
175
  message?: string | undefined;
174
176
  results?: unknown;
177
+ fn?: string | undefined;
175
178
  }): void;
176
- DATABASE_ERROR({ message, results, erro }: {
179
+ DATABASE_ERROR({ message, erro }: {
177
180
  message?: string | undefined;
178
181
  results?: unknown;
182
+ fn?: string | undefined;
179
183
  } & {
180
184
  erro: any;
181
185
  }): void;
182
186
  SCHEMA_VALIDATION({ results }: {
183
187
  message?: string | undefined;
184
188
  results?: unknown;
189
+ fn?: string | undefined;
185
190
  }): void;
186
191
  UNAUTHORIZED({ message }: {
187
192
  message?: string | undefined;
188
193
  results?: unknown;
194
+ fn?: string | undefined;
189
195
  }): void;
190
196
  INVALID_TOKEN({ message }: {
191
197
  message?: string | undefined;
192
198
  results?: unknown;
199
+ fn?: string | undefined;
193
200
  }): void;
194
201
  NOT_FOUND({ message }: {
195
202
  message?: string | undefined;
196
203
  results?: unknown;
204
+ fn?: string | undefined;
197
205
  }): void;
198
206
  BUSINESS_RULE_VIOLATION({ message, results }: {
199
207
  message?: string | undefined;
200
208
  results?: unknown;
209
+ fn?: string | undefined;
201
210
  }): void;
202
211
  PLAN_FORBIDDEN({ message, results }: {
203
212
  message?: string | undefined;
204
213
  results?: unknown;
214
+ fn?: string | undefined;
205
215
  }): void;
206
216
  };
207
217
  };
@@ -248,5 +258,45 @@ declare const helpers: {
248
258
  get get_now_format_number(): number;
249
259
  verificar_data(value: number): number;
250
260
  };
261
+ logger: {
262
+ storage: import("async_hooks").AsyncLocalStorage<{
263
+ requestId: string;
264
+ logs: {
265
+ level: "error" | "info" | "warn";
266
+ fn: string;
267
+ message: string;
268
+ results: any;
269
+ timestamp?: number;
270
+ }[];
271
+ startedAt: number;
272
+ }>;
273
+ run<T>(callback: () => Promise<T>): Promise<T>;
274
+ add(entry: Omit<{
275
+ level: "error" | "info" | "warn";
276
+ fn: string;
277
+ message: string;
278
+ results: any;
279
+ timestamp?: number;
280
+ }, "timestamp">): void;
281
+ build(): {
282
+ requestId: string;
283
+ durationMs: number;
284
+ level: "error" | "info" | "warn";
285
+ logs: {
286
+ level: "error" | "info" | "warn";
287
+ fn: string;
288
+ message: string;
289
+ results: any;
290
+ timestamp?: number;
291
+ }[];
292
+ } | null;
293
+ resolveLevel(logs: {
294
+ level: "error" | "info" | "warn";
295
+ fn: string;
296
+ message: string;
297
+ results: any;
298
+ timestamp?: number;
299
+ }[]): "error" | "info" | "warn";
300
+ };
251
301
  };
252
302
  export default helpers;
@@ -1,7 +1,49 @@
1
1
  import v4 from "zod/v4";
2
+ import { AsyncLocalStorage } from "node:async_hooks";
2
3
  import { SignJWT, jwtVerify } from "jose";
3
4
  import { randomBytes, scrypt, timingSafeEqual } from "node:crypto";
4
5
  import { promisify } from "node:util";
6
+ class RequestLoggerClass {
7
+ storage = new AsyncLocalStorage();
8
+ run(callback) {
9
+ return this.storage.run({
10
+ requestId: crypto.randomUUID(),
11
+ logs: [],
12
+ startedAt: Date.now()
13
+ }, callback);
14
+ }
15
+ add(entry) {
16
+ const store = this.storage.getStore();
17
+ if (!store) return;
18
+ store.logs.push({
19
+ ...entry,
20
+ timestamp: Date.now()
21
+ });
22
+ }
23
+ build() {
24
+ const store = this.storage.getStore();
25
+ if (!store || 0 === store.logs.length) return null;
26
+ const durationMs = Date.now() - store.startedAt;
27
+ const level = this.resolveLevel(store.logs);
28
+ const payload = {
29
+ requestId: store.requestId,
30
+ durationMs,
31
+ level,
32
+ logs: store.logs
33
+ };
34
+ const output = JSON.stringify(payload);
35
+ if ("error" === level) console.error(output);
36
+ else console.log(output);
37
+ return payload;
38
+ }
39
+ resolveLevel(logs) {
40
+ if (logs.some((l)=>"error" === l.level)) return "error";
41
+ if (logs.some((l)=>"warn" === l.level)) return "warn";
42
+ return "info";
43
+ }
44
+ }
45
+ const _logger = new RequestLoggerClass();
46
+ const helpers_logger = _logger;
5
47
  const set_response = new class {
6
48
  c = new class {
7
49
  SUCCESS({ message, results, c }) {
@@ -73,6 +115,12 @@ const set_response = new class {
73
115
  message: "Erro ao validar dados!",
74
116
  results: v4.treeifyError(error)
75
117
  };
118
+ helpers_logger.add({
119
+ fn: "SCHEMA_VALIDATION",
120
+ level: "error",
121
+ message: payload?.message,
122
+ results: payload?.results
123
+ });
76
124
  return c.json(payload, {
77
125
  status: 500
78
126
  });
@@ -84,6 +132,12 @@ const set_response = new class {
84
132
  message: error?.message || "Erro interno no servidor!",
85
133
  results: error?.results || []
86
134
  };
135
+ if (payload?.type === "error") helpers_logger.add({
136
+ fn: "SERVER_ERROR",
137
+ level: payload?.type,
138
+ message: payload?.message,
139
+ results: payload?.results
140
+ });
87
141
  return c.json(payload, {
88
142
  status: error?.status || error?.status || 500
89
143
  });
@@ -178,7 +232,7 @@ const set_response = new class {
178
232
  };
179
233
  throw payload;
180
234
  }
181
- DATABASE_ERROR({ message, results, erro }) {
235
+ DATABASE_ERROR({ message, erro }) {
182
236
  const errorCode = erro?.cause?.code || erro?.cause?.cause?.code || erro?.code || "";
183
237
  const map = {
184
238
  SQLITE_CONSTRAINT_UNIQUE: {
@@ -221,6 +275,12 @@ const set_response = new class {
221
275
  message: mapped?.message || message || "Erro no banco de dados!",
222
276
  results: []
223
277
  };
278
+ helpers_logger.add({
279
+ fn: "DATABASE_ERROR",
280
+ level: "error",
281
+ message: payload?.message,
282
+ results: erro
283
+ });
224
284
  throw payload;
225
285
  }
226
286
  SCHEMA_VALIDATION({ results }) {
@@ -480,7 +540,8 @@ const helpers = {
480
540
  set_response: _set_response,
481
541
  token: helpers_token,
482
542
  secret: helpers_secret,
483
- data: helpers_data
543
+ data: helpers_data,
544
+ logger: helpers_logger
484
545
  };
485
546
  const src_helpers = helpers;
486
547
  export { src_helpers as default };
package/build/es/index.js CHANGED
@@ -132,7 +132,8 @@ var _usuario_TypeControllerUsuario;
132
132
  ]).optional(),
133
133
  type: TypeSchema.optional().default("success"),
134
134
  code: CodeSchema,
135
- status: v4.number().optional().default(200)
135
+ status: v4.number().optional().default(200),
136
+ fn: v4.string().optional()
136
137
  });
137
138
  })(TypeControllerResponse.Error || (TypeControllerResponse.Error = {}));
138
139
  })(_type_response_TypeControllerResponse || (_type_response_TypeControllerResponse = {}));
@@ -43,6 +43,7 @@ declare namespace TypeControllerResponse {
43
43
  type: z4.ZodDefault<z4.ZodOptional<z4.ZodDefault<z4.ZodUnion<readonly [z4.ZodLiteral<"success">, z4.ZodLiteral<"warning">, z4.ZodLiteral<"error">]>>>>;
44
44
  code: z4.ZodUnion<readonly [z4.ZodLiteral<"SUCCESS">, z4.ZodLiteral<"ACTION_REQUIRED">, z4.ZodLiteral<"CREATED">, z4.ZodLiteral<"WARNING">, z4.ZodLiteral<"AUTHORIZATION_ERROR">, z4.ZodLiteral<"SCHEMA_VALIDATION">, z4.ZodLiteral<"SERVER_ERROR">, z4.ZodLiteral<"UNAUTHORIZED">, z4.ZodLiteral<"INVALID_TOKEN">, z4.ZodLiteral<"NOT_FOUND">, z4.ZodLiteral<"SUCCESS_FILE">, z4.ZodLiteral<"DATABASE_ERROR">, z4.ZodLiteral<"BUSINESS_RULE_VIOLATION">, z4.ZodLiteral<"PLAN_FORBIDDEN">]>;
45
45
  status: z4.ZodDefault<z4.ZodOptional<z4.ZodNumber>>;
46
+ fn: z4.ZodOptional<z4.ZodString>;
46
47
  }, z4.core.$strip>;
47
48
  type Input<T = unknown> = z4.infer<typeof InputSchema> & {
48
49
  results?: T;
@@ -0,0 +1,28 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ type Level = "info" | "warn" | "error";
3
+ type LogEntry = {
4
+ level: Level;
5
+ fn: string;
6
+ message: string;
7
+ results: any;
8
+ timestamp?: number;
9
+ };
10
+ type RequestStore = {
11
+ requestId: string;
12
+ logs: LogEntry[];
13
+ startedAt: number;
14
+ };
15
+ declare class RequestLoggerClass {
16
+ storage: AsyncLocalStorage<RequestStore>;
17
+ run<T>(callback: () => Promise<T>): Promise<T>;
18
+ add(entry: Omit<LogEntry, "timestamp">): void;
19
+ build(): {
20
+ requestId: string;
21
+ durationMs: number;
22
+ level: Level;
23
+ logs: LogEntry[];
24
+ } | null;
25
+ resolveLevel(logs: LogEntry[]): Level;
26
+ }
27
+ declare const _logger: RequestLoggerClass;
28
+ export default _logger;
@@ -135,7 +135,7 @@ declare const set_response: {
135
135
  ACTION_REQUIRED({ message, results }: ResponseErrorOmmit): void;
136
136
  WARNING({ message, results }: ResponseErrorOmmit): void;
137
137
  AUTHORIZATION_ERROR({ message, results }: ResponseErrorOmmit): void;
138
- DATABASE_ERROR({ message, results, erro }: ResponseErrorOmmit & {
138
+ DATABASE_ERROR({ message, erro }: ResponseErrorOmmit & {
139
139
  erro: any;
140
140
  }): void;
141
141
  SCHEMA_VALIDATION({ results }: ResponseErrorOmmit): void;
@@ -164,44 +164,54 @@ declare const helpers: {
164
164
  ACTION_REQUIRED({ message, results }: {
165
165
  message?: string | undefined;
166
166
  results?: unknown;
167
+ fn?: string | undefined;
167
168
  }): void;
168
169
  WARNING({ message, results }: {
169
170
  message?: string | undefined;
170
171
  results?: unknown;
172
+ fn?: string | undefined;
171
173
  }): void;
172
174
  AUTHORIZATION_ERROR({ message, results }: {
173
175
  message?: string | undefined;
174
176
  results?: unknown;
177
+ fn?: string | undefined;
175
178
  }): void;
176
- DATABASE_ERROR({ message, results, erro }: {
179
+ DATABASE_ERROR({ message, erro }: {
177
180
  message?: string | undefined;
178
181
  results?: unknown;
182
+ fn?: string | undefined;
179
183
  } & {
180
184
  erro: any;
181
185
  }): void;
182
186
  SCHEMA_VALIDATION({ results }: {
183
187
  message?: string | undefined;
184
188
  results?: unknown;
189
+ fn?: string | undefined;
185
190
  }): void;
186
191
  UNAUTHORIZED({ message }: {
187
192
  message?: string | undefined;
188
193
  results?: unknown;
194
+ fn?: string | undefined;
189
195
  }): void;
190
196
  INVALID_TOKEN({ message }: {
191
197
  message?: string | undefined;
192
198
  results?: unknown;
199
+ fn?: string | undefined;
193
200
  }): void;
194
201
  NOT_FOUND({ message }: {
195
202
  message?: string | undefined;
196
203
  results?: unknown;
204
+ fn?: string | undefined;
197
205
  }): void;
198
206
  BUSINESS_RULE_VIOLATION({ message, results }: {
199
207
  message?: string | undefined;
200
208
  results?: unknown;
209
+ fn?: string | undefined;
201
210
  }): void;
202
211
  PLAN_FORBIDDEN({ message, results }: {
203
212
  message?: string | undefined;
204
213
  results?: unknown;
214
+ fn?: string | undefined;
205
215
  }): void;
206
216
  };
207
217
  };
@@ -248,5 +258,45 @@ declare const helpers: {
248
258
  get get_now_format_number(): number;
249
259
  verificar_data(value: number): number;
250
260
  };
261
+ logger: {
262
+ storage: import("async_hooks").AsyncLocalStorage<{
263
+ requestId: string;
264
+ logs: {
265
+ level: "error" | "info" | "warn";
266
+ fn: string;
267
+ message: string;
268
+ results: any;
269
+ timestamp?: number;
270
+ }[];
271
+ startedAt: number;
272
+ }>;
273
+ run<T>(callback: () => Promise<T>): Promise<T>;
274
+ add(entry: Omit<{
275
+ level: "error" | "info" | "warn";
276
+ fn: string;
277
+ message: string;
278
+ results: any;
279
+ timestamp?: number;
280
+ }, "timestamp">): void;
281
+ build(): {
282
+ requestId: string;
283
+ durationMs: number;
284
+ level: "error" | "info" | "warn";
285
+ logs: {
286
+ level: "error" | "info" | "warn";
287
+ fn: string;
288
+ message: string;
289
+ results: any;
290
+ timestamp?: number;
291
+ }[];
292
+ } | null;
293
+ resolveLevel(logs: {
294
+ level: "error" | "info" | "warn";
295
+ fn: string;
296
+ message: string;
297
+ results: any;
298
+ timestamp?: number;
299
+ }[]): "error" | "info" | "warn";
300
+ };
251
301
  };
252
302
  export default helpers;
@@ -57,9 +57,59 @@ __webpack_require__.d(__webpack_exports__, {
57
57
  ;// CONCATENATED MODULE: external "zod/v4"
58
58
  const v4_namespaceObject = require("zod/v4");
59
59
  var v4_default = /*#__PURE__*/__webpack_require__.n(v4_namespaceObject);
60
+ ;// CONCATENATED MODULE: external "node:async_hooks"
61
+ const external_node_async_hooks_namespaceObject = require("node:async_hooks");
62
+ ;// CONCATENATED MODULE: ./src/helpers/_logger.ts
63
+
64
+ class RequestLoggerClass {
65
+ storage = new external_node_async_hooks_namespaceObject.AsyncLocalStorage();
66
+ run(callback) {
67
+ return this.storage.run({
68
+ requestId: crypto.randomUUID(),
69
+ logs: [],
70
+ startedAt: Date.now()
71
+ }, callback);
72
+ }
73
+ add(entry) {
74
+ const store = this.storage.getStore();
75
+ if (!store) return;
76
+ store.logs.push({
77
+ ...entry,
78
+ timestamp: Date.now()
79
+ });
80
+ }
81
+ build() {
82
+ const store = this.storage.getStore();
83
+ if (!store || store.logs.length === 0) return null;
84
+ const durationMs = Date.now() - store.startedAt;
85
+ const level = this.resolveLevel(store.logs);
86
+ const payload = {
87
+ requestId: store.requestId,
88
+ durationMs,
89
+ level,
90
+ logs: store.logs
91
+ };
92
+ const output = JSON.stringify(payload);
93
+ if (level === "error") {
94
+ console.error(output);
95
+ } else {
96
+ console.log(output);
97
+ }
98
+ return payload;
99
+ }
100
+ resolveLevel(logs) {
101
+ if (logs.some((l)=>l.level === "error")) return "error";
102
+ if (logs.some((l)=>l.level === "warn")) return "warn";
103
+ return "info";
104
+ }
105
+ }
106
+ const _logger = new RequestLoggerClass();
107
+ /* export default */ const helpers_logger = (_logger);
108
+
60
109
  ;// CONCATENATED MODULE: ./src/helpers/_set_response.ts
61
110
  // TYPES
62
111
 
112
+
63
113
  const set_response = new class set_response {
64
114
  c = new class c {
65
115
  SUCCESS({ message, results, c }) {
@@ -131,6 +181,12 @@ const set_response = new class set_response {
131
181
  message: "Erro ao validar dados!",
132
182
  results: v4_default().treeifyError(error)
133
183
  };
184
+ helpers_logger.add({
185
+ fn: "SCHEMA_VALIDATION",
186
+ level: "error",
187
+ message: payload?.message,
188
+ results: payload?.results
189
+ });
134
190
  return c.json(payload, {
135
191
  status: 500
136
192
  });
@@ -142,6 +198,14 @@ const set_response = new class set_response {
142
198
  message: error?.message || "Erro interno no servidor!",
143
199
  results: error?.results || []
144
200
  };
201
+ if (payload?.type === "error") {
202
+ helpers_logger.add({
203
+ fn: "SERVER_ERROR",
204
+ level: payload?.type,
205
+ message: payload?.message,
206
+ results: payload?.results
207
+ });
208
+ }
145
209
  return c.json(payload, {
146
210
  status: error?.status || error?.status || 500
147
211
  });
@@ -236,7 +300,7 @@ const set_response = new class set_response {
236
300
  };
237
301
  throw payload;
238
302
  }
239
- DATABASE_ERROR({ message, results, erro }) {
303
+ DATABASE_ERROR({ message, erro }) {
240
304
  const errorCode = erro?.cause?.code || erro?.cause?.cause?.code || erro?.code || "";
241
305
  const map = {
242
306
  "SQLITE_CONSTRAINT_UNIQUE": {
@@ -279,6 +343,12 @@ const set_response = new class set_response {
279
343
  message: mapped?.message || message || "Erro no banco de dados!",
280
344
  results: []
281
345
  };
346
+ helpers_logger.add({
347
+ fn: "DATABASE_ERROR",
348
+ level: "error",
349
+ message: payload?.message,
350
+ results: erro
351
+ });
282
352
  throw payload;
283
353
  }
284
354
  SCHEMA_VALIDATION({ results }) {
@@ -577,11 +647,13 @@ class _data {
577
647
 
578
648
 
579
649
 
650
+
580
651
  const helpers = {
581
652
  set_response: _set_response,
582
653
  token: helpers_token,
583
654
  secret: helpers_secret,
584
- data: helpers_data
655
+ data: helpers_data,
656
+ logger: helpers_logger
585
657
  };
586
658
  /* export default */ const src_helpers = (helpers);
587
659
 
@@ -194,7 +194,8 @@ var _usuario_TypeControllerUsuario;
194
194
  ]).optional(),
195
195
  type: TypeSchema.optional().default("success"),
196
196
  code: CodeSchema,
197
- status: v4_default().number().optional().default(200)
197
+ status: v4_default().number().optional().default(200),
198
+ fn: v4_default().string().optional()
198
199
  });
199
200
  })(TypeControllerResponse.Error || (TypeControllerResponse.Error = {}));
200
201
  })(_type_response_TypeControllerResponse || (_type_response_TypeControllerResponse = {}));
@@ -43,6 +43,7 @@ declare namespace TypeControllerResponse {
43
43
  type: z4.ZodDefault<z4.ZodOptional<z4.ZodDefault<z4.ZodUnion<readonly [z4.ZodLiteral<"success">, z4.ZodLiteral<"warning">, z4.ZodLiteral<"error">]>>>>;
44
44
  code: z4.ZodUnion<readonly [z4.ZodLiteral<"SUCCESS">, z4.ZodLiteral<"ACTION_REQUIRED">, z4.ZodLiteral<"CREATED">, z4.ZodLiteral<"WARNING">, z4.ZodLiteral<"AUTHORIZATION_ERROR">, z4.ZodLiteral<"SCHEMA_VALIDATION">, z4.ZodLiteral<"SERVER_ERROR">, z4.ZodLiteral<"UNAUTHORIZED">, z4.ZodLiteral<"INVALID_TOKEN">, z4.ZodLiteral<"NOT_FOUND">, z4.ZodLiteral<"SUCCESS_FILE">, z4.ZodLiteral<"DATABASE_ERROR">, z4.ZodLiteral<"BUSINESS_RULE_VIOLATION">, z4.ZodLiteral<"PLAN_FORBIDDEN">]>;
45
45
  status: z4.ZodDefault<z4.ZodOptional<z4.ZodNumber>>;
46
+ fn: z4.ZodOptional<z4.ZodString>;
46
47
  }, z4.core.$strip>;
47
48
  type Input<T = unknown> = z4.infer<typeof InputSchema> & {
48
49
  results?: T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navservice/core",
3
- "version": "1.113.0",
3
+ "version": "1.115.0",
4
4
  "description": "Service core de todos os micro serviços",
5
5
  "type": "module",
6
6
  "exports": {