@notificationapi/react 0.0.35 → 0.0.37

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,8 @@
1
- import { jsx as z } from "react/jsx-runtime";
2
- import { createContext as Q, useState as I, useCallback as m, useMemo as X, useRef as T, useEffect as P, useContext as Y } from "react";
3
- const Z = async (e, r, o, a, f, b, h) => {
4
- const l = ee(a, f, b), g = await fetch(
5
- `https://${r}/${a}/users/${encodeURIComponent(
1
+ import { jsx as Q } from "react/jsx-runtime";
2
+ import { createContext as X, useState as I, useCallback as m, useMemo as Y, useRef as C, useEffect as P, useContext as Z } from "react";
3
+ const ee = async (e, c, o, a, f, N, h) => {
4
+ const l = te(a, f, N), g = await fetch(
5
+ `https://${c}/${a}/users/${encodeURIComponent(
6
6
  f
7
7
  )}/${o}`,
8
8
  {
@@ -18,7 +18,7 @@ const Z = async (e, r, o, a, f, b, h) => {
18
18
  } catch {
19
19
  return;
20
20
  }
21
- }, ee = (e, r, o) => btoa(o ? e + ":" + r + ":" + o : e + ":" + r), H = {
21
+ }, te = (e, c, o) => btoa(o ? e + ":" + c + ":" + o : e + ":" + c), q = {
22
22
  host: "api.notificationapi.com",
23
23
  websocketHost: "ws.notificationapi.com",
24
24
  userId: "",
@@ -32,18 +32,18 @@ const Z = async (e, r, o, a, f, b, h) => {
32
32
  keepWebSocketAliveForSeconds: 86400
33
33
  // 24 hours
34
34
  }, i = {
35
- config: H,
35
+ config: q,
36
36
  init: function(e) {
37
- return this.config = { ...H, ...e }, {
37
+ return this.config = { ...q, ...e }, {
38
38
  ...this
39
39
  };
40
40
  },
41
41
  rest: {
42
- generic: function(e, r, o) {
43
- return Z(
42
+ generic: function(e, c, o) {
43
+ return ee(
44
44
  e,
45
45
  i.config.host,
46
- r,
46
+ c,
47
47
  i.config.clientId,
48
48
  i.config.userId,
49
49
  i.config.hashedUserId,
@@ -53,10 +53,10 @@ const Z = async (e, r, o, a, f, b, 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, r) {
56
+ getNotifications: function(e, c) {
57
57
  return i.rest.generic(
58
58
  "GET",
59
- `notifications/INAPP_WEB?count=${r}&before=${e}`
59
+ `notifications/INAPP_WEB?count=${c}&before=${e}`
60
60
  );
61
61
  },
62
62
  patchNotifications: function(e) {
@@ -87,8 +87,8 @@ const Z = async (e, r, o, a, f, b, h) => {
87
87
  object: void 0,
88
88
  connect: function() {
89
89
  let e = `wss://${i.config.websocketHost}?userId=${encodeURIComponent(i.config.userId)}&envId=${i.config.clientId}`;
