akeyless-client-commons 1.1.6 → 1.1.8

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,4 +1,12 @@
1
1
  "use strict";
2
+ function _array_like_to_array(arr, len) {
3
+ if (len == null || len > arr.length) len = arr.length;
4
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
5
+ return arr2;
6
+ }
7
+ function _array_with_holes(arr) {
8
+ if (Array.isArray(arr)) return arr;
9
+ }
2
10
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
3
11
  try {
4
12
  var info = gen[key](arg);
@@ -28,6 +36,25 @@ function _async_to_generator(fn) {
28
36
  });
29
37
  };
30
38
  }
39
+ function _class_call_check(instance, Constructor) {
40
+ if (!(instance instanceof Constructor)) {
41
+ throw new TypeError("Cannot call a class as a function");
42
+ }
43
+ }
44
+ function _defineProperties(target, props) {
45
+ for(var i = 0; i < props.length; i++){
46
+ var descriptor = props[i];
47
+ descriptor.enumerable = descriptor.enumerable || false;
48
+ descriptor.configurable = true;
49
+ if ("value" in descriptor) descriptor.writable = true;
50
+ Object.defineProperty(target, descriptor.key, descriptor);
51
+ }
52
+ }
53
+ function _create_class(Constructor, protoProps, staticProps) {
54
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
55
+ if (staticProps) _defineProperties(Constructor, staticProps);
56
+ return Constructor;
57
+ }
31
58
  function _define_property(obj, key, value) {
32
59
  if (key in obj) {
33
60
  Object.defineProperty(obj, key, {
@@ -41,6 +68,33 @@ function _define_property(obj, key, value) {
41
68
  }
42
69
  return obj;
43
70
  }
71
+ function _iterable_to_array_limit(arr, i) {
72
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
73
+ if (_i == null) return;
74
+ var _arr = [];
75
+ var _n = true;
76
+ var _d = false;
77
+ var _s, _e;
78
+ try {
79
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
80
+ _arr.push(_s.value);
81
+ if (i && _arr.length === i) break;
82
+ }
83
+ } catch (err) {
84
+ _d = true;
85
+ _e = err;
86
+ } finally{
87
+ try {
88
+ if (!_n && _i["return"] != null) _i["return"]();
89
+ } finally{
90
+ if (_d) throw _e;
91
+ }
92
+ }
93
+ return _arr;
94
+ }
95
+ function _non_iterable_rest() {
96
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
97
+ }
44
98
  function _object_spread(target) {
45
99
  for(var i = 1; i < arguments.length; i++){
46
100
  var source = arguments[i] != null ? arguments[i] : {};
@@ -80,10 +134,21 @@ function _object_spread_props(target, source) {
80
134
  }
81
135
  return target;
82
136
  }
137
+ function _sliced_to_array(arr, i) {
138
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
139
+ }
83
140
  function _type_of(obj) {
84
141
  "@swc/helpers - typeof";
85
142
  return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
86
143
  }
144
+ function _unsupported_iterable_to_array(o, minLen) {
145
+ if (!o) return;
146
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
147
+ var n = Object.prototype.toString.call(o).slice(8, -1);
148
+ if (n === "Object" && o.constructor) n = o.constructor.name;
149
+ if (n === "Map" || n === "Set") return Array.from(n);
150
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
151
+ }
87
152
  function _ts_generator(thisArg, body) {
88
153
  var f, y, t, g, _ = {
89
154
  label: 0,
@@ -255,8 +320,14 @@ __export(index_exports, {
255
320
  useSetUserCountry: function() {
256
321
  return useSetUserCountry;
257
322
  },
323
+ useSmartSnapshot: function() {
324
+ return useSmartSnapshot;
325
+ },
258
326
  useSnapshotBulk: function() {
259
327
  return useSnapshotBulk;
328
+ },
329
+ useSocketSubscription: function() {
330
+ return useSocketSubscription;
260
331
  }
261
332
  });
262
333
  module.exports = __toCommonJS(index_exports);
@@ -366,6 +437,50 @@ var simpleExtractData = function(doc2) {
366
437
  id: doc2.id
367
438
  });
368
439
  };
440
+ var get_document_by_id = /*#__PURE__*/ function() {
441
+ var _ref = _async_to_generator(function(collection_path, doc_id) {
442
+ var doc_ref, doc_snap, error;
443
+ return _ts_generator(this, function(_state) {
444
+ switch(_state.label){
445
+ case 0:
446
+ _state.trys.push([
447
+ 0,
448
+ 2,
449
+ ,
450
+ 3
451
+ ]);
452
+ doc_ref = (0, import_firestore.doc)(db, collection_path, doc_id);
453
+ return [
454
+ 4,
455
+ (0, import_firestore.getDoc)(doc_ref)
456
+ ];
457
+ case 1:
458
+ doc_snap = _state.sent();
459
+ if (!doc_snap.exists()) {
460
+ throw new Error("Document not found, document id: " + doc_id);
461
+ }
462
+ return [
463
+ 2,
464
+ simpleExtractData(doc_snap)
465
+ ];
466
+ case 2:
467
+ error = _state.sent();
468
+ console.error("Error from get_document_by_id", error);
469
+ return [
470
+ 2,
471
+ null
472
+ ];
473
+ case 3:
474
+ return [
475
+ 2
476
+ ];
477
+ }
478
+ });
479
+ });
480
+ return function get_document_by_id(collection_path, doc_id) {
481
+ return _ref.apply(this, arguments);
482
+ };
483
+ }();
369
484
  var snapshot = function(config, snapshotsFirstTime, settings) {
370
485
  var resolvePromise;
371
486
  var isResolved = false;
@@ -513,6 +628,277 @@ var biDomain = isLocal ? "http://localhost:9002/api/bi" : baseDomain + "/bi";
513
628
  var notificationsDomain = isLocal ? "http://localhost:9006/api/notifications" : baseDomain + "/notifications";
514
629
  var callCenterGeoDomain = isLocal ? "http://localhost:9007/api/call-center/geo" : baseDomain + "/call-center/geo";
515
630
  var callCenterEventsDomain = isLocal ? "http://localhost:9008/api/call-center/events" : baseDomain + "/call-center/events";
631
+ // src/helpers/socket.ts
632
+ var import_socket = require("socket.io-client");
633
+ var SocketService = /*#__PURE__*/ function() {
634
+ "use strict";
635
+ function _SocketService() {
636
+ _class_call_check(this, _SocketService);
637
+ this.socket = null;
638
+ this.connectCallbacks = [];
639
+ this.disconnectCallbacks = [];
640
+ }
641
+ _create_class(_SocketService, [
642
+ {
643
+ /// Initialize the socket connection
644
+ key: "initSocket",
645
+ value: function initSocket() {
646
+ var _this = this;
647
+ if (!this.socket) {
648
+ var SOCKET_SERVER_URL = "http://localhost:9009";
649
+ var SOCKET_PATH = "/api/data-socket/connect";
650
+ this.socket = (0, import_socket.io)(SOCKET_SERVER_URL, {
651
+ path: SOCKET_PATH,
652
+ transports: [
653
+ "websocket"
654
+ ]
655
+ });
656
+ this.socket.on("connect", function() {
657
+ var _this_socket;
658
+ console.log("Socket connected:", (_this_socket = _this.socket) === null || _this_socket === void 0 ? void 0 : _this_socket.id);
659
+ _this.connectCallbacks.forEach(function(cb) {
660
+ return cb();
661
+ });
662
+ });
663
+ this.socket.on("disconnect", function(reason) {
664
+ console.log("Socket disconnected:", reason);
665
+ _this.disconnectCallbacks.forEach(function(cb) {
666
+ return cb();
667
+ });
668
+ });
669
+ this.socket.on("connect_error", function(error) {
670
+ console.error("Socket connection error:", error);
671
+ });
672
+ }
673
+ }
674
+ },
675
+ {
676
+ /// get socket instance
677
+ key: "getSocketInstance",
678
+ value: function getSocketInstance() {
679
+ if (!this.socket) {
680
+ this.initSocket();
681
+ }
682
+ if (!this.socket) {
683
+ throw new Error("Socket not initialized");
684
+ }
685
+ if (!this.socket.connected) {
686
+ this.socket.connect();
687
+ }
688
+ return this.socket;
689
+ }
690
+ },
691
+ {
692
+ /// subscribe to collections
693
+ key: "subscribeToCollections",
694
+ value: function subscribeToCollections(config) {
695
+ var s = this.getSocketInstance();
696
+ var collectionsNames = config.map(function(c) {
697
+ return c.collectionName;
698
+ });
699
+ var eventHandlers = [];
700
+ config.forEach(function(configuration) {
701
+ var collectionName = configuration.collectionName, onAdd = configuration.onAdd, onFirstTime = configuration.onFirstTime, onModify = configuration.onModify, onRemove = configuration.onRemove, extraParsers = configuration.extraParsers, conditions = configuration.conditions, orderBy2 = configuration.orderBy;
702
+ s.on("initial:".concat(collectionName), onFirstTime);
703
+ eventHandlers.push({
704
+ eventName: "initial:".concat(collectionName),
705
+ handler: onFirstTime
706
+ });
707
+ s.on("add:".concat(collectionName), onAdd);
708
+ eventHandlers.push({
709
+ eventName: "add:".concat(collectionName),
710
+ handler: onAdd
711
+ });
712
+ s.on("update:".concat(collectionName), onModify);
713
+ eventHandlers.push({
714
+ eventName: "update:".concat(collectionName),
715
+ handler: onModify
716
+ });
717
+ s.on("delete:".concat(collectionName), onRemove);
718
+ eventHandlers.push({
719
+ eventName: "delete:".concat(collectionName),
720
+ handler: onRemove
721
+ });
722
+ extraParsers === null || extraParsers === void 0 ? void 0 : extraParsers.forEach(function(parsers) {
723
+ var extraOnAdd = parsers.onAdd, extraOnFirstTime = parsers.onFirstTime, extraOnModify = parsers.onModify, extraOnRemove = parsers.onRemove;
724
+ s.on("initial:".concat(collectionName), extraOnFirstTime);
725
+ eventHandlers.push({
726
+ eventName: "initial:".concat(collectionName),
727
+ handler: extraOnFirstTime
728
+ });
729
+ s.on("add:".concat(collectionName), extraOnAdd);
730
+ eventHandlers.push({
731
+ eventName: "add:".concat(collectionName),
732
+ handler: extraOnAdd
733
+ });
734
+ s.on("update:".concat(collectionName), extraOnModify);
735
+ eventHandlers.push({
736
+ eventName: "update:".concat(collectionName),
737
+ handler: extraOnModify
738
+ });
739
+ s.on("delete:".concat(collectionName), extraOnRemove);
740
+ eventHandlers.push({
741
+ eventName: "delete:".concat(collectionName),
742
+ handler: extraOnRemove
743
+ });
744
+ });
745
+ });
746
+ s.emit("subscribe_collections", collectionsNames, function(callback) {
747
+ if (callback.success) {
748
+ console.log("Successfully subscribed to: ".concat(collectionsNames.join(", ")));
749
+ } else {
750
+ console.error("Failed to subscribe to ".concat(config.join(", "), ": ").concat(callback.message));
751
+ }
752
+ });
753
+ return function() {
754
+ console.log("Cleaning up subscriptions for: ".concat(collectionsNames.join(", ")));
755
+ s.emit("unsubscribe_collections", collectionsNames);
756
+ eventHandlers.forEach(function(eh) {
757
+ s.off(eh.eventName, eh.handler);
758
+ });
759
+ };
760
+ }
761
+ },
762
+ {
763
+ /// set data
764
+ key: "setData",
765
+ value: function setData(payload) {
766
+ var s = this.getSocketInstance();
767
+ return new Promise(function(resolve, reject) {
768
+ s.emit("set_data", payload, function(callback) {
769
+ if (callback.success) {
770
+ console.log("Data saved successfully:", payload);
771
+ console.log("ack", callback);
772
+ resolve(callback);
773
+ } else {
774
+ reject(new Error(callback.message || "Save operation failed"));
775
+ }
776
+ });
777
+ });
778
+ }
779
+ },
780
+ {
781
+ /// get data
782
+ key: "getCollectionData",
783
+ value: function getCollectionData(payload) {
784
+ var s = this.getSocketInstance();
785
+ s.emit("get_data", {
786
+ collection_name: payload.collection_name
787
+ }, function(socketCallback) {
788
+ if (socketCallback.success && socketCallback.data) {
789
+ payload.callback(socketCallback.data);
790
+ } else {
791
+ payload.callback(payload.defaultValue);
792
+ }
793
+ });
794
+ }
795
+ },
796
+ {
797
+ key: "getDocumentData",
798
+ value: function getDocumentData(payload) {
799
+ var s = this.getSocketInstance();
800
+ s.emit("get_data", {
801
+ collection_name: payload.collection_name,
802
+ key: payload.key
803
+ }, function(socketCallback) {
804
+ if (socketCallback.success && socketCallback.data) {
805
+ payload.callback(socketCallback.data);
806
+ } else {
807
+ payload.callback(payload.defaultValue);
808
+ }
809
+ });
810
+ }
811
+ },
812
+ {
813
+ /// delete data
814
+ key: "deleteData",
815
+ value: function deleteData(payload) {
816
+ var s = this.getSocketInstance();
817
+ return new Promise(function(resolve, reject) {
818
+ s.emit("delete_data", payload, function(callback) {
819
+ if (callback.success) {
820
+ console.log("Data deleted successfully:", payload);
821
+ console.log("delete ack", callback);
822
+ resolve(callback);
823
+ } else {
824
+ reject(new Error(callback.message || "Delete operation failed"));
825
+ }
826
+ });
827
+ });
828
+ }
829
+ },
830
+ {
831
+ key: "clearAllRedisData",
832
+ value: function clearAllRedisData() {
833
+ var s = this.getSocketInstance();
834
+ return new Promise(function(resolve, reject) {
835
+ s.emit("clear_all_redis_data", function(ack) {
836
+ if (ack.success) {
837
+ resolve(ack);
838
+ } else {
839
+ reject(new Error(ack.message || "Clear all Redis data operation failed"));
840
+ }
841
+ });
842
+ });
843
+ }
844
+ },
845
+ {
846
+ /// connection management methods
847
+ key: "onConnect",
848
+ value: function onConnect(callback) {
849
+ var _this_socket;
850
+ this.connectCallbacks.push(callback);
851
+ if ((_this_socket = this.socket) === null || _this_socket === void 0 ? void 0 : _this_socket.connected) {
852
+ callback();
853
+ }
854
+ }
855
+ },
856
+ {
857
+ key: "offConnect",
858
+ value: function offConnect(callback) {
859
+ this.connectCallbacks = this.connectCallbacks.filter(function(cb) {
860
+ return cb !== callback;
861
+ });
862
+ }
863
+ },
864
+ {
865
+ key: "onDisconnect",
866
+ value: function onDisconnect(callback) {
867
+ this.disconnectCallbacks.push(callback);
868
+ if (this.socket && !this.socket.connected) {
869
+ callback();
870
+ }
871
+ }
872
+ },
873
+ {
874
+ key: "offDisconnect",
875
+ value: function offDisconnect(callback) {
876
+ this.disconnectCallbacks = this.disconnectCallbacks.filter(function(cb) {
877
+ return cb !== callback;
878
+ });
879
+ }
880
+ },
881
+ {
882
+ key: "isConnected",
883
+ value: function isConnected() {
884
+ var _this_socket;
885
+ return ((_this_socket = this.socket) === null || _this_socket === void 0 ? void 0 : _this_socket.connected) || false;
886
+ }
887
+ }
888
+ ], [
889
+ {
890
+ key: "getInstance",
891
+ value: function getInstance() {
892
+ if (!_SocketService.instance) {
893
+ _SocketService.instance = new _SocketService();
894
+ }
895
+ return _SocketService.instance;
896
+ }
897
+ }
898
+ ]);
899
+ return _SocketService;
900
+ }();
901
+ var socketServiceInstance = SocketService.getInstance();
516
902
  // src/hooks/react.ts
517
903
  var import_lodash2 = require("lodash");
518
904
  var import_react2 = require("react");
@@ -637,6 +1023,76 @@ var useSetUserCountry = function(setUserCountry, changLang) {
637
1023
  };
638
1024
  // src/hooks/WebWorker.ts
639
1025
  var import_react5 = require("react");
1026
+ // src/hooks/socket.ts
1027
+ var import_react6 = require("react");
1028
+ var useSocketSubscription = function(config) {
1029
+ var _ref = _sliced_to_array((0, import_react6.useState)(socketServiceInstance.isConnected()), 2), socketConnected = _ref[0], setSocketConnected = _ref[1];
1030
+ useDeepCompareEffect(function() {
1031
+ var cleanupSubscriptions;
1032
+ if (socketConnected) {
1033
+ cleanupSubscriptions = socketServiceInstance.subscribeToCollections(config);
1034
+ }
1035
+ var uiOnConnect = function() {
1036
+ setSocketConnected(true);
1037
+ };
1038
+ var uiOnDisconnect = function() {
1039
+ cleanupSubscriptions === null || cleanupSubscriptions === void 0 ? void 0 : cleanupSubscriptions();
1040
+ setSocketConnected(false);
1041
+ };
1042
+ socketServiceInstance.onConnect(uiOnConnect);
1043
+ socketServiceInstance.onDisconnect(uiOnDisconnect);
1044
+ setSocketConnected(socketServiceInstance.isConnected());
1045
+ return function() {
1046
+ console.log("cleanupSubscriptions");
1047
+ cleanupSubscriptions === null || cleanupSubscriptions === void 0 ? void 0 : cleanupSubscriptions();
1048
+ socketServiceInstance.offConnect(uiOnConnect);
1049
+ socketServiceInstance.offDisconnect(uiOnDisconnect);
1050
+ };
1051
+ }, [
1052
+ socketConnected
1053
+ ]);
1054
+ return {
1055
+ socketConnected: socketConnected
1056
+ };
1057
+ };
1058
+ var useSmartSnapshot = function(configs, options) {
1059
+ var _ref = _sliced_to_array((0, import_react6.useState)(null), 2), cacheCollectionsConfig = _ref[0], setCacheCollectionsConfig = _ref[1];
1060
+ (0, import_react6.useEffect)(function() {
1061
+ get_document_by_id("nx-settings", "cache_collections_config").then(function(res) {
1062
+ return setCacheCollectionsConfig(res);
1063
+ });
1064
+ return function() {
1065
+ return setCacheCollectionsConfig(null);
1066
+ };
1067
+ }, []);
1068
+ var groupedConfig = useDeepCompareMemo(function() {
1069
+ if (!cacheCollectionsConfig) {
1070
+ return {
1071
+ configForDb: [],
1072
+ configForCache: []
1073
+ };
1074
+ }
1075
+ var configForDb = [];
1076
+ var configForCache = [];
1077
+ configs.forEach(function(cfg) {
1078
+ var collectionName = cfg.collectionName, _cfg_subscribeTo = cfg.subscribeTo, subscribeTo = _cfg_subscribeTo === void 0 ? "cache" : _cfg_subscribeTo;
1079
+ if (subscribeTo === "cache" && cacheCollectionsConfig[collectionName]) {
1080
+ configForCache.push(cfg);
1081
+ } else {
1082
+ configForDb.push(cfg);
1083
+ }
1084
+ });
1085
+ return {
1086
+ configForDb: configForDb,
1087
+ configForCache: configForCache
1088
+ };
1089
+ }, [
1090
+ configs,
1091
+ cacheCollectionsConfig
1092
+ ]);
1093
+ useSnapshotBulk(groupedConfig.configForDb, options === null || options === void 0 ? void 0 : options.label, options === null || options === void 0 ? void 0 : options.settings);
1094
+ useSocketSubscription(groupedConfig.configForCache);
1095
+ };
640
1096
  // Annotate the CommonJS export names for ESM import in node:
641
1097
  0 && (module.exports = {
642
1098
  useDeepCompareEffect: useDeepCompareEffect,
@@ -644,6 +1100,8 @@ var import_react5 = require("react");
644
1100
  useDocumentTitle: useDocumentTitle,
645
1101
  useSafeEffect: useSafeEffect,
646
1102
  useSetUserCountry: useSetUserCountry,
647
- useSnapshotBulk: useSnapshotBulk
1103
+ useSmartSnapshot: useSmartSnapshot,
1104
+ useSnapshotBulk: useSnapshotBulk,
1105
+ useSocketSubscription: useSocketSubscription
648
1106
  });
649
1107
  //# sourceMappingURL=index.js.map