1
1
import { api } from './api' ;
2
+ import { Logger } from './logger' ;
2
3
import {
3
4
API_REGION ,
4
5
BaseDeliveryOptions ,
@@ -29,6 +30,9 @@ type NotificationAPIClientSDKConfig = {
29
30
websocketHost : string | WS_REGION ;
30
31
keepWebSocketAliveForSeconds : number ;
31
32
onNewInAppNotifications ?: ( notifications : InAppNotification [ ] ) => unknown ;
33
+
34
+ // Debug mode:
35
+ debug : boolean ;
32
36
} ;
33
37
34
38
const defaultConfig : NotificationAPIClientSDKConfig = {
@@ -42,11 +46,13 @@ const defaultConfig: NotificationAPIClientSDKConfig = {
42
46
Date . now ( ) - 30 * 24 * 60 * 60 * 1000
43
47
) . toISOString ( ) ,
44
48
onNewInAppNotifications : undefined ,
45
- keepWebSocketAliveForSeconds : 24 * 60 * 60 // 24 hours
49
+ keepWebSocketAliveForSeconds : 24 * 60 * 60 , // 24 hours
50
+ debug : false
46
51
} ;
47
52
48
53
type NotificationAPIClientSDK = {
49
54
config : NotificationAPIClientSDKConfig ;
55
+ logger : Logger ;
50
56
init (
51
57
config : Partial < NotificationAPIClientSDKConfig > & {
52
58
userId : string ;
@@ -117,22 +123,37 @@ type NotificationAPIClientSDK = {
117
123
118
124
export const NotificationAPIClientSDK : NotificationAPIClientSDK = {
119
125
config : defaultConfig ,
126
+ logger : new Logger ( false ) ,
120
127
init : function ( config ) {
121
128
this . config = { ...defaultConfig , ...config } ;
129
+ this . logger = new Logger ( this . config . debug ) ;
130
+ this . logger . log ( 'Initialized with config:' , {
131
+ userId : this . config . userId ,
132
+ clientId : this . config . clientId ,
133
+ host : this . config . host ,
134
+ websocketHost : this . config . websocketHost ,
135
+ debug : this . config . debug ,
136
+ hasHashedUserId : ! ! this . config . hashedUserId
137
+ } ) ;
122
138
return {
123
139
...this
124
140
} ;
125
141
} ,
126
142
rest : {
127
143
generic : function ( method , resource , data ) {
144
+ NotificationAPIClientSDK . logger . log (
145
+ `API Call: ${ method } ${ resource } ` ,
146
+ data ? { body : data } : ''
147
+ ) ;
128
148
return api (
129
149
method ,
130
150
NotificationAPIClientSDK . config . host ,
131
151
resource ,
132
152
NotificationAPIClientSDK . config . clientId ,
133
153
NotificationAPIClientSDK . config . userId ,
134
154
NotificationAPIClientSDK . config . hashedUserId ,
135
- data
155
+ data ,
156
+ NotificationAPIClientSDK . logger
136
157
) ;
137
158
} ,
138
159
@@ -177,15 +198,41 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
177
198
if ( NotificationAPIClientSDK . config . hashedUserId ) {
178
199
address += `&userIdHash=${ encodeURIComponent ( NotificationAPIClientSDK . config . hashedUserId ) } ` ;
179
200
}
201
+ NotificationAPIClientSDK . logger . log ( 'WebSocket connecting to:' , address ) ;
180
202
NotificationAPIClientSDK . websocket . object = new WebSocket ( address ) ;
203
+
204
+ NotificationAPIClientSDK . websocket . object . onopen = ( ) => {
205
+ NotificationAPIClientSDK . logger . log ( 'WebSocket connection opened' ) ;
206
+ } ;
207
+
208
+ NotificationAPIClientSDK . websocket . object . onclose = ( event ) => {
209
+ NotificationAPIClientSDK . logger . log ( 'WebSocket connection closed:' , {
210
+ code : event . code ,
211
+ reason : event . reason ,
212
+ wasClean : event . wasClean
213
+ } ) ;
214
+ } ;
215
+
216
+ NotificationAPIClientSDK . websocket . object . onerror = ( error ) => {
217
+ NotificationAPIClientSDK . logger . error ( 'WebSocket error:' , error ) ;
218
+ } ;
219
+
181
220
NotificationAPIClientSDK . websocket . object . onmessage = ( m ) => {
221
+ NotificationAPIClientSDK . logger . log (
222
+ 'WebSocket message received:' ,
223
+ m . data
224
+ ) ;
182
225
const body = JSON . parse ( m . data ) ;
183
226
if ( ! body || ! body . route ) {
184
227
return ;
185
228
}
186
229
187
230
if ( body . route === 'inapp_web/new_notifications' ) {
188
231
const message = body as WebSocket_NewNotification_Message ;
232
+ NotificationAPIClientSDK . logger . log (
233
+ 'New notifications received:' ,
234
+ message . payload . notifications
235
+ ) ;
189
236
if ( NotificationAPIClientSDK . config . onNewInAppNotifications ) {
190
237
NotificationAPIClientSDK . config . onNewInAppNotifications (
191
238
message . payload . notifications
@@ -197,6 +244,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
197
244
} ,
198
245
disconnect : function ( callback ) {
199
246
if ( NotificationAPIClientSDK . websocket . object ) {
247
+ NotificationAPIClientSDK . logger . log ( 'WebSocket disconnecting' ) ;
200
248
NotificationAPIClientSDK . websocket . object ?. close ( ) ;
201
249
if ( callback ) {
202
250
callback ( NotificationAPIClientSDK . websocket . object ) ;
@@ -223,17 +271,29 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
223
271
// e.g. identify simply maps to postUsers
224
272
225
273
getInAppNotifications : async ( params ) => {
274
+ NotificationAPIClientSDK . logger . log (
275
+ 'getInAppNotifications called with params:' ,
276
+ params
277
+ ) ;
278
+
226
279
const maxCountNeeded =
227
280
params . maxCountNeeded ||
228
281
NotificationAPIClientSDK . config . getInAppDefaultCount ;
229
282
const oldestNeeded =
230
283
params . oldestNeeded ||
231
284
NotificationAPIClientSDK . config . getInAppDefaultOldest ;
232
285
286
+ NotificationAPIClientSDK . logger . log ( 'Fetching notifications with:' , {
287
+ maxCountNeeded,
288
+ oldestNeeded,
289
+ before : params . before
290
+ } ) ;
291
+
233
292
let result : InAppNotification [ ] = [ ] ;
234
293
let oldestReceived = params . before ;
235
294
let hasMore = true ;
236
295
let shouldLoadMore = true ;
296
+
237
297
while ( shouldLoadMore ) {
238
298
const res = await NotificationAPIClientSDK . rest . getNotifications (
239
299
oldestReceived ,
@@ -243,6 +303,11 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
243
303
const notisWithoutDuplicates = notis . filter (
244
304
( n ) => ! result . find ( ( nn ) => nn . id === n . id )
245
305
) ;
306
+
307
+ NotificationAPIClientSDK . logger . log (
308
+ `Received ${ notis . length } notifications, ${ notisWithoutDuplicates . length } unique`
309
+ ) ;
310
+
246
311
oldestReceived = notisWithoutDuplicates . reduce (
247
312
( min : string , n ) => ( min < n . date ? min : n . date ) ,
248
313
params . before
@@ -258,16 +323,34 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
258
323
oldestReceived < oldestNeeded
259
324
) {
260
325
shouldLoadMore = false ;
326
+ NotificationAPIClientSDK . logger . log ( 'Stopping fetch loop:' , {
327
+ hasMore,
328
+ totalResults : result . length ,
329
+ maxCountNeeded,
330
+ oldestReceived,
331
+ oldestNeeded
332
+ } ) ;
261
333
}
262
334
}
263
335
336
+ NotificationAPIClientSDK . logger . log ( 'getInAppNotifications completed:' , {
337
+ totalItems : result . length ,
338
+ hasMore,
339
+ oldestReceived
340
+ } ) ;
341
+
264
342
return {
265
343
items : result ,
266
344
hasMore,
267
345
oldestReceived
268
346
} ;
269
347
} ,
270
348
updateInAppNotifications : async ( params ) => {
349
+ NotificationAPIClientSDK . logger . log (
350
+ 'updateInAppNotifications called with params:' ,
351
+ params
352
+ ) ;
353
+
271
354
const body : {
272
355
[ key : string ] : any ;
273
356
} = {
@@ -290,6 +373,10 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
290
373
body . opened = null ;
291
374
}
292
375
376
+ NotificationAPIClientSDK . logger . log (
377
+ 'Updating notifications with body:' ,
378
+ body
379
+ ) ;
293
380
return NotificationAPIClientSDK . rest . patchNotifications ( body ) ;
294
381
} ,
295
382
getPreferences : async ( ) => {
0 commit comments