@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,6 +1,8 @@
1
- import { __privateAdd, __privateSet, __privateGet, __async, __objRest, __spreadValues, __privateMethod, __spreadProps } from './chunk-STZMOEWR.mjs';
1
+ import { buildSubscriber } from './chunk-KYYDDVBR.mjs';
2
+ import { __privateAdd, __privateSet, __privateGet, __async, __objRest, __spreadValues, __spreadProps, __privateMethod } from './chunk-STZMOEWR.mjs';
2
3
  import mitt from 'mitt';
3
4
  import io from 'socket.io-client';
5
+ import { WebSocket } from 'partysocket';
4
6
 
5
7
  // src/types.ts
6
8
  var NotificationStatus = /* @__PURE__ */ ((NotificationStatus2) => {
@@ -62,12 +64,12 @@ var areDataEqual = (data1, data2) => {
62
64
  }
63
65
  };
64
66
  var isSameFilter = (filter1, filter2) => {
65
- return areDataEqual(filter1.data, filter2.data) && areTagsEqual(filter1.tags, filter2.tags) && filter1.read === filter2.read && filter1.archived === filter2.archived && filter1.snoozed === filter2.snoozed;
67
+ 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;
66
68
  };
67
69
 
68
70
  // src/api/http-client.ts
69
71
  var DEFAULT_API_VERSION = "v1";
70
- var DEFAULT_USER_AGENT = `${"@novu/js"}@${"3.5.1-nightly.20250610"}`;
72
+ var DEFAULT_USER_AGENT = `${"@novu/js"}@${"3.6.0-nightly.20250720"}`;
71
73
  var HttpClient = class {
72
74
  constructor(options = {}) {
73
75
  // Environment variable for local development that overrides the default API endpoint without affecting the Inbox DX
@@ -216,6 +218,7 @@ var InboxService = class {
216
218
  read: read2,
217
219
  tags,
218
220
  snoozed,
221
+ seen: seen2,
219
222
  data
220
223
  }) {
221
224
  const searchParams = new URLSearchParams(`limit=${limit}`);
@@ -237,6 +240,9 @@ var InboxService = class {
237
240
  if (snoozed !== void 0) {
238
241
  searchParams.append("snoozed", `${snoozed}`);
239
242
  }
243
+ if (seen2 !== void 0) {
244
+ searchParams.append("seen", `${seen2}`);
245
+ }
240
246
  if (data !== void 0) {
241
247
  searchParams.append("data", JSON.stringify(data));
242
248
  }
@@ -284,11 +290,25 @@ var InboxService = class {
284
290
  });
285
291
  }
286
292
  archiveAllRead({ tags, data }) {
287
- return __privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/read-archive`, {
293
+ return __privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/archive/read`, {
288
294
  tags,
289
295
  data: data ? JSON.stringify(data) : void 0
290
296
  });
291
297
  }
298
+ markAsSeen({
299
+ notificationIds,
300
+ tags,
301
+ data
302
+ }) {
303
+ return __privateGet(this, _httpClient).post(`${INBOX_NOTIFICATIONS_ROUTE}/seen`, {
304
+ notificationIds,
305
+ tags,
306
+ data: data ? JSON.stringify(data) : void 0
307
+ });
308
+ }
309
+ seen(notificationId) {
310
+ return this.markAsSeen({ notificationIds: [notificationId] });
311
+ }
292
312
  completeAction({
293
313
  actionType,
294
314
  notificationId
@@ -437,12 +457,14 @@ var Notification = class {
437
457
  this.body = notification.body;
438
458
  this.to = notification.to;
439
459
  this.isRead = notification.isRead;
460
+ this.isSeen = notification.isSeen;
440
461
  this.isArchived = notification.isArchived;
441
462
  this.isSnoozed = notification.isSnoozed;
442
463
  this.snoozedUntil = notification.snoozedUntil;
443
464
  this.deliveredAt = notification.deliveredAt;
444
465
  this.createdAt = notification.createdAt;
445
466
  this.readAt = notification.readAt;
467
+ this.firstSeenAt = notification.firstSeenAt;
446
468
  this.archivedAt = notification.archivedAt;
447
469
  this.avatar = notification.avatar;
448
470
  this.primaryAction = notification.primaryAction;
@@ -471,6 +493,15 @@ var Notification = class {
471
493
  }
472
494
  });
473
495
  }
496
+ seen() {
497
+ return seen({
498
+ emitter: __privateGet(this, _emitter),
499
+ apiService: __privateGet(this, _inboxService),
500
+ args: {
501
+ notification: this
502
+ }
503
+ });
504
+ }
474
505
  archive() {
475
506
  return archive({
476
507
  emitter: __privateGet(this, _emitter),
@@ -640,6 +671,38 @@ var unread = (_0) => __async(void 0, [_0], function* ({
640
671
  return { error: new NovuError("Failed to unread notification", error) };
641
672
  }
642
673
  });
674
+ var seen = (_0) => __async(void 0, [_0], function* ({
675
+ emitter,
676
+ apiService,
677
+ args
678
+ }) {
679
+ const { notificationId, optimisticValue } = getNotificationDetails(
680
+ args,
681
+ {
682
+ isSeen: true
683
+ },
684
+ {
685
+ emitter,
686
+ apiService
687
+ }
688
+ );
689
+ try {
690
+ emitter.emit("notification.seen.pending", {
691
+ args,
692
+ data: optimisticValue
693
+ });
694
+ yield apiService.seen(notificationId);
695
+ if (!optimisticValue) {
696
+ throw new Error("Failed to create optimistic value for notification");
697
+ }
698
+ const updatedNotification = new Notification(optimisticValue, emitter, apiService);
699
+ emitter.emit("notification.seen.resolved", { args, data: updatedNotification });
700
+ return { data: updatedNotification };
701
+ } catch (error) {
702
+ emitter.emit("notification.seen.resolved", { args, error });
703
+ return { error: new NovuError("Failed to mark notification as seen", error) };
704
+ }
705
+ });
643
706
  var archive = (_0) => __async(void 0, [_0], function* ({
644
707
  emitter,
645
708
  apiService,
@@ -876,6 +939,42 @@ var readAll = (_0) => __async(void 0, [_0], function* ({
876
939
  return { error: new NovuError("Failed to read all notifications", error) };
877
940
  }
878
941
  });
942
+ var seenAll = (_0) => __async(void 0, [_0], function* ({
943
+ emitter,
944
+ inboxService,
945
+ notificationsCache,
946
+ notificationIds,
947
+ tags,
948
+ data
949
+ }) {
950
+ try {
951
+ const notifications = notificationsCache.getUniqueNotifications({ tags, data });
952
+ const filteredNotifications = notificationIds && notificationIds.length > 0 ? notifications.filter((notification) => notificationIds.includes(notification.id)) : notifications;
953
+ const optimisticNotifications = filteredNotifications.map(
954
+ (notification) => new Notification(
955
+ __spreadProps(__spreadValues({}, notification), {
956
+ isSeen: true,
957
+ firstSeenAt: notification.firstSeenAt || (/* @__PURE__ */ new Date()).toISOString()
958
+ }),
959
+ emitter,
960
+ inboxService
961
+ )
962
+ );
963
+ emitter.emit("notifications.seen_all.pending", {
964
+ args: { notificationIds, tags, data },
965
+ data: optimisticNotifications
966
+ });
967
+ yield inboxService.markAsSeen({ notificationIds, tags, data });
968
+ emitter.emit("notifications.seen_all.resolved", {
969
+ args: { notificationIds, tags, data },
970
+ data: optimisticNotifications
971
+ });
972
+ return {};
973
+ } catch (error) {
974
+ emitter.emit("notifications.seen_all.resolved", { args: { notificationIds, tags, data }, error });
975
+ return { error: new NovuError("Failed to mark all notifications as seen", error) };
976
+ }
977
+ });
879
978
  var archiveAll = (_0) => __async(void 0, [_0], function* ({
880
979
  emitter,
881
980
  inboxService,
@@ -964,21 +1063,32 @@ var InMemoryCache = class {
964
1063
  _cache = new WeakMap();
965
1064
 
966
1065
  // src/cache/notifications-cache.ts
967
- 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]) => {
1066
+ 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]) => {
968
1067
  acc[key] = value;
969
1068
  return acc;
970
1069
  }, {});
971
- var getCacheKey = ({ tags, data, read: read2, archived, snoozed, limit, offset, after }) => {
972
- return JSON.stringify(excludeEmpty({ tags, data, read: read2, archived, snoozed, limit, offset, after }));
1070
+ var getCacheKey = ({
1071
+ tags,
1072
+ data,
1073
+ read: read2,
1074
+ archived,
1075
+ snoozed,
1076
+ seen: seen2,
1077
+ limit,
1078
+ offset,
1079
+ after
1080
+ }) => {
1081
+ return JSON.stringify(excludeEmpty({ tags, data, read: read2, archived, snoozed, seen: seen2, limit, offset, after }));
973
1082
  };
974
1083
  var getFilterKey = ({
975
1084
  tags,
976
1085
  data,
977
1086
  read: read2,
978
1087
  archived,
979
- snoozed
1088
+ snoozed,
1089
+ seen: seen2
980
1090
  }) => {
981
- return JSON.stringify(excludeEmpty({ tags, data, read: read2, archived, snoozed }));
1091
+ return JSON.stringify(excludeEmpty({ tags, data, read: read2, archived, snoozed, seen: seen2 }));
982
1092
  };
983
1093
  var getFilter = (key) => {
984
1094
  return JSON.parse(key);
@@ -1115,7 +1225,8 @@ var NotificationsCache = class {
1115
1225
  data: args.data,
1116
1226
  read: args.read,
1117
1227
  snoozed: args.snoozed,
1118
- archived: args.archived
1228
+ archived: args.archived,
1229
+ seen: args.seen
1119
1230
  });
1120
1231
  }
1121
1232
  }
@@ -1175,6 +1286,9 @@ var Notifications = class extends BaseModule {
1175
1286
  });
1176
1287
  __privateSet(this, _useCache, useCache);
1177
1288
  }
1289
+ get inboxService() {
1290
+ return this._inboxService;
1291
+ }
1178
1292
  list() {
1179
1293
  return __async(this, arguments, function* (_a = {}) {
1180
1294
  var _b = _a, { limit = 10 } = _b, restOptions = __objRest(_b, ["limit"]);
@@ -1255,6 +1369,19 @@ var Notifications = class extends BaseModule {
1255
1369
  );
1256
1370
  });
1257
1371
  }
1372
+ seen(args) {
1373
+ return __async(this, null, function* () {
1374
+ return this.callWithSession(
1375
+ () => __async(this, null, function* () {
1376
+ return seen({
1377
+ emitter: this._emitter,
1378
+ apiService: this._inboxService,
1379
+ args
1380
+ });
1381
+ })
1382
+ );
1383
+ });
1384
+ }
1258
1385
  archive(args) {
1259
1386
  return __async(this, null, function* () {
1260
1387
  return this.callWithSession(
@@ -1381,6 +1508,28 @@ var Notifications = class extends BaseModule {
1381
1508
  );
1382
1509
  });
1383
1510
  }
1511
+ seenAll() {
1512
+ return __async(this, arguments, function* (args = {}) {
1513
+ return this.callWithSession(() => __async(this, null, function* () {
1514
+ if ("notificationIds" in args) {
1515
+ return seenAll({
1516
+ emitter: this._emitter,
1517
+ inboxService: this._inboxService,
1518
+ notificationsCache: this.cache,
1519
+ notificationIds: args.notificationIds
1520
+ });
1521
+ } else {
1522
+ return seenAll({
1523
+ emitter: this._emitter,
1524
+ inboxService: this._inboxService,
1525
+ notificationsCache: this.cache,
1526
+ tags: "tags" in args ? args.tags : void 0,
1527
+ data: "data" in args ? args.data : void 0
1528
+ });
1529
+ }
1530
+ }));
1531
+ });
1532
+ }
1384
1533
  archiveAll() {
1385
1534
  return __async(this, arguments, function* ({
1386
1535
  tags,
@@ -1776,6 +1925,11 @@ var Preferences = class extends BaseModule {
1776
1925
  };
1777
1926
  _useCache3 = new WeakMap();
1778
1927
 
1928
+ // src/utils/is-browser.ts
1929
+ function isBrowser() {
1930
+ return typeof window !== "undefined";
1931
+ }
1932
+
1779
1933
  // src/session/session.ts
1780
1934
  var _emitter5, _inboxService2, _options;
1781
1935
  var Session = class {
@@ -1819,12 +1973,19 @@ var Session = class {
1819
1973
  }
1820
1974
  initialize(options) {
1821
1975
  return __async(this, null, function* () {
1822
- var _a, _b;
1976
+ var _a, _b, _c, _d, _e, _f;
1977
+ if (((_a = __privateGet(this, _options).subscriber) == null ? void 0 : _a.subscriberId) === ((_b = options == null ? void 0 : options.subscriber) == null ? void 0 : _b.subscriberId)) {
1978
+ return;
1979
+ }
1823
1980
  try {
1824
1981
  if (options) {
1825
1982
  __privateSet(this, _options, options);
1826
1983
  }
1827
1984
  const { subscriber, subscriberHash, applicationIdentifier } = __privateGet(this, _options);
1985
+ let currentTimezone;
1986
+ if (isBrowser()) {
1987
+ currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
1988
+ }
1828
1989
  let finalApplicationIdentifier = applicationIdentifier;
1829
1990
  if (!finalApplicationIdentifier) {
1830
1991
  const storedAppId = this.handleApplicationIdentifier("get");
@@ -1838,12 +1999,15 @@ var Session = class {
1838
1999
  const response = yield __privateGet(this, _inboxService2).initializeSession({
1839
2000
  applicationIdentifier: finalApplicationIdentifier,
1840
2001
  subscriberHash,
1841
- subscriber
2002
+ subscriber: __spreadProps(__spreadValues({}, subscriber), {
2003
+ subscriberId: (_c = subscriber == null ? void 0 : subscriber.subscriberId) != null ? _c : "",
2004
+ timezone: (_d = subscriber == null ? void 0 : subscriber.timezone) != null ? _d : currentTimezone
2005
+ })
1842
2006
  });
1843
- if ((_a = response == null ? void 0 : response.applicationIdentifier) == null ? void 0 : _a.startsWith("pk_keyless_")) {
2007
+ if ((_e = response == null ? void 0 : response.applicationIdentifier) == null ? void 0 : _e.startsWith("pk_keyless_")) {
1844
2008
  this.handleApplicationIdentifier("store", response.applicationIdentifier);
1845
2009
  }
1846
- if (!((_b = response == null ? void 0 : response.applicationIdentifier) == null ? void 0 : _b.startsWith("pk_keyless_"))) {
2010
+ if (!((_f = response == null ? void 0 : response.applicationIdentifier) == null ? void 0 : _f.startsWith("pk_keyless_"))) {
1847
2011
  this.handleApplicationIdentifier("delete");
1848
2012
  }
1849
2013
  __privateGet(this, _emitter5).emit("session.initialize.resolved", { args: __privateGet(this, _options), data: response });
@@ -1864,11 +2028,13 @@ var mapToNotification = ({
1864
2028
  _id,
1865
2029
  content,
1866
2030
  read: read2,
2031
+ seen: seen2,
1867
2032
  archived,
1868
2033
  snoozedUntil,
1869
2034
  deliveredAt,
1870
2035
  createdAt,
1871
2036
  lastReadDate,
2037
+ firstSeenDate,
1872
2038
  archivedAt,
1873
2039
  channel,
1874
2040
  subscriber,
@@ -1902,6 +2068,7 @@ var mapToNotification = ({
1902
2068
  body: content,
1903
2069
  to,
1904
2070
  isRead: read2,
2071
+ isSeen: seen2,
1905
2072
  isArchived: archived,
1906
2073
  isSnoozed: !!snoozedUntil
1907
2074
  }, deliveredAt && {
@@ -1911,6 +2078,7 @@ var mapToNotification = ({
1911
2078
  }), {
1912
2079
  createdAt,
1913
2080
  readAt: lastReadDate,
2081
+ firstSeenAt: firstSeenDate,
1914
2082
  archivedAt,
1915
2083
  avatar,
1916
2084
  primaryAction: primaryCta && {
@@ -2052,59 +2220,330 @@ handleDisconnectSocket_fn = function() {
2052
2220
  }
2053
2221
  });
2054
2222
  };
2223
+ var PRODUCTION_SOCKET_URL2 = "wss://socket.novu.co";
2224
+ var NOTIFICATION_RECEIVED2 = "notifications.notification_received";
2225
+ var UNSEEN_COUNT_CHANGED2 = "notifications.unseen_count_changed";
2226
+ var UNREAD_COUNT_CHANGED2 = "notifications.unread_count_changed";
2227
+ var mapToNotification2 = ({
2228
+ _id,
2229
+ content,
2230
+ read: read2,
2231
+ seen: seen2,
2232
+ archived,
2233
+ snoozedUntil,
2234
+ deliveredAt,
2235
+ createdAt,
2236
+ lastReadDate,
2237
+ firstSeenDate,
2238
+ archivedAt,
2239
+ channel,
2240
+ subscriber,
2241
+ subject,
2242
+ avatar,
2243
+ cta,
2244
+ tags,
2245
+ data,
2246
+ workflow
2247
+ }) => {
2248
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2249
+ const to = {
2250
+ id: subscriber == null ? void 0 : subscriber._id,
2251
+ subscriberId: subscriber == null ? void 0 : subscriber.subscriberId,
2252
+ firstName: subscriber == null ? void 0 : subscriber.firstName,
2253
+ lastName: subscriber == null ? void 0 : subscriber.lastName,
2254
+ avatar: subscriber == null ? void 0 : subscriber.avatar,
2255
+ locale: subscriber == null ? void 0 : subscriber.locale,
2256
+ data: subscriber == null ? void 0 : subscriber.data,
2257
+ timezone: subscriber == null ? void 0 : subscriber.timezone,
2258
+ email: subscriber == null ? void 0 : subscriber.email,
2259
+ phone: subscriber == null ? void 0 : subscriber.phone
2260
+ };
2261
+ const primaryCta = (_b = (_a = cta.action) == null ? void 0 : _a.buttons) == null ? void 0 : _b.find((button) => button.type === "primary" /* PRIMARY */);
2262
+ const secondaryCta = (_d = (_c = cta.action) == null ? void 0 : _c.buttons) == null ? void 0 : _d.find((button) => button.type === "secondary" /* SECONDARY */);
2263
+ const actionType = (_f = (_e = cta.action) == null ? void 0 : _e.result) == null ? void 0 : _f.type;
2264
+ const actionStatus = (_g = cta.action) == null ? void 0 : _g.status;
2265
+ return __spreadProps(__spreadValues(__spreadValues({
2266
+ id: _id,
2267
+ subject,
2268
+ body: content,
2269
+ to,
2270
+ isRead: read2,
2271
+ isSeen: seen2,
2272
+ isArchived: archived,
2273
+ isSnoozed: !!snoozedUntil
2274
+ }, deliveredAt && {
2275
+ deliveredAt
2276
+ }), snoozedUntil && {
2277
+ snoozedUntil
2278
+ }), {
2279
+ createdAt,
2280
+ readAt: lastReadDate,
2281
+ firstSeenAt: firstSeenDate,
2282
+ archivedAt,
2283
+ avatar,
2284
+ primaryAction: primaryCta && {
2285
+ label: primaryCta.content,
2286
+ isCompleted: actionType === "primary" /* PRIMARY */ && actionStatus === "done" /* DONE */,
2287
+ redirect: primaryCta.url ? {
2288
+ target: primaryCta.target,
2289
+ url: primaryCta.url
2290
+ } : void 0
2291
+ },
2292
+ secondaryAction: secondaryCta && {
2293
+ label: secondaryCta.content,
2294
+ isCompleted: actionType === "secondary" /* SECONDARY */ && actionStatus === "done" /* DONE */,
2295
+ redirect: secondaryCta.url ? {
2296
+ target: secondaryCta.target,
2297
+ url: secondaryCta.url
2298
+ } : void 0
2299
+ },
2300
+ channelType: channel,
2301
+ tags,
2302
+ redirect: ((_h = cta.data) == null ? void 0 : _h.url) ? {
2303
+ url: cta.data.url,
2304
+ target: cta.data.target
2305
+ } : void 0,
2306
+ data,
2307
+ workflow
2308
+ });
2309
+ };
2310
+ var _token2, _emitter7, _partySocket, _socketUrl2, _notificationReceived2, _unseenCountChanged2, _unreadCountChanged2, _handleMessage, _PartySocketClient_instances, initializeSocket_fn2, handleConnectSocket_fn2, handleDisconnectSocket_fn2;
2311
+ var PartySocketClient = class extends BaseModule {
2312
+ constructor({
2313
+ socketUrl,
2314
+ inboxServiceInstance,
2315
+ eventEmitterInstance
2316
+ }) {
2317
+ super({
2318
+ eventEmitterInstance,
2319
+ inboxServiceInstance
2320
+ });
2321
+ __privateAdd(this, _PartySocketClient_instances);
2322
+ __privateAdd(this, _token2);
2323
+ __privateAdd(this, _emitter7);
2324
+ __privateAdd(this, _partySocket);
2325
+ __privateAdd(this, _socketUrl2);
2326
+ __privateAdd(this, _notificationReceived2, (event) => {
2327
+ try {
2328
+ const data = JSON.parse(event.data);
2329
+ if (data.event === "notification_received" /* RECEIVED */) {
2330
+ __privateGet(this, _emitter7).emit(NOTIFICATION_RECEIVED2, {
2331
+ result: new Notification(mapToNotification2(data.data.message), __privateGet(this, _emitter7), this._inboxService)
2332
+ });
2333
+ }
2334
+ } catch (error) {
2335
+ console.log("error", error);
2336
+ }
2337
+ });
2338
+ __privateAdd(this, _unseenCountChanged2, (event) => {
2339
+ try {
2340
+ const data = JSON.parse(event.data);
2341
+ if (data.event === "unseen_count_changed" /* UNSEEN */) {
2342
+ __privateGet(this, _emitter7).emit(UNSEEN_COUNT_CHANGED2, {
2343
+ result: data.data.unseenCount
2344
+ });
2345
+ }
2346
+ } catch (error) {
2347
+ }
2348
+ });
2349
+ __privateAdd(this, _unreadCountChanged2, (event) => {
2350
+ try {
2351
+ const data = JSON.parse(event.data);
2352
+ if (data.event === "unread_count_changed" /* UNREAD */) {
2353
+ __privateGet(this, _emitter7).emit(UNREAD_COUNT_CHANGED2, {
2354
+ result: data.data.unreadCount
2355
+ });
2356
+ }
2357
+ } catch (error) {
2358
+ }
2359
+ });
2360
+ __privateAdd(this, _handleMessage, (event) => {
2361
+ try {
2362
+ const data = JSON.parse(event.data);
2363
+ switch (data.event) {
2364
+ case "notification_received" /* RECEIVED */:
2365
+ __privateGet(this, _notificationReceived2).call(this, event);
2366
+ break;
2367
+ case "unseen_count_changed" /* UNSEEN */:
2368
+ __privateGet(this, _unseenCountChanged2).call(this, event);
2369
+ break;
2370
+ case "unread_count_changed" /* UNREAD */:
2371
+ __privateGet(this, _unreadCountChanged2).call(this, event);
2372
+ break;
2373
+ default:
2374
+ }
2375
+ } catch (error) {
2376
+ }
2377
+ });
2378
+ __privateSet(this, _emitter7, eventEmitterInstance);
2379
+ __privateSet(this, _socketUrl2, socketUrl != null ? socketUrl : PRODUCTION_SOCKET_URL2);
2380
+ }
2381
+ onSessionSuccess({ token }) {
2382
+ __privateSet(this, _token2, token);
2383
+ }
2384
+ isSocketEvent(eventName) {
2385
+ return eventName === NOTIFICATION_RECEIVED2 || eventName === UNSEEN_COUNT_CHANGED2 || eventName === UNREAD_COUNT_CHANGED2;
2386
+ }
2387
+ connect() {
2388
+ return __async(this, null, function* () {
2389
+ if (__privateGet(this, _token2)) {
2390
+ return __privateMethod(this, _PartySocketClient_instances, handleConnectSocket_fn2).call(this);
2391
+ }
2392
+ return this.callWithSession(__privateMethod(this, _PartySocketClient_instances, handleConnectSocket_fn2).bind(this));
2393
+ });
2394
+ }
2395
+ disconnect() {
2396
+ return __async(this, null, function* () {
2397
+ if (__privateGet(this, _partySocket)) {
2398
+ return __privateMethod(this, _PartySocketClient_instances, handleDisconnectSocket_fn2).call(this);
2399
+ }
2400
+ return this.callWithSession(__privateMethod(this, _PartySocketClient_instances, handleDisconnectSocket_fn2).bind(this));
2401
+ });
2402
+ }
2403
+ };
2404
+ _token2 = new WeakMap();
2405
+ _emitter7 = new WeakMap();
2406
+ _partySocket = new WeakMap();
2407
+ _socketUrl2 = new WeakMap();
2408
+ _notificationReceived2 = new WeakMap();
2409
+ _unseenCountChanged2 = new WeakMap();
2410
+ _unreadCountChanged2 = new WeakMap();
2411
+ _handleMessage = new WeakMap();
2412
+ _PartySocketClient_instances = new WeakSet();
2413
+ initializeSocket_fn2 = function() {
2414
+ return __async(this, null, function* () {
2415
+ if (__privateGet(this, _partySocket)) {
2416
+ return;
2417
+ }
2418
+ const args = { socketUrl: __privateGet(this, _socketUrl2) };
2419
+ __privateGet(this, _emitter7).emit("socket.connect.pending", { args });
2420
+ const url = new URL(__privateGet(this, _socketUrl2));
2421
+ url.searchParams.set("token", __privateGet(this, _token2));
2422
+ __privateSet(this, _partySocket, new WebSocket(url.toString()));
2423
+ __privateGet(this, _partySocket).addEventListener("open", () => {
2424
+ __privateGet(this, _emitter7).emit("socket.connect.resolved", { args });
2425
+ });
2426
+ __privateGet(this, _partySocket).addEventListener("error", (error) => {
2427
+ __privateGet(this, _emitter7).emit("socket.connect.resolved", { args, error });
2428
+ });
2429
+ __privateGet(this, _partySocket).addEventListener("message", __privateGet(this, _handleMessage));
2430
+ });
2431
+ };
2432
+ handleConnectSocket_fn2 = function() {
2433
+ return __async(this, null, function* () {
2434
+ try {
2435
+ yield __privateMethod(this, _PartySocketClient_instances, initializeSocket_fn2).call(this);
2436
+ return {};
2437
+ } catch (error) {
2438
+ return { error: new NovuError("Failed to initialize the PartySocket", error) };
2439
+ }
2440
+ });
2441
+ };
2442
+ handleDisconnectSocket_fn2 = function() {
2443
+ return __async(this, null, function* () {
2444
+ var _a;
2445
+ try {
2446
+ (_a = __privateGet(this, _partySocket)) == null ? void 0 : _a.close();
2447
+ __privateSet(this, _partySocket, void 0);
2448
+ return {};
2449
+ } catch (error) {
2450
+ return { error: new NovuError("Failed to disconnect from the PartySocket", error) };
2451
+ }
2452
+ });
2453
+ };
2454
+
2455
+ // src/ws/socket-factory.ts
2456
+ var PARTY_SOCKET_URLS = [
2457
+ "wss://eu.socket.novu.co",
2458
+ PRODUCTION_SOCKET_URL2,
2459
+ "wss://socket.novu-staging.co",
2460
+ "wss://socket-worker-local.cli-shortener.workers.dev"
2461
+ ];
2462
+ var URL_TRANSFORMATIONS = {
2463
+ "https://eu.ws.novu.co": "wss://eu.socket.novu.co",
2464
+ "https://ws.novu.co": PRODUCTION_SOCKET_URL2,
2465
+ "https://dev.ws.novu.co": "wss://socket.novu-staging.co"
2466
+ };
2467
+ function transformSocketUrl(socketUrl) {
2468
+ if (!socketUrl) return PRODUCTION_SOCKET_URL2;
2469
+ return URL_TRANSFORMATIONS[socketUrl] || socketUrl;
2470
+ }
2471
+ function shouldUsePartySocket(socketUrl) {
2472
+ return !socketUrl || PARTY_SOCKET_URLS.includes(socketUrl);
2473
+ }
2474
+ function createSocket({
2475
+ socketUrl,
2476
+ inboxServiceInstance,
2477
+ eventEmitterInstance
2478
+ }) {
2479
+ const transformedSocketUrl = transformSocketUrl(socketUrl);
2480
+ const socketType = shouldUsePartySocket(transformedSocketUrl) ? "partysocket" /* PARTY_SOCKET */ : "socket.io" /* SOCKET_IO */;
2481
+ switch (socketType) {
2482
+ case "partysocket" /* PARTY_SOCKET */:
2483
+ return new PartySocketClient({
2484
+ socketUrl: transformedSocketUrl,
2485
+ inboxServiceInstance,
2486
+ eventEmitterInstance
2487
+ });
2488
+ case "socket.io" /* SOCKET_IO */:
2489
+ default:
2490
+ return new Socket({
2491
+ socketUrl: transformedSocketUrl,
2492
+ inboxServiceInstance,
2493
+ eventEmitterInstance
2494
+ });
2495
+ }
2496
+ }
2055
2497
 
2056
2498
  // src/novu.ts
2057
- var _emitter7, _session, _inboxService3, _currentSubscriberId;
2499
+ var _emitter8, _session, _inboxService3;
2058
2500
  var Novu = class {
2059
2501
  constructor(options) {
2060
- __privateAdd(this, _emitter7);
2502
+ __privateAdd(this, _emitter8);
2061
2503
  __privateAdd(this, _session);
2062
2504
  __privateAdd(this, _inboxService3);
2063
- __privateAdd(this, _currentSubscriberId);
2064
2505
  var _a, _b;
2065
2506
  __privateSet(this, _inboxService3, new InboxService({
2066
2507
  apiUrl: options.apiUrl || options.backendUrl,
2067
2508
  userAgent: options.__userAgent
2068
2509
  }));
2069
- __privateSet(this, _emitter7, new NovuEventEmitter());
2510
+ __privateSet(this, _emitter8, new NovuEventEmitter());
2070
2511
  __privateSet(this, _session, new Session(
2071
2512
  {
2072
2513
  applicationIdentifier: options.applicationIdentifier || "",
2073
2514
  subscriberHash: options.subscriberHash,
2074
- subscriber: buildSubscriber(options)
2515
+ subscriber: buildSubscriber({ subscriberId: options.subscriberId, subscriber: options.subscriber })
2075
2516
  },
2076
2517
  __privateGet(this, _inboxService3),
2077
- __privateGet(this, _emitter7)
2518
+ __privateGet(this, _emitter8)
2078
2519
  ));
2079
- const initialSubscriber = buildSubscriber(options);
2080
- __privateSet(this, _currentSubscriberId, initialSubscriber.subscriberId);
2081
2520
  __privateGet(this, _session).initialize();
2082
2521
  this.notifications = new Notifications({
2083
2522
  useCache: (_a = options.useCache) != null ? _a : true,
2084
2523
  inboxServiceInstance: __privateGet(this, _inboxService3),
2085
- eventEmitterInstance: __privateGet(this, _emitter7)
2524
+ eventEmitterInstance: __privateGet(this, _emitter8)
2086
2525
  });
2087
2526
  this.preferences = new Preferences({
2088
2527
  useCache: (_b = options.useCache) != null ? _b : true,
2089
2528
  inboxServiceInstance: __privateGet(this, _inboxService3),
2090
- eventEmitterInstance: __privateGet(this, _emitter7)
2529
+ eventEmitterInstance: __privateGet(this, _emitter8)
2091
2530
  });
2092
- this.socket = new Socket({
2531
+ this.socket = createSocket({
2093
2532
  socketUrl: options.socketUrl,
2094
- eventEmitterInstance: __privateGet(this, _emitter7),
2533
+ eventEmitterInstance: __privateGet(this, _emitter8),
2095
2534
  inboxServiceInstance: __privateGet(this, _inboxService3)
2096
2535
  });
2097
2536
  this.on = (eventName, listener) => {
2098
2537
  if (this.socket.isSocketEvent(eventName)) {
2099
2538
  this.socket.connect();
2100
2539
  }
2101
- const cleanup = __privateGet(this, _emitter7).on(eventName, listener);
2540
+ const cleanup = __privateGet(this, _emitter8).on(eventName, listener);
2102
2541
  return () => {
2103
2542
  cleanup();
2104
2543
  };
2105
2544
  };
2106
2545
  this.off = (eventName, listener) => {
2107
- __privateGet(this, _emitter7).off(eventName, listener);
2546
+ __privateGet(this, _emitter8).off(eventName, listener);
2108
2547
  };
2109
2548
  }
2110
2549
  get applicationIdentifier() {
@@ -2115,30 +2554,16 @@ var Novu = class {
2115
2554
  }
2116
2555
  changeSubscriber(options) {
2117
2556
  return __async(this, null, function* () {
2118
- if (__privateGet(this, _currentSubscriberId) === options.subscriber.subscriberId) {
2119
- return;
2120
- }
2121
2557
  yield __privateGet(this, _session).initialize({
2122
2558
  applicationIdentifier: __privateGet(this, _session).applicationIdentifier || "",
2123
2559
  subscriberHash: options.subscriberHash,
2124
2560
  subscriber: options.subscriber
2125
2561
  });
2126
- __privateSet(this, _currentSubscriberId, options.subscriber.subscriberId);
2127
2562
  });
2128
2563
  }
2129
2564
  };
2130
- _emitter7 = new WeakMap();
2565
+ _emitter8 = new WeakMap();
2131
2566
  _session = new WeakMap();
2132
2567
  _inboxService3 = new WeakMap();
2133
- _currentSubscriberId = new WeakMap();
2134
- function buildSubscriber(options) {
2135
- if (options.subscriber) {
2136
- return typeof options.subscriber === "string" ? { subscriberId: options.subscriber } : options.subscriber;
2137
- }
2138
- if (options.subscriberId) {
2139
- return { subscriberId: options.subscriberId };
2140
- }
2141
- return { subscriberId: "" };
2142
- }
2143
2568
 
2144
- export { ChannelType, DEFAULT_API_VERSION, NotificationStatus, Novu, PreferenceLevel, WebSocketEvent, areTagsEqual, isSameFilter };
2569
+ export { ChannelType, DEFAULT_API_VERSION, NotificationStatus, Novu, PreferenceLevel, WebSocketEvent, areTagsEqual, isBrowser, isSameFilter };