@anteros/core 0.0.7 → 0.0.9

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.
@@ -168,7 +168,7 @@ class MongoRest {
168
168
 
169
169
  const { request: requestMeta, ...restMeta } = ctxMeta ?? {}
170
170
 
171
- const meta: Record<string, any> = {
171
+ const meta: any = {
172
172
  ...restMeta,
173
173
  platform: restMeta?.platform ?? os.platform(),
174
174
  core_version: cfg.version ?? restMeta?.core_version,
@@ -273,17 +273,9 @@ class MongoRest {
273
273
  throw isSafe.error;
274
274
  }
275
275
  pipeline = func.toBson(pipeline, { col })
276
+ const meta: any = { action, collection, pipeline }
276
277
  if (col.hooks?.beforeOperation) {
277
- await col.hooks.beforeOperation({
278
- rest: this,
279
- io,
280
- action: action,
281
- meta: {
282
- action,
283
- collection: collection,
284
- pipeline: pipeline,
285
- }
286
- })
278
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
287
279
  }
288
280
 
289
281
  let result = await this.db.collection(collection).aggregate(pipeline, {
@@ -291,18 +283,9 @@ class MongoRest {
291
283
  allowDiskUse: true,
292
284
  }).toArray()
293
285
  result = func.toJson(result)
286
+ meta.result = result
294
287
 
295
- await col.hooks?.afterOperation?.({
296
- rest: this,
297
- io,
298
- action: action,
299
- meta: {
300
- action,
301
- collection: collection,
302
- pipeline: pipeline,
303
- result: result,
304
- }
305
- })
288
+ await col.hooks?.afterOperation?.({ rest: this, io, action, meta })
306
289
 
307
290
  return result as any[]
308
291
  } catch (err: any) {
@@ -320,18 +303,9 @@ class MongoRest {
320
303
  pipeline = await func.buildInput(pipeline, { rest: this })
321
304
  pipeline = func.toBson(pipeline, { col })
322
305
 
306
+ const meta: any = { action, collection, params, options }
323
307
  if (col.hooks?.beforeOperation) {
324
- await col.hooks.beforeOperation({
325
- rest: this,
326
- io,
327
- action: action,
328
- meta: {
329
- action: action,
330
- collection: collection,
331
- params: func.clone(params),
332
- options: func.clone(options),
333
- }
334
- })
308
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
335
309
  }
336
310
 
337
311
  let result = await this.db.collection(collection).aggregate(pipeline, {
@@ -339,20 +313,10 @@ class MongoRest {
339
313
  allowDiskUse: true
340
314
  }).toArray()
341
315
  result = func.toJson(result)
316
+ meta.result = result
342
317
 
343
318
  if (col.hooks?.afterOperation) {
344
- await col.hooks.afterOperation({
345
- rest: this,
346
- io,
347
- action: action,
348
- meta: {
349
- action: action,
350
- collection: collection,
351
- params: func.clone(params),
352
- options: func.clone(options),
353
- result: func.clone(result),
354
- }
355
- })
319
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
356
320
  }
357
321
 
358
322
  return result as any[]
@@ -374,18 +338,9 @@ class MongoRest {
374
338
  pipeline = await func.buildInput(pipeline, { rest: this })
375
339
  pipeline = func.toBson(pipeline, { col })
376
340
 
341
+ const meta: any = { action, collection, params, id: _id }
377
342
  if (col.hooks?.beforeOperation) {
378
- await col.hooks.beforeOperation({
379
- rest: this,
380
- io,
381
- action: action,
382
- meta: {
383
- action: action,
384
- collection: collection,
385
- params: func.clone(params),
386
- id: _id,
387
- }
388
- })
343
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
389
344
  }
390
345
 
391
346
  let result = (await this.db.collection(collection).aggregate(pipeline, {
@@ -393,20 +348,10 @@ class MongoRest {
393
348
  allowDiskUse: true
394
349
  }).toArray()).at(0) ?? null
395
350
  result = func.toJson(result)
351
+ meta.result = result
396
352
 
397
353
  if (col.hooks?.afterOperation) {
398
- await col.hooks.afterOperation({
399
- rest: this,
400
- io,
401
- action: action,
402
- meta: {
403
- action: action,
404
- collection: collection,
405
- result: func.clone(result),
406
- params: func.clone(params),
407
- id: _id,
408
- }
409
- })
354
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
410
355
  }
411
356
 
412
357
  return result
@@ -427,17 +372,9 @@ class MongoRest {
427
372
  })
428
373
  this._validate({ collection, action: action, data: data })
429
374
 
375
+ const meta: any = { action, collection, data }
430
376
  if (col.hooks?.beforeOperation) {
431
- await col.hooks.beforeOperation({
432
- rest: this,
433
- io,
434
- action,
435
- meta: {
436
- action,
437
- collection,
438
- data: func.clone(data),
439
- }
440
- })
377
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
441
378
  }
442
379
 
443
380
  await this.db.collection(collection).insertOne(func.toBson(data, { col }) as any, {
@@ -445,18 +382,9 @@ class MongoRest {
445
382
  })
446
383
 
447
384
  const result: T & { _id: string } = func.toJson(data as T & { _id: string })
385
+ meta.result = result
448
386
  if (col?.hooks?.afterOperation) {
449
- await col.hooks.afterOperation({
450
- rest: this,
451
- io,
452
- action: action,
453
- meta: {
454
- action: action,
455
- collection: collection,
456
- data: func.clone(data),
457
- result: func.clone(result),
458
- }
459
- })
387
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
460
388
  }
461
389
 
462
390
  return result
@@ -482,17 +410,9 @@ class MongoRest {
482
410
 
483
411
  this._validate({ collection, action: action, data: dataInput })
484
412
 
413
+ const meta: any = { action, collection, data: dataInput }
485
414
  if (col.hooks?.beforeOperation) {
486
- await col.hooks.beforeOperation({
487
- rest: this,
488
- io,
489
- action: action,
490
- meta: {
491
- action: action,
492
- collection: collection,
493
- data: func.clone(dataInput),
494
- }
495
- })
415
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
496
416
  }
497
417
 
498
418
  await this.db.collection(collection).insertMany(func.toBson(dataInput, { col }) as any, {
@@ -500,18 +420,10 @@ class MongoRest {
500
420
  })
501
421
 
502
422
  const result: (T & { _id: string })[] = func.toJson(dataInput as (T & { _id: string })[])
423
+ meta.result = result
503
424
 
504
425
  if (col.hooks?.afterOperation) {
505
- await col.hooks.afterOperation({
506
- rest: this,
507
- io,
508
- action: action,
509
- meta: {
510
- action: action,
511
- collection: collection,
512
- result: func.clone(result),
513
- }
514
- })
426
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
515
427
  }
516
428
 
517
429
  return result as (T & { _id: string })[]
@@ -533,18 +445,9 @@ class MongoRest {
533
445
  this._validate({ collection, action: action, update: update })
534
446
  update = func.toBson(update, { col })
535
447
 
448
+ const meta: any = { action, collection, update, id: _id }
536
449
  if (col.hooks?.beforeOperation) {
537
- await col.hooks.beforeOperation({
538
- rest: this,
539
- io,
540
- action: action,
541
- meta: {
542
- action: action,
543
- update: func.clone(update),
544
- collection: collection,
545
- id: _id,
546
- }
547
- })
450
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
548
451
  }
549
452
 
550
453
  const data = await this.db.collection(collection).findOneAndUpdate(
@@ -554,20 +457,10 @@ class MongoRest {
554
457
  )
555
458
 
556
459
  const result = func.toJson(data)
460
+ meta.result = result
557
461
 
558
462
  if (col.hooks?.afterOperation) {
559
- await col.hooks.afterOperation({
560
- rest: this,
561
- io,
562
- action: action,
563
- meta: {
564
- action: action,
565
- collection: collection,
566
- update: func.clone(update),
567
- id: _id,
568
- result: func.clone(result),
569
- }
570
- })
463
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
571
464
  }
572
465
 
573
466
  return result
@@ -616,24 +509,11 @@ class MongoRest {
616
509
  update = func.toBson(update, { col })
617
510
  filter = func.toBson(filter, { col })
618
511
 
512
+ const meta: any = { action, collection, filter, update }
619
513
  if (col.hooks?.beforeOperation) {
620
- await col.hooks.beforeOperation({
621
- rest: this,
622
- io,
623
- action: action,
624
- meta: {
625
- action: action,
626
- collection: collection,
627
- filter: func.clone(filter),
628
- update: func.clone(update),
629
- }
630
- })
514
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
631
515
  }
632
516
 
633
-
634
-
635
-
636
-
637
517
  const data = await this.db.collection(collection).findOneAndUpdate(
638
518
  filter,
639
519
  {
@@ -644,20 +524,10 @@ class MongoRest {
644
524
  )
645
525
 
646
526
  const result = func.toJson(data)
527
+ meta.result = result
647
528
 
648
529
  if (col.hooks?.afterOperation) {
649
- await col.hooks.afterOperation({
650
- rest: this,
651
- io,
652
- action: action,
653
- meta: {
654
- action: action,
655
- collection: collection,
656
- filter: func.clone(filter),
657
- update: func.clone(update),
658
- result: func.clone(result),
659
- }
660
- })
530
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
661
531
  }
662
532
 
663
533
  return result as Document | null
@@ -678,18 +548,9 @@ class MongoRest {
678
548
  this._validate({ collection, action: action, update: update })
679
549
  update = func.toBson(update, { col })
680
550
 
551
+ const meta: any = { action, collection, update, ids: _ids }
681
552
  if (col.hooks?.beforeOperation) {
682
- await col.hooks.beforeOperation({
683
- rest: this,
684
- io,
685
- action: action,
686
- meta: {
687
- action: action,
688
- collection: collection,
689
- update: func.clone(update),
690
- ids: _ids,
691
- }
692
- })
553
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
693
554
  }
694
555
 
695
556
  const result = await this.db.collection(collection).updateMany(
@@ -697,20 +558,10 @@ class MongoRest {
697
558
  { ...(update as any) },
698
559
  { session: this.session }
699
560
  )
561
+ meta.result = result
700
562
 
701
563
  if (col.hooks?.afterOperation) {
702
- await col.hooks.afterOperation({
703
- rest: this,
704
- io,
705
- action: action,
706
- meta: {
707
- action: action,
708
- collection: collection,
709
- update: func.clone(update),
710
- ids: _ids,
711
- result: func.clone(result),
712
- }
713
- })
564
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
714
565
  }
715
566
 
716
567
  return result as UpdateResult
@@ -724,13 +575,9 @@ class MongoRest {
724
575
  return this.#executeWithAudit(action, collection, { id: _id }, async () => {
725
576
  const col = getCollection(collection, this.#tenant.id) as Collection
726
577
 
578
+ const meta: any = { action, collection, id: _id }
727
579
  if (col.hooks?.beforeOperation) {
728
- await col.hooks.beforeOperation({
729
- rest: this,
730
- io,
731
- action: action,
732
- meta: { action: action, collection: collection, id: _id }
733
- })
580
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
734
581
  }
735
582
 
736
583
  let result = await this.db.collection(collection).findOneAndDelete(
@@ -738,14 +585,10 @@ class MongoRest {
738
585
  { session: this.session }
739
586
  )
740
587
  result = func.toJson(result)
588
+ meta.result = result
741
589
 
742
590
  if (col.hooks?.afterOperation) {
743
- await col.hooks.afterOperation({
744
- rest: this,
745
- io,
746
- action: action,
747
- meta: { action: action, collection: collection, id: _id, result: func.clone(result) }
748
- })
591
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
749
592
  }
750
593
 
751
594
  return result
@@ -760,13 +603,9 @@ class MongoRest {
760
603
  return this.#executeWithAudit(action, collection, { ids: _ids }, async () => {
761
604
  const col = getCollection(collection, this.#tenant.id) as Collection
762
605
 
606
+ const meta: any = { action, collection, ids: _ids }
763
607
  if (col.hooks?.beforeOperation) {
764
- await col.hooks.beforeOperation({
765
- rest: this,
766
- io,
767
- action: action,
768
- meta: { action: action, collection: collection, ids: _ids }
769
- })
608
+ await col.hooks.beforeOperation({ rest: this, io, action, meta })
770
609
  }
771
610
 
772
611
  let result: DeleteResult = await this.db.collection(collection).deleteMany(
@@ -774,14 +613,10 @@ class MongoRest {
774
613
  { session: this.session }
775
614
  )
776
615
  result = func.toJson(result) as DeleteResult
616
+ meta.result = result
777
617
 
778
618
  if (col.hooks?.afterOperation) {
779
- await col.hooks.afterOperation({
780
- rest: this,
781
- io,
782
- action: action,
783
- meta: { action: action, collection: collection, ids: _ids, result: func.clone(result) }
784
- })
619
+ await col.hooks.afterOperation({ rest: this, io, action, meta })
785
620
  }
786
621
 
787
622
  return result
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@anteros/core",
3
3
  "module": "index.ts",
4
- "version": "0.0.7",
4
+ "version": "0.0.9",
5
5
  "type": "module",
6
6
  "description": "Anteros Core",
7
7
  "private": false,
package/utils/func.ts CHANGED
@@ -209,22 +209,14 @@ function cleanDeep<T extends DeepRecord | DeepRecord[]>(data: T): Partial<T> {
209
209
  }
210
210
 
211
211
  /**
212
- * Vérifie si une string est une date valide.
213
- * Accepte : ISO 8601 (YYYY-MM-DD, YYYY-MM-DDTHH:mm...) et timestamps UNIX (s ou ms).
214
- * Rejette les strings ambiguës que certains moteurs JS parsent en date (ex: "REF-2024").
212
+ * Vérifie si une string est une date ISO 8601 valide.
213
+ * Les timestamps numériques (ex: "1704067200") ne sont PAS convertis ici —
214
+ * ils passent par `isEpochNumber` quand la valeur est un `number`.
215
215
  */
216
216
  function isDate(value: string): boolean {
217
217
  if (!value || typeof value !== 'string') return false;
218
218
  const trimmed = value.trim();
219
219
 
220
- // Timestamps numériques purs (10 ou 13 chiffres)
221
- if (/^\d+$/.test(trimmed)) {
222
- const len = trimmed.length;
223
- if (len !== 10 && len !== 13) return false;
224
- const ms = new Date(Number(trimmed)).getTime();
225
- return !isNaN(ms);
226
- }
227
-
228
220
  // ISO 8601 : YYYY-MM-DD avec heure optionnelle
229
221
  if (!/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d{1,3})?)?(Z|[+-]\d{2}:\d{2})?)?$/.test(trimmed)) {
230
222
  return false;
@@ -234,26 +226,31 @@ function isDate(value: string): boolean {
234
226
  return !isNaN(date.getTime());
235
227
  }
236
228
 
237
- /** Aligné sur `isDate` : `new Date("1438839831092")` est NaN, il faut passer par `Number`. */
229
+ /** Convertit une string ISO 8601 en Date. */
238
230
  function stringToDate(value: string): Date {
239
- const trimmed = value.trim();
240
- if (/^\d+$/.test(trimmed)) {
241
- const len = trimmed.length;
242
- if (len === 10 || len === 13) {
243
- return new Date(Number(trimmed));
244
- }
245
- }
246
- return new Date(trimmed);
231
+ return new Date(value.trim());
247
232
  }
248
233
 
249
- /** Nombre entier type timestamp UNIX (s ou ms), ex. `1438839831092`. */
234
+ /**
235
+ * Nombre entier type timestamp UNIX (s ou ms).
236
+ * Restreint à ≥ an 2000 pour éviter les faux positifs (ex: numéros de téléphone).
237
+ */
250
238
  function isEpochNumber(value: number): boolean {
251
239
  if (typeof value !== 'number' || !Number.isFinite(value)) return false;
252
240
  const int = Math.trunc(value);
253
241
  if (int !== value) return false;
254
242
  const s = String(Math.abs(int));
255
- if (s.length !== 10 && s.length !== 13) return false;
256
- return !isNaN(new Date(int).getTime());
243
+ if (s.length === 10) {
244
+ // Secondes UNIX : ≥ 2000-01-01
245
+ if (int < 946684800) return false;
246
+ return !isNaN(new Date(int * 1000).getTime());
247
+ }
248
+ if (s.length === 13) {
249
+ // Millisecondes UNIX : ≥ 2000-01-01
250
+ if (int < 946684800000) return false;
251
+ return !isNaN(new Date(int).getTime());
252
+ }
253
+ return false;
257
254
  }
258
255
 
259
256
  function deepCopy<T>(value: T): T {
@@ -339,7 +336,10 @@ function formatToDate(data: any, options?: FormatToDateOptions, path = ''): any
339
336
  }
340
337
  } else if (typeof value === 'number' && isEpochNumber(value)) {
341
338
  if (shouldFormatDateAtPath(p, options)) {
342
- result[key] = new Date(value);
339
+ // 10 chiffres = secondes (×1000), 13 chiffres = millisecondes (déjà en ms)
340
+ const absVal = Math.abs(Math.trunc(value));
341
+ const asMs = String(absVal).length === 10 ? value * 1000 : value;
342
+ result[key] = new Date(asMs);
343
343
  } else {
344
344
  result[key] = value;
345
345
  }
@@ -608,8 +608,6 @@ function collectDateFieldPathsFromCollection(col: Collection): string[] {
608
608
 
609
609
  function toBson<T>(data: any | any[], _options?: FormatInputOptions): T {
610
610
  let data_ = formatToObjectId(data);
611
- // Toujours convertir toutes les strings date valide en Date,
612
- // quel que soit le contexte (insert, update, match, pipeline).
613
611
  data_ = formatToDate(data_);
614
612
 
615
613
  if (Array.isArray(data)) {