@novu/js 3.5.1-nightly.20250610 → 3.6.0-nightly.20250720

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,8 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ var chunkQFZ5WJGI_js = require('./chunk-QFZ5WJGI.js');
3
4
  var chunk7B52C2XE_js = require('./chunk-7B52C2XE.js');
4
5
  var mitt = require('mitt');
5
6
  var io = require('socket.io-client');
7
+ var partysocket = require('partysocket');
6
8
 
7
9
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
10
 
@@ -69,12 +71,12 @@ var areDataEqual = (data1, data2) => {
69
71
  }
70
72
  };
71
73
  var isSameFilter = (filter1, filter2) => {
72
- return areDataEqual(filter1.data, filter2.data) && areTagsEqual(filter1.tags, filter2.tags) && filter1.read === filter2.read && filter1.archived === filter2.archived && filter1.snoozed === filter2.snoozed;
74
+ return areDataEqual(filter1.data, filter2.data) && areTagsEqual(filter1.tags, filter2.tags) && filter1.read === filter2.read && filter1.archived === filter2.archived && filter1.snoozed === filter2.snoozed && filter1.seen === filter2.seen;
73
75
  };
74
76
 
75
77
  // src/api/http-client.ts
76
78
  var DEFAULT_API_VERSION = "v1";
77
- var DEFAULT_USER_AGENT = `${"@novu/js"}@${"3.5.1-nightly.20250610"}`;
79
+ var DEFAULT_USER_AGENT = `${"@novu/js"}@${"3.6.0-nightly.20250720"}`;
78
80
  var HttpClient = class {
79
81
  constructor(options = {}) {
80
82
  // Environment variable for local development that overrides the default API endpoint without affecting the Inbox DX
@@ -223,6 +225,7 @@ var InboxService = class {
223
225
  read: read2,
224
226
  tags,
225
227
  snoozed,
228
+ seen: seen2,
226
229
  data
227
230
  }) {
228
231
  const searchParams = new URLSearchParams(`limit=${limit}`);
@@ -244,6 +247,9 @@ var InboxService = class {
244
247
  if (snoozed !== void 0) {
245
248
  searchParams.append("snoozed", `${snoozed}`);
246
249
  }
250
+ if (seen2 !== void 0) {
251
+ searchParams.append("seen", `${seen2}`);
252
+ }
247
253
  if (data !== void 0) {
248
254
  searchParams.append("data", JSON.stringify(data));
249
255
  }
@@ -291,11 +297,25 @@ var InboxService = class {
291
297
  });
292
298
  }
293
299
  archiveAllRead({ tags, data }) {
294
- return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/read-archive`, {
300
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/archive/read`, {
295
301
  tags,
296
302
  data: data ? JSON.stringify(data) : void 0
297
303
  });
298
304
  }
