@navservice/core 1.64.0 → 1.65.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.
- package/build/es/helpers/index.d.ts +11 -11
- package/build/lib/helpers/index.d.ts +11 -11
- package/build/lib/{index.browser.cjs → index.cjs} +43 -85
- package/package.json +4 -9
- package/build/es/index.node.js +0 -374
- package/build/es/utils.js +0 -638
- package/build/lib/index.node.cjs +0 -510
- package/build/lib/utils.cjs +0 -877
- /package/build/es/{index.browser.d.ts → index.d.ts} +0 -0
- /package/build/es/{index.browser.js → index.js} +0 -0
- /package/build/lib/{index.browser.d.ts → index.d.ts} +0 -0
package/build/es/index.node.js
DELETED
|
@@ -1,374 +0,0 @@
|
|
|
1
|
-
import v4 from "zod/v4";
|
|
2
|
-
import { SignJWT, jwtVerify } from "jose";
|
|
3
|
-
import { randomBytes, scrypt, timingSafeEqual } from "node:crypto";
|
|
4
|
-
import { promisify } from "node:util";
|
|
5
|
-
const set_response = class {
|
|
6
|
-
static c = class {
|
|
7
|
-
static SUCCESS({ message, results, c }) {
|
|
8
|
-
const payload = {
|
|
9
|
-
status: 200,
|
|
10
|
-
code: "SUCCESS",
|
|
11
|
-
type: "success",
|
|
12
|
-
message: message || "Realizado com sucesso!",
|
|
13
|
-
results: results || []
|
|
14
|
-
};
|
|
15
|
-
if (c) return c.json(payload, {
|
|
16
|
-
status: 200
|
|
17
|
-
});
|
|
18
|
-
return payload;
|
|
19
|
-
}
|
|
20
|
-
static ACTION_REQUIRED({ message, results, c }) {
|
|
21
|
-
const payload = {
|
|
22
|
-
status: 428,
|
|
23
|
-
code: "ACTION_REQUIRED",
|
|
24
|
-
type: "warning",
|
|
25
|
-
message: message || "Ação adicional necessária!",
|
|
26
|
-
results: results || []
|
|
27
|
-
};
|
|
28
|
-
if (c) return c.json(payload, {
|
|
29
|
-
status: 428
|
|
30
|
-
});
|
|
31
|
-
return payload;
|
|
32
|
-
}
|
|
33
|
-
static CREATED({ message, results, c }) {
|
|
34
|
-
const payload = {
|
|
35
|
-
status: 201,
|
|
36
|
-
code: "CREATED",
|
|
37
|
-
type: "success",
|
|
38
|
-
message: message || "Criado com sucesso!",
|
|
39
|
-
results: results || []
|
|
40
|
-
};
|
|
41
|
-
return c ? c.json(payload, {
|
|
42
|
-
status: 201
|
|
43
|
-
}) : payload;
|
|
44
|
-
}
|
|
45
|
-
static WARNING({ message, results, c }) {
|
|
46
|
-
const payload = {
|
|
47
|
-
status: 400,
|
|
48
|
-
code: "WARNING",
|
|
49
|
-
type: "warning",
|
|
50
|
-
message: message || "Aviso!",
|
|
51
|
-
results: results || []
|
|
52
|
-
};
|
|
53
|
-
if (c) c.json(payload, {
|
|
54
|
-
status: 400
|
|
55
|
-
});
|
|
56
|
-
throw payload;
|
|
57
|
-
}
|
|
58
|
-
static AUTHORIZATION_ERROR({ message, results, c }) {
|
|
59
|
-
const payload = {
|
|
60
|
-
status: 405,
|
|
61
|
-
code: "WARNING",
|
|
62
|
-
type: "warning",
|
|
63
|
-
message: message || "Aviso!",
|
|
64
|
-
results: results || []
|
|
65
|
-
};
|
|
66
|
-
if (c) c.json(payload, {
|
|
67
|
-
status: 405
|
|
68
|
-
});
|
|
69
|
-
throw payload;
|
|
70
|
-
}
|
|
71
|
-
static SERVER_ERROR({ error, c }) {
|
|
72
|
-
if (error instanceof v4.ZodError) {
|
|
73
|
-
const payload = {
|
|
74
|
-
status: 500,
|
|
75
|
-
code: "SCHEMA_VALIDATION",
|
|
76
|
-
type: "warning",
|
|
77
|
-
message: "Erro ao validar dados!",
|
|
78
|
-
results: v4.treeifyError(error)
|
|
79
|
-
};
|
|
80
|
-
return c.json(payload, {
|
|
81
|
-
status: 500
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
const payload = {
|
|
85
|
-
status: error?.status || 500,
|
|
86
|
-
code: error?.code || "SERVER_ERROR",
|
|
87
|
-
type: error?.type || "error",
|
|
88
|
-
message: error?.message || "Erro interno no servidor!",
|
|
89
|
-
results: error?.results || []
|
|
90
|
-
};
|
|
91
|
-
return c.json(payload, {
|
|
92
|
-
status: error?.status || error?.status || 500
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
static UNAUTHORIZED({ message, c }) {
|
|
96
|
-
const payload = {
|
|
97
|
-
status: 401,
|
|
98
|
-
code: "UNAUTHORIZED",
|
|
99
|
-
type: "error",
|
|
100
|
-
message: message || "Não autorizado!",
|
|
101
|
-
results: []
|
|
102
|
-
};
|
|
103
|
-
return c ? c.json(payload, {
|
|
104
|
-
status: 401
|
|
105
|
-
}) : payload;
|
|
106
|
-
}
|
|
107
|
-
static INVALID_TOKEN({ message, c }) {
|
|
108
|
-
const payload = {
|
|
109
|
-
status: 401,
|
|
110
|
-
code: "INVALID_TOKEN",
|
|
111
|
-
type: "warning",
|
|
112
|
-
message: message || "Token inválido!",
|
|
113
|
-
results: []
|
|
114
|
-
};
|
|
115
|
-
return c ? c.json(payload, {
|
|
116
|
-
status: 401
|
|
117
|
-
}) : payload;
|
|
118
|
-
}
|
|
119
|
-
static NOT_FOUND({ message, c }) {
|
|
120
|
-
const payload = {
|
|
121
|
-
status: 404,
|
|
122
|
-
code: "NOT_FOUND",
|
|
123
|
-
type: "error",
|
|
124
|
-
message: message || "Recurso não encontrado!",
|
|
125
|
-
results: []
|
|
126
|
-
};
|
|
127
|
-
return c ? c.json(payload, {
|
|
128
|
-
status: 404
|
|
129
|
-
}) : payload;
|
|
130
|
-
}
|
|
131
|
-
static async SUCCESS_FILE({ message, file_buffer, content_type, filename, c }) {
|
|
132
|
-
if (c && content_type) {
|
|
133
|
-
const headers = {
|
|
134
|
-
"Content-Type": content_type,
|
|
135
|
-
"Content-Disposition": filename ? `inline; filename="${filename}"` : "inline"
|
|
136
|
-
};
|
|
137
|
-
return new Response(file_buffer, {
|
|
138
|
-
status: 200,
|
|
139
|
-
headers
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
const payload = {
|
|
143
|
-
status: 200,
|
|
144
|
-
code: "SUCCESS_FILE",
|
|
145
|
-
type: "success",
|
|
146
|
-
message: message || "Erro ao retornar arquivo!",
|
|
147
|
-
results: []
|
|
148
|
-
};
|
|
149
|
-
return c ? c.json(payload, {
|
|
150
|
-
status: 200
|
|
151
|
-
}) : payload;
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
static error = class {
|
|
155
|
-
static ACTION_REQUIRED({ message, results }) {
|
|
156
|
-
const payload = {
|
|
157
|
-
status: 428,
|
|
158
|
-
code: "ACTION_REQUIRED",
|
|
159
|
-
type: "warning",
|
|
160
|
-
message: message || "Ação adicional necessária!",
|
|
161
|
-
results: results || []
|
|
162
|
-
};
|
|
163
|
-
throw payload;
|
|
164
|
-
}
|
|
165
|
-
static WARNING({ message, results }) {
|
|
166
|
-
const payload = {
|
|
167
|
-
status: 400,
|
|
168
|
-
code: "WARNING",
|
|
169
|
-
type: "warning",
|
|
170
|
-
message: message || "Aviso!",
|
|
171
|
-
results: results || []
|
|
172
|
-
};
|
|
173
|
-
throw payload;
|
|
174
|
-
}
|
|
175
|
-
static AUTHORIZATION_ERROR({ message, results }) {
|
|
176
|
-
const payload = {
|
|
177
|
-
status: 400,
|
|
178
|
-
code: "WARNING",
|
|
179
|
-
type: "warning",
|
|
180
|
-
message: message || "Aviso!",
|
|
181
|
-
results: results || []
|
|
182
|
-
};
|
|
183
|
-
throw payload;
|
|
184
|
-
}
|
|
185
|
-
static DATABASE_ERROR({ message, results, erro }) {
|
|
186
|
-
const errorCode = erro?.cause?.code || erro?.cause?.cause?.code || erro?.code || "";
|
|
187
|
-
const map = {
|
|
188
|
-
SQLITE_CONSTRAINT_UNIQUE: {
|
|
189
|
-
status: 409,
|
|
190
|
-
message: "Registro duplicado. Violação de UNIQUE."
|
|
191
|
-
},
|
|
192
|
-
SQLITE_CONSTRAINT_FOREIGNKEY: {
|
|
193
|
-
status: 409,
|
|
194
|
-
message: "Registro relacionado não encontrado (FOREIGN KEY)."
|
|
195
|
-
},
|
|
196
|
-
SQLITE_CONSTRAINT_NOTNULL: {
|
|
197
|
-
status: 400,
|
|
198
|
-
message: "Campo obrigatório ausente."
|
|
199
|
-
},
|
|
200
|
-
SQLITE_CONSTRAINT_CHECK: {
|
|
201
|
-
status: 400,
|
|
202
|
-
message: "Regra de validação violada."
|
|
203
|
-
},
|
|
204
|
-
SQLITE_CONSTRAINT_PRIMARYKEY: {
|
|
205
|
-
status: 409,
|
|
206
|
-
message: "Chave primária duplicada."
|
|
207
|
-
},
|
|
208
|
-
SQLITE_BUSY: {
|
|
209
|
-
status: 503,
|
|
210
|
-
message: "Banco ocupado. Tente novamente."
|
|
211
|
-
},
|
|
212
|
-
SQLITE_READONLY: {
|
|
213
|
-
status: 500,
|
|
214
|
-
message: "Banco de dados em modo somente leitura."
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
const causeMessage = erro?.cause?.message;
|
|
218
|
-
const isUniqueMessage = causeMessage?.includes("UNIQUE constraint failed");
|
|
219
|
-
const isForeignMessage = causeMessage?.includes("FOREIGN KEY constraint failed");
|
|
220
|
-
const mapped = map[errorCode] || (isUniqueMessage ? map["SQLITE_CONSTRAINT_UNIQUE"] : null) || (isForeignMessage ? map["SQLITE_CONSTRAINT_FOREIGNKEY"] : null);
|
|
221
|
-
const payload = {
|
|
222
|
-
status: mapped?.status || 500,
|
|
223
|
-
code: "DATABASE_ERROR",
|
|
224
|
-
type: "error",
|
|
225
|
-
message: mapped?.message || message || "Erro no banco de dados!",
|
|
226
|
-
results: []
|
|
227
|
-
};
|
|
228
|
-
throw payload;
|
|
229
|
-
}
|
|
230
|
-
static SCHEMA_VALIDATION({ results }) {
|
|
231
|
-
const payload = {
|
|
232
|
-
status: 500,
|
|
233
|
-
code: "SCHEMA_VALIDATION",
|
|
234
|
-
type: "error",
|
|
235
|
-
message: "Erro ao validar dados!",
|
|
236
|
-
results: results || []
|
|
237
|
-
};
|
|
238
|
-
throw payload;
|
|
239
|
-
}
|
|
240
|
-
static UNAUTHORIZED({ message }) {
|
|
241
|
-
const payload = {
|
|
242
|
-
status: 401,
|
|
243
|
-
code: "UNAUTHORIZED",
|
|
244
|
-
type: "error",
|
|
245
|
-
message: message || "Não autorizado!"
|
|
246
|
-
};
|
|
247
|
-
throw payload;
|
|
248
|
-
}
|
|
249
|
-
static INVALID_TOKEN({ message }) {
|
|
250
|
-
const payload = {
|
|
251
|
-
status: 401,
|
|
252
|
-
code: "INVALID_TOKEN",
|
|
253
|
-
type: "warning",
|
|
254
|
-
message: message || "Token inválido!",
|
|
255
|
-
results: []
|
|
256
|
-
};
|
|
257
|
-
throw payload;
|
|
258
|
-
}
|
|
259
|
-
static NOT_FOUND({ message }) {
|
|
260
|
-
const payload = {
|
|
261
|
-
status: 404,
|
|
262
|
-
code: "NOT_FOUND",
|
|
263
|
-
type: "error",
|
|
264
|
-
message: message || "Recurso não encontrado!"
|
|
265
|
-
};
|
|
266
|
-
throw payload;
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
};
|
|
270
|
-
const _set_response = set_response;
|
|
271
|
-
const _token = class {
|
|
272
|
-
static async verificar_token(c, next) {
|
|
273
|
-
try {
|
|
274
|
-
const authHeader = c.req.header("Authorization");
|
|
275
|
-
if (!authHeader) return src_helpers.set_response.c.INVALID_TOKEN({
|
|
276
|
-
message: 'token não enviado!!!',
|
|
277
|
-
c: c,
|
|
278
|
-
results: []
|
|
279
|
-
});
|
|
280
|
-
const token = authHeader.split(" ")[1];
|
|
281
|
-
if (!token) return src_helpers.set_response.c.INVALID_TOKEN({
|
|
282
|
-
message: 'Acesso negado!!!',
|
|
283
|
-
c: c,
|
|
284
|
-
results: []
|
|
285
|
-
});
|
|
286
|
-
const secret = new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_USER);
|
|
287
|
-
const { payload } = await jwtVerify(token, secret);
|
|
288
|
-
if (!payload._id || !payload.email) return src_helpers.set_response.c.INVALID_TOKEN({
|
|
289
|
-
message: 'Token inválido!!!!',
|
|
290
|
-
c: c,
|
|
291
|
-
results: []
|
|
292
|
-
});
|
|
293
|
-
const setar_token = {
|
|
294
|
-
_id: payload._id,
|
|
295
|
-
email: payload.email,
|
|
296
|
-
ativo: payload.ativo,
|
|
297
|
-
data_criacao: payload.data_criacao,
|
|
298
|
-
nome: payload.nome,
|
|
299
|
-
app: payload.app,
|
|
300
|
-
usuario_tipo: payload.usuario_tipo
|
|
301
|
-
};
|
|
302
|
-
c.set("usuario_auth", setar_token);
|
|
303
|
-
return await next();
|
|
304
|
-
} catch (error) {
|
|
305
|
-
return src_helpers.set_response.c.INVALID_TOKEN({
|
|
306
|
-
message: 'Token inválido!!!',
|
|
307
|
-
c: c,
|
|
308
|
-
results: []
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
static async criar_token_login_usuario({ _id, email, app, usuario_tipo, ativo, data_criacao, nome, c }) {
|
|
313
|
-
if (!c.env.JSON_WEB_TOKEN_AUTH_USER) return src_helpers.set_response.error.WARNING({
|
|
314
|
-
message: "Erro ao gerar token!!",
|
|
315
|
-
results: []
|
|
316
|
-
});
|
|
317
|
-
const token = await new SignJWT({
|
|
318
|
-
_id: _id,
|
|
319
|
-
email: email,
|
|
320
|
-
app: app,
|
|
321
|
-
usuario_tipo: usuario_tipo,
|
|
322
|
-
ativo: ativo,
|
|
323
|
-
data_criacao: data_criacao,
|
|
324
|
-
nome: nome
|
|
325
|
-
}).setProtectedHeader({
|
|
326
|
-
alg: "HS256"
|
|
327
|
-
}).setIssuedAt().sign(new TextEncoder().encode(c.env.JSON_WEB_TOKEN_AUTH_USER)).catch((error)=>src_helpers.set_response.error.WARNING({
|
|
328
|
-
message: "Erro ao gerar token!",
|
|
329
|
-
results: error?.message
|
|
330
|
-
}));
|
|
331
|
-
return token;
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
const helpers_token = _token;
|
|
335
|
-
const scryptAsync = promisify(scrypt);
|
|
336
|
-
class _secret {
|
|
337
|
-
static SALT_LENGTH = 22;
|
|
338
|
-
static KEY_LENGTH = 35;
|
|
339
|
-
static async gerar_hash_senha(password) {
|
|
340
|
-
const salt = randomBytes(this.SALT_LENGTH);
|
|
341
|
-
const derivedKey = await scryptAsync(password, salt, this.KEY_LENGTH);
|
|
342
|
-
return salt.toString('base64') + '.' + derivedKey.toString('base64');
|
|
343
|
-
}
|
|
344
|
-
static async verify({ password, dashed_senha }) {
|
|
345
|
-
try {
|
|
346
|
-
const [saltBase64, hashBase64] = dashed_senha.split('.');
|
|
347
|
-
if (!saltBase64 || !hashBase64) return false;
|
|
348
|
-
const salt = Buffer.from(saltBase64, 'base64');
|
|
349
|
-
const storedHash = Buffer.from(hashBase64, 'base64');
|
|
350
|
-
const derivedKey = await scryptAsync(password, salt, this.KEY_LENGTH);
|
|
351
|
-
return timingSafeEqual(storedHash, derivedKey);
|
|
352
|
-
} catch {
|
|
353
|
-
return false;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
const helpers_secret = _secret;
|
|
358
|
-
const _data = {
|
|
359
|
-
get_now_format_number: Math.floor(Date.now() / 1000),
|
|
360
|
-
verificar_data: (value)=>{
|
|
361
|
-
const data = new Date(1000 * value);
|
|
362
|
-
if (isNaN(data.getTime())) throw new Error("Timestamp está inválido.");
|
|
363
|
-
return value;
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
const helpers_data = _data;
|
|
367
|
-
const helpers = {
|
|
368
|
-
set_response: _set_response,
|
|
369
|
-
token: helpers_token,
|
|
370
|
-
secret: helpers_secret,
|
|
371
|
-
data: helpers_data
|
|
372
|
-
};
|
|
373
|
-
const src_helpers = helpers;
|
|
374
|
-
export { src_helpers as helpers };
|