@notificationapi/react 0.0.32 → 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.
@@ -33,6 +33,7 @@ type Props = ({
33
33
  initialLoadMaxAge?: Date;
34
34
  playSoundOnNewNotification?: boolean;
35
35
  newNotificationSoundPath?: string;
36
+ client?: typeof NotificationAPIClientSDK;
36
37
  };
37
38
  export declare const NotificationAPIProvider: React.FunctionComponent<PropsWithChildren<Props>> & {
38
39
  useNotificationAPIContext: typeof useNotificationAPIContext;
@@ -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, p) => {
4
- const h = _(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
- body: JSON.stringify(p),
10
+ body: JSON.stringify(h),
11
11
  headers: {
12
- Authorization: `Basic ${h}`
12
+ Authorization: `Basic ${g}`
13
13
  }
14
14
  }
15
15
  );
@@ -18,7 +18,7 @@ const J = async (e, s, i, a, f, w, p) => {
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, p) => {
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, p) => {
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, p) => {
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, p) => {
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, p) => {
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, p = !0;
120
- for (; p; ) {
121
- const h = (await n.rest.getNotifications(
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
+ for (; h; ) {
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 = h.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, ...h], w = h.length > 0, p = !0, (!w || a.length >= s || f < i) && (p = !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, p) => {
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, p) => {
164
164
  },
165
165
  ...e,
166
166
  user: "userId" in e ? { id: e.userId } : e.user
167
- }, [a, f] = N(), [w, p] = N(), [h, 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(() => {
178
- const o = n.init({
180
+ }, []), u = G(() => {
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,24 +197,25 @@ const J = async (e, s, i, a, f, w, p) => {
194
197
  i.user.number,
195
198
  i.hashedUserId,
196
199
  S,
197
- D
198
- ]), O = m(
199
- async (o, r) => {
200
- const t = await u.rest.getNotifications(o, r);
201
- y(t.oldestReceived), P(t.couldLoadMore), S(t.notifications);
200
+ O,
201
+ e.client
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);
202
206
  },
203
207
  [S, u.rest]
204
- ), v = k(b), C = k(h), x = k(g);
205
- U(() => {
206
- v.current = b, C.current = h, x.current = g;
207
- }, [b, h, 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]);
208
212
  const A = m(
209
213
  async (o) => {
210
- if (!(!o && (!v.current || C.current))) {
214
+ if (!(!o && (!C.current || x.current))) {
211
215
  l(!0);
212
216
  try {
213
- await O(
214
- o ? (/* @__PURE__ */ new Date()).toISOString() : x.current,
217
+ await v(
218
+ o ? (/* @__PURE__ */ new Date()).toISOString() : L.current,
215
219
  o ? i.initialLoadMaxCount : 1e3
216
220
  );
217
221
  } finally {
@@ -219,93 +223,93 @@ const J = async (e, s, i, a, f, w, p) => {
219
223
  }
220
224
  }
221
225
  },
222
- [i.initialLoadMaxCount, O]
223
- ), R = async (o) => {
226
+ [i.initialLoadMaxCount, v]
227
+ ), $ = async (o) => {
224
228
  if (!a) return;
225
- const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((c) => o.includes(c.id) && !c.clicked).map((c) => c.id);
226
- u.updateInAppNotifications({ ids: t, clicked: !0 }), f((c) => {
227
- if (!c) return [];
228
- const d = [...c];
229
- return d.filter((I) => t.includes(I.id)).forEach((I) => {
230
- 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;
231
235
  }), d;
232
236
  });
233
- }, $ = async () => {
237
+ }, T = async () => {
234
238
  if (!a) return;
235
- const o = (/* @__PURE__ */ new Date()).toISOString(), r = a.filter((t) => !t.opened || !t.seen).map((t) => t.id);
236
- r.length !== 0 && (u.updateInAppNotifications({
237
- 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,
238
242
  opened: !0
239
243
  }), f((t) => {
240
244
  if (!t) return [];
241
- const c = [...t];
242
- 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) => {
243
247
  d.opened = o, d.seen = !0;
244
- }), c;
248
+ }), r;
245
249
  }));
246
250
  }, j = async (o) => {
247
251
  if (!a) return;
248
- const r = a.filter((t) => t.archived && (o === "ALL" || o.includes(t.id))).map((t) => t.id);
249
- r.length !== 0 && (u.updateInAppNotifications({
250
- 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,
251
255
  archived: !1
252
256
  }), f((t) => {
253
257
  if (!t) return [];
254
- const c = [...t];
255
- 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) => {
256
260
  d.archived = void 0;
257
- }), c;
261
+ }), r;
258
262
  }));
259
- }, T = async (o) => {
263
+ }, W = async (o) => {
260
264
  if (!a) return;
261
- const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((c) => !c.archived && (o === "ALL" || o.includes(c.id))).map((c) => c.id);
262
- t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((c) => {
263
- if (!c) return [];
264
- const d = [...c];
265
- return d.filter((I) => t.includes(I.id)).forEach((I) => {
266
- 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;
267
271
  }), d;
268
272
  }));
269
- }, W = (o, r, t, c) => L([
273
+ }, H = (o, s, t, r) => U([
270
274
  {
271
275
  notificationId: o,
272
- channel: r,
276
+ channel: s,
273
277
  delivery: t,
274
- subNotificationId: c
278
+ subNotificationId: r
275
279
  }
276
- ]), L = (o) => {
280
+ ]), U = (o) => {
277
281
  u.rest.postPreferences(o).then(() => {
278
- u.getPreferences().then((r) => {
279
- p(r);
282
+ u.getPreferences().then((s) => {
283
+ h(s);
280
284
  });
281
285
  });
282
286
  };
283
- U(() => {
284
- f([]), l(!1), p(void 0), y((/* @__PURE__ */ new Date()).toISOString()), P(!0), A(!0), u.openWebSocket(), u.getPreferences().then((o) => {
285
- p(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) => {
289
+ h(o);
286
290
  });
287
291
  }, [u, A]);
288
- const H = {
292
+ const B = {
289
293
  notifications: a,
290
- preferences: w,
294
+ preferences: I,
291
295
  loadNotifications: A,
292
- markAsOpened: $,
293
- markAsArchived: T,
296
+ markAsOpened: T,
297
+ markAsArchived: W,
294
298
  markAsUnarchived: j,
295
- markAsClicked: R,
296
- updateDelivery: W,
297
- updateDeliveries: L,
299
+ markAsClicked: $,
300
+ updateDelivery: H,
301
+ updateDeliveries: U,
298
302
  getClient: () => u
299
303
  };
300
- return /* @__PURE__ */ B(E.Provider, { value: H, children: e.children });
301
- }, K = () => {
302
- const e = G(E);
304
+ return /* @__PURE__ */ F(R.Provider, { value: B, children: e.children });
305
+ }, Q = () => {
306
+ const e = J(R);
303
307
  if (!e)
304
308
  throw new Error("useMyContext must be used within a MyProvider");
305
309
  return e;
306
310
  };
307
- q.useNotificationAPIContext = K;
311
+ K.useNotificationAPIContext = Q;
308
312
  export {
309
- E as NotificationAPIContext,
310
- q as NotificationAPIProvider
313
+ R as NotificationAPIContext,
314
+ K as NotificationAPIProvider
311
315
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@notificationapi/react",
3
3
  "private": false,
4
- "version": "0.0.32",
4
+ "version": "0.0.34",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",