@@ -29,6 +29,16 @@ type NotificationAPIClientSDKConfig = {
29
29
websocketHost : string | WS_REGION ;
30
30
keepWebSocketAliveForSeconds : number ;
31
31
onNewInAppNotifications ?: ( notifications : InAppNotification [ ] ) => unknown ;
32
+
33
+ // Debug mode:
34
+ debug : boolean ;
35
+ } ;
36
+
37
+ // Debug logger utility
38
+ const debugLog = ( config : NotificationAPIClientSDKConfig , ...args : any [ ] ) => {
39
+ if ( config . debug ) {
40
+ console . log ( '[NotificationAPI js core Debug]' , ...args ) ;
41
+ }
32
42
} ;
33
43
34
44
const defaultConfig : NotificationAPIClientSDKConfig = {
@@ -42,7 +52,8 @@ const defaultConfig: NotificationAPIClientSDKConfig = {
42
52
Date . now ( ) - 30 * 24 * 60 * 60 * 1000
43
53
) . toISOString ( ) ,
44
54
onNewInAppNotifications : undefined ,
45
- keepWebSocketAliveForSeconds : 24 * 60 * 60 // 24 hours
55
+ keepWebSocketAliveForSeconds : 24 * 60 * 60 , // 24 hours
56
+ debug : false
46
57
} ;
47
58
48
59
type NotificationAPIClientSDK = {
@@ -119,20 +130,38 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
119
130
config : defaultConfig ,
120
131
init : function ( config ) {
121
132
this . config = { ...defaultConfig , ...config } ;
133
+ debugLog (
134
+ this . config ,
135
+ 'NotificationAPI js core SDK initialized with config:' ,
136
+ {
137
+ userId : this . config . userId ,
138
+ clientId : this . config . clientId ,
139
+ host : this . config . host ,
140
+ websocketHost : this . config . websocketHost ,
141
+ debug : this . config . debug ,
142
+ hasHashedUserId : ! ! this . config . hashedUserId
143
+ }
144
+ ) ;
122
145
return {
123
146
...this
124
147
} ;
125
148
} ,
126
149
rest : {
127
150
generic : function ( method , resource , data ) {
151
+ debugLog (
152
+ NotificationAPIClientSDK . config ,
153
+ `API Call: ${ method } ${ resource } ` ,
154
+ data ? { body : data } : ''
155
+ ) ;
128
156
return api (
129
157
method ,
130
158
NotificationAPIClientSDK . config . host ,
131
159
resource ,
132
160
NotificationAPIClientSDK . config . clientId ,
133
161
NotificationAPIClientSDK . config . userId ,
134
162
NotificationAPIClientSDK . config . hashedUserId ,
135
- data
163
+ data ,
164
+ NotificationAPIClientSDK . config . debug
136
165
) ;
137
166
} ,
138
167
@@ -177,15 +206,54 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
177
206
if ( NotificationAPIClientSDK . config . hashedUserId ) {
178
207
address += `&userIdHash=${ encodeURIComponent ( NotificationAPIClientSDK . config . hashedUserId ) } ` ;
179
208
}
209
+ debugLog (
210
+ NotificationAPIClientSDK . config ,
211
+ 'WebSocket connecting to:' ,
212
+ address
213
+ ) ;
180
214
NotificationAPIClientSDK . websocket . object = new WebSocket ( address ) ;
215
+
216
+ NotificationAPIClientSDK . websocket . object . onopen = ( ) => {
217
+ debugLog (
218
+ NotificationAPIClientSDK . config ,
219
+ 'WebSocket connection opened'
220
+ ) ;
221
+ } ;
222
+
223
+ NotificationAPIClientSDK . websocket . object . onclose = ( event ) => {
224
+ debugLog (
225
+ NotificationAPIClientSDK . config ,
226
+ 'WebSocket connection closed:' ,
227
+ {
228
+ code : event . code ,
229
+ reason : event . reason ,
230
+ wasClean : event . wasClean
231
+ }
232
+ ) ;
233
+ } ;
234
+
235
+ NotificationAPIClientSDK . websocket . object . onerror = ( error ) => {
236
+ debugLog ( NotificationAPIClientSDK . config , 'WebSocket error:' , error ) ;
237
+ } ;
238
+
181
239
NotificationAPIClientSDK . websocket . object . onmessage = ( m ) => {
240
+ debugLog (
241
+ NotificationAPIClientSDK . config ,
242
+ 'WebSocket message received:' ,
243
+ m . data
244
+ ) ;
182
245
const body = JSON . parse ( m . data ) ;
183
246
if ( ! body || ! body . route ) {
184
247
return ;
185
248
}
186
249
187
250
if ( body . route === 'inapp_web/new_notifications' ) {
188
251
const message = body as WebSocket_NewNotification_Message ;
252
+ debugLog (
253
+ NotificationAPIClientSDK . config ,
254
+ 'New notifications received:' ,
255
+ message . payload . notifications
256
+ ) ;
189
257
if ( NotificationAPIClientSDK . config . onNewInAppNotifications ) {
190
258
NotificationAPIClientSDK . config . onNewInAppNotifications (
191
259
message . payload . notifications
@@ -197,6 +265,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
197
265
} ,
198
266
disconnect : function ( callback ) {
199
267
if ( NotificationAPIClientSDK . websocket . object ) {
268
+ debugLog ( NotificationAPIClientSDK . config , 'WebSocket disconnecting' ) ;
200
269
NotificationAPIClientSDK . websocket . object ?. close ( ) ;
201
270
if ( callback ) {
202
271
callback ( NotificationAPIClientSDK . websocket . object ) ;
@@ -223,18 +292,38 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
223
292
// e.g. identify simply maps to postUsers
224
293
225
294
getInAppNotifications : async ( params ) => {
295
+ debugLog (
296
+ NotificationAPIClientSDK . config ,
297
+ 'getInAppNotifications called with params:' ,
298
+ params
299
+ ) ;
300
+
226
301
const maxCountNeeded =
227
302
params . maxCountNeeded ||
228
303
NotificationAPIClientSDK . config . getInAppDefaultCount ;
229
304
const oldestNeeded =
230
305
params . oldestNeeded ||
231
306
NotificationAPIClientSDK . config . getInAppDefaultOldest ;
232
307
308
+ debugLog ( NotificationAPIClientSDK . config , 'Fetching notifications with:' , {
309
+ maxCountNeeded,
310
+ oldestNeeded,
311
+ before : params . before
312
+ } ) ;
313
+
233
314
let result : InAppNotification [ ] = [ ] ;
234
315
let oldestReceived = params . before ;
235
316
let hasMore = true ;
236
317
let shouldLoadMore = true ;
318
+ let fetchCount = 0 ;
319
+
237
320
while ( shouldLoadMore ) {
321
+ fetchCount ++ ;
322
+ debugLog ( NotificationAPIClientSDK . config , `Fetch attempt ${ fetchCount } ` , {
323
+ oldestReceived,
324
+ resultCount : result . length
325
+ } ) ;
326
+
238
327
const res = await NotificationAPIClientSDK . rest . getNotifications (
239
328
oldestReceived ,
240
329
maxCountNeeded
@@ -243,6 +332,12 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
243
332
const notisWithoutDuplicates = notis . filter (
244
333
( n ) => ! result . find ( ( nn ) => nn . id === n . id )
245
334
) ;
335
+
336
+ debugLog (
337
+ NotificationAPIClientSDK . config ,
338
+ `Received ${ notis . length } notifications, ${ notisWithoutDuplicates . length } unique`
339
+ ) ;
340
+
246
341
oldestReceived = notisWithoutDuplicates . reduce (
247
342
( min : string , n ) => ( min < n . date ? min : n . date ) ,
248
343
params . before
@@ -258,16 +353,39 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
258
353
oldestReceived < oldestNeeded
259
354
) {
260
355
shouldLoadMore = false ;
356
+ debugLog ( NotificationAPIClientSDK . config , 'Stopping fetch loop:' , {
357
+ hasMore,
358
+ totalResults : result . length ,
359
+ maxCountNeeded,
360
+ oldestReceived,
361
+ oldestNeeded
362
+ } ) ;
261
363
}
262
364
}
263
365
366
+ debugLog (
367
+ NotificationAPIClientSDK . config ,
368
+ 'getInAppNotifications completed:' ,
369
+ {
370
+ totalItems : result . length ,
371
+ hasMore,
372
+ oldestReceived
373
+ }
374
+ ) ;
375
+
264
376
return {
265
377
items : result ,
266
378
hasMore,
267
379
oldestReceived
268
380
} ;
269
381
} ,
270
382
updateInAppNotifications : async ( params ) => {
383
+ debugLog (
384
+ NotificationAPIClientSDK . config ,
385
+ 'updateInAppNotifications called with params:' ,
386
+ params
387
+ ) ;
388
+
271
389
const body : {
272
390
[ key : string ] : any ;
273
391
} = {
@@ -290,6 +408,11 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
290
408
body . opened = null ;
291
409
}
292
410
411
+ debugLog (
412
+ NotificationAPIClientSDK . config ,
413
+ 'Updating notifications with body:' ,
414
+ body
415
+ ) ;
293
416
return NotificationAPIClientSDK . rest . patchNotifications ( body ) ;
294
417
} ,
295
418
getPreferences : async ( ) => {
0 commit comments