@suprsend/web-sdk 4.1.0 → 4.1.1

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.
package/dist/es/index.js CHANGED
@@ -5,7 +5,7 @@ import K from "mitt";
5
5
  import x from "jwt-decode";
6
6
  import { createStore as B } from "zustand/vanilla";
7
7
  import { io as Q } from "socket.io-client";
8
- var p = /* @__PURE__ */ ((a) => (a.OPT_IN = "opt_in", a.OPT_OUT = "opt_out", a))(p || {}), D = /* @__PURE__ */ ((a) => (a.ALL = "all", a.REQUIRED = "required", a))(D || {}), d = /* @__PURE__ */ ((a) => (a.VALIDATION_ERROR = "VALIDATION_ERROR", a.NETWORK_ERROR = "NETWORK_ERROR", a.UNKNOWN_ERROR = "UNKNOWN_ERROR", a.PERMISSION_DENIED = "PERMISSION_DENIED", a.UNSUPPORTED_ACTION = "UNSUPPORTED_ACTION", a.NOT_FOUND = "NOT_FOUND", a))(d || {}), n = /* @__PURE__ */ ((a) => (a.SUCCESS = "success", a.ERROR = "error", a))(n || {}), _ = /* @__PURE__ */ ((a) => (a.INITIAL = "INITIAL", a.LOADING = "LOADING", a.SUCCESS = "SUCCESS", a.ERROR = "ERROR", a.FETCHING_MORE = "FETCHING_MORE", a))(_ || {});
8
+ var p = /* @__PURE__ */ ((a) => (a.OPT_IN = "opt_in", a.OPT_OUT = "opt_out", a))(p || {}), C = /* @__PURE__ */ ((a) => (a.ALL = "all", a.REQUIRED = "required", a))(C || {}), d = /* @__PURE__ */ ((a) => (a.VALIDATION_ERROR = "VALIDATION_ERROR", a.NETWORK_ERROR = "NETWORK_ERROR", a.UNKNOWN_ERROR = "UNKNOWN_ERROR", a.PERMISSION_DENIED = "PERMISSION_DENIED", a.UNSUPPORTED_ACTION = "UNSUPPORTED_ACTION", a.NOT_FOUND = "NOT_FOUND", a))(d || {}), n = /* @__PURE__ */ ((a) => (a.SUCCESS = "success", a.ERROR = "error", a))(n || {}), _ = /* @__PURE__ */ ((a) => (a.INITIAL = "INITIAL", a.LOADING = "LOADING", a.SUCCESS = "SUCCESS", a.ERROR = "ERROR", a.FETCHING_MORE = "FETCHING_MORE", a))(_ || {});
9
9
  function N() {
10
10
  let a = (/* @__PURE__ */ new Date()).getTime();
11
11
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
@@ -89,33 +89,36 @@ class U {
89
89
  requestApiInstance(e) {
90
90
  switch (e.type) {
91
91
  case "get":
92
- return this.get(e.url);
92
+ return this.get(e.url, e.signal);
93
93
  case "post":
94
- return this.post(e.url, (e == null ? void 0 : e.payload) || {});
94
+ return this.post(e.url, (e == null ? void 0 : e.payload) || {}, e.signal);
95
95
  case "patch":
96
- return this.patch(e.url, (e == null ? void 0 : e.payload) || {});
96
+ return this.patch(e.url, (e == null ? void 0 : e.payload) || {}, e.signal);
97
97
  default:
98
- return this.get(e.url);
98
+ return this.get(e.url, e.signal);
99
99
  }
100
100
  }
101
- get(e) {
101
+ get(e, t) {
102
102
  return fetch(e, {
103
103
  method: "GET",
104
- headers: this.getHeaders()
104
+ headers: this.getHeaders(),
105
+ signal: t
105
106
  });
106
107
  }
107
- post(e, t) {
108
+ post(e, t, s) {
108
109
  return fetch(e, {
109
110
  method: "POST",
110
111
  body: JSON.stringify(t),
111
- headers: this.getHeaders()
112
+ headers: this.getHeaders(),
113
+ signal: s
112
114
  });
113
115
  }
114
- patch(e, t) {
116
+ patch(e, t, s) {
115
117
  return fetch(e, {
116
118
  method: "PATCH",
117
119
  body: JSON.stringify(t),
118
- headers: this.getHeaders()
120
+ headers: this.getHeaders(),
121
+ signal: s
119
122
  });
120
123
  }
121
124
  async request(e) {
@@ -280,7 +283,7 @@ class q {
280
283
  * Used to update user's category level preference.
281
284
  */
282
285
  async updateCategoryPreference(e, t, s) {
283
- var h, S, v, T, y;
286
+ var h, S, b, T, g;
284
287
  if (!e || ![p.OPT_IN, p.OPT_OUT].includes(
285
288
  t
286
289
  ))
@@ -303,13 +306,13 @@ class q {
303
306
  });
304
307
  let r = null, i = !1;
305
308
  for (const R of this.data.sections) {
306
- let g = !1;
309
+ let y = !1;
307
310
  if (R.subcategories) {
308
311
  for (const O of R.subcategories)
309
312
  if (O.category === e)
310
313
  if (r = O, O.is_editable) {
311
314
  if (O.preference !== t) {
312
- O.preference = t, i = !0, g = !0;
315
+ O.preference = t, i = !0, y = !0;
313
316
  break;
314
317
  }
315
318
  } else
@@ -318,7 +321,7 @@ class q {
318
321
  errorType: d.VALIDATION_ERROR,
319
322
  errorMessage: "Category preference is not editable"
320
323
  });
321
- if (g) break;
324
+ if (y) break;
322
325
  }
323
326
  }
324
327
  if (!r)
@@ -348,10 +351,10 @@ class q {
348
351
  l,
349
352
  r,
350
353
  {
351
- tenant_id: (s == null ? void 0 : s.tenantId) || ((v = this.preferenceArgs) == null ? void 0 : v.tenantId),
354
+ tenant_id: (s == null ? void 0 : s.tenantId) || ((b = this.preferenceArgs) == null ? void 0 : b.tenantId),
352
355
  show_opt_out_channels: u,
353
356
  tags: (s == null ? void 0 : s.tags) || ((T = this.preferenceArgs) == null ? void 0 : T.tags),
354
- locale: (s == null ? void 0 : s.locale) || ((y = this.preferenceArgs) == null ? void 0 : y.locale)
357
+ locale: (s == null ? void 0 : s.locale) || ((g = this.preferenceArgs) == null ? void 0 : g.locale)
355
358
  }
356
359
  ), o({
357
360
  status: n.SUCCESS,
@@ -362,7 +365,7 @@ class q {
362
365
  * Used to update user's category level channel preference.
363
366
  */
364
367
  async updateChannelPreferenceInCategory(e, t, s, r) {
365
- var T, y, R, g, O;
368
+ var T, g, R, y, O;
366
369
  if (!e || !s)
367
370
  return o({
368
371
  status: n.ERROR,
@@ -391,7 +394,7 @@ class q {
391
394
  });
392
395
  let i = null, c = null, u = !1;
393
396
  for (const m of this.data.sections) {
394
- let b = !1;
397
+ let v = !1;
395
398
  if (m.subcategories) {
396
399
  for (const E of m.subcategories) {
397
400
  if (E.category === s) {
@@ -400,7 +403,7 @@ class q {
400
403
  if (w.channel === e)
401
404
  if (c = w, w.is_editable) {
402
405
  if (w.preference !== t) {
403
- w.preference = t, t === p.OPT_IN && (E.preference = p.OPT_IN), u = !0, b = !0;
406
+ w.preference = t, t === p.OPT_IN && (E.preference = p.OPT_IN), u = !0, v = !0;
404
407
  break;
405
408
  }
406
409
  } else
@@ -410,9 +413,9 @@ class q {
410
413
  errorMessage: "Channel preference is not editable"
411
414
  });
412
415
  }
413
- if (b) break;
416
+ if (v) break;
414
417
  }
415
- if (b) break;
418
+ if (v) break;
416
419
  }
417
420
  }
418
421
  if (!i)
@@ -437,20 +440,20 @@ class q {
437
440
  m.preference === p.OPT_OUT && l.push(m.channel);
438
441
  });
439
442
  let h = !0;
440
- typeof (r == null ? void 0 : r.showOptOutChannels) == "boolean" ? h = r == null ? void 0 : r.showOptOutChannels : typeof ((y = this.preferenceArgs) == null ? void 0 : y.showOptOutChannels) == "boolean" && (h = this.preferenceArgs.showOptOutChannels);
441
- const v = {
443
+ typeof (r == null ? void 0 : r.showOptOutChannels) == "boolean" ? h = r == null ? void 0 : r.showOptOutChannels : typeof ((g = this.preferenceArgs) == null ? void 0 : g.showOptOutChannels) == "boolean" && (h = this.preferenceArgs.showOptOutChannels);
444
+ const b = {
442
445
  preference: h && i.preference === p.OPT_OUT && t === p.OPT_IN ? p.OPT_IN : i.preference,
443
446
  opt_out_channels: l
444
447
  };
445
448
  return this.debouncedUpdateCategoryPreferences(
446
449
  s,
447
450
  s,
448
- v,
451
+ b,
449
452
  i,
450
453
  {
451
454
  tenant_id: (r == null ? void 0 : r.tenantId) || ((R = this.preferenceArgs) == null ? void 0 : R.tenantId),
452
455
  show_opt_out_channels: h,
453
- tags: (r == null ? void 0 : r.tags) || ((g = this.preferenceArgs) == null ? void 0 : g.tags),
456
+ tags: (r == null ? void 0 : r.tags) || ((y = this.preferenceArgs) == null ? void 0 : y.tags),
454
457
  locale: (r == null ? void 0 : r.locale) || ((O = this.preferenceArgs) == null ? void 0 : O.locale)
455
458
  }
456
459
  ), o({
@@ -463,8 +466,8 @@ class q {
463
466
  */
464
467
  async updateOverallChannelPreference(e, t) {
465
468
  if (!e || ![
466
- D.ALL,
467
- D.REQUIRED
469
+ C.ALL,
470
+ C.REQUIRED
468
471
  ].includes(t))
469
472
  return o({
470
473
  status: n.ERROR,
@@ -484,7 +487,7 @@ class q {
484
487
  errorMessage: "Channel preferences doesn't exist"
485
488
  });
486
489
  let s = null, r = !1;
487
- const i = t === D.REQUIRED;
490
+ const i = t === C.REQUIRED;
488
491
  for (const c of this.data.channel_preferences)
489
492
  if (c.channel === e && (s = c, c.is_restricted !== i)) {
490
493
  c.is_restricted = i, r = !0;
@@ -961,7 +964,7 @@ class te {
961
964
  return !!await this.getPushSubscription();
962
965
  }
963
966
  }
964
- const W = 20, se = "default", re = 100, C = {
967
+ const W = 20, se = "default", re = 100, D = {
965
968
  storeId: "$suprsend_default_store",
966
969
  label: ""
967
970
  }, ie = {
@@ -974,7 +977,7 @@ const W = 20, se = "default", re = 100, C = {
974
977
  }
975
978
  }, P = {
976
979
  notifications: [],
977
- store: C,
980
+ store: D,
978
981
  pageInfo: {
979
982
  total: 0,
980
983
  pageSize: W,
@@ -1012,6 +1015,7 @@ class ae {
1012
1015
  f(this, "store");
1013
1016
  f(this, "socket");
1014
1017
  f(this, "expiryTimerId");
1018
+ f(this, "fetchAbortController");
1015
1019
  f(this, "emitter", K());
1016
1020
  this.config = e, this.setOptions(t), this.store = this.createFeedStore();
1017
1021
  }
@@ -1052,7 +1056,7 @@ class ae {
1052
1056
  var e;
1053
1057
  return {
1054
1058
  ...P,
1055
- store: ((e = this.feedOptions.stores) == null ? void 0 : e[0]) || C
1059
+ store: ((e = this.feedOptions.stores) == null ? void 0 : e[0]) || D
1056
1060
  };
1057
1061
  });
1058
1062
  }
@@ -1160,12 +1164,12 @@ class ae {
1160
1164
  }), this.emitter.emit("feed.store_update", this.data);
1161
1165
  }
1162
1166
  notificationBelongToStore(e, t) {
1163
- var g, O, m, b;
1164
- const s = !!e.read_on, r = e.archived, i = e.tags, c = e.n_category, u = (g = t == null ? void 0 : t.query) == null ? void 0 : g.read, l = (O = t == null ? void 0 : t.query) == null ? void 0 : O.archived, h = (m = t == null ? void 0 : t.query) == null ? void 0 : m.tags, S = (b = t == null ? void 0 : t.query) == null ? void 0 : b.categories, v = u == null || s === u, T = !!r == !!l;
1165
- let y = !1, R = !1;
1167
+ var y, O, m, v;
1168
+ const s = !!e.read_on, r = e.archived, i = e.tags, c = e.n_category, u = (y = t == null ? void 0 : t.query) == null ? void 0 : y.read, l = (O = t == null ? void 0 : t.query) == null ? void 0 : O.archived, h = (m = t == null ? void 0 : t.query) == null ? void 0 : m.tags, S = (v = t == null ? void 0 : t.query) == null ? void 0 : v.categories, b = u == null || s === u, T = !!r == !!l;
1169
+ let g = !1, R = !1;
1166
1170
  return Array.isArray(h) && h.length > 0 ? h.forEach((E) => {
1167
- i != null && i.includes(E) && (y = !0);
1168
- }) : y = !0, Array.isArray(S) && S.length > 0 ? S.includes(c) && (R = !0) : R = !0, v && y && R && T;
1171
+ i != null && i.includes(E) && (g = !0);
1172
+ }) : g = !0, Array.isArray(S) && S.length > 0 ? S.includes(c) && (R = !0) : R = !0, b && g && R && T;
1169
1173
  }
1170
1174
  orderNotificationsBasedOnPinFlag(e, t) {
1171
1175
  if (e.is_pinned)
@@ -1233,7 +1237,7 @@ class ae {
1233
1237
  const s = (r = this.feedOptions.stores) == null ? void 0 : r.find(
1234
1238
  (i) => i.storeId === e
1235
1239
  );
1236
- return s ? (this.store.setState({
1240
+ return s ? (this.fetchAbortController && (this.fetchAbortController.abort(), this.fetchAbortController = void 0), this.store.setState({
1237
1241
  ...P,
1238
1242
  store: s,
1239
1243
  meta: t.meta
@@ -1284,27 +1288,30 @@ class ae {
1284
1288
  distinct_id: this.config.distinctId,
1285
1289
  tenant_id: this.feedOptions.tenantId,
1286
1290
  page_size: s,
1287
- store: t.store.storeId !== C.storeId ? this.storeQueryParamObj(t.store) : null
1291
+ store: t.store.storeId !== D.storeId ? this.storeQueryParamObj(t.store) : null
1288
1292
  };
1289
1293
  if (t.notifications.length > 0) {
1290
- const u = t.notifications[t.notifications.length - 1];
1294
+ const l = t.notifications[t.notifications.length - 1];
1291
1295
  r.search_after = [
1292
- u.is_pinned,
1293
- u.created_on
1296
+ l.is_pinned,
1297
+ l.created_on
1294
1298
  ];
1295
1299
  } else
1296
1300
  r.search_after = [];
1297
- const i = this.getUrl("notifications", r), c = await this.config.client().request({ type: "get", url: i });
1298
- return c.status === n.ERROR ? (this.store.setState({ apiStatus: _.ERROR }), this.emitter.emit("feed.store_update", this.data), c) : (this.store.setState({
1299
- apiStatus: _.SUCCESS,
1300
- notifications: t.isFirstFetch ? c.body.results : [...t.notifications, ...c.body.results],
1301
- pageInfo: {
1302
- ...t.pageInfo,
1303
- total: c.body.meta.total_count,
1304
- hasMore: c.body.meta.has_more
1305
- },
1306
- isFirstFetch: !1
1307
- }), this.emitter.emit("feed.store_update", this.data), this.startExpiryTimer(), c);
1301
+ const i = this.getUrl("notifications", r), c = new AbortController();
1302
+ this.fetchAbortController = c;
1303
+ const u = await this.config.client().request({ type: "get", url: i, signal: c.signal });
1304
+ if (!c.signal.aborted)
1305
+ return u.status === n.ERROR ? (this.store.setState({ apiStatus: _.ERROR }), this.emitter.emit("feed.store_update", this.data), u) : (this.store.setState({
1306
+ apiStatus: _.SUCCESS,
1307
+ notifications: t.isFirstFetch ? u.body.results : [...t.notifications, ...u.body.results],
1308
+ pageInfo: {
1309
+ ...t.pageInfo,
1310
+ total: u.body.meta.total_count,
1311
+ hasMore: u.body.meta.has_more
1312
+ },
1313
+ isFirstFetch: !1
1314
+ }), this.emitter.emit("feed.store_update", this.data), this.startExpiryTimer(), u);
1308
1315
  }
1309
1316
  // TODO: support other stores
1310
1317
  async fetchNextPage() {
@@ -1431,7 +1438,7 @@ class ae {
1431
1438
  var e;
1432
1439
  this.store.setState({
1433
1440
  ...P,
1434
- store: ((e = this.feedOptions.stores) == null ? void 0 : e[0]) || C
1441
+ store: ((e = this.feedOptions.stores) == null ? void 0 : e[0]) || D
1435
1442
  }), this.emitter.emit("feed.store_update", this.data), this.expiryTimerId && (clearInterval(this.expiryTimerId), this.expiryTimerId = void 0);
1436
1443
  }
1437
1444
  remove() {
@@ -1567,7 +1574,7 @@ class pe {
1567
1574
  }
1568
1575
  export {
1569
1576
  _ as ApiResponseStatus,
1570
- D as ChannelLevelPreferenceOptions,
1577
+ C as ChannelLevelPreferenceOptions,
1571
1578
  d as ERROR_TYPE,
1572
1579
  ae as Feed,
1573
1580
  p as PreferenceOptions,