1
1
import { api } from './api' ;
2
+ import { Logger } from './logger' ;
2
3
import {
3
4
API_REGION ,
4
5
BaseDeliveryOptions ,
@@ -34,13 +35,6 @@ type NotificationAPIClientSDKConfig = {
34
35
debug : boolean ;
35
36
} ;
36
37
37
- // Debug logger utility
38
- const debugLog = ( config : NotificationAPIClientSDKConfig , ...args : any [ ] ) => {
39
- if ( config . debug ) {
40
- console . log ( '[NotificationAPI js core Debug]' , ...args ) ;
41
- }
42
- } ;
43
-
44
38
const defaultConfig : NotificationAPIClientSDKConfig = {
45
39
host : 'api.notificationapi.com' ,
46
40
websocketHost : 'ws.notificationapi.com' ,
@@ -58,6 +52,7 @@ const defaultConfig: NotificationAPIClientSDKConfig = {
58
52
59
53
type NotificationAPIClientSDK = {
60
54
config : NotificationAPIClientSDKConfig ;
55
+ logger : Logger ;
61
56
init (
62
57
config : Partial < NotificationAPIClientSDKConfig > & {
63
58
userId : string ;
@@ -128,28 +123,25 @@ type NotificationAPIClientSDK = {
128
123
129
124
export const NotificationAPIClientSDK : NotificationAPIClientSDK = {
130
125
config : defaultConfig ,
126
+ logger : new Logger ( false ) ,
131
127
init : function ( config ) {
132
128
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
- ) ;
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
+ } ) ;
145
138
return {
146
139
...this
147
140
} ;
148
141
} ,
149
142
rest : {
150
143
generic : function ( method , resource , data ) {
151
- debugLog (
152
- NotificationAPIClientSDK . config ,
144
+ NotificationAPIClientSDK . logger . log (
153
145
`API Call: ${ method } ${ resource } ` ,
154
146
data ? { body : data } : ''
155
147
) ;
@@ -161,7 +153,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
161
153
NotificationAPIClientSDK . config . userId ,
162
154
NotificationAPIClientSDK . config . hashedUserId ,
163
155
data ,
164
- NotificationAPIClientSDK . config . debug
156
+ NotificationAPIClientSDK . logger
165
157
) ;
166
158
} ,
167
159
@@ -206,39 +198,27 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
206
198
if ( NotificationAPIClientSDK . config . hashedUserId ) {
207
199
address += `&userIdHash=${ encodeURIComponent ( NotificationAPIClientSDK . config . hashedUserId ) } ` ;
208
200
}
209
- debugLog (
210
- NotificationAPIClientSDK . config ,
211
- 'WebSocket connecting to:' ,
212
- address
213
- ) ;
201
+ NotificationAPIClientSDK . logger . log ( 'WebSocket connecting to:' , address ) ;
214
202
NotificationAPIClientSDK . websocket . object = new WebSocket ( address ) ;
215
203
216
204
NotificationAPIClientSDK . websocket . object . onopen = ( ) => {
217
- debugLog (
218
- NotificationAPIClientSDK . config ,
219
- 'WebSocket connection opened'
220
- ) ;
205
+ NotificationAPIClientSDK . logger . log ( 'WebSocket connection opened' ) ;
221
206
} ;
222
207
223
208
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
- ) ;
209
+ NotificationAPIClientSDK . logger . log ( 'WebSocket connection closed:' , {
210
+ code : event . code ,
211
+ reason : event . reason ,
212
+ wasClean : event . wasClean
213
+ } ) ;
233
214
} ;
234
215
235
216
NotificationAPIClientSDK . websocket . object . onerror = ( error ) => {
236
- debugLog ( NotificationAPIClientSDK . config , 'WebSocket error:' , error ) ;
217
+ NotificationAPIClientSDK . logger . error ( 'WebSocket error:' , error ) ;
237
218
} ;
238
219
239
220
NotificationAPIClientSDK . websocket . object . onmessage = ( m ) => {
240
- debugLog (
241
- NotificationAPIClientSDK . config ,
221
+ NotificationAPIClientSDK . logger . log (
242
222
'WebSocket message received:' ,
243
223
m . data
244
224
) ;
@@ -249,8 +229,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
249
229
250
230
if ( body . route === 'inapp_web/new_notifications' ) {
251
231
const message = body as WebSocket_NewNotification_Message ;
252
- debugLog (
253
- NotificationAPIClientSDK . config ,
232
+ NotificationAPIClientSDK . logger . log (
254
233
'New notifications received:' ,
255
234
message . payload . notifications
256
235
) ;
@@ -265,7 +244,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
265
244
} ,
266
245
disconnect : function ( callback ) {
267
246
if ( NotificationAPIClientSDK . websocket . object ) {
268
- debugLog ( NotificationAPIClientSDK . config , 'WebSocket disconnecting' ) ;
247
+ NotificationAPIClientSDK . logger . log ( 'WebSocket disconnecting' ) ;
269
248
NotificationAPIClientSDK . websocket . object ?. close ( ) ;
270
249
if ( callback ) {
271
250
callback ( NotificationAPIClientSDK . websocket . object ) ;
@@ -292,8 +271,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
292
271
// e.g. identify simply maps to postUsers
293
272
294
273
getInAppNotifications : async ( params ) => {
295
- debugLog (
296
- NotificationAPIClientSDK . config ,
274
+ NotificationAPIClientSDK . logger . log (
297
275
'getInAppNotifications called with params:' ,
298
276
params
299
277
) ;
@@ -305,7 +283,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
305
283
params . oldestNeeded ||
306
284
NotificationAPIClientSDK . config . getInAppDefaultOldest ;
307
285
308
- debugLog ( NotificationAPIClientSDK . config , 'Fetching notifications with:' , {
286
+ NotificationAPIClientSDK . logger . log ( 'Fetching notifications with:' , {
309
287
maxCountNeeded,
310
288
oldestNeeded,
311
289
before : params . before
@@ -319,7 +297,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
319
297
320
298
while ( shouldLoadMore ) {
321
299
fetchCount ++ ;
322
- debugLog ( NotificationAPIClientSDK . config , `Fetch attempt ${ fetchCount } ` , {
300
+ NotificationAPIClientSDK . logger . log ( `Fetch attempt ${ fetchCount } ` , {
323
301
oldestReceived,
324
302
resultCount : result . length
325
303
} ) ;
@@ -333,8 +311,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
333
311
( n ) => ! result . find ( ( nn ) => nn . id === n . id )
334
312
) ;
335
313
336
- debugLog (
337
- NotificationAPIClientSDK . config ,
314
+ NotificationAPIClientSDK . logger . log (
338
315
`Received ${ notis . length } notifications, ${ notisWithoutDuplicates . length } unique`
339
316
) ;
340
317
@@ -353,7 +330,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
353
330
oldestReceived < oldestNeeded
354
331
) {
355
332
shouldLoadMore = false ;
356
- debugLog ( NotificationAPIClientSDK . config , 'Stopping fetch loop:' , {
333
+ NotificationAPIClientSDK . logger . log ( 'Stopping fetch loop:' , {
357
334
hasMore,
358
335
totalResults : result . length ,
359
336
maxCountNeeded,
@@ -363,15 +340,11 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
363
340
}
364
341
}
365
342
366
- debugLog (
367
- NotificationAPIClientSDK . config ,
368
- 'getInAppNotifications completed:' ,
369
- {
370
- totalItems : result . length ,
371
- hasMore,
372
- oldestReceived
373
- }
374
- ) ;
343
+ NotificationAPIClientSDK . logger . log ( 'getInAppNotifications completed:' , {
344
+ totalItems : result . length ,
345
+ hasMore,
346
+ oldestReceived
347
+ } ) ;
375
348
376
349
return {
377
350
items : result ,
@@ -380,8 +353,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
380
353
} ;
381
354
} ,
382
355
updateInAppNotifications : async ( params ) => {
383
- debugLog (
384
- NotificationAPIClientSDK . config ,
356
+ NotificationAPIClientSDK . logger . log (
385
357
'updateInAppNotifications called with params:' ,
386
358
params
387
359
) ;
@@ -408,8 +380,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
408
380
body . opened = null ;
409
381
}
410
382
411
- debugLog (
412
- NotificationAPIClientSDK . config ,
383
+ NotificationAPIClientSDK . logger . log (
413
384
'Updating notifications with body:' ,
414
385
body
415
386
) ;
0 commit comments