@notificationapi/react 0.0.33 → 0.0.34

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,15 +1,15 @@
1
- import { jsx as B } from "react/jsx-runtime";
2
- import { createContext as z, useState as N, useCallback as m, useMemo as F, useRef as k, useEffect as U, useContext as G } from "react";
3
- const J = async (e, s, i, a, f, w, h) => {
4
- const p = _(a, f, w), l = await fetch(
5
- `https://${s}/${a}/users/${encodeURIComponent(
1
+ import { jsx as F } from "react/jsx-runtime";
2
+ import { createContext as z, useState as N, useCallback as m, useMemo as G, useRef as y, useEffect as M, useContext as J } from "react";
3
+ const _ = async (e, c, i, a, f, I, h) => {
4
+ const g = q(a, f, I), l = await fetch(
5
+ `https://${c}/${a}/users/${encodeURIComponent(
6
6
  f
7
7
  )}/${i}`,
8
8
  {
9
9
  method: e,
10
10
  body: JSON.stringify(h),
11
11
  headers: {
12
- Authorization: `Basic ${p}`
12
+ Authorization: `Basic ${g}`
13
13
  }
14
14
  }
15
15
  );
@@ -18,7 +18,7 @@ const J = async (e, s, i, a, f, w, h) => {
18
18
  } catch {
19
19
  return;
20
20
  }
21
- }, _ = (e, s, i) => btoa(i ? e + ":" + s + ":" + i : e + ":" + s), M = {
21
+ }, q = (e, c, i) => btoa(i ? e + ":" + c + ":" + i : e + ":" + c), E = {
22
22
  host: "api.notificationapi.com",
23
23
  websocketHost: "ws.notificationapi.com",
24
24
  userId: "",
@@ -32,18 +32,18 @@ const J = async (e, s, i, a, f, w, h) => {
32
32
  keepWebSocketAliveForSeconds: 86400
33
33
  // 24 hours
34
34
  }, n = {
35
- config: M,
35
+ config: E,
36
36
  init: function(e) {
37
- return this.config = { ...M, ...e }, {
37
+ return this.config = { ...E, ...e }, {
38
38
  ...this
39
39
  };
40
40
  },
41
41
  rest: {
42
- generic: function(e, s, i) {
43
- return J(
42
+ generic: function(e, c, i) {
43
+ return _(
44
44
  e,
45
45
  n.config.host,
46
- s,
46
+ c,
47
47
  n.config.clientId,
48
48
  n.config.userId,
49
49
  n.config.hashedUserId,
@@ -53,10 +53,10 @@ const J = async (e, s, i, a, f, w, h) => {
53
53
  // The functions below are nice wrappers over the generic
54
54
  // rest api function above. They must follow REST API naming:
55
55
  // Method + Resource, representing the end-point.
56
- getNotifications: function(e, s) {
56
+ getNotifications: function(e, c) {
57
57
  return n.rest.generic(
58
58
  "GET",
59
- `notifications/INAPP_WEB?count=${s}&before=${e}`
59
+ `notifications/INAPP_WEB?count=${c}&before=${e}`
60
60
  );
61
61
  },
62
62
  patchNotifications: function(e) {
@@ -84,8 +84,8 @@ const J = async (e, s, i, a, f, w, h) => {
84
84
  object: void 0,
85
85
  connect: function() {
86
86
  let e = `wss://${n.config.websocketHost}?userId=${encodeURIComponent(n.config.userId)}&envId=${n.config.clientId}`;
87
- return n.config.hashedUserId && (e += `&userIdHash=${encodeURIComponent(n.config.hashedUserId)}`), n.websocket.object = new WebSocket(e), n.websocket.object.onmessage = (s) => {
88
- const i = JSON.parse(s.data);
87
+ return n.config.hashedUserId && (e += `&userIdHash=${encodeURIComponent(n.config.hashedUserId)}`), n.websocket.object = new WebSocket(e), n.websocket.object.onmessage = (c) => {
88
+ const i = JSON.parse(c.data);
89
89
  if (!(!i || !i.route) && i.route === "inapp_web/new_notifications") {
90
90
  const a = i;
91
91
  n.config.onNewInAppNotifications && n.config.onNewInAppNotifications(
@@ -95,8 +95,8 @@ const J = async (e, s, i, a, f, w, h) => {
95
95
  }, n.websocket.object;
96
96
  },
97
97
  disconnect: function(e) {
98
- var s;
99
- n.websocket.object && ((s = n.websocket.object) == null || s.close(), e && e(n.websocket.object));
98
+ var c;
99
+ n.websocket.object && ((c = n.websocket.object) == null || c.close(), e && e(n.websocket.object));
100
100
  }
101
101
  },
102
102
  openWebSocket: function() {
@@ -115,31 +115,31 @@ const J = async (e, s, i, a, f, w, h) => {
115
115
  // They may or may not do additional tasks.
116
116
  // e.g. identify simply maps to postUsers
117
117
  getInAppNotifications: async (e) => {
118
- const s = e.maxCountNeeded || n.config.getInAppDefaultCount, i = e.oldestNeeded || n.config.getInAppDefaultOldest;
119
- let a = [], f = e.before, w = !0, h = !0;
118
+ const c = e.maxCountNeeded || n.config.getInAppDefaultCount, i = e.oldestNeeded || n.config.getInAppDefaultOldest;
119
+ let a = [], f = e.before, I = !0, h = !0;
120
120
  for (; h; ) {
121
- const p = (await n.rest.getNotifications(
121
+ const g = (await n.rest.getNotifications(
122
122
  f,
123
- s
123
+ c
124
124
  )).notifications.filter(
125
- (l) => !a.find((g) => g.id === l.id)
125
+ (l) => !a.find((w) => w.id === l.id)
126
126
  );
127
- f = p.reduce(
128
- (l, g) => l < g.date ? l : g.date,
127
+ f = g.reduce(
128
+ (l, w) => l < w.date ? l : w.date,
129
129
  e.before
130
- ), a = [...a, ...p], w = p.length > 0, h = !0, (!w || a.length >= s || f < i) && (h = !1);
130
+ ), a = [...a, ...g], I = g.length > 0, h = !0, (!I || a.length >= c || f < i) && (h = !1);
131
131
  }
132
132
  return {
133
133
  items: a,
134
- hasMore: w,
134
+ hasMore: I,
135
135
  oldestReceived: f
136
136
  };
137
137
  },
138
138
  updateInAppNotifications: async (e) => {
139
- const s = {
139
+ const c = {
140
140
  trackingIds: e.ids
141
141
  };
142
- return e.archived === !0 ? s.archived = (/* @__PURE__ */ new Date()).toISOString() : e.archived === !1 && (s.archived = null), e.clicked === !0 ? s.clicked = (/* @__PURE__ */ new Date()).toISOString() : e.clicked === !1 && (s.clicked = null), e.opened === !0 ? s.opened = (/* @__PURE__ */ new Date()).toISOString() : e.opened === !1 && (s.opened = null), n.rest.patchNotifications(s);
142
+ return e.archived === !0 ? c.archived = (/* @__PURE__ */ new Date()).toISOString() : e.archived === !1 && (c.archived = null), e.clicked === !0 ? c.clicked = (/* @__PURE__ */ new Date()).toISOString() : e.clicked === !1 && (c.clicked = null), e.opened === !0 ? c.opened = (/* @__PURE__ */ new Date()).toISOString() : e.opened === !1 && (c.opened = null), n.rest.patchNotifications(c);
143
143
  },
144
144
  getPreferences: async () => n.rest.getPreferences(),
145
145
  updateDeliveryOption: async (e) => n.rest.postPreferences([e]),
@@ -150,9 +150,9 @@ const J = async (e, s, i, a, f, w, h) => {
150
150
  );
151
151
  return n.rest.postUser(e);
152
152
  }
153
- }, E = z(
153
+ }, R = z(
154
154
  void 0
155
- ), q = (e) => {
155
+ ), K = (e) => {
156
156
  const i = {
157
157
  ...{
158
158
  apiURL: "https://api.notificationapi.com",
@@ -164,23 +164,26 @@ const J = async (e, s, i, a, f, w, h) => {
164
164
  },
165
165
  ...e,
166
166
  user: "userId" in e ? { id: e.userId } : e.user
167
- }, [a, f] = N(), [w, h] = N(), [p, l] = N(!1), [g, y] = N((/* @__PURE__ */ new Date()).toISOString()), [b, P] = N(!0), D = m(() => {
168
- i.playSoundOnNewNotification && new Audio(i.newNotificationSoundPath).play().catch((r) => {
169
- console.log("Failed to play new notification sound:", r);
167
+ }, [a, f] = N(), [I, h] = N(), [g, l] = N(!1), [w, D] = N((/* @__PURE__ */ new Date()).toISOString()), [b, P] = N(!0), O = m(() => {
168
+ i.playSoundOnNewNotification && new Audio(i.newNotificationSoundPath).play().catch((s) => {
169
+ console.log("Failed to play new notification sound:", s);
170
170
  });
171
171
  }, [i.newNotificationSoundPath, i.playSoundOnNewNotification]), S = m((o) => {
172
- const r = (/* @__PURE__ */ new Date()).toISOString();
173
- f((t) => (o = o.filter((c) => !(c.expDate && new Date(c.expDate * 1e3).toISOString() > r || c.date > r)), t ? [
174
- ...o.filter((c) => !t.find((d) => d.id === c.id)),
172
+ const s = (/* @__PURE__ */ new Date()).toISOString();
173
+ f((t) => (o = o.filter((d) => {
174
+ const p = d.expDate && new Date(d.expDate * 1e3).toISOString() < s, k = new Date(d.date).getTime() > new Date(s).getTime() + 1e3;
175
+ return !p && !k;
176
+ }), t ? [
177
+ ...o.filter((d) => !t.find((k) => k.id === d.id)),
175
178
  ...t
176
179
  ] : o));
177
- }, []), u = F(() => {
180
+ }, []), u = G(() => {
178
181
  const o = e.client ? e.client : n.init({
179
182
  clientId: i.clientId,
180
183
  userId: i.user.id,
181
184
  hashedUserId: i.hashedUserId,
182
- onNewInAppNotifications: (r) => {
183
- D(), S(r);
185
+ onNewInAppNotifications: (s) => {
186
+ O(), S(s);
184
187
  }
185
188
  });
186
189
  return o.identify({
@@ -194,25 +197,25 @@ const J = async (e, s, i, a, f, w, h) => {
194
197
  i.user.number,
195
198
  i.hashedUserId,
196
199
  S,
197
- D,
200
+ O,
198
201
  e.client
199
- ]), O = m(
200
- async (o, r) => {
201
- const t = await u.rest.getNotifications(o, r);
202
- y(t.oldestReceived), P(t.couldLoadMore), S(t.notifications);
202
+ ]), v = m(
203
+ async (o, s) => {
204
+ const t = await u.rest.getNotifications(o, s);
205
+ D(t.oldestReceived), P(t.couldLoadMore), S(t.notifications);
203
206
  },
204
207
  [S, u.rest]
205
- ), v = k(b), C = k(p), x = k(g);
206
- U(() => {
207
- v.current = b, C.current = p, x.current = g;
208
- }, [b, p, g]);
208
+ ), C = y(b), x = y(g), L = y(w);
209
+ M(() => {
210
+ C.current = b, x.current = g, L.current = w;
211
+ }, [b, g, w]);
209
212
  const A = m(
210
213
  async (o) => {
211
- if (!(!o && (!v.current || C.current))) {
214
+ if (!(!o && (!C.current || x.current))) {
212
215
  l(!0);
213
216
  try {
214
- await O(
215
- o ? (/* @__PURE__ */ new Date()).toISOString() : x.current,
217
+ await v(
218
+ o ? (/* @__PURE__ */ new Date()).toISOString() : L.current,
216
219
  o ? i.initialLoadMaxCount : 1e3
217
220
  );
218
221
  } finally {
@@ -220,93 +223,93 @@ const J = async (e, s, i, a, f, w, h) => {
220
223
  }
221
224
  }
222
225
  },
223
- [i.initialLoadMaxCount, O]
224
- ), R = async (o) => {
226
+ [i.initialLoadMaxCount, v]
227
+ ), $ = async (o) => {
225
228
  if (!a) return;
226
- const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((c) => o.includes(c.id) && !c.clicked).map((c) => c.id);
227
- u.updateInAppNotifications({ ids: t, clicked: !0 }), f((c) => {
228
- if (!c) return [];
229
- const d = [...c];
230
- return d.filter((I) => t.includes(I.id)).forEach((I) => {
231
- I.clicked = r;
229
+ const s = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((r) => o.includes(r.id) && !r.clicked).map((r) => r.id);
230
+ u.updateInAppNotifications({ ids: t, clicked: !0 }), f((r) => {
231
+ if (!r) return [];
232
+ const d = [...r];
233
+ return d.filter((p) => t.includes(p.id)).forEach((p) => {
234
+ p.clicked = s;
232
235
  }), d;
233
236
  });
234
- }, $ = async () => {
237
+ }, T = async () => {
235
238
  if (!a) return;
236
- const o = (/* @__PURE__ */ new Date()).toISOString(), r = a.filter((t) => !t.opened || !t.seen).map((t) => t.id);
237
- r.length !== 0 && (u.updateInAppNotifications({
238
- ids: r,
239
+ const o = (/* @__PURE__ */ new Date()).toISOString(), s = a.filter((t) => !t.opened || !t.seen).map((t) => t.id);
240
+ s.length !== 0 && (u.updateInAppNotifications({
241
+ ids: s,
239
242
  opened: !0
240
243
  }), f((t) => {
241
244
  if (!t) return [];
242
- const c = [...t];
243
- return c.filter((d) => r.includes(d.id)).forEach((d) => {
245
+ const r = [...t];
246
+ return r.filter((d) => s.includes(d.id)).forEach((d) => {
244
247
  d.opened = o, d.seen = !0;
245
- }), c;
248
+ }), r;
246
249
  }));
247
250
  }, j = async (o) => {
248
251
  if (!a) return;
249
- const r = a.filter((t) => t.archived && (o === "ALL" || o.includes(t.id))).map((t) => t.id);
250
- r.length !== 0 && (u.updateInAppNotifications({
251
- ids: r,
252
+ const s = a.filter((t) => t.archived && (o === "ALL" || o.includes(t.id))).map((t) => t.id);
253
+ s.length !== 0 && (u.updateInAppNotifications({
254
+ ids: s,
252
255
  archived: !1
253
256
  }), f((t) => {
254
257
  if (!t) return [];
255
- const c = [...t];
256
- return c.filter((d) => r.includes(d.id)).forEach((d) => {
258
+ const r = [...t];
259
+ return r.filter((d) => s.includes(d.id)).forEach((d) => {
257
260
  d.archived = void 0;
258
- }), c;
261
+ }), r;
259
262
  }));
260
- }, T = async (o) => {
263
+ }, W = async (o) => {
261
264
  if (!a) return;
262
- const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((c) => !c.archived && (o === "ALL" || o.includes(c.id))).map((c) => c.id);
263
- t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((c) => {
264
- if (!c) return [];
265
- const d = [...c];
266
- return d.filter((I) => t.includes(I.id)).forEach((I) => {
267
- I.archived = r;
265
+ const s = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((r) => !r.archived && (o === "ALL" || o.includes(r.id))).map((r) => r.id);
266
+ t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((r) => {
267
+ if (!r) return [];
268
+ const d = [...r];
269
+ return d.filter((p) => t.includes(p.id)).forEach((p) => {
270
+ p.archived = s;
268
271
  }), d;
269
272
  }));
270
- }, W = (o, r, t, c) => L([
273
+ }, H = (o, s, t, r) => U([
271
274
  {
272
275
  notificationId: o,
273
- channel: r,
276
+ channel: s,
274
277
  delivery: t,
275
- subNotificationId: c
278
+ subNotificationId: r
276
279
  }
277
- ]), L = (o) => {
280
+ ]), U = (o) => {
278
281
  u.rest.postPreferences(o).then(() => {
279
- u.getPreferences().then((r) => {
280
- h(r);
282
+ u.getPreferences().then((s) => {
283
+ h(s);
281
284
  });
282
285
  });
283
286
  };
284
- U(() => {
285
- f([]), l(!1), h(void 0), y((/* @__PURE__ */ new Date()).toISOString()), P(!0), A(!0), u.openWebSocket(), u.getPreferences().then((o) => {
287
+ M(() => {
288
+ f([]), l(!1), h(void 0), D((/* @__PURE__ */ new Date()).toISOString()), P(!0), A(!0), u.openWebSocket(), u.getPreferences().then((o) => {
286
289
  h(o);
287
290
  });
288
291
  }, [u, A]);
289
- const H = {
292
+ const B = {
290
293
  notifications: a,
291
- preferences: w,
294
+ preferences: I,
292
295
  loadNotifications: A,
293
- markAsOpened: $,
294
- markAsArchived: T,
296
+ markAsOpened: T,
297
+ markAsArchived: W,
295
298
  markAsUnarchived: j,
296
- markAsClicked: R,
297
- updateDelivery: W,
298
- updateDeliveries: L,
299
+ markAsClicked: $,
300
+ updateDelivery: H,
301
+ updateDeliveries: U,
299
302
  getClient: () => u
300
303
  };
301
- return /* @__PURE__ */ B(E.Provider, { value: H, children: e.children });
302
- }, K = () => {
303
- const e = G(E);
304
+ return /* @__PURE__ */ F(R.Provider, { value: B, children: e.children });
305
+ }, Q = () => {
306
+ const e = J(R);
304
307
  if (!e)
305
308
  throw new Error("useMyContext must be used within a MyProvider");
306
309
  return e;
307
310
  };
308
- q.useNotificationAPIContext = K;
311
+ K.useNotificationAPIContext = Q;
309
312
  export {
310
- E as NotificationAPIContext,
311
- q as NotificationAPIProvider
313
+ R as NotificationAPIContext,
314
+ K as NotificationAPIProvider
312
315
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@notificationapi/react",
3
3
  "private": false,
4
- "version": "0.0.33",
4
+ "version": "0.0.34",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",