@navservice/core 1.96.0 → 1.99.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.
@@ -1,5 +1,6 @@
1
1
  declare class _data {
2
- static get get_now_format_number(): number;
3
- static verificar_data(value: number): number;
2
+ get get_now_format_number(): number;
3
+ verificar_data(value: number): number;
4
4
  }
5
- export default _data;
5
+ declare const _default: _data;
6
+ export default _default;
@@ -1,10 +1,11 @@
1
1
  declare class _secret {
2
- private static readonly SALT_LENGTH;
3
- private static readonly KEY_LENGTH;
4
- static gerar_hash_senha(password: string): Promise<string>;
5
- static verify({ password, dashed_senha }: {
2
+ readonly SALT_LENGTH = 22;
3
+ readonly KEY_LENGTH = 35;
4
+ gerar_hash_senha(password: string): Promise<string>;
5
+ verify({ password, dashed_senha }: {
6
6
  password: string;
7
7
  dashed_senha: string;
8
8
  }): Promise<boolean>;
9
9
  }
10
- export default _secret;
10
+ declare const _default: _secret;
11
+ export default _default;
@@ -3,9 +3,7 @@ type ResponseCOmmit = Omit<t.Geral.Response.C.Input, "type" | "code" | "status">
3
3
  type ResponseErrorOmmit = Omit<t.Geral.Response.Error.Input, "type" | "code" | "status">;
4
4
  export type ResponseCPayload<T = unknown> = Omit<t.Geral.Response.C.Input<T>, "c">;
5
5
  declare const set_response: {
6
- new (): {};
7
6
  c: {
8
- new (): {};
9
7
  SUCCESS<T>({ message, results, c }: ResponseCOmmit): Response & import("hono").TypedResponse<{
10
8
  status: 200 | 201 | 202 | 204 | 400 | 401 | 403 | 404 | 409 | 422 | 500 | 428 | 405;
11
9
  code: "SUCCESS" | "ACTION_REQUIRED" | "CREATED" | "WARNING" | "AUTHORIZATION_ERROR" | "SCHEMA_VALIDATION" | "SERVER_ERROR" | "UNAUTHORIZED" | "INVALID_TOKEN" | "NOT_FOUND" | "SUCCESS_FILE" | "DATABASE_ERROR";
@@ -134,7 +132,6 @@ declare const set_response: {
134
132
  }, 200, "json">)>;
135
133
  };
136
134
  error: {
137
- new (): {};
138
135
  ACTION_REQUIRED({ message, results }: ResponseErrorOmmit): void;
139
136
  WARNING({ message, results }: ResponseErrorOmmit): void;
140
137
  AUTHORIZATION_ERROR({ message, results }: ResponseErrorOmmit): void;
@@ -1,7 +1,6 @@
1
1
  import type { Next } from 'hono';
2
2
  import t from '../types';
3
3
  declare const _token: {
4
- new (): {};
5
4
  verificar_token(c: t.Context, next: Next): Promise<void | (Response & import("hono").TypedResponse<{
6
5
  status: 200 | 201 | 202 | 204 | 400 | 401 | 403 | 404 | 409 | 422 | 500 | 428 | 405;
7
6
  code: "SUCCESS" | "ACTION_REQUIRED" | "CREATED" | "WARNING" | "AUTHORIZATION_ERROR" | "SCHEMA_VALIDATION" | "SERVER_ERROR" | "UNAUTHORIZED" | "INVALID_TOKEN" | "NOT_FOUND" | "SUCCESS_FILE" | "DATABASE_ERROR";
@@ -12,5 +11,15 @@ declare const _token: {
12
11
  criar_token_login_usuario({ _id, email, app, usuario_tipo, ativo, data_criacao, nome, c }: t.Controller.Usuario.TokenPayload & {
13
12
  c: t.Context;
14
13
  }): Promise<string>;
14
+ verificar_token_micro_servico(c: t.Context, next: Next): Promise<void | (Response & import("hono").TypedResponse<{
15
+ status: 200 | 201 | 202 | 204 | 400 | 401 | 403 | 404 | 409 | 422 | 500 | 428 | 405;
16
+ code: "SUCCESS" | "ACTION_REQUIRED" | "CREATED" | "WARNING" | "AUTHORIZATION_ERROR" | "SCHEMA_VALIDATION" | "SERVER_ERROR" | "UNAUTHORIZED" | "INVALID_TOKEN" | "NOT_FOUND" | "SUCCESS_FILE" | "DATABASE_ERROR";
17
+ type: "success" | "warning" | "error";
18
+ message?: string | undefined;
19
+ results: any;
20
+ }, 409, "json">)>;
21
+ criar_token_micro_servico({ c }: {
22
+ c: t.Context;
23
+ }): Promise<string>;
15
24
  };
16
25
  export default _token;
@@ -1,10 +1,6 @@
1
- import _secret from "./_secret";
2
- import _data from "./_data";
3
1
  declare const helpers: {
4
2
  set_response: {
5
- new (): {};
6
3
  c: {
7
- new (): {};
8
4
  SUCCESS<T>({ message, results, c }: {
9
5
  message?: string | undefined;
10
6
  results: any;
@@ -165,7 +161,6 @@ declare const helpers: {
165
161
  }, 200, "json">)>;
166
162
  };
167
163
  error: {
168
- new (): {};
169
164
  ACTION_REQUIRED({ message, results }: {
170
165
  message?: string | undefined;
171
166
  results?: unknown;
@@ -203,7 +198,6 @@ declare const helpers: {
203
198
  };
204
199
  };
205
200
  token: {
206
- new (): {};
207
201
  verificar_token(c: import("..").TypesCore.Context, next: import("hono").Next): Promise<void | (Response & import("hono").TypedResponse<{
208
202
  status: 200 | 201 | 202 | 204 | 400 | 401 | 403 | 404 | 409 | 422 | 500 | 428 | 405;
209
203
  code: "SUCCESS" | "ACTION_REQUIRED" | "CREATED" | "WARNING" | "AUTHORIZATION_ERROR" | "SCHEMA_VALIDATION" | "SERVER_ERROR" | "UNAUTHORIZED" | "INVALID_TOKEN" | "NOT_FOUND" | "SUCCESS_FILE" | "DATABASE_ERROR";
@@ -214,8 +208,29 @@ declare const helpers: {
214
208
  criar_token_login_usuario({ _id, email, app, usuario_tipo, ativo, data_criacao, nome, c }: import("../types/_usuario").default.TokenPayload & {
215
209
  c: import("..").TypesCore.Context;
216
210
  }): Promise<string>;
211
+ verificar_token_micro_servico(c: import("..").TypesCore.Context, next: import("hono").Next): Promise<void | (Response & import("hono").TypedResponse<{
212
+ status: 200 | 201 | 202 | 204 | 400 | 401 | 403 | 404 | 409 | 422 | 500 | 428 | 405;
213
+ code: "SUCCESS" | "ACTION_REQUIRED" | "CREATED" | "WARNING" | "AUTHORIZATION_ERROR" | "SCHEMA_VALIDATION" | "SERVER_ERROR" | "UNAUTHORIZED" | "INVALID_TOKEN" | "NOT_FOUND" | "SUCCESS_FILE" | "DATABASE_ERROR";
214
+ type: "success" | "warning" | "error";
215
+ message?: string | undefined;
216
+ results: any;
217
+ }, 409, "json">)>;
218
+ criar_token_micro_servico({ c }: {
219
+ c: import("..").TypesCore.Context;
220
+ }): Promise<string>;
221
+ };
222
+ secret: {
223
+ readonly SALT_LENGTH: 22;
224
+ readonly KEY_LENGTH: 35;
225
+ gerar_hash_senha(password: string): Promise<string>;
226
+ verify({ password, dashed_senha }: {
227
+ password: string;
228
+ dashed_senha: string;
229
+ }): Promise<boolean>;
230
+ };
231
+ data: {
232
+ get get_now_format_number(): number;
233
+ verificar_data(value: number): number;
217
234
  };
218
- secret: typeof _secret;
219
- data: typeof _data;
220
235
  };
221
236
  export default helpers;
@@ -2,9 +2,9 @@ import v4 from "zod/v4";
2
2
  import { SignJWT, jwtVerify } from "jose";
3
3
  import { randomBytes, scrypt, timingSafeEqual } from "node:crypto";
4
4
  import { promisify } from "node:util";
5
- const set_response = class {
6
- static c = class {
7
- static SUCCESS({ message, results, c }) {
5
+ const set_response = new class {
6
+ c = new class {
7
+ SUCCESS({ message, results, c }) {
8
8
  const payload = {
9
9
  status: 200,
10
10
  code: "SUCCESS",
@@ -16,7 +16,7 @@ const set_response = class {
16
16
  status: 200
17
17
  });
18
18
  }
19
- static ACTION_REQUIRED({ message, results, c }) {
19
+ ACTION_REQUIRED({ message, results, c }) {
20
20
  const payload = {
21
21
  status: 428,
22
22
  code: "ACTION_REQUIRED",
@@ -28,7 +28,7 @@ const set_response = class {
28
28
  status: 428
29
29
  });
30
30
  }
31
- static CREATED({ message, results, c }) {
31
+ CREATED({ message, results, c }) {
32
32
  const payload = {
33
33
  status: 201,
34
34
  code: "CREATED",
@@ -40,7 +40,7 @@ const set_response = class {
40
40
  status: 201
41
41
  });
42
42
  }
43
- static WARNING({ message, results, c }) {
43
+ WARNING({ message, results, c }) {
44
44
  const payload = {
45
45
  status: 400,
46
46
  code: "WARNING",
@@ -52,7 +52,7 @@ const set_response = class {
52
52
  status: 400
53
53
  });
54
54
  }
55
- static AUTHORIZATION_ERROR({ message, results, c }) {
55
+ AUTHORIZATION_ERROR({ message, results, c }) {
56
56
  const payload = {
57
57
  status: 405,
58
58
  code: "WARNING",
@@ -64,7 +64,7 @@ const set_response = class {
64
64
  status: 405
65
65
  });
66
66
  }
67
- static SERVER_ERROR({ error, c }) {
67
+ SERVER_ERROR({ error, c }) {
68
68
  if (error instanceof v4.ZodError) {
69
69
  const payload = {
70
70
  status: 500,
@@ -88,7 +88,7 @@ const set_response = class {
88
88
  status: error?.status || error?.status || 500
89
89
  });
90
90
  }
91
- static UNAUTHORIZED({ message, c }) {
91
+ UNAUTHORIZED({ message, c }) {
92
92
  const payload = {
93
93
  status: 401,
94
94
  code: "UNAUTHORIZED",
@@ -100,7 +100,7 @@ const set_response = class {
100
100
  status: 401
101
101
  });
102
102
  }
103
- static INVALID_TOKEN({ message, c }) {
103
+ INVALID_TOKEN({ message, c }) {
104
104
  const payload = {
105
105
  status: 409,
106
106
  code: "INVALID_TOKEN",
@@ -112,7 +112,7 @@ const set_response = class {
112
112
  status: 409
113
113
  });
114
114
  }
115
- static NOT_FOUND({ message, c }) {
115
+ NOT_FOUND({ message, c }) {
116
116
  const payload = {
117
117
  status: 404,
118
118
  code: "NOT_FOUND",
@@ -124,7 +124,7 @@ const set_response = class {
124
124
  status: 404
125
125
  });
126
126
  }
127
- static async SUCCESS_FILE({ message, file_buffer, content_type, filename, c }) {
127
+ async SUCCESS_FILE({ message, file_buffer, content_type, filename, c }) {
128
128
  if (c && content_type) {
129
129
  const headers = {
130
130
  "Content-Type": content_type,
@@ -147,8 +147,8 @@ const set_response = class {
147
147
  });
148
148
  }
149
149
  };
150
- static error = class {
151
- static ACTION_REQUIRED({ message, results }) {
150
+ error = new class {
151
+ ACTION_REQUIRED({ message, results }) {
152
152
  const payload = {
153
153
  status: 428,
154
154
  code: "ACTION_REQUIRED",
@@ -158,7 +158,7 @@ const set_response = class {
158
158
  };
159
159
  throw payload;
160
160
  }
161
- static WARNING({ message, results }) {
161
+ WARNING({ message, results }) {
162
162
  const payload = {
163
163
  status: 400,
164
164
  code: "WARNING",
@@ -168,7 +168,7 @@ const set_response = class {
168
168
  };
169
169
  throw payload;
170
170
  }
171
- static AUTHORIZATION_ERROR({ message, results }) {
171
+ AUTHORIZATION_ERROR({ message, results }) {
172
172
  const payload = {
173
173
  status: 400,
174
174
  code: "WARNING",
@@ -178,7 +178,7 @@ const set_response = class {
178
178
  };
179
179
  throw payload;
180
180
  }
181
- static DATABASE_ERROR({ message, results, erro }) {
181
+ DATABASE_ERROR({ message, results, erro }) {
182
182
  const errorCode = erro?.cause?.code || erro?.cause?.cause?.code || erro?.code || "";
183
183
  const map = {
184
184
  SQLITE_CONSTRAINT_UNIQUE: {
@@ -223,7 +223,7 @@ const set_response = class {
223
223
  };
224
224
  throw payload;
225
225
  }
226
- static SCHEMA_VALIDATION({ results }) {
226
+ SCHEMA_VALIDATION({ results }) {
227
227
  const payload = {
228
228
  status: 500,
229
229
  code: "SCHEMA_VALIDATION",
@@ -233,7 +233,7 @@ const set_response = class {
233
233
  };
234
234
  throw payload;
235
235
  }
236
- static UNAUTHORIZED({ message }) {
236
+ UNAUTHORIZED({ message }) {
237
237
  const payload = {
238
238
  status: 401,
239
239
  code: "UNAUTHORIZED",
@@ -242,7 +242,7 @@ const set_response = class {
242
242
  };
243
243
  throw payload;
244
244
  }
245
- static INVALID_TOKEN({ message }) {
245
+ INVALID_TOKEN({ message }) {
246
246
  const payload = {
247
247
  status: 401,
248
248
  code: "INVALID_TOKEN",
@@ -252,7 +252,7 @@ const set_response = class {
252
252
  };
253
253
  throw payload;
254
254
  }
255
- static NOT_FOUND({ message }) {
255
+ NOT_FOUND({ message }) {
256
256
  const payload = {
257
257
  status: 404,
258
258
  code: "NOT_FOUND",
@@ -264,8 +264,8 @@ const set_response = class {
264
264
  };
265
265
  };
266
266
  const _set_response = set_response;
267
- const _token = class {
268
- static async verificar_token(c, next) {
267
+ const _token = new class {
268
+ async verificar_token(c, next) {
269
269
  try {
270
270
  const authHeader = c.req.header("Authorization");
271
271
  if (!authHeader) return _set_response.c.INVALID_TOKEN({
@@ -305,7 +305,7 @@ const _token = class {
305
305
  });
306
306
  }
307
307
  }
308
- static async criar_token_login_usuario({ _id, email, app, usuario_tipo, ativo, data_criacao, nome, c }) {
308
+ async criar_token_login_usuario({ _id, email, app, usuario_tipo, ativo, data_criacao, nome, c }) {
309
309
  if (!c.env.JSON_WEB_TOKEN_AUTH_USER) return _set_response.error.WARNING({
310
310
  message: "Erro ao gerar token!!",
311
311
  results: []
@@ -326,18 +326,80 @@ const _token = class {
326
326
  }));
327
327
  return token;
328
328
  }
329
+ async verificar_token_micro_servico(c, next) {
330
+ try {
331
+ const authHeader = c.req.header("Authorization");
332
+ if (!authHeader) return _set_response.c.INVALID_TOKEN({
333
+ message: 'token service não enviado!',
334
+ c: c,
335
+ results: []
336
+ });
337
+ const token = authHeader.split(" ")[1];
338
+ if (!token) return _set_response.c.INVALID_TOKEN({
339
+ message: 'Acesso negado ao serviço!',
340
+ c: c,
341
+ results: []
342
+ });
343
+ const secret = new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_MICRO_SERVICO);
344
+ const { payload } = await jwtVerify(token, secret);
345
+ if (!payload._id || !payload.email) return _set_response.c.INVALID_TOKEN({
346
+ message: 'Token inválido para o serviço!',
347
+ c: c,
348
+ results: []
349
+ });
350
+ const setar_token = {
351
+ _id: payload._id,
352
+ email: payload.email,
353
+ ativo: payload.ativo,
354
+ data_criacao: payload.data_criacao,
355
+ nome: payload.nome,
356
+ app: payload.app,
357
+ usuario_tipo: payload.usuario_tipo
358
+ };
359
+ c.set("usuario_auth", setar_token);
360
+ return await next();
361
+ } catch (error) {
362
+ return _set_response.c.INVALID_TOKEN({
363
+ message: 'Erro token inválido!',
364
+ c: c,
365
+ results: []
366
+ });
367
+ }
368
+ }
369
+ async criar_token_micro_servico({ c }) {
370
+ if (!c.env.JSON_WEB_TOKEN_AUTH_MICRO_SERVICO) return _set_response.error.WARNING({
371
+ message: "Erro ao gerar token!!",
372
+ results: []
373
+ });
374
+ const usuario_auth = c.get("usuario_auth");
375
+ const token = await new SignJWT({
376
+ _id: usuario_auth?._id,
377
+ email: usuario_auth?.email,
378
+ app: usuario_auth?.app,
379
+ usuario_tipo: usuario_auth?.usuario_tipo,
380
+ ativo: usuario_auth?.ativo,
381
+ data_criacao: usuario_auth?.data_criacao,
382
+ nome: usuario_auth?.nome
383
+ }).setProtectedHeader({
384
+ alg: "HS256"
385
+ }).setIssuedAt().setExpirationTime("30s").sign(new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_MICRO_SERVICO)).catch((error)=>_set_response.error.WARNING({
386
+ message: "Erro ao gerar token!",
387
+ results: error?.message
388
+ }));
389
+ return token;
390
+ }
329
391
  };
330
392
  const helpers_token = _token;
331
393
  const scryptAsync = promisify(scrypt);
332
394
  class _secret {
333
- static SALT_LENGTH = 22;
334
- static KEY_LENGTH = 35;
335
- static async gerar_hash_senha(password) {
395
+ SALT_LENGTH = 22;
396
+ KEY_LENGTH = 35;
397
+ async gerar_hash_senha(password) {
336
398
  const salt = randomBytes(this.SALT_LENGTH);
337
399
  const derivedKey = await scryptAsync(password, salt, this.KEY_LENGTH);
338
400
  return salt.toString('base64') + '.' + derivedKey.toString('base64');
339
401
  }
340
- static async verify({ password, dashed_senha }) {
402
+ async verify({ password, dashed_senha }) {
341
403
  try {
342
404
  const [saltBase64, hashBase64] = dashed_senha.split('.');
343
405
  if (!saltBase64 || !hashBase64) return false;
@@ -350,18 +412,18 @@ class _secret {
350
412
  }
351
413
  }
352
414
  }
353
- const helpers_secret = _secret;
415
+ const helpers_secret = new _secret;
354
416
  class _data {
355
- static get get_now_format_number() {
417
+ get get_now_format_number() {
356
418
  return Math.floor(Date.now() / 1000);
357
419
  }
358
- static verificar_data(value) {
420
+ verificar_data(value) {
359
421
  const data = new Date(1000 * value);
360
422
  if (isNaN(data.getTime())) throw new Error("Timestamp está inválido.");
361
423
  return value;
362
424
  }
363
425
  }
364
- const helpers_data = _data;
426
+ const helpers_data = new _data;
365
427
  const helpers = {
366
428
  set_response: _set_response,
367
429
  token: helpers_token,