@dnax/core 0.16.4 → 0.16.6
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/app/hono.ts +40 -9
- package/driver/mongo/rest.ts +179 -3
- package/lib/asyncLocalStorage.ts +22 -16
- package/package.json +1 -1
- package/tsconfig.json +2 -0
- package/types/index.ts +8 -2
- package/utils/index.ts +7 -2
package/app/hono.ts
CHANGED
|
@@ -92,14 +92,10 @@ function HonoInstance(): typeof app {
|
|
|
92
92
|
var token = jwt.getToken("Bearer", c.req.header()["authorization"]) || "";
|
|
93
93
|
var { decode, valid, error } = jwt.verify(token);
|
|
94
94
|
let sessionData = {};
|
|
95
|
-
if (valid && decode) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
//console.log("Data", data);
|
|
100
|
-
sessionData = data;
|
|
101
|
-
})
|
|
102
|
-
.catch();
|
|
95
|
+
if (valid && decode && decode?.session) {
|
|
96
|
+
sessionData = decode?.session;
|
|
97
|
+
} else {
|
|
98
|
+
// console.log("No valid token", error);
|
|
103
99
|
}
|
|
104
100
|
|
|
105
101
|
let _v = {
|
|
@@ -107,7 +103,10 @@ function HonoInstance(): typeof app {
|
|
|
107
103
|
ip:
|
|
108
104
|
c.req.raw.headers?.get("CF-Connecting-IP") ||
|
|
109
105
|
c.req.raw.headers?.get("x-forwarded-for") ||
|
|
110
|
-
c.req.raw.headers?.get("x-real-ip")
|
|
106
|
+
c.req.raw.headers?.get("x-real-ip") ||
|
|
107
|
+
c.req.raw.headers?.get("remoteaddr") ||
|
|
108
|
+
"::1",
|
|
109
|
+
|
|
111
110
|
isAuth: valid ? true : false,
|
|
112
111
|
reqAt: moment().format().toString(),
|
|
113
112
|
setAt: sessionData?._v?.setAt || null,
|
|
@@ -115,6 +114,8 @@ function HonoInstance(): typeof app {
|
|
|
115
114
|
c.set("_v", _v);
|
|
116
115
|
c.set("tenant-id", c.req.header()["tenant-id"]);
|
|
117
116
|
|
|
117
|
+
//console.log("_v", _v);
|
|
118
|
+
|
|
118
119
|
if (token && valid) {
|
|
119
120
|
session.set({
|
|
120
121
|
generateToken: false,
|
|
@@ -340,12 +341,42 @@ function HonoInstance(): typeof app {
|
|
|
340
341
|
responseAuth !== null &&
|
|
341
342
|
responseAuth
|
|
342
343
|
) {
|
|
344
|
+
rest.activity.save({
|
|
345
|
+
collection: collection,
|
|
346
|
+
data: {
|
|
347
|
+
input: body,
|
|
348
|
+
},
|
|
349
|
+
action: "authCollection",
|
|
350
|
+
auth: true,
|
|
351
|
+
user: sessionStorage().get().state?.user,
|
|
352
|
+
ip:
|
|
353
|
+
c.req.raw.headers?.get("CF-Connecting-IP") ||
|
|
354
|
+
c.req.raw.headers?.get("x-forwarded-for") ||
|
|
355
|
+
c.req.raw.headers?.get("x-real-ip") ||
|
|
356
|
+
c.req.raw.headers?.get("remoteaddr") ||
|
|
357
|
+
"::1",
|
|
358
|
+
});
|
|
343
359
|
return c.json({
|
|
344
360
|
auth: true,
|
|
345
361
|
data: responseAuth,
|
|
346
362
|
token: sessionStorage().get().token,
|
|
347
363
|
});
|
|
348
364
|
} else {
|
|
365
|
+
rest.activity.save({
|
|
366
|
+
collection: collection,
|
|
367
|
+
data: {
|
|
368
|
+
input: body,
|
|
369
|
+
},
|
|
370
|
+
action: "authCollection",
|
|
371
|
+
auth: false,
|
|
372
|
+
user: sessionStorage().get().state?.user,
|
|
373
|
+
ip:
|
|
374
|
+
c.req.raw.headers?.get("CF-Connecting-IP") ||
|
|
375
|
+
c.req.raw.headers?.get("x-forwarded-for") ||
|
|
376
|
+
c.req.raw.headers?.get("x-real-ip") ||
|
|
377
|
+
c.req.raw.headers?.get("remoteaddr") ||
|
|
378
|
+
"::1",
|
|
379
|
+
});
|
|
349
380
|
c.status(401);
|
|
350
381
|
return c.json({ message: "Authentification failed" });
|
|
351
382
|
}
|
package/driver/mongo/rest.ts
CHANGED
|
@@ -75,6 +75,23 @@ class useRest {
|
|
|
75
75
|
Direct Api Mongo , use it with caution
|
|
76
76
|
*/
|
|
77
77
|
db: Tenant["database"]["db"];
|
|
78
|
+
activity: {
|
|
79
|
+
save?: (activity: {
|
|
80
|
+
collection?: string;
|
|
81
|
+
data: {
|
|
82
|
+
input: any;
|
|
83
|
+
};
|
|
84
|
+
action: string;
|
|
85
|
+
ip?: string;
|
|
86
|
+
user?: object;
|
|
87
|
+
}) => Promise<any>;
|
|
88
|
+
list?: (filter?: {
|
|
89
|
+
$match: {};
|
|
90
|
+
$limit?: number;
|
|
91
|
+
$sort?: {};
|
|
92
|
+
$skip?: number;
|
|
93
|
+
}) => Promise<Array<any>>;
|
|
94
|
+
};
|
|
78
95
|
|
|
79
96
|
constructor(options: options) {
|
|
80
97
|
this.#c = options.c;
|
|
@@ -85,6 +102,18 @@ class useRest {
|
|
|
85
102
|
this.#useHook = options?.useHook ?? true;
|
|
86
103
|
this.db = this.#tenant.database.db;
|
|
87
104
|
|
|
105
|
+
this.activity = {
|
|
106
|
+
save: async (activityInput) => {
|
|
107
|
+
return await restActivity.save(this.#tenant, activityInput);
|
|
108
|
+
},
|
|
109
|
+
list: async (activityFilter) => {
|
|
110
|
+
return await restActivity
|
|
111
|
+
.list(this.#tenant, activityFilter || {})
|
|
112
|
+
.then((e) => e)
|
|
113
|
+
.catch((err) => []);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
88
117
|
/* if (options?.useHook == undefined || options?.useHook == null) {
|
|
89
118
|
this.#useHook = true;
|
|
90
119
|
} else {
|
|
@@ -335,6 +364,18 @@ class useRest {
|
|
|
335
364
|
}),
|
|
336
365
|
});
|
|
337
366
|
}
|
|
367
|
+
restActivity.save(this.#tenant, {
|
|
368
|
+
collection: collection,
|
|
369
|
+
data: {
|
|
370
|
+
input: data,
|
|
371
|
+
},
|
|
372
|
+
transaction: this.#session ? true : false,
|
|
373
|
+
action: "insertOne",
|
|
374
|
+
user:
|
|
375
|
+
sessionStorage().get().state?.user ||
|
|
376
|
+
sessionStorage().get().state?.by ||
|
|
377
|
+
null,
|
|
378
|
+
});
|
|
338
379
|
return resolve(toJson(data));
|
|
339
380
|
} catch (err) {
|
|
340
381
|
return reject(err);
|
|
@@ -442,6 +483,18 @@ class useRest {
|
|
|
442
483
|
}),
|
|
443
484
|
});
|
|
444
485
|
}
|
|
486
|
+
restActivity.save(this.#tenant, {
|
|
487
|
+
collection: collection,
|
|
488
|
+
data: {
|
|
489
|
+
input: toJson(data),
|
|
490
|
+
},
|
|
491
|
+
action: "insertMany",
|
|
492
|
+
transaction: this.#session ? true : false,
|
|
493
|
+
user:
|
|
494
|
+
sessionStorage().get().state?.user ||
|
|
495
|
+
sessionStorage().get().state?.by ||
|
|
496
|
+
null,
|
|
497
|
+
});
|
|
445
498
|
|
|
446
499
|
return resolve(toJson(data));
|
|
447
500
|
} catch (err) {
|
|
@@ -492,6 +545,7 @@ class useRest {
|
|
|
492
545
|
* @param params - Find params
|
|
493
546
|
* @param options
|
|
494
547
|
*/
|
|
548
|
+
|
|
495
549
|
async find(
|
|
496
550
|
collection: string,
|
|
497
551
|
params: findParam,
|
|
@@ -835,6 +889,20 @@ class useRest {
|
|
|
835
889
|
});
|
|
836
890
|
}
|
|
837
891
|
|
|
892
|
+
restActivity.save(this.#tenant, {
|
|
893
|
+
collection: collection,
|
|
894
|
+
data: {
|
|
895
|
+
input: update,
|
|
896
|
+
id: id,
|
|
897
|
+
ids: [id],
|
|
898
|
+
},
|
|
899
|
+
transaction: this.#session ? true : false,
|
|
900
|
+
action: "updateOne",
|
|
901
|
+
user:
|
|
902
|
+
sessionStorage().get().state?.user ||
|
|
903
|
+
sessionStorage().get().state?.by ||
|
|
904
|
+
null,
|
|
905
|
+
});
|
|
838
906
|
return resolve(result.doc);
|
|
839
907
|
} catch (err) {
|
|
840
908
|
return reject(err);
|
|
@@ -1098,7 +1166,19 @@ class useRest {
|
|
|
1098
1166
|
}),
|
|
1099
1167
|
});
|
|
1100
1168
|
}
|
|
1101
|
-
|
|
1169
|
+
restActivity.save(this.#tenant, {
|
|
1170
|
+
collection: collection,
|
|
1171
|
+
data: {
|
|
1172
|
+
input: update,
|
|
1173
|
+
ids: ids,
|
|
1174
|
+
},
|
|
1175
|
+
action: "updateMany",
|
|
1176
|
+
transaction: this.#session ? true : false,
|
|
1177
|
+
user:
|
|
1178
|
+
sessionStorage().get().state?.user ||
|
|
1179
|
+
sessionStorage().get().state?.by ||
|
|
1180
|
+
null,
|
|
1181
|
+
});
|
|
1102
1182
|
return resolve(result.docs);
|
|
1103
1183
|
} else {
|
|
1104
1184
|
throw new contextError("List of id required", 400);
|
|
@@ -1194,7 +1274,20 @@ class useRest {
|
|
|
1194
1274
|
}),
|
|
1195
1275
|
});
|
|
1196
1276
|
}
|
|
1197
|
-
|
|
1277
|
+
restActivity.save(this.#tenant, {
|
|
1278
|
+
collection: collection,
|
|
1279
|
+
data: {
|
|
1280
|
+
input: doc,
|
|
1281
|
+
id: id,
|
|
1282
|
+
ids: [id],
|
|
1283
|
+
},
|
|
1284
|
+
transaction: this.#session ? true : false,
|
|
1285
|
+
action: "deleteOne",
|
|
1286
|
+
user:
|
|
1287
|
+
sessionStorage().get().state?.user ||
|
|
1288
|
+
sessionStorage().get().state?.by ||
|
|
1289
|
+
null,
|
|
1290
|
+
});
|
|
1198
1291
|
return resolve(doc);
|
|
1199
1292
|
} catch (err) {
|
|
1200
1293
|
return resolve(err);
|
|
@@ -1288,6 +1381,20 @@ class useRest {
|
|
|
1288
1381
|
});
|
|
1289
1382
|
}
|
|
1290
1383
|
|
|
1384
|
+
restActivity.save(this.#tenant, {
|
|
1385
|
+
collection: collection,
|
|
1386
|
+
data: {
|
|
1387
|
+
input: null,
|
|
1388
|
+
ids: ids,
|
|
1389
|
+
},
|
|
1390
|
+
transaction: this.#session ? true : false,
|
|
1391
|
+
action: "deleteMany",
|
|
1392
|
+
user:
|
|
1393
|
+
sessionStorage().get().state?.user ||
|
|
1394
|
+
sessionStorage().get().state?.by ||
|
|
1395
|
+
null,
|
|
1396
|
+
});
|
|
1397
|
+
|
|
1291
1398
|
return resolve(deletedIds);
|
|
1292
1399
|
} catch (err) {
|
|
1293
1400
|
return reject(err);
|
|
@@ -1301,7 +1408,23 @@ class useRest {
|
|
|
1301
1408
|
.estimatedDocumentCount();
|
|
1302
1409
|
}
|
|
1303
1410
|
async dropCollection(Collection: string) {
|
|
1304
|
-
return
|
|
1411
|
+
return new Promise(async (resolve, reject) => {
|
|
1412
|
+
try {
|
|
1413
|
+
let r = await this.#tenant.database.db?.collection(Collection).drop();
|
|
1414
|
+
restActivity.save(this.#tenant, {
|
|
1415
|
+
collection: Collection,
|
|
1416
|
+
data: {},
|
|
1417
|
+
action: "dropCollection",
|
|
1418
|
+
user:
|
|
1419
|
+
sessionStorage().get().state?.user ||
|
|
1420
|
+
sessionStorage().get().state?.by ||
|
|
1421
|
+
null,
|
|
1422
|
+
});
|
|
1423
|
+
return resolve(r);
|
|
1424
|
+
} catch (err) {
|
|
1425
|
+
return reject(err);
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1305
1428
|
}
|
|
1306
1429
|
|
|
1307
1430
|
async dropIndexes(Collection: string) {
|
|
@@ -1344,4 +1467,57 @@ class useRest {
|
|
|
1344
1467
|
}
|
|
1345
1468
|
}
|
|
1346
1469
|
|
|
1470
|
+
const restActivity = {
|
|
1471
|
+
list: async (
|
|
1472
|
+
tenant: Tenant,
|
|
1473
|
+
filter?: {
|
|
1474
|
+
$match?: {};
|
|
1475
|
+
$limit?: number;
|
|
1476
|
+
$sort?: {};
|
|
1477
|
+
$skip?: number;
|
|
1478
|
+
}
|
|
1479
|
+
) => {
|
|
1480
|
+
return await tenant.database.db
|
|
1481
|
+
?.collection("___activity__")
|
|
1482
|
+
.find({
|
|
1483
|
+
...filter?.$match,
|
|
1484
|
+
})
|
|
1485
|
+
.limit(filter?.$limit || 100)
|
|
1486
|
+
.sort({ createdAt: -1, ...filter?.$sort })
|
|
1487
|
+
.skip(filter?.$skip || 0)
|
|
1488
|
+
.toArray()
|
|
1489
|
+
.catch((err) => []);
|
|
1490
|
+
},
|
|
1491
|
+
save: async (
|
|
1492
|
+
tenant: Tenant,
|
|
1493
|
+
activity: {
|
|
1494
|
+
collection?: string;
|
|
1495
|
+
data: any;
|
|
1496
|
+
action: string;
|
|
1497
|
+
ip?: string;
|
|
1498
|
+
user?: object;
|
|
1499
|
+
transaction?: boolean;
|
|
1500
|
+
auth?: boolean;
|
|
1501
|
+
}
|
|
1502
|
+
) => {
|
|
1503
|
+
return await tenant.database.db
|
|
1504
|
+
?.collection("___activity__")
|
|
1505
|
+
.insertOne(
|
|
1506
|
+
toBson({
|
|
1507
|
+
ip: activity?.ip,
|
|
1508
|
+
tenant: tenant.id,
|
|
1509
|
+
data: activity?.data || {},
|
|
1510
|
+
action: activity.action,
|
|
1511
|
+
collection: activity?.collection,
|
|
1512
|
+
auth: activity?.auth ?? false,
|
|
1513
|
+
createdAt: moment().format(),
|
|
1514
|
+
updatedAt: moment().format(),
|
|
1515
|
+
user: activity?.user,
|
|
1516
|
+
transaction: activity?.transaction ?? false,
|
|
1517
|
+
})
|
|
1518
|
+
)
|
|
1519
|
+
.catch();
|
|
1520
|
+
},
|
|
1521
|
+
};
|
|
1522
|
+
|
|
1347
1523
|
export { useRest };
|
package/lib/asyncLocalStorage.ts
CHANGED
|
@@ -39,26 +39,15 @@ const sessionStorage = () => ({
|
|
|
39
39
|
id: "",
|
|
40
40
|
state: {},
|
|
41
41
|
_v: {},
|
|
42
|
-
generateToken: true,
|
|
43
42
|
}
|
|
44
43
|
): any {
|
|
45
|
-
input.generateToken = input?.generateToken ?? true;
|
|
46
44
|
let store = asyncLocalStorage.getStore() as InstanceType<typeof Map>;
|
|
47
45
|
if (!input?.id) {
|
|
48
46
|
input.id = v4();
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
if (input?.generateToken) {
|
|
52
|
-
input.token =
|
|
53
|
-
input.token ||
|
|
54
|
-
jwt.sign(input.id, {
|
|
55
|
-
expiresIn: input?.expiresIn || "7d",
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
49
|
let data = {
|
|
60
|
-
|
|
61
|
-
state: input?.state,
|
|
50
|
+
state: input?.state || {},
|
|
62
51
|
role: input?.role || null,
|
|
63
52
|
_v: {
|
|
64
53
|
...(input?._v || {}),
|
|
@@ -66,11 +55,28 @@ const sessionStorage = () => ({
|
|
|
66
55
|
},
|
|
67
56
|
};
|
|
68
57
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
58
|
+
input.token =
|
|
59
|
+
input.token ||
|
|
60
|
+
jwt.sign(
|
|
61
|
+
{
|
|
62
|
+
id: input.id,
|
|
63
|
+
state: input?.state,
|
|
64
|
+
role: input?.role || null,
|
|
65
|
+
_v: {
|
|
66
|
+
...(input?._v || {}),
|
|
67
|
+
setAt: new Date().toString(),
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
expiresIn: input?.expiresIn || "7d",
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
store.set(key, {
|
|
76
|
+
...data,
|
|
77
|
+
token: input.token,
|
|
78
|
+
});
|
|
72
79
|
|
|
73
|
-
store.set(key, data);
|
|
74
80
|
return {
|
|
75
81
|
state: input.state,
|
|
76
82
|
_v: input._v,
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED
package/types/index.ts
CHANGED
|
@@ -162,14 +162,20 @@ export type accessCtx = (ctx: {
|
|
|
162
162
|
|
|
163
163
|
export type sessionCtx = {
|
|
164
164
|
set: (session: {
|
|
165
|
-
state:
|
|
165
|
+
state: {
|
|
166
|
+
[key: string]: any;
|
|
167
|
+
user?: object;
|
|
168
|
+
};
|
|
166
169
|
token?: string;
|
|
167
170
|
_v?: object;
|
|
168
171
|
role?: string | null | undefined;
|
|
169
172
|
expiresIn?: string;
|
|
170
173
|
}) => void;
|
|
171
174
|
get: () => {
|
|
172
|
-
state:
|
|
175
|
+
state: {
|
|
176
|
+
[key: string]: any;
|
|
177
|
+
user?: object;
|
|
178
|
+
};
|
|
173
179
|
_v: object;
|
|
174
180
|
role: string | null | undefined;
|
|
175
181
|
token: string | null | undefined;
|
package/utils/index.ts
CHANGED
|
@@ -59,7 +59,7 @@ const jwt = {
|
|
|
59
59
|
) => {
|
|
60
60
|
// console.log("payload", payload, options);
|
|
61
61
|
|
|
62
|
-
return jwto.sign({
|
|
62
|
+
return jwto.sign({ session: payload }, JWT_SECRET, {
|
|
63
63
|
...options,
|
|
64
64
|
});
|
|
65
65
|
},
|
|
@@ -98,9 +98,14 @@ function isDate(date: string): boolean {
|
|
|
98
98
|
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
99
99
|
const dateRegex2 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/;
|
|
100
100
|
const dateRegex3 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/;
|
|
101
|
+
// le regex pour cette forme 2024-11-30T21:36:12+00:00
|
|
102
|
+
const dateRegex4 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}$/;
|
|
101
103
|
let isDate_ =
|
|
102
104
|
!isNaN(Date.parse(date)) &&
|
|
103
|
-
(dateRegex.test(date) ||
|
|
105
|
+
(dateRegex.test(date) ||
|
|
106
|
+
dateRegex2.test(date) ||
|
|
107
|
+
dateRegex4.test(date) ||
|
|
108
|
+
dateRegex3.test(date));
|
|
104
109
|
if (isDate_) return true;
|
|
105
110
|
try {
|
|
106
111
|
isDate_ = moment(date, "YYYY-MM-DD", true).isValid();
|