@novu/js 2.6.6 → 3.0.0-canary.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.
@@ -87,6 +87,229 @@ var areTagsEqual = (tags1, tags2) => {
87
87
  var isSameFilter = (filter1, filter2) => {
88
88
  return areTagsEqual(filter1.tags, filter2.tags) && filter1.read === filter2.read && filter1.archived === filter2.archived;
89
89
  };
90
+
91
+ // src/api/http-client.ts
92
+ var DEFAULT_API_VERSION = "v1";
93
+ var DEFAULT_BACKEND_URL = "https://api.novu.co";
94
+ var DEFAULT_USER_AGENT = `${"@novu/js"}@${"3.0.0-canary.0"}`;
95
+ var HttpClient = class {
96
+ constructor(options = {}) {
97
+ const {
98
+ apiVersion = DEFAULT_API_VERSION,
99
+ apiUrl = DEFAULT_BACKEND_URL,
100
+ userAgent = DEFAULT_USER_AGENT
101
+ } = options || {};
102
+ this.apiVersion = apiVersion;
103
+ this.apiUrl = `${apiUrl}/${this.apiVersion}`;
104
+ this.headers = {
105
+ "Novu-API-Version": "2024-06-26",
106
+ "Content-Type": "application/json",
107
+ "User-Agent": userAgent
108
+ };
109
+ }
110
+ setAuthorizationToken(token) {
111
+ this.headers.Authorization = `Bearer ${token}`;
112
+ }
113
+ setHeaders(headers) {
114
+ this.headers = chunk7B52C2XE_js.__spreadValues(chunk7B52C2XE_js.__spreadValues({}, this.headers), headers);
115
+ }
116
+ get(path, searchParams, unwrapEnvelope = true) {
117
+ return chunk7B52C2XE_js.__async(this, null, function* () {
118
+ return this.doFetch({
119
+ path,
120
+ searchParams,
121
+ options: {
122
+ method: "GET"
123
+ },
124
+ unwrapEnvelope
125
+ });
126
+ });
127
+ }
128
+ post(path, body) {
129
+ return chunk7B52C2XE_js.__async(this, null, function* () {
130
+ return this.doFetch({
131
+ path,
132
+ options: {
133
+ method: "POST",
134
+ body
135
+ }
136
+ });
137
+ });
138
+ }
139
+ patch(path, body) {
140
+ return chunk7B52C2XE_js.__async(this, null, function* () {
141
+ return this.doFetch({
142
+ path,
143
+ options: {
144
+ method: "PATCH",
145
+ body
146
+ }
147
+ });
148
+ });
149
+ }
150
+ delete(path, body) {
151
+ return chunk7B52C2XE_js.__async(this, null, function* () {
152
+ return this.doFetch({
153
+ path,
154
+ options: {
155
+ method: "DELETE",
156
+ body
157
+ }
158
+ });
159
+ });
160
+ }
161
+ doFetch(_0) {
162
+ return chunk7B52C2XE_js.__async(this, arguments, function* ({
163
+ path,
164
+ searchParams,
165
+ options,
166
+ unwrapEnvelope = true
167
+ }) {
168
+ const fullUrl = combineUrl(this.apiUrl, path, searchParams ? `?${searchParams.toString()}` : "");
169
+ const reqInit = {
170
+ method: (options == null ? void 0 : options.method) || "GET",
171
+ headers: chunk7B52C2XE_js.__spreadValues(chunk7B52C2XE_js.__spreadValues({}, this.headers), (options == null ? void 0 : options.headers) || {}),
172
+ body: (options == null ? void 0 : options.body) ? JSON.stringify(options.body) : void 0
173
+ };
174
+ const response = yield fetch(fullUrl, reqInit);
175
+ if (!response.ok) {
176
+ const errorData = yield response.json();
177
+ throw new Error(`${this.headers["User-Agent"]} error. Status: ${response.status}, Message: ${errorData.message}`);
178
+ }
179
+ if (response.status === 204) {
180
+ return void 0;
181
+ }
182
+ const res = yield response.json();
183
+ return unwrapEnvelope ? res.data : res;
184
+ });
185
+ }
186
+ };
187
+ function combineUrl(...args) {
188
+ return args.reduce((acc, part) => {
189
+ if (part) {
190
+ acc.push(part.replace(new RegExp("(?<!https?:)\\/+", "g"), "/").replace(/^\/+|\/+$/g, ""));
191
+ }
192
+ return acc;
193
+ }, []).join("/").replace(/\/\?/, "?");
194
+ }
195
+
196
+ // src/api/inbox-service.ts
197
+ var INBOX_ROUTE = "/inbox";
198
+ var INBOX_NOTIFICATIONS_ROUTE = `${INBOX_ROUTE}/notifications`;
199
+ var _httpClient;
200
+ var InboxService = class {
201
+ constructor(options = {}) {
202
+ this.isSessionInitialized = false;
203
+ chunk7B52C2XE_js.__privateAdd(this, _httpClient);
204
+ chunk7B52C2XE_js.__privateSet(this, _httpClient, new HttpClient(options));
205
+ }
206
+ initializeSession(_0) {
207
+ return chunk7B52C2XE_js.__async(this, arguments, function* ({
208
+ applicationIdentifier,
209
+ subscriberId,
210
+ subscriberHash
211
+ }) {
212
+ const response = yield chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_ROUTE}/session`, {
213
+ applicationIdentifier,
214
+ subscriberId,
215
+ subscriberHash
216
+ });
217
+ chunk7B52C2XE_js.__privateGet(this, _httpClient).setAuthorizationToken(response.token);
218
+ this.isSessionInitialized = true;
219
+ return response;
220
+ });
221
+ }
222
+ fetchNotifications({
223
+ after,
224
+ archived,
225
+ limit = 10,
226
+ offset,
227
+ read: read2,
228
+ tags
229
+ }) {
230
+ const searchParams = new URLSearchParams(`limit=${limit}`);
231
+ if (after) {
232
+ searchParams.append("after", after);
233
+ }
234
+ if (offset) {
235
+ searchParams.append("offset", `${offset}`);
236
+ }
237
+ if (tags) {
238
+ tags.forEach((tag) => searchParams.append("tags[]", tag));
239
+ }
240
+ if (read2 !== void 0) {
241
+ searchParams.append("read", `${read2}`);
242
+ }
243
+ if (archived !== void 0) {
244
+ searchParams.append("archived", `${archived}`);
245
+ }
246
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).get(INBOX_NOTIFICATIONS_ROUTE, searchParams, false);
247
+ }
248
+ count({ filters }) {
249
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).get(
250
+ `${INBOX_NOTIFICATIONS_ROUTE}/count`,
251
+ new URLSearchParams({
252
+ filters: JSON.stringify(filters)
253
+ }),
254
+ false
255
+ );
256
+ }
257
+ read(notificationId) {
258
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/read`);
259
+ }
260
+ unread(notificationId) {
261
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/unread`);
262
+ }
263
+ archive(notificationId) {
264
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/archive`);
265
+ }
266
+ unarchive(notificationId) {
267
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/unarchive`);
268
+ }
269
+ readAll({ tags }) {
270
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/read`, { tags });
271
+ }
272
+ archiveAll({ tags }) {
273
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/archive`, { tags });
274
+ }
275
+ archiveAllRead({ tags }) {
276
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/read-archive`, { tags });
277
+ }
278
+ completeAction({
279
+ actionType,
280
+ notificationId
281
+ }) {
282
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/complete`, {
283
+ actionType
284
+ });
285
+ }
286
+ revertAction({
287
+ actionType,
288
+ notificationId
289
+ }) {
290
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/revert`, {
291
+ actionType
292
+ });
293
+ }
294
+ fetchPreferences(tags) {
295
+ const queryParams = new URLSearchParams();
296
+ if (tags) {
297
+ tags.forEach((tag) => queryParams.append("tags[]", tag));
298
+ }
299
+ const query = queryParams.size ? `?${queryParams.toString()}` : "";
300
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).get(`${INBOX_ROUTE}/preferences${query}`);
301
+ }
302
+ updateGlobalPreferences(channels) {
303
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_ROUTE}/preferences`, channels);
304
+ }
305
+ updateWorkflowPreferences({
306
+ workflowId,
307
+ channels
308
+ }) {
309
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_ROUTE}/preferences/${workflowId}`, channels);
310
+ }
311
+ };
312
+ _httpClient = new WeakMap();
90
313
  var _mittEmitter;
91
314
  var NovuEventEmitter = class {
92
315
  constructor() {
@@ -1029,38 +1252,6 @@ var Notifications = class extends BaseModule {
1029
1252
  };
1030
1253
  _useCache = new WeakMap();
1031
1254
 
1032
- // src/session/session.ts
1033
- var _emitter3, _inboxService2, _options;
1034
- var Session = class {
1035
- constructor(options, inboxServiceInstance, eventEmitterInstance) {
1036
- chunk7B52C2XE_js.__privateAdd(this, _emitter3);
1037
- chunk7B52C2XE_js.__privateAdd(this, _inboxService2);
1038
- chunk7B52C2XE_js.__privateAdd(this, _options);
1039
- chunk7B52C2XE_js.__privateSet(this, _emitter3, eventEmitterInstance);
1040
- chunk7B52C2XE_js.__privateSet(this, _inboxService2, inboxServiceInstance);
1041
- chunk7B52C2XE_js.__privateSet(this, _options, options);
1042
- }
1043
- initialize() {
1044
- return chunk7B52C2XE_js.__async(this, null, function* () {
1045
- try {
1046
- const { applicationIdentifier, subscriberId, subscriberHash } = chunk7B52C2XE_js.__privateGet(this, _options);
1047
- chunk7B52C2XE_js.__privateGet(this, _emitter3).emit("session.initialize.pending", { args: chunk7B52C2XE_js.__privateGet(this, _options) });
1048
- const response = yield chunk7B52C2XE_js.__privateGet(this, _inboxService2).initializeSession({
1049
- applicationIdentifier,
1050
- subscriberId,
1051
- subscriberHash
1052
- });
1053
- chunk7B52C2XE_js.__privateGet(this, _emitter3).emit("session.initialize.resolved", { args: chunk7B52C2XE_js.__privateGet(this, _options), data: response });
1054
- } catch (error) {
1055
- chunk7B52C2XE_js.__privateGet(this, _emitter3).emit("session.initialize.resolved", { args: chunk7B52C2XE_js.__privateGet(this, _options), error });
1056
- }
1057
- });
1058
- }
1059
- };
1060
- _emitter3 = new WeakMap();
1061
- _inboxService2 = new WeakMap();
1062
- _options = new WeakMap();
1063
-
1064
1255
  // src/preferences/helpers.ts
1065
1256
  var updatePreference = (_0) => chunk7B52C2XE_js.__async(void 0, [_0], function* ({
1066
1257
  emitter,
@@ -1144,7 +1335,7 @@ var optimisticUpdateWorkflowPreferences = ({
1144
1335
  };
1145
1336
 
1146
1337
  // src/preferences/preference.ts
1147
- var _emitter4, _apiService, _cache3, _useCache2;
1338
+ var _emitter3, _apiService, _cache3, _useCache2;
1148
1339
  var Preference = class {
1149
1340
  constructor(preference, {
1150
1341
  emitterInstance,
@@ -1152,11 +1343,11 @@ var Preference = class {
1152
1343
  cache,
1153
1344
  useCache
1154
1345
  }) {
1155
- chunk7B52C2XE_js.__privateAdd(this, _emitter4);
1346
+ chunk7B52C2XE_js.__privateAdd(this, _emitter3);
1156
1347
  chunk7B52C2XE_js.__privateAdd(this, _apiService);
1157
1348
  chunk7B52C2XE_js.__privateAdd(this, _cache3);
1158
1349
  chunk7B52C2XE_js.__privateAdd(this, _useCache2);
1159
- chunk7B52C2XE_js.__privateSet(this, _emitter4, emitterInstance);
1350
+ chunk7B52C2XE_js.__privateSet(this, _emitter3, emitterInstance);
1160
1351
  chunk7B52C2XE_js.__privateSet(this, _apiService, inboxServiceInstance);
1161
1352
  chunk7B52C2XE_js.__privateSet(this, _cache3, cache);
1162
1353
  chunk7B52C2XE_js.__privateSet(this, _useCache2, useCache);
@@ -1167,12 +1358,12 @@ var Preference = class {
1167
1358
  }
1168
1359
  update({
1169
1360
  channels,
1170
- // @deprecated use channels instead
1361
+ /** @deprecated Use channels instead */
1171
1362
  channelPreferences
1172
1363
  }) {
1173
1364
  var _a;
1174
1365
  return updatePreference({
1175
- emitter: chunk7B52C2XE_js.__privateGet(this, _emitter4),
1366
+ emitter: chunk7B52C2XE_js.__privateGet(this, _emitter3),
1176
1367
  apiService: chunk7B52C2XE_js.__privateGet(this, _apiService),
1177
1368
  cache: chunk7B52C2XE_js.__privateGet(this, _cache3),
1178
1369
  useCache: chunk7B52C2XE_js.__privateGet(this, _useCache2),
@@ -1189,7 +1380,7 @@ var Preference = class {
1189
1380
  });
1190
1381
  }
1191
1382
  };
1192
- _emitter4 = new WeakMap();
1383
+ _emitter3 = new WeakMap();
1193
1384
  _apiService = new WeakMap();
1194
1385
  _cache3 = new WeakMap();
1195
1386
  _useCache2 = new WeakMap();
@@ -1206,10 +1397,10 @@ var excludeEmpty2 = ({ tags }) => Object.entries({ tags }).reduce((acc, [key, va
1206
1397
  var getCacheKey2 = ({ tags }) => {
1207
1398
  return JSON.stringify(excludeEmpty2({ tags }));
1208
1399
  };
1209
- var _emitter5, _cache4;
1400
+ var _emitter4, _cache4;
1210
1401
  var PreferencesCache = class {
1211
1402
  constructor({ emitterInstance }) {
1212
- chunk7B52C2XE_js.__privateAdd(this, _emitter5);
1403
+ chunk7B52C2XE_js.__privateAdd(this, _emitter4);
1213
1404
  chunk7B52C2XE_js.__privateAdd(this, _cache4);
1214
1405
  this.updatePreference = (key, data) => {
1215
1406
  const preferences = chunk7B52C2XE_js.__privateGet(this, _cache4).get(key);
@@ -1240,14 +1431,14 @@ var PreferencesCache = class {
1240
1431
  if (!hasUpdatedPreference || !updatedPreference) {
1241
1432
  return;
1242
1433
  }
1243
- chunk7B52C2XE_js.__privateGet(this, _emitter5).emit("preferences.list.updated", {
1434
+ chunk7B52C2XE_js.__privateGet(this, _emitter4).emit("preferences.list.updated", {
1244
1435
  data: updatedPreference
1245
1436
  });
1246
1437
  });
1247
1438
  };
1248
- chunk7B52C2XE_js.__privateSet(this, _emitter5, emitterInstance);
1439
+ chunk7B52C2XE_js.__privateSet(this, _emitter4, emitterInstance);
1249
1440
  updateEvents2.forEach((event) => {
1250
- chunk7B52C2XE_js.__privateGet(this, _emitter5).on(event, this.handlePreferenceEvent);
1441
+ chunk7B52C2XE_js.__privateGet(this, _emitter4).on(event, this.handlePreferenceEvent);
1251
1442
  });
1252
1443
  chunk7B52C2XE_js.__privateSet(this, _cache4, new InMemoryCache());
1253
1444
  }
@@ -1266,7 +1457,7 @@ var PreferencesCache = class {
1266
1457
  chunk7B52C2XE_js.__privateGet(this, _cache4).clear();
1267
1458
  }
1268
1459
  };
1269
- _emitter5 = new WeakMap();
1460
+ _emitter4 = new WeakMap();
1270
1461
  _cache4 = new WeakMap();
1271
1462
 
1272
1463
  // src/preferences/preferences.ts
@@ -1319,7 +1510,39 @@ var Preferences = class extends BaseModule {
1319
1510
  }
1320
1511
  };
1321
1512
  _useCache3 = new WeakMap();
1322
- var PRODUCTION_SOCKET_URL = "https://ws.novu.co";
1513
+
1514
+ // src/session/session.ts
1515
+ var _emitter5, _inboxService2, _options;
1516
+ var Session = class {
1517
+ constructor(options, inboxServiceInstance, eventEmitterInstance) {
1518
+ chunk7B52C2XE_js.__privateAdd(this, _emitter5);
1519
+ chunk7B52C2XE_js.__privateAdd(this, _inboxService2);
1520
+ chunk7B52C2XE_js.__privateAdd(this, _options);
1521
+ chunk7B52C2XE_js.__privateSet(this, _emitter5, eventEmitterInstance);
1522
+ chunk7B52C2XE_js.__privateSet(this, _inboxService2, inboxServiceInstance);
1523
+ chunk7B52C2XE_js.__privateSet(this, _options, options);
1524
+ }
1525
+ initialize() {
1526
+ return chunk7B52C2XE_js.__async(this, null, function* () {
1527
+ try {
1528
+ const { applicationIdentifier, subscriberId, subscriberHash } = chunk7B52C2XE_js.__privateGet(this, _options);
1529
+ chunk7B52C2XE_js.__privateGet(this, _emitter5).emit("session.initialize.pending", { args: chunk7B52C2XE_js.__privateGet(this, _options) });
1530
+ const response = yield chunk7B52C2XE_js.__privateGet(this, _inboxService2).initializeSession({
1531
+ applicationIdentifier,
1532
+ subscriberId,
1533
+ subscriberHash
1534
+ });
1535
+ chunk7B52C2XE_js.__privateGet(this, _emitter5).emit("session.initialize.resolved", { args: chunk7B52C2XE_js.__privateGet(this, _options), data: response });
1536
+ } catch (error) {
1537
+ chunk7B52C2XE_js.__privateGet(this, _emitter5).emit("session.initialize.resolved", { args: chunk7B52C2XE_js.__privateGet(this, _options), error });
1538
+ }
1539
+ });
1540
+ }
1541
+ };
1542
+ _emitter5 = new WeakMap();
1543
+ _inboxService2 = new WeakMap();
1544
+ _options = new WeakMap();
1545
+ var PRODUCTION_SOCKET_URL = "https://ws.novu.co";
1323
1546
  var NOTIFICATION_RECEIVED = "notifications.notification_received";
1324
1547
  var UNSEEN_COUNT_CHANGED = "notifications.unseen_count_changed";
1325
1548
  var UNREAD_COUNT_CHANGED = "notifications.unread_count_changed";
@@ -1387,7 +1610,7 @@ var mapToNotification = ({
1387
1610
  data
1388
1611
  };
1389
1612
  };
1390
- var _token, _emitter6, _socketIo, _socketUrl, _notificationReceived, _unseenCountChanged, _unreadCountChanged, _Socket_instances, initializeSocket_fn;
1613
+ var _token, _emitter6, _socketIo, _socketUrl, _notificationReceived, _unseenCountChanged, _unreadCountChanged, _Socket_instances, initializeSocket_fn, handleConnectSocket_fn, handleDisconnectSocket_fn;
1391
1614
  var Socket = class extends BaseModule {
1392
1615
  constructor({
1393
1616
  socketUrl,
@@ -1427,19 +1650,21 @@ var Socket = class extends BaseModule {
1427
1650
  isSocketEvent(eventName) {
1428
1651
  return eventName === NOTIFICATION_RECEIVED || eventName === UNSEEN_COUNT_CHANGED || eventName === UNREAD_COUNT_CHANGED;
1429
1652
  }
1430
- initialize() {
1431
- if (chunk7B52C2XE_js.__privateGet(this, _token)) {
1432
- chunk7B52C2XE_js.__privateMethod(this, _Socket_instances, initializeSocket_fn).call(this).catch((error) => {
1433
- console.error(error);
1434
- });
1435
- return;
1436
- }
1437
- this.callWithSession(() => chunk7B52C2XE_js.__async(this, null, function* () {
1438
- chunk7B52C2XE_js.__privateMethod(this, _Socket_instances, initializeSocket_fn).call(this).catch((error) => {
1439
- console.error(error);
1440
- });
1441
- return {};
1442
- }));
1653
+ connect() {
1654
+ return chunk7B52C2XE_js.__async(this, null, function* () {
1655
+ if (chunk7B52C2XE_js.__privateGet(this, _token)) {
1656
+ return chunk7B52C2XE_js.__privateMethod(this, _Socket_instances, handleConnectSocket_fn).call(this);
1657
+ }
1658
+ return this.callWithSession(chunk7B52C2XE_js.__privateMethod(this, _Socket_instances, handleConnectSocket_fn).bind(this));
1659
+ });
1660
+ }
1661
+ disconnect() {
1662
+ return chunk7B52C2XE_js.__async(this, null, function* () {
1663
+ if (chunk7B52C2XE_js.__privateGet(this, _socketIo)) {
1664
+ return chunk7B52C2XE_js.__privateMethod(this, _Socket_instances, handleDisconnectSocket_fn).call(this);
1665
+ }
1666
+ return this.callWithSession(chunk7B52C2XE_js.__privateMethod(this, _Socket_instances, handleDisconnectSocket_fn).bind(this));
1667
+ });
1443
1668
  }
1444
1669
  };
1445
1670
  _token = new WeakMap();
@@ -1476,232 +1701,35 @@ initializeSocket_fn = function() {
1476
1701
  (_c = chunk7B52C2XE_js.__privateGet(this, _socketIo)) == null ? void 0 : _c.on("unread_count_changed" /* UNREAD */, chunk7B52C2XE_js.__privateGet(this, _unreadCountChanged));
1477
1702
  });
1478
1703
  };
1479
-
1480
- // src/api/http-client.ts
1481
- var DEFAULT_API_VERSION = "v1";
1482
- var DEFAULT_BACKEND_URL = "https://api.novu.co";
1483
- var DEFAULT_USER_AGENT = `${"@novu/js"}@${"2.6.6"}`;
1484
- var HttpClient = class {
1485
- constructor(options = {}) {
1486
- const {
1487
- apiVersion = DEFAULT_API_VERSION,
1488
- apiUrl = DEFAULT_BACKEND_URL,
1489
- userAgent = DEFAULT_USER_AGENT
1490
- } = options || {};
1491
- this.apiVersion = apiVersion;
1492
- this.apiUrl = `${apiUrl}/${this.apiVersion}`;
1493
- this.headers = {
1494
- "Novu-API-Version": "2024-06-26",
1495
- "Content-Type": "application/json",
1496
- "User-Agent": userAgent
1497
- };
1498
- }
1499
- setAuthorizationToken(token) {
1500
- this.headers.Authorization = `Bearer ${token}`;
1501
- }
1502
- setHeaders(headers) {
1503
- this.headers = chunk7B52C2XE_js.__spreadValues(chunk7B52C2XE_js.__spreadValues({}, this.headers), headers);
1504
- }
1505
- get(path, searchParams, unwrapEnvelope = true) {
1506
- return chunk7B52C2XE_js.__async(this, null, function* () {
1507
- return this.doFetch({
1508
- path,
1509
- searchParams,
1510
- options: {
1511
- method: "GET"
1512
- },
1513
- unwrapEnvelope
1514
- });
1515
- });
1516
- }
1517
- post(path, body) {
1518
- return chunk7B52C2XE_js.__async(this, null, function* () {
1519
- return this.doFetch({
1520
- path,
1521
- options: {
1522
- method: "POST",
1523
- body
1524
- }
1525
- });
1526
- });
1527
- }
1528
- patch(path, body) {
1529
- return chunk7B52C2XE_js.__async(this, null, function* () {
1530
- return this.doFetch({
1531
- path,
1532
- options: {
1533
- method: "PATCH",
1534
- body
1535
- }
1536
- });
1537
- });
1538
- }
1539
- delete(path, body) {
1540
- return chunk7B52C2XE_js.__async(this, null, function* () {
1541
- return this.doFetch({
1542
- path,
1543
- options: {
1544
- method: "DELETE",
1545
- body
1546
- }
1547
- });
1548
- });
1549
- }
1550
- doFetch(_0) {
1551
- return chunk7B52C2XE_js.__async(this, arguments, function* ({
1552
- path,
1553
- searchParams,
1554
- options,
1555
- unwrapEnvelope = true
1556
- }) {
1557
- const fullUrl = combineUrl(this.apiUrl, path, searchParams ? `?${searchParams.toString()}` : "");
1558
- const reqInit = {
1559
- method: (options == null ? void 0 : options.method) || "GET",
1560
- headers: chunk7B52C2XE_js.__spreadValues(chunk7B52C2XE_js.__spreadValues({}, this.headers), (options == null ? void 0 : options.headers) || {}),
1561
- body: (options == null ? void 0 : options.body) ? JSON.stringify(options.body) : void 0
1562
- };
1563
- const response = yield fetch(fullUrl, reqInit);
1564
- if (!response.ok) {
1565
- const errorData = yield response.json();
1566
- throw new Error(`${this.headers["User-Agent"]} error. Status: ${response.status}, Message: ${errorData.message}`);
1567
- }
1568
- if (response.status === 204) {
1569
- return void 0;
1570
- }
1571
- const res = yield response.json();
1572
- return unwrapEnvelope ? res.data : res;
1573
- });
1574
- }
1575
- };
1576
- function combineUrl(...args) {
1577
- return args.map((part) => part.replace(/^\/+|\/+$/g, "")).join("/");
1578
- }
1579
-
1580
- // src/api/inbox-service.ts
1581
- var INBOX_ROUTE = "/inbox";
1582
- var INBOX_NOTIFICATIONS_ROUTE = `${INBOX_ROUTE}/notifications`;
1583
- var _httpClient;
1584
- var InboxService = class {
1585
- constructor(options = {}) {
1586
- this.isSessionInitialized = false;
1587
- chunk7B52C2XE_js.__privateAdd(this, _httpClient);
1588
- chunk7B52C2XE_js.__privateSet(this, _httpClient, new HttpClient(options));
1589
- }
1590
- initializeSession(_0) {
1591
- return chunk7B52C2XE_js.__async(this, arguments, function* ({
1592
- applicationIdentifier,
1593
- subscriberId,
1594
- subscriberHash
1595
- }) {
1596
- const response = yield chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_ROUTE}/session`, {
1597
- applicationIdentifier,
1598
- subscriberId,
1599
- subscriberHash
1600
- });
1601
- chunk7B52C2XE_js.__privateGet(this, _httpClient).setAuthorizationToken(response.token);
1602
- this.isSessionInitialized = true;
1603
- return response;
1604
- });
1605
- }
1606
- fetchNotifications({
1607
- after,
1608
- archived,
1609
- limit = 10,
1610
- offset,
1611
- read: read2,
1612
- tags
1613
- }) {
1614
- const searchParams = new URLSearchParams(`limit=${limit}`);
1615
- if (after) {
1616
- searchParams.append("after", after);
1617
- }
1618
- if (offset) {
1619
- searchParams.append("offset", `${offset}`);
1620
- }
1621
- if (tags) {
1622
- tags.forEach((tag) => searchParams.append("tags[]", tag));
1623
- }
1624
- if (read2 !== void 0) {
1625
- searchParams.append("read", `${read2}`);
1626
- }
1627
- if (archived !== void 0) {
1628
- searchParams.append("archived", `${archived}`);
1704
+ handleConnectSocket_fn = function() {
1705
+ return chunk7B52C2XE_js.__async(this, null, function* () {
1706
+ try {
1707
+ yield chunk7B52C2XE_js.__privateMethod(this, _Socket_instances, initializeSocket_fn).call(this);
1708
+ return {};
1709
+ } catch (error) {
1710
+ return { error: new NovuError("Failed to initialize the socket", error) };
1629
1711
  }
1630
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).get(INBOX_NOTIFICATIONS_ROUTE, searchParams, false);
1631
- }
1632
- count({ filters }) {
1633
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).get(
1634
- `${INBOX_NOTIFICATIONS_ROUTE}/count`,
1635
- new URLSearchParams({
1636
- filters: JSON.stringify(filters)
1637
- }),
1638
- false
1639
- );
1640
- }
1641
- read(notificationId) {
1642
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/read`);
1643
- }
1644
- unread(notificationId) {
1645
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/unread`);
1646
- }
1647
- archive(notificationId) {
1648
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/archive`);
1649
- }
1650
- unarchive(notificationId) {
1651
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/unarchive`);
1652
- }
1653
- readAll({ tags }) {
1654
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/read`, { tags });
1655
- }
1656
- archiveAll({ tags }) {
1657
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/archive`, { tags });
1658
- }
1659
- archiveAllRead({ tags }) {
1660
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/read-archive`, { tags });
1661
- }
1662
- completeAction({
1663
- actionType,
1664
- notificationId
1665
- }) {
1666
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/complete`, {
1667
- actionType
1668
- });
1669
- }
1670
- revertAction({
1671
- actionType,
1672
- notificationId
1673
- }) {
1674
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_NOTIFICATIONS_ROUTE}/${notificationId}/revert`, {
1675
- actionType
1676
- });
1677
- }
1678
- fetchPreferences(tags) {
1679
- const queryParams = new URLSearchParams();
1680
- if (tags) {
1681
- tags.forEach((tag) => queryParams.append("tags[]", tag));
1712
+ });
1713
+ };
1714
+ handleDisconnectSocket_fn = function() {
1715
+ return chunk7B52C2XE_js.__async(this, null, function* () {
1716
+ var _a;
1717
+ try {
1718
+ (_a = chunk7B52C2XE_js.__privateGet(this, _socketIo)) == null ? void 0 : _a.disconnect();
1719
+ chunk7B52C2XE_js.__privateSet(this, _socketIo, void 0);
1720
+ return {};
1721
+ } catch (error) {
1722
+ return { error: new NovuError("Failed to disconnect from the socket", error) };
1682
1723
  }
1683
- const query = queryParams.size ? `?${queryParams.toString()}` : "";
1684
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).get(`${INBOX_ROUTE}/preferences${query}`);
1685
- }
1686
- updateGlobalPreferences(channels) {
1687
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_ROUTE}/preferences`, channels);
1688
- }
1689
- updateWorkflowPreferences({
1690
- workflowId,
1691
- channels
1692
- }) {
1693
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).patch(`${INBOX_ROUTE}/preferences/${workflowId}`, channels);
1694
- }
1724
+ });
1695
1725
  };
1696
- _httpClient = new WeakMap();
1697
1726
 
1698
1727
  // src/novu.ts
1699
- var _emitter7, _session, _socket, _inboxService3;
1728
+ var _emitter7, _session, _inboxService3;
1700
1729
  var Novu = class {
1701
1730
  constructor(options) {
1702
1731
  chunk7B52C2XE_js.__privateAdd(this, _emitter7);
1703
1732
  chunk7B52C2XE_js.__privateAdd(this, _session);
1704
- chunk7B52C2XE_js.__privateAdd(this, _socket);
1705
1733
  chunk7B52C2XE_js.__privateAdd(this, _inboxService3);
1706
1734
  var _a, _b;
1707
1735
  chunk7B52C2XE_js.__privateSet(this, _inboxService3, new InboxService({
@@ -1729,14 +1757,14 @@ var Novu = class {
1729
1757
  inboxServiceInstance: chunk7B52C2XE_js.__privateGet(this, _inboxService3),
1730
1758
  eventEmitterInstance: chunk7B52C2XE_js.__privateGet(this, _emitter7)
1731
1759
  });
1732
- chunk7B52C2XE_js.__privateSet(this, _socket, new Socket({
1760
+ this.socket = new Socket({
1733
1761
  socketUrl: options.socketUrl,
1734
1762
  eventEmitterInstance: chunk7B52C2XE_js.__privateGet(this, _emitter7),
1735
1763
  inboxServiceInstance: chunk7B52C2XE_js.__privateGet(this, _inboxService3)
1736
- }));
1764
+ });
1737
1765
  this.on = (eventName, listener) => {
1738
- if (chunk7B52C2XE_js.__privateGet(this, _socket).isSocketEvent(eventName)) {
1739
- chunk7B52C2XE_js.__privateGet(this, _socket).initialize();
1766
+ if (this.socket.isSocketEvent(eventName)) {
1767
+ this.socket.connect();
1740
1768
  }
1741
1769
  const cleanup = chunk7B52C2XE_js.__privateGet(this, _emitter7).on(eventName, listener);
1742
1770
  return () => {
@@ -1750,7 +1778,6 @@ var Novu = class {
1750
1778
  };
1751
1779
  _emitter7 = new WeakMap();
1752
1780
  _session = new WeakMap();
1753
- _socket = new WeakMap();
1754
1781
  _inboxService3 = new WeakMap();
1755
1782
 
1756
1783
  exports.ActionTypeEnum = ActionTypeEnum;