90
- return i.config.hashedUserId && (e += `&userIdHash=${encodeURIComponent(i.config.hashedUserId)}`), i.websocket.object = new WebSocket(e), i.websocket.object.onmessage = (r) => {
91
- const o = JSON.parse(r.data);
90
+ return i.config.hashedUserId && (e += `&userIdHash=${encodeURIComponent(i.config.hashedUserId)}`), i.websocket.object = new WebSocket(e), i.websocket.object.onmessage = (c) => {
91
+ const o = JSON.parse(c.data);
92
92
  if (!(!o || !o.route) && o.route === "inapp_web/new_notifications") {
93
93
  const a = o;
94
94
  i.config.onNewInAppNotifications && i.config.onNewInAppNotifications(
@@ -98,8 +98,8 @@ const Z = async (e, r, o, a, f, b, h) => {
98
98
  }, i.websocket.object;
99
99
  },
100
100
  disconnect: function(e) {
101
- var r;
102
- i.websocket.object && ((r = i.websocket.object) == null || r.close(), e && e(i.websocket.object));
101
+ var c;
102
+ i.websocket.object && ((c = i.websocket.object) == null || c.close(), e && e(i.websocket.object));
103
103
  }
104
104
  },
105
105
  openWebSocket: function() {
@@ -118,31 +118,31 @@ const Z = async (e, r, o, a, f, b, h) => {
118
118
  // They may or may not do additional tasks.
119
119
  // e.g. identify simply maps to postUsers
120
120
  getInAppNotifications: async (e) => {
121
- const r = e.maxCountNeeded || i.config.getInAppDefaultCount, o = e.oldestNeeded || i.config.getInAppDefaultOldest;
122
- let a = [], f = e.before, b = !0, h = !0;
121
+ const c = e.maxCountNeeded || i.config.getInAppDefaultCount, o = e.oldestNeeded || i.config.getInAppDefaultOldest;
122
+ let a = [], f = e.before, N = !0, h = !0;
123
123
  for (; h; ) {
124
124
  const l = (await i.rest.getNotifications(
125
125
  f,
126
- r
126
+ c
127
127
  )).notifications.filter(
128
128
  (g) => !a.find((w) => w.id === g.id)
129
129
  );
130
130
  f = l.reduce(
131
131
  (g, w) => g < w.date ? g : w.date,
132
132
  e.before
133
- ), a = [...a, ...l], b = l.length > 0, h = !0, (!b || a.length >= r || f < o) && (h = !1);
133
+ ), a = [...a, ...l], N = l.length > 0, h = !0, (!N || a.length >= c || f < o) && (h = !1);
134
134
  }
135
135
  return {
136
136
  items: a,
137
- hasMore: b,
137
+ hasMore: N,
138
138
  oldestReceived: f
139
139
  };
140
140
  },
141
141
  updateInAppNotifications: async (e) => {
142
- const r = {
142
+ const c = {
143
143
  trackingIds: e.ids
144
144
  };
145
- return e.archived === !0 ? r.archived = (/* @__PURE__ */ new Date()).toISOString() : e.archived === !1 && (r.archived = null), e.clicked === !0 ? r.clicked = (/* @__PURE__ */ new Date()).toISOString() : e.clicked === !1 && (r.clicked = null), e.opened === !0 ? r.opened = (/* @__PURE__ */ new Date()).toISOString() : e.opened === !1 && (r.opened = null), i.rest.patchNotifications(r);
145
+ 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), i.rest.patchNotifications(c);
146
146
  },
147
147
  getPreferences: async () => i.rest.getPreferences(),
148
148
  updateDeliveryOption: async (e) => i.rest.postPreferences([e]),
@@ -154,9 +154,9 @@ const Z = async (e, r, o, a, f, b, h) => {
154
154
  return i.rest.postUser(e);
155
155
  },
156
156
  getUserAccountMetadata: async () => i.rest.getUserAccountMetadata()
157
- }, F = Q(
157
+ }, H = X(
158
158
  void 0
159
- ), te = (e) => {
159
+ ), F = typeof window < "u", ne = (e) => {
160
160
  const o = {
161
161
  ...{
162
162
  apiURL: "https://api.notificationapi.com",
@@ -170,26 +170,26 @@ const Z = async (e, r, o, a, f, b, h) => {
170
170
  },
171
171
  ...e,
172
172
  user: "userId" in e ? { id: e.userId } : e.user
173
- }, [a, f] = I(), [b, h] = I(), [l, g] = I(), [w, y] = I(!1), [k, C] = I((/* @__PURE__ */ new Date()).toISOString()), [A, U] = I(!0), [v, N] = I(o.webPushOptInMessage), [x, J] = I(!1), W = m(() => {
173
+ }, [a, f] = I(), [N, h] = I(), [l, g] = I(), [w, S] = I(!1), [k, T] = I((/* @__PURE__ */ new Date()).toISOString()), [A, U] = I(!0), [v, b] = I(o.webPushOptInMessage), [x, J] = I(!1), W = m(() => {
174
174
  o.playSoundOnNewNotification && new Audio(o.newNotificationSoundPath).play().catch((s) => {
175
175
  console.log("Failed to play new notification sound:", s);
176
176
  });
177
- }, [o.newNotificationSoundPath, o.playSoundOnNewNotification]), S = m((t) => {
177
+ }, [o.newNotificationSoundPath, o.playSoundOnNewNotification]), y = m((t) => {
178
178
  const s = (/* @__PURE__ */ new Date()).toISOString();
179
- f((n) => (t = t.filter((d) => {
179
+ f((n) => (t = Array.isArray(t) ? t : [], t = t.filter((d) => {
180
180
  const p = d.expDate && new Date(d.expDate * 1e3).toISOString() < s, D = new Date(d.date).getTime() > new Date(s).getTime() + 1e3;
181
181
  return !p && !D;
182
- }), n ? [
182
+ }), n = Array.isArray(n) ? n : [], [
183
183
  ...t.filter((d) => !n.find((D) => D.id === d.id)),
184
184
  ...n
185
- ] : t));
186
- }, []), u = X(() => {
185
+ ]));
186
+ }, []), u = Y(() => {
187
187
  const t = e.client ? e.client : i.init({
188
188
  clientId: o.clientId,
189
189
  userId: o.user.id,
190
190
  hashedUserId: o.hashedUserId,
191
191
  onNewInAppNotifications: (s) => {
192
- W(), S(s);
192
+ W(), y(s);
193
193
  }
194
194
  });
195
195
  return t.identify({
@@ -202,45 +202,45 @@ const Z = async (e, r, o, a, f, b, h) => {
202
202
  o.user.email,
203
203
  o.user.number,
204
204
  o.hashedUserId,
205
- S,
205
+ y,
206
206
  W,
207
207
  e.client
208
- ]), L = m(
208
+ ]), E = m(
209
209
  async (t, s) => {
210
210
  const n = await u.rest.getNotifications(t, s);
211
- C(n.oldestReceived), U(n.couldLoadMore), S(n.notifications);
211
+ T(n.oldestReceived), U(n.couldLoadMore), y(n.notifications);
212
212
  },
213
- [S, u.rest]
214
- ), E = T(A), R = T(w), $ = T(k);
213
+ [y, u.rest]
214
+ ), L = C(A), R = C(w), $ = C(k);
215
215
  P(() => {
216
- E.current = A, R.current = w, $.current = k;
216
+ L.current = A, R.current = w, $.current = k;
217
217
  }, [A, w, k]);
218
218
  const O = m(
219
219
  async (t) => {
220
- if (!(!t && (!E.current || R.current))) {
221
- y(!0);
220
+ if (!(!t && (!L.current || R.current))) {
221
+ S(!0);
222
222
  try {
223
- await L(
223
+ await E(
224
224
  t ? (/* @__PURE__ */ new Date()).toISOString() : $.current,
225
225
  t ? o.initialLoadMaxCount : 1e3
226
226
  );
227
227
  } finally {
228
- y(!1);
228
+ S(!1);
229
229
  }
230
230
  }
231
231
  },
232
- [o.initialLoadMaxCount, L]
233
- ), q = async (t) => {
232
+ [o.initialLoadMaxCount, E]
233
+ ), B = async (t) => {
234
234
  if (!a) return;
235
- const s = (/* @__PURE__ */ new Date()).toISOString(), n = a.filter((c) => t.includes(c.id) && !c.clicked).map((c) => c.id);
236
- u.updateInAppNotifications({ ids: n, clicked: !0 }), f((c) => {
237
- if (!c) return [];
238
- const d = [...c];
235
+ const s = (/* @__PURE__ */ new Date()).toISOString(), n = a.filter((r) => t.includes(r.id) && !r.clicked).map((r) => r.id);
236
+ u.updateInAppNotifications({ ids: n, clicked: !0 }), f((r) => {
237
+ if (!r) return [];
238
+ const d = [...r];
239
239
  return d.filter((p) => n.includes(p.id)).forEach((p) => {
240
240
  p.clicked = s;
241
241
  }), d;
242
242
  });
243
- }, B = async () => {
243
+ }, G = async () => {
244
244
  if (!a) return;
245
245
  const t = (/* @__PURE__ */ new Date()).toISOString(), s = a.filter((n) => !n.opened || !n.seen).map((n) => n.id);
246
246
  s.length !== 0 && (u.updateInAppNotifications({
@@ -248,12 +248,12 @@ const Z = async (e, r, o, a, f, b, h) => {
248
248
  opened: !0
249
249
  }), f((n) => {
250
250
  if (!n) return [];
251
- const c = [...n];
252
- return c.filter((d) => s.includes(d.id)).forEach((d) => {
251
+ const r = [...n];
252
+ return r.filter((d) => s.includes(d.id)).forEach((d) => {
253
253
  d.opened = t, d.seen = !0;
254
- }), c;
254
+ }), r;
255
255
  }));
256
- }, G = async (t) => {
256
+ }, K = async (t) => {
257
257
  if (!a) return;
258
258
  const s = a.filter((n) => n.archived && (t === "ALL" || t.includes(n.id))).map((n) => n.id);
259
259
  s.length !== 0 && (u.updateInAppNotifications({
@@ -261,27 +261,27 @@ const Z = async (e, r, o, a, f, b, h) => {
261
261
  archived: !1
262
262
  }), f((n) => {
263
263
  if (!n) return [];
264
- const c = [...n];
265
- return c.filter((d) => s.includes(d.id)).forEach((d) => {
264
+ const r = [...n];
265
+ return r.filter((d) => s.includes(d.id)).forEach((d) => {
266
266
  d.archived = void 0;
267
- }), c;
267
+ }), r;
268
268
  }));
269
- }, K = async (t) => {
269
+ }, V = async (t) => {
270
270
  if (!a) return;
271
- const s = (/* @__PURE__ */ new Date()).toISOString(), n = a.filter((c) => !c.archived && (t === "ALL" || t.includes(c.id))).map((c) => c.id);
272
- n.length !== 0 && (u.updateInAppNotifications({ ids: n, archived: !0 }), f((c) => {
273
- if (!c) return [];
274
- const d = [...c];
271
+ const s = (/* @__PURE__ */ new Date()).toISOString(), n = a.filter((r) => !r.archived && (t === "ALL" || t.includes(r.id))).map((r) => r.id);
272
+ n.length !== 0 && (u.updateInAppNotifications({ ids: n, archived: !0 }), f((r) => {
273
+ if (!r) return [];
274
+ const d = [...r];
275
275
  return d.filter((p) => n.includes(p.id)).forEach((p) => {
276
276
  p.archived = s;
277
277
  }), d;
278
278
  }));
279
- }, V = (t, s, n, c) => j([
279
+ }, _ = (t, s, n, r) => j([
280
280
  {
281
281
  notificationId: t,
282
282
  channel: s,
283
283
  delivery: n,
284
- subNotificationId: c
284
+ subNotificationId: r
285
285
  }
286
286
  ]), j = (t) => {
287
287
  u.rest.postPreferences(t).then(() => {
@@ -291,12 +291,12 @@ const Z = async (e, r, o, a, f, b, h) => {
291
291
  });
292
292
  }, M = m(() => {
293
293
  "serviceWorker" in navigator && navigator.serviceWorker.register(o.customServiceWorkerPath).then(async (t) => {
294
- N(!1), Notification.requestPermission().then(async (s) => {
294
+ b(!1), oe().then(async (s) => {
295
295
  s === "granted" ? await t.pushManager.subscribe({
296
296
  userVisibleOnly: !0,
297
297
  applicationServerKey: l == null ? void 0 : l.userAccountMetadata.environmentVapidPublicKey
298
298
  }).then(async (n) => {
299
- const c = {
299
+ const r = {
300
300
  webPushTokens: [
301
301
  {
302
302
  sub: {
@@ -306,7 +306,7 @@ const Z = async (e, r, o, a, f, b, h) => {
306
306
  }
307
307
  ]
308
308
  };
309
- await u.identify(c), console.log("index"), localStorage.setItem("hideWebPushOptInMessage", "true");
309
+ await u.identify(r), console.log("index"), localStorage.setItem("hideWebPushOptInMessage", "true");
310
310
  }) : s === "denied" && console.log("Permission for notifications was denied");
311
311
  });
312
312
  }).catch((t) => {
@@ -328,43 +328,53 @@ const Z = async (e, r, o, a, f, b, h) => {
328
328
  l == null ? void 0 : l.userAccountMetadata.environmentVapidPublicKey
329
329
  ]);
330
330
  P(() => {
331
- f([]), y(!1), h(void 0), C((/* @__PURE__ */ new Date()).toISOString()), U(!0), O(!0), u.openWebSocket(), u.getPreferences().then((t) => {
331
+ f([]), S(!1), h(void 0), T((/* @__PURE__ */ new Date()).toISOString()), U(!0), O(!0), u.openWebSocket(), u.getPreferences().then((t) => {
332
332
  h(t);
333
333
  });
334
334
  }, [u, O, M]), P(() => {
335
- Notification.permission !== "default" && N(!1), v === "AUTOMATIC" && (N(
335
+ F && "Notification" in window && typeof Notification.requestPermission == "function" && Notification.permission !== "default" && b(!1), v === "AUTOMATIC" && (b(
336
336
  localStorage.getItem("hideWebPushOptInMessage") !== "true"
337
337
  ), u.getUserAccountMetadata().then((t) => {
338
- g(t), N(t.userAccountMetadata.hasWebPushEnabled);
338
+ g(t), b(t.userAccountMetadata.hasWebPushEnabled);
339
339
  }));
340
340
  }, [u, v]), P(() => {
341
341
  x && M();
342
342
  }, [x, M]);
343
- const _ = {
343
+ const z = {
344
344
  notifications: a,
345
- preferences: b,
345
+ preferences: N,
346
346
  userAccountMetaData: l,
347
347
  webPushOptInMessage: v,
348
348
  loadNotifications: O,
349
- markAsOpened: B,
350
- markAsArchived: K,
351
- markAsUnarchived: G,
352
- markAsClicked: q,
353
- updateDelivery: V,
349
+ markAsOpened: G,
350
+ markAsArchived: V,
351
+ markAsUnarchived: K,
352
+ markAsClicked: B,
353
+ updateDelivery: _,
354
354
  updateDeliveries: j,
355
355
  getClient: () => u,
356
- setWebPushOptInMessage: N,
356
+ setWebPushOptInMessage: b,
357
357
  setWebPushOptIn: J
358
358
  };
359
- return /* @__PURE__ */ z(F.Provider, { value: _, children: e.children });
360
- }, ne = () => {
361
- const e = Y(F);
359
+ return /* @__PURE__ */ Q(H.Provider, { value: z, children: e.children });
360
+ }, ie = () => {
361
+ const e = Z(H);
362
362
  if (!e)
363
363
  throw new Error("useMyContext must be used within a MyProvider");
364
364
  return e;
365
365
  };
366
- te.useNotificationAPIContext = ne;
366
+ ne.useNotificationAPIContext = ie;
367
+ const oe = async () => {
368
+ if (F && "Notification" in window && typeof Notification.requestPermission == "function")
369
+ try {
370
+ return await Notification.requestPermission();
371
+ } catch (e) {
372
+ return console.error("Error requesting notification permission:", e), "default";
373
+ }
374
+ else
375
+ return console.warn("Web Push Notifications are not supported in this browser."), "default";
376
+ };
367
377
  export {
368
- F as NotificationAPIContext,
369
- te as NotificationAPIProvider
378
+ H as NotificationAPIContext,
379
+ ne as NotificationAPIProvider
370
380
  };
package/package.json CHANGED
@@ -1,8 +1,19 @@
1
1
  {
2
2
  "name": "@notificationapi/react",
3
3
  "private": false,
4
- "version": "0.0.35",
4
+ "version": "0.0.37",
5
5
  "type": "module",
6
+ "browserslist": [
7
+ "last 2 versions",
8
+ "last 2 ChromeAndroid versions",
9
+ "last 2 FirefoxAndroid versions",
10
+ "last 2 Safari versions",
11
+ "last 2 iOS versions",
12
+ "not IE >= 1",
13
+ "not bb >=1",
14
+ "not IE_Mob >=1",
15
+ "not op_mini all"
16
+ ],
6
17
  "scripts": {
7
18
  "dev": "vite",
8
19
  "build": "tsc --p ./tsconfig.build.json && vite build",
@@ -25,6 +36,7 @@
25
36
  "@typescript-eslint/parser": "^7.2.0",
26
37
  "@vitejs/plugin-react": "^4.2.1",
27
38
  "eslint": "^8.57.0",
39
+ "eslint-plugin-compat": "^6.0.1",
28
40
  "eslint-plugin-react-hooks": "^4.6.0",
29
41
  "eslint-plugin-react-refresh": "^0.4.6",
30
42
  "glob": "^10.4.1",