@dnax/core 0.76.8 → 0.76.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.
Files changed (2) hide show
  1. package/package.json +2 -1
  2. package/utils/index.ts +64 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.76.8",
3
+ "version": "0.76.9",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},
@@ -37,6 +37,7 @@
37
37
  "joi": "18.0.2",
38
38
  "json-joy": "16.8.0",
39
39
  "jsonwebtoken": "^9.0.3",
40
+ "liquidjs": "^10.24.0",
40
41
  "mime-types": "^2.1.35",
41
42
  "moment": "^2.30.1",
42
43
  "mongodb": "7.0.0",
package/utils/index.ts CHANGED
@@ -26,6 +26,9 @@ import type { SupportedCryptoAlgorithms } from "bun";
26
26
  //import {applyPatch} from "json-joy/lib/json-patch"
27
27
  import cleanDeep from "clean-deep";
28
28
 
29
+ import { Liquid } from "liquidjs";
30
+ const engineTemplate = new Liquid();
31
+
29
32
  function copy(data: object): object | any {
30
33
  return deepCopy(data);
31
34
  }
@@ -595,11 +598,70 @@ function getRecentDate<T extends Record<string, any>>(
595
598
  return { data: latestItem, date: latest };
596
599
  }
597
600
 
601
+ /**
602
+ * Compile et exécute un template Liquid avec les paramètres fournis
603
+ * @param {string} templateContent - Le contenu du template avec les variables {{variable}}
604
+ * @param {Object} params - Les paramètres à remplacer dans le template
605
+ * @returns {Promise<string>} - Le template compilé avec les valeurs remplacées
606
+ */
607
+ async function renderTemplate(templateContent: string, params: object = {}) {
608
+ try {
609
+ // Rendre le template avec les paramètres
610
+ const result = await engineTemplate.parseAndRender(templateContent, params);
611
+ return result;
612
+ } catch (error) {
613
+ console.error("Erreur lors du rendu du template:", error);
614
+ throw error;
615
+ }
616
+ }
617
+
618
+ // Version synchrone (si besoin)
619
+ function renderTemplateSync(templateContent: string, params: object = {}) {
620
+ try {
621
+ const result = engineTemplate.parseAndRenderSync(templateContent, params);
622
+ return result;
623
+ } catch (error) {
624
+ console.error(error);
625
+ throw error;
626
+ }
627
+ }
628
+
629
+ // eventBus.ts
630
+ type Handler<Payload = any> = (payload: Payload) => void | Promise<void>;
631
+
632
+ class EventBus<Events extends Record<string, any> = Record<string, any>> {
633
+ private listeners = new Map<keyof Events, Handler[]>();
634
+
635
+ on<K extends keyof Events>(event: K, handler: Handler<Events[K]>) {
636
+ const list = this.listeners.get(event) ?? [];
637
+ list.push(handler);
638
+ this.listeners.set(event, list);
639
+ }
640
+
641
+ off<K extends keyof Events>(event: K, handler: Handler<Events[K]>) {
642
+ const list = this.listeners.get(event);
643
+ if (!list) return;
644
+ this.listeners.set(
645
+ event,
646
+ list.filter((h) => h !== handler)
647
+ );
648
+ }
649
+
650
+ async emit<K extends keyof Events>(event: K, payload: Events[K]) {
651
+ const list = this.listeners.get(event);
652
+ if (!list) return;
653
+ for (const handler of list) {
654
+ await handler(payload);
655
+ }
656
+ }
657
+ }
658
+
598
659
  const contextError = ContextError;
599
660
  export {
600
661
  file,
601
662
  mime,
602
663
  dotJson,
664
+ EventBus,
603
665
  uuid,
604
666
  pick,
605
667
  password, // Hash and verify Password utils
@@ -635,4 +697,6 @@ export {
635
697
  getRecentDate,
636
698
  getPidUsage,
637
699
  cleanDeep,
700
+ renderTemplate,
701
+ renderTemplateSync,
638
702
  };