305
+ markAsSeen({
306
+ notificationIds,
307
+ tags,
308
+ data
309
+ }) {
310
+ return chunk7B52C2XE_js.__privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/seen`, {
311
+ notificationIds,
312
+ tags,
313
+ data: data ? JSON.stringify(data) : void 0
314
+ });
315
+ }
316
+ seen(notificationId) {
317
+ return this.markAsSeen({ notificationIds: [notificationId] });
318
+ }
299
319
  completeAction({
300
320
  actionType,
301
321
  notificationId
@@ -444,12 +464,14 @@ var Notification = class {
444
464
  this.body = notification.body;
445
465
  this.to = notification.to;
446
466
  this.isRead = notification.isRead;
467
+ this.isSeen = notification.isSeen;
447
468
  this.isArchived = notification.isArchived;
448
469
  this.isSnoozed = notification.isSnoozed;
449
470
  this.snoozedUntil = notification.snoozedUntil;
450
471
  this.deliveredAt = notification.deliveredAt;
451
472
  this.createdAt = notification.createdAt;
452
473
  this.readAt = notification.readAt;
474
+ this.firstSeenAt = notification.firstSeenAt;
453
475
  this.archivedAt = notification.archivedAt;
454
476
  this.avatar = notification.avatar;
455
477
  this.primaryAction = notification.primaryAction;
@@ -478,6 +500,15 @@ var Notification = class {
478
500
  }
479
501
  });
480
502
  }
503
+ seen() {
504
+ return seen({
505
+ emitter: chunk7B52C2XE_js.__privateGet(this, _emitter),
506
+ apiService: chunk7B52C2XE_js.__privateGet(this, _inboxService),
507
+ args: {
508
+ notification: this
509
+ }
510
+ });
511
+ }
481
512
  archive() {
482
513
  return archive({
483
514
  emitter: chunk7B52C2XE_js.__privateGet(this, _emitter),
@@ -647,6 +678,38 @@ var unread = (_0) => chunk7B52C2XE_js.__async(void 0, [_0], function* ({
647
678
  return { error: new NovuError("Failed to unread notification", error) };
648
679
  }
649
680
  });
681
+ var seen = (_0) => chunk7B52C2XE_js.__async(void 0, [_0], function* ({
682
+ emitter,
683
+ apiService,
684
+ args
685
+ }) {
686
+ const { notificationId, optimisticValue } = getNotificationDetails(
687
+ args,
688
+ {
689
+ isSeen: true
690
+ },
691
+ {
692
+ emitter,
693
+ apiService
694
+ }
695
+ );
696
+ try {
697
+ emitter.emit("notification.seen.pending", {
698
+ args,
699
+ data: optimisticValue
700
+ });
701
+ yield apiService.seen(notificationId);
702
+ if (!optimisticValue) {
703
+ throw new Error("Failed to create optimistic value for notification");
704
+ }
705
+ const updatedNotification = new Notification(optimisticValue, emitter, apiService);
706
+ emitter.emit("notification.seen.resolved", { args, data: updatedNotification });
707
+ return { data: updatedNotification };
708
+ } catch (error) {
709
+ emitter.emit("notification.seen.resolved", { args, error });
710
+ return { error: new NovuError("Failed to mark notification as seen", error) };
711
+ }
712
+ });
650
713
  var archive = (_0) => chunk7B52C2XE_js.__async(void 0, [_0], function* ({
651
714
  emitter,
652
715
  apiService,
@@ -883,6 +946,42 @@ var readAll = (_0) => chunk7B52C2XE_js.__async(void 0, [_0], function* ({
883
946
  return { error: new NovuError("Failed to read all notifications", error) };
884
947
  }
885
948
  });
949
+ var seenAll = (_0) => chunk7B52C2XE_js.__async(void 0, [_0], function* ({
950
+ emitter,
951
+ inboxService,
952
+ notificationsCache,
953
+ notificationIds,
954
+ tags,
955
+ data
956
+ }) {
957
+ try {
958
+ const notifications = notificationsCache.getUniqueNotifications({ tags, data });
959
+ const filteredNotifications = notificationIds && notificationIds.length > 0 ? notifications.filter((notification) => notificationIds.includes(notification.id)) : notifications;
960
+ const optimisticNotifications = filteredNotifications.map(
961
+ (notification) => new Notification(
962
+ chunk7B52C2XE_js.__spreadProps(chunk7B52C2XE_js.__spreadValues({}, notification), {
963
+ isSeen: true,
964
+ firstSeenAt: notification.firstSeenAt || (/* @__PURE__ */ new Date()).toISOString()
965
+ }),
966
+ emitter,
967
+ inboxService
968
+ )
969
+ );
970
+ emitter.emit("notifications.seen_all.pending", {
971
+ args: { notificationIds, tags, data },
972
+ data: optimisticNotifications
973
+ });
974
+ yield inboxService.markAsSeen({ notificationIds, tags, data });
975
+ emitter.emit("notifications.seen_all.resolved", {
976
+ args: { notificationIds, tags, data },
977
+ data: optimisticNotifications
978
+ });
979
+ return {};
980
+ } catch (error) {
981
+ emitter.emit("notifications.seen_all.resolved", { args: { notificationIds, tags, data }, error });
982
+ return { error: new NovuError("Failed to mark all notifications as seen", error) };
983
+ }
984
+ });
886
985
  var archiveAll = (_0) => chunk7B52C2XE_js.__async(void 0, [_0], function* ({
887
986
  emitter,
888
987
  inboxService,
@@ -971,21 +1070,32 @@ var InMemoryCache = class {
971
1070
  _cache = new WeakMap();
972
1071
 
973
1072
  // src/cache/notifications-cache.ts
974
- var excludeEmpty = ({ tags, data, read: read2, archived, snoozed, limit, offset, after }) => Object.entries({ tags, data, read: read2, archived, snoozed, limit, offset, after }).filter(([_, value]) => value !== null && value !== void 0 && !(Array.isArray(value) && value.length === 0)).reduce((acc, [key, value]) => {
1073
+ var excludeEmpty = ({ tags, data, read: read2, archived, snoozed, seen: seen2, limit, offset, after }) => Object.entries({ tags, data, read: read2, archived, snoozed, seen: seen2, limit, offset, after }).filter(([_, value]) => value !== null && value !== void 0 && !(Array.isArray(value) && value.length === 0)).reduce((acc, [key, value]) => {
975
1074
  acc[key] = value;
976
1075
  return acc;
977
1076
  }, {});
978
- var getCacheKey = ({ tags, data, read: read2, archived, snoozed, limit, offset, after }) => {
979
- return JSON.stringify(excludeEmpty({ tags, data, read: read2, archived, snoozed, limit, offset, after }));
1077
+ var getCacheKey = ({
1078
+ tags,
1079
+ data,
1080
+ read: read2,
1081
+ archived,
1082
+ snoozed,
1083
+ seen: seen2,
1084
+ limit,
1085
+ offset,
1086
+ after
1087
+ }) => {
1088
+ return JSON.stringify(excludeEmpty({ tags, data, read: read2, archived, snoozed, seen: seen2, limit, offset, after }));
980
1089
  };
981
1090
  var getFilterKey = ({
982
1091
  tags,
983
1092
  data,
984
1093
  read: read2,
985
1094
  archived,
986
- snoozed
1095
+ snoozed,
1096
+ seen: seen2
987
1097
  }) => {
988
- return JSON.stringify(excludeEmpty({ tags, data, read: read2, archived, snoozed }));
1098
+ return JSON.stringify(excludeEmpty({ tags, data, read: read2, archived, snoozed, seen: seen2 }));
989
1099
  };
990
1100
  var getFilter = (key) => {
991
1101
  return JSON.parse(key);
@@ -1122,7 +1232,8 @@ var NotificationsCache = class {
1122
1232
  data: args.data,
1123
1233
  read: args.read,
1124
1234
  snoozed: args.snoozed,
1125
- archived: args.archived
1235
+ archived: args.archived,
1236
+ seen: args.seen
1126
1237
  });
1127
1238
  }
1128
1239
  }
@@ -1182,6 +1293,9 @@ var Notifications = class extends BaseModule {
1182
1293
  });
1183
1294
  chunk7B52C2XE_js.__privateSet(this, _useCache, useCache);
1184
1295
  }
1296
+ get inboxService() {
1297
+ return this._inboxService;
1298
+ }
1185
1299
  list() {
1186
1300
  return chunk7B52C2XE_js.__async(this, arguments, function* (_a = {}) {
1187
1301
  var _b = _a, { limit = 10 } = _b, restOptions = chunk7B52C2XE_js.__objRest(_b, ["limit"]);
@@ -1262,6 +1376,19 @@ var Notifications = class extends BaseModule {
1262
1376
  );
1263
1377
  });
1264
1378
  }
1379
+ seen(args) {
1380
+ return chunk7B52C2XE_js.__async(this, null, function* () {
1381
+ return this.callWithSession(
1382
+ () => chunk7B52C2XE_js.__async(this, null, function* () {
1383
+ return seen({
1384
+ emitter: this._emitter,
1385
+ apiService: this._inboxService,
1386
+ args
1387
+ });
1388
+ })
1389
+ );
1390
+ });
1391
+ }
1265
1392
  archive(args) {
1266
1393
  return chunk7B52C2XE_js.__async(this, null, function* () {
1267
1394
  return this.callWithSession(
@@ -1388,6 +1515,28 @@ var Notifications = class extends BaseModule {
1388
1515
  );
1389
1516
  });
1390
1517
  }
1518
+ seenAll() {
1519
+ return chunk7B52C2XE_js.__async(this, arguments, function* (args = {}) {
1520
+ return this.callWithSession(() => chunk7B52C2XE_js.__async(this, null, function* () {
1521
+ if ("notificationIds" in args) {
1522
+ return seenAll({
1523
+ emitter: this._emitter,
1524
+ inboxService: this._inboxService,
1525
+ notificationsCache: this.cache,
1526
+ notificationIds: args.notificationIds
1527
+ });
1528
+ } else {
1529
+ return seenAll({
1530
+ emitter: this._emitter,
1531
+ inboxService: this._inboxService,
1532
+ notificationsCache: this.cache,
1533
+ tags: "tags" in args ? args.tags : void 0,
1534
+ data: "data" in args ? args.data : void 0
1535
+ });
1536
+ }
1537
+ }));
1538
+ });
1539
+ }
1391
1540
  archiveAll() {
1392
1541
  return chunk7B52C2XE_js.__async(this, arguments, function* ({
1393
1542
  tags,
@@ -1783,6 +1932,11 @@ var Preferences = class extends BaseModule {
1783
1932
  };
1784
1933
  _useCache3 = new WeakMap();
1785
1934
 
1935
+ // src/utils/is-browser.ts
1936
+ function isBrowser() {
1937
+ return typeof window !== "undefined";
1938
+ }
1939
+
1786
1940
  // src/session/session.ts
1787
1941
  var _emitter5, _inboxService2, _options;
1788
1942
  var Session = class {
@@ -1826,12 +1980,19 @@ var Session = class {
1826
1980
  }
1827
1981
  initialize(options) {
1828
1982
  return chunk7B52C2XE_js.__async(this, null, function* () {
1829
- var _a, _b;
1983
+ var _a, _b, _c, _d, _e, _f;
1984
+ if (((_a = chunk7B52C2XE_js.__privateGet(this, _options).subscriber) == null ? void 0 : _a.subscriberId) === ((_b = options == null ? void 0 : options.subscriber) == null ? void 0 : _b.subscriberId)) {
1985
+ return;
1986
+ }
1830
1987
  try {
1831
1988
  if (options) {
1832
1989
  chunk7B52C2XE_js.__privateSet(this, _options, options);
1833
1990
  }
1834
1991
  const { subscriber, subscriberHash, applicationIdentifier } = chunk7B52C2XE_js.__privateGet(this, _options);
1992
+ let currentTimezone;
1993
+ if (isBrowser()) {
1994
+ currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
1995
+ }
1835
1996
  let finalApplicationIdentifier = applicationIdentifier;
1836
1997
  if (!finalApplicationIdentifier) {
1837
1998
  const storedAppId = this.handleApplicationIdentifier("get");
@@ -1845,12 +2006,15 @@ var Session = class {
1845
2006
  const response = yield chunk7B52C2XE_js.__privateGet(this, _inboxService2).initializeSession({
1846
2007
  applicationIdentifier: finalApplicationIdentifier,
1847
2008
  subscriberHash,
1848
- subscriber
2009
+ subscriber: chunk7B52C2XE_js.__spreadProps(chunk7B52C2XE_js.__spreadValues({}, subscriber), {
2010
+ subscriberId: (_c = subscriber == null ? void 0 : subscriber.subscriberId) != null ? _c : "",
2011
+ timezone: (_d = subscriber == null ? void 0 : subscriber.timezone) != null ? _d : currentTimezone
2012
+ })
1849
2013
  });
1850
- if ((_a = response == null ? void 0 : response.applicationIdentifier) == null ? void 0 : _a.startsWith("pk_keyless_")) {
2014
+ if ((_e = response == null ? void 0 : response.applicationIdentifier) == null ? void 0 : _e.startsWith("pk_keyless_")) {
1851
2015
  this.handleApplicationIdentifier("store", response.applicationIdentifier);
1852
2016
  }
1853
- if (!((_b = response == null ? void 0 : response.applicationIdentifier) == null ? void 0 : _b.startsWith("pk_keyless_"))) {
2017
+ if (!((_f = response == null ? void 0 : response.applicationIdentifier) == null ? void 0 : _f.startsWith("pk_keyless_"))) {
1854
2018
  this.handleApplicationIdentifier("delete");
1855
2019
  }
1856
2020
  chunk7B52C2XE_js.__privateGet(this, _emitter5).emit("session.initialize.resolved", { args: chunk7B52C2XE_js.__privateGet(this, _options), data: response });
@@ -1871,11 +2035,13 @@ var mapToNotification = ({
1871
2035
  _id,
1872
2036
  content,
1873
2037
  read: read2,
2038
+ seen: seen2,
1874
2039
  archived,
1875
2040
  snoozedUntil,
1876
2041
  deliveredAt,
1877
2042
  createdAt,
1878
2043
  lastReadDate,
2044
+ firstSeenDate,
1879
2045
  archivedAt,
1880
2046
  channel,
1881
2047
  subscriber,
@@ -1909,6 +2075,7 @@ var mapToNotification = ({
1909
2075
  body: content,
1910
2076
  to,
1911
2077
  isRead: read2,
2078
+ isSeen: seen2,
1912
2079
  isArchived: archived,
1913
2080
  isSnoozed: !!snoozedUntil
1914
2081
  }, deliveredAt && {
@@ -1918,6 +2085,7 @@ var mapToNotification = ({
1918
2085
  }), {
1919
2086
  createdAt,
1920
2087
  readAt: lastReadDate,
2088
+ firstSeenAt: firstSeenDate,
1921
2089
  archivedAt,
1922
2090
  avatar,
1923
2091
  primaryAction: primaryCta && {
@@ -2059,59 +2227,330 @@ handleDisconnectSocket_fn = function() {
2059
2227
  }
2060
2228
  });
2061
2229
  };
2230
+ var PRODUCTION_SOCKET_URL2 = "wss://socket.novu.co";
2231
+ var NOTIFICATION_RECEIVED2 = "notifications.notification_received";
2232
+ var UNSEEN_COUNT_CHANGED2 = "notifications.unseen_count_changed";
2233
+ var UNREAD_COUNT_CHANGED2 = "notifications.unread_count_changed";
2234
+ var mapToNotification2 = ({
2235
+ _id,
2236
+ content,
2237
+ read: read2,
2238
+ seen: seen2,
2239
+ archived,
2240
+ snoozedUntil,
2241
+ deliveredAt,
2242
+ createdAt,
2243
+ lastReadDate,
2244
+ firstSeenDate,
2245
+ archivedAt,
2246
+ channel,
2247
+ subscriber,
2248
+ subject,
2249
+ avatar,
2250
+ cta,
2251
+ tags,
2252
+ data,
2253
+ workflow
2254
+ }) => {
2255
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2256
+ const to = {
2257
+ id: subscriber == null ? void 0 : subscriber._id,
2258
+ subscriberId: subscriber == null ? void 0 : subscriber.subscriberId,
2259
+ firstName: subscriber == null ? void 0 : subscriber.firstName,
2260
+ lastName: subscriber == null ? void 0 : subscriber.lastName,
2261
+ avatar: subscriber == null ? void 0 : subscriber.avatar,
2262
+ locale: subscriber == null ? void 0 : subscriber.locale,
2263
+ data: subscriber == null ? void 0 : subscriber.data,
2264
+ timezone: subscriber == null ? void 0 : subscriber.timezone,
2265
+ email: subscriber == null ? void 0 : subscriber.email,
2266
+ phone: subscriber == null ? void 0 : subscriber.phone
2267
+ };
2268
+ const primaryCta = (_b = (_a = cta.action) == null ? void 0 : _a.buttons) == null ? void 0 : _b.find((button) => button.type === "primary" /* PRIMARY */);
2269
+ const secondaryCta = (_d = (_c = cta.action) == null ? void 0 : _c.buttons) == null ? void 0 : _d.find((button) => button.type === "secondary" /* SECONDARY */);
2270
+ const actionType = (_f = (_e = cta.action) == null ? void 0 : _e.result) == null ? void 0 : _f.type;
2271
+ const actionStatus = (_g = cta.action) == null ? void 0 : _g.status;
2272
+ return chunk7B52C2XE_js.__spreadProps(chunk7B52C2XE_js.__spreadValues(chunk7B52C2XE_js.__spreadValues({
2273
+ id: _id,
2274
+ subject,
2275
+ body: content,
2276
+ to,
2277
+ isRead: read2,
2278
+ isSeen: seen2,
2279
+ isArchived: archived,
2280
+ isSnoozed: !!snoozedUntil
2281
+ }, deliveredAt && {
2282
+ deliveredAt
2283
+ }), snoozedUntil && {
2284
+ snoozedUntil
2285
+ }), {
2286
+ createdAt,
2287
+ readAt: lastReadDate,
2288
+ firstSeenAt: firstSeenDate,
2289
+ archivedAt,
2290
+ avatar,
2291
+ primaryAction: primaryCta && {
2292
+ label: primaryCta.content,
2293
+ isCompleted: actionType === "primary" /* PRIMARY */ && actionStatus === "done" /* DONE */,
2294
+ redirect: primaryCta.url ? {
2295
+ target: primaryCta.target,
2296
+ url: primaryCta.url
2297
+ } : void 0
2298
+ },
2299
+ secondaryAction: secondaryCta && {
2300
+ label: secondaryCta.content,
2301
+ isCompleted: actionType === "secondary" /* SECONDARY */ && actionStatus === "done" /* DONE */,
2302
+ redirect: secondaryCta.url ? {
2303
+ target: secondaryCta.target,
2304
+ url: secondaryCta.url
2305
+ } : void 0
2306
+ },
2307
+ channelType: channel,
2308
+ tags,
2309
+ redirect: ((_h = cta.data) == null ? void 0 : _h.url) ? {
2310
+ url: cta.data.url,
2311
+ target: cta.data.target
2312
+ } : void 0,
2313
+ data,
2314
+ workflow
2315
+ });
2316
+ };
2317
+ var _token2, _emitter7, _partySocket, _socketUrl2, _notificationReceived2, _unseenCountChanged2, _unreadCountChanged2, _handleMessage, _PartySocketClient_instances, initializeSocket_fn2, handleConnectSocket_fn2, handleDisconnectSocket_fn2;
2318
+ var PartySocketClient = class extends BaseModule {
2319
+ constructor({
2320
+ socketUrl,
2321
+ inboxServiceInstance,
2322
+ eventEmitterInstance
2323
+ }) {
2324
+ super({
2325
+ eventEmitterInstance,
2326
+ inboxServiceInstance
2327
+ });
2328
+ chunk7B52C2XE_js.__privateAdd(this, _PartySocketClient_instances);
2329
+ chunk7B52C2XE_js.__privateAdd(this, _token2);
2330
+ chunk7B52C2XE_js.__privateAdd(this, _emitter7);
2331
+ chunk7B52C2XE_js.__privateAdd(this, _partySocket);
2332
+ chunk7B52C2XE_js.__privateAdd(this, _socketUrl2);
2333
+ chunk7B52C2XE_js.__privateAdd(this, _notificationReceived2, (event) => {
2334
+ try {
2335
+ const data = JSON.parse(event.data);
2336
+ if (data.event === "notification_received" /* RECEIVED */) {
2337
+ chunk7B52C2XE_js.__privateGet(this, _emitter7).emit(NOTIFICATION_RECEIVED2, {
2338
+ result: new Notification(mapToNotification2(data.data.message), chunk7B52C2XE_js.__privateGet(this, _emitter7), this._inboxService)
2339
+ });
2340
+ }
2341
+ } catch (error) {
2342
+ console.log("error", error);
2343
+ }
2344
+ });
2345
+ chunk7B52C2XE_js.__privateAdd(this, _unseenCountChanged2, (event) => {
2346
+ try {
2347
+ const data = JSON.parse(event.data);
2348
+ if (data.event === "unseen_count_changed" /* UNSEEN */) {
2349
+ chunk7B52C2XE_js.__privateGet(this, _emitter7).emit(UNSEEN_COUNT_CHANGED2, {
2350
+ result: data.data.unseenCount
2351
+ });
2352
+ }
2353
+ } catch (error) {
2354
+ }
2355
+ });
2356
+ chunk7B52C2XE_js.__privateAdd(this, _unreadCountChanged2, (event) => {
2357
+ try {
2358
+ const data = JSON.parse(event.data);
2359
+ if (data.event === "unread_count_changed" /* UNREAD */) {
2360
+ chunk7B52C2XE_js.__privateGet(this, _emitter7).emit(UNREAD_COUNT_CHANGED2, {
2361
+ result: data.data.unreadCount
2362
+ });
2363
+ }
2364
+ } catch (error) {
2365
+ }
2366
+ });
2367
+ chunk7B52C2XE_js.__privateAdd(this, _handleMessage, (event) => {
2368
+ try {
2369
+ const data = JSON.parse(event.data);
2370
+ switch (data.event) {
2371
+ case "notification_received" /* RECEIVED */:
2372
+ chunk7B52C2XE_js.__privateGet(this, _notificationReceived2).call(this, event);
2373
+ break;
2374
+ case "unseen_count_changed" /* UNSEEN */:
2375
+ chunk7B52C2XE_js.__privateGet(this, _unseenCountChanged2).call(this, event);
2376
+ break;
2377
+ case "unread_count_changed" /* UNREAD */:
2378
+ chunk7B52C2XE_js.__privateGet(this, _unreadCountChanged2).call(this, event);
2379
+ break;
2380
+ default:
2381
+ }
2382
+ } catch (error) {
2383
+ }
2384
+ });
2385
+ chunk7B52C2XE_js.__privateSet(this, _emitter7, eventEmitterInstance);
2386
+ chunk7B52C2XE_js.__privateSet(this, _socketUrl2, socketUrl != null ? socketUrl : PRODUCTION_SOCKET_URL2);
2387
+ }
2388
+ onSessionSuccess({ token }) {
2389
+ chunk7B52C2XE_js.__privateSet(this, _token2, token);
2390
+ }
2391
+ isSocketEvent(eventName) {
2392
+ return eventName === NOTIFICATION_RECEIVED2 || eventName === UNSEEN_COUNT_CHANGED2 || eventName === UNREAD_COUNT_CHANGED2;
2393
+ }
2394
+ connect() {
2395
+ return chunk7B52C2XE_js.__async(this, null, function* () {
2396
+ if (chunk7B52C2XE_js.__privateGet(this, _token2)) {
2397
+ return chunk7B52C2XE_js.__privateMethod(this, _PartySocketClient_instances, handleConnectSocket_fn2).call(this);
2398
+ }
2399
+ return this.callWithSession(chunk7B52C2XE_js.__privateMethod(this, _PartySocketClient_instances, handleConnectSocket_fn2).bind(this));
2400
+ });
2401
+ }
2402
+ disconnect() {
2403
+ return chunk7B52C2XE_js.__async(this, null, function* () {
2404
+ if (chunk7B52C2XE_js.__privateGet(this, _partySocket)) {
2405
+ return chunk7B52C2XE_js.__privateMethod(this, _PartySocketClient_instances, handleDisconnectSocket_fn2).call(this);
2406
+ }
2407
+ return this.callWithSession(chunk7B52C2XE_js.__privateMethod(this, _PartySocketClient_instances, handleDisconnectSocket_fn2).bind(this));
2408
+ });
2409
+ }
2410
+ };
2411
+ _token2 = new WeakMap();
2412
+ _emitter7 = new WeakMap();
2413
+ _partySocket = new WeakMap();
2414
+ _socketUrl2 = new WeakMap();
2415
+ _notificationReceived2 = new WeakMap();
2416
+ _unseenCountChanged2 = new WeakMap();
2417
+ _unreadCountChanged2 = new WeakMap();
2418
+ _handleMessage = new WeakMap();
2419
+ _PartySocketClient_instances = new WeakSet();
2420
+ initializeSocket_fn2 = function() {
2421
+ return chunk7B52C2XE_js.__async(this, null, function* () {
2422
+ if (chunk7B52C2XE_js.__privateGet(this, _partySocket)) {
2423
+ return;
2424
+ }
2425
+ const args = { socketUrl: chunk7B52C2XE_js.__privateGet(this, _socketUrl2) };
2426
+ chunk7B52C2XE_js.__privateGet(this, _emitter7).emit("socket.connect.pending", { args });
2427
+ const url = new URL(chunk7B52C2XE_js.__privateGet(this, _socketUrl2));
2428
+ url.searchParams.set("token", chunk7B52C2XE_js.__privateGet(this, _token2));
2429
+ chunk7B52C2XE_js.__privateSet(this, _partySocket, new partysocket.WebSocket(url.toString()));
2430
+ chunk7B52C2XE_js.__privateGet(this, _partySocket).addEventListener("open", () => {
2431
+ chunk7B52C2XE_js.__privateGet(this, _emitter7).emit("socket.connect.resolved", { args });
2432
+ });
2433
+ chunk7B52C2XE_js.__privateGet(this, _partySocket).addEventListener("error", (error) => {
2434
+ chunk7B52C2XE_js.__privateGet(this, _emitter7).emit("socket.connect.resolved", { args, error });
2435
+ });
2436
+ chunk7B52C2XE_js.__privateGet(this, _partySocket).addEventListener("message", chunk7B52C2XE_js.__privateGet(this, _handleMessage));
2437
+ });
2438
+ };
2439
+ handleConnectSocket_fn2 = function() {
2440
+ return chunk7B52C2XE_js.__async(this, null, function* () {
2441
+ try {
2442
+ yield chunk7B52C2XE_js.__privateMethod(this, _PartySocketClient_instances, initializeSocket_fn2).call(this);
2443
+ return {};
2444
+ } catch (error) {
2445
+ return { error: new NovuError("Failed to initialize the PartySocket", error) };
2446
+ }
2447
+ });
2448
+ };
2449
+ handleDisconnectSocket_fn2 = function() {
2450
+ return chunk7B52C2XE_js.__async(this, null, function* () {
2451
+ var _a;
2452
+ try {
2453
+ (_a = chunk7B52C2XE_js.__privateGet(this, _partySocket)) == null ? void 0 : _a.close();
2454
+ chunk7B52C2XE_js.__privateSet(this, _partySocket, void 0);
2455
+ return {};
2456
+ } catch (error) {
2457
+ return { error: new NovuError("Failed to disconnect from the PartySocket", error) };
2458
+ }
2459
+ });
2460
+ };
2461
+
2462
+ // src/ws/socket-factory.ts
2463
+ var PARTY_SOCKET_URLS = [
2464
+ "wss://eu.socket.novu.co",
2465
+ PRODUCTION_SOCKET_URL2,
2466
+ "wss://socket.novu-staging.co",
2467
+ "wss://socket-worker-local.cli-shortener.workers.dev"
2468
+ ];
2469
+ var URL_TRANSFORMATIONS = {
2470
+ "https://eu.ws.novu.co": "wss://eu.socket.novu.co",
2471
+ "https://ws.novu.co": PRODUCTION_SOCKET_URL2,
2472
+ "https://dev.ws.novu.co": "wss://socket.novu-staging.co"
2473
+ };
2474
+ function transformSocketUrl(socketUrl) {
2475
+ if (!socketUrl) return PRODUCTION_SOCKET_URL2;
2476
+ return URL_TRANSFORMATIONS[socketUrl] || socketUrl;
2477
+ }
2478
+ function shouldUsePartySocket(socketUrl) {
2479
+ return !socketUrl || PARTY_SOCKET_URLS.includes(socketUrl);
2480
+ }
2481
+ function createSocket({
2482
+ socketUrl,
2483
+ inboxServiceInstance,
2484
+ eventEmitterInstance
2485
+ }) {
2486
+ const transformedSocketUrl = transformSocketUrl(socketUrl);
2487
+ const socketType = shouldUsePartySocket(transformedSocketUrl) ? "partysocket" /* PARTY_SOCKET */ : "socket.io" /* SOCKET_IO */;
2488
+ switch (socketType) {
2489
+ case "partysocket" /* PARTY_SOCKET */:
2490
+ return new PartySocketClient({
2491
+ socketUrl: transformedSocketUrl,
2492
+ inboxServiceInstance,
2493
+ eventEmitterInstance
2494
+ });
2495
+ case "socket.io" /* SOCKET_IO */:
2496
+ default:
2497
+ return new Socket({
2498
+ socketUrl: transformedSocketUrl,
2499
+ inboxServiceInstance,
2500
+ eventEmitterInstance
2501
+ });
2502
+ }
2503
+ }
2062
2504
 
2063
2505
  // src/novu.ts
2064
- var _emitter7, _session, _inboxService3, _currentSubscriberId;
2506
+ var _emitter8, _session, _inboxService3;
2065
2507
  var Novu = class {
2066
2508
  constructor(options) {
2067
- chunk7B52C2XE_js.__privateAdd(this, _emitter7);
2509
+ chunk7B52C2XE_js.__privateAdd(this, _emitter8);
2068
2510
  chunk7B52C2XE_js.__privateAdd(this, _session);
2069
2511
  chunk7B52C2XE_js.__privateAdd(this, _inboxService3);
2070
- chunk7B52C2XE_js.__privateAdd(this, _currentSubscriberId);
2071
2512
  var _a, _b;
2072
2513
  chunk7B52C2XE_js.__privateSet(this, _inboxService3, new InboxService({
2073
2514
  apiUrl: options.apiUrl || options.backendUrl,
2074
2515
  userAgent: options.__userAgent
2075
2516
  }));
2076
- chunk7B52C2XE_js.__privateSet(this, _emitter7, new NovuEventEmitter());
2517
+ chunk7B52C2XE_js.__privateSet(this, _emitter8, new NovuEventEmitter());
2077
2518
  chunk7B52C2XE_js.__privateSet(this, _session, new Session(
2078
2519
  {
2079
2520
  applicationIdentifier: options.applicationIdentifier || "",
2080
2521
  subscriberHash: options.subscriberHash,
2081
- subscriber: buildSubscriber(options)
2522
+ subscriber: chunkQFZ5WJGI_js.buildSubscriber({ subscriberId: options.subscriberId, subscriber: options.subscriber })
2082
2523
  },
2083
2524
  chunk7B52C2XE_js.__privateGet(this, _inboxService3),
2084
- chunk7B52C2XE_js.__privateGet(this, _emitter7)
2525
+ chunk7B52C2XE_js.__privateGet(this, _emitter8)
2085
2526
  ));
2086
- const initialSubscriber = buildSubscriber(options);
2087
- chunk7B52C2XE_js.__privateSet(this, _currentSubscriberId, initialSubscriber.subscriberId);
2088
2527
  chunk7B52C2XE_js.__privateGet(this, _session).initialize();
2089
2528
  this.notifications = new Notifications({
2090
2529
  useCache: (_a = options.useCache) != null ? _a : true,
2091
2530
  inboxServiceInstance: chunk7B52C2XE_js.__privateGet(this, _inboxService3),
2092
- eventEmitterInstance: chunk7B52C2XE_js.__privateGet(this, _emitter7)
2531
+ eventEmitterInstance: chunk7B52C2XE_js.__privateGet(this, _emitter8)
2093
2532
  });
2094
2533
  this.preferences = new Preferences({
2095
2534
  useCache: (_b = options.useCache) != null ? _b : true,
2096
2535
  inboxServiceInstance: chunk7B52C2XE_js.__privateGet(this, _inboxService3),
2097
- eventEmitterInstance: chunk7B52C2XE_js.__privateGet(this, _emitter7)
2536
+ eventEmitterInstance: chunk7B52C2XE_js.__privateGet(this, _emitter8)
2098
2537
  });
2099
- this.socket = new Socket({
2538
+ this.socket = createSocket({
2100
2539
  socketUrl: options.socketUrl,
2101
- eventEmitterInstance: chunk7B52C2XE_js.__privateGet(this, _emitter7),
2540
+ eventEmitterInstance: chunk7B52C2XE_js.__privateGet(this, _emitter8),
2102
2541
  inboxServiceInstance: chunk7B52C2XE_js.__privateGet(this, _inboxService3)
2103
2542
  });
2104
2543
  this.on = (eventName, listener) => {
2105
2544
  if (this.socket.isSocketEvent(eventName)) {
2106
2545
  this.socket.connect();
2107
2546
  }
2108
- const cleanup = chunk7B52C2XE_js.__privateGet(this, _emitter7).on(eventName, listener);
2547
+ const cleanup = chunk7B52C2XE_js.__privateGet(this, _emitter8).on(eventName, listener);
2109
2548
  return () => {
2110
2549
  cleanup();
2111
2550
  };
2112
2551
  };
2113
2552
  this.off = (eventName, listener) => {
2114
- chunk7B52C2XE_js.__privateGet(this, _emitter7).off(eventName, listener);
2553
+ chunk7B52C2XE_js.__privateGet(this, _emitter8).off(eventName, listener);
2115
2554
  };
2116
2555
  }
2117
2556
  get applicationIdentifier() {
@@ -2122,31 +2561,17 @@ var Novu = class {
2122
2561
  }
2123
2562
  changeSubscriber(options) {
2124
2563
  return chunk7B52C2XE_js.__async(this, null, function* () {
2125
- if (chunk7B52C2XE_js.__privateGet(this, _currentSubscriberId) === options.subscriber.subscriberId) {
2126
- return;
2127
- }
2128
2564
  yield chunk7B52C2XE_js.__privateGet(this, _session).initialize({
2129
2565
  applicationIdentifier: chunk7B52C2XE_js.__privateGet(this, _session).applicationIdentifier || "",
2130
2566
  subscriberHash: options.subscriberHash,
2131
2567
  subscriber: options.subscriber
2132
2568
  });
2133
- chunk7B52C2XE_js.__privateSet(this, _currentSubscriberId, options.subscriber.subscriberId);
2134
2569
  });
2135
2570
  }
2136
2571
  };
2137
- _emitter7 = new WeakMap();
2572
+ _emitter8 = new WeakMap();
2138
2573
  _session = new WeakMap();
2139
2574
  _inboxService3 = new WeakMap();
2140
- _currentSubscriberId = new WeakMap();
2141
- function buildSubscriber(options) {
2142
- if (options.subscriber) {
2143
- return typeof options.subscriber === "string" ? { subscriberId: options.subscriber } : options.subscriber;
2144
- }
2145
- if (options.subscriberId) {
2146
- return { subscriberId: options.subscriberId };
2147
- }
2148
- return { subscriberId: "" };
2149
- }
2150
2575
 
2151
2576
  exports.ChannelType = ChannelType;
2152
2577
  exports.DEFAULT_API_VERSION = DEFAULT_API_VERSION;
@@ -2155,4 +2580,5 @@ exports.Novu = Novu;
2155
2580
  exports.PreferenceLevel = PreferenceLevel;
2156
2581
  exports.WebSocketEvent = WebSocketEvent;
2157
2582
  exports.areTagsEqual = areTagsEqual;
2583
+ exports.isBrowser = isBrowser;
2158
2584
  exports.isSameFilter = isSameFilter;