11class ApiLogger {
2+
23 constructor ( limit ) {
34 if ( window . apiLogger ) {
45 return window . apiLogger ;
56 }
67 const errorQueue = this . getErrorQueueFromStorage ( ) ;
7- if ( limit && errorQueue && errorQueue . limit ) {
8- console . log (
9- `ApiLogger has already a limit. please use changeLimit function from Logger Instance.`
10- )
11- }
128 errorQueue . limit = errorQueue . limit || limit || 50 ;
13- this . limit = errorQueue . limit || limit || 50 ;
149 this . count = errorQueue . errors . length || 0 ;
1510 this . errorQueue = errorQueue ;
1611 window . apiLogger = this ;
1712 this . wrappedFetch = this . wrappedFetch . bind ( this ) ;
1813 this . originalFetch = window . fetch ;
14+ if ( errorQueue . limit !== limit ) this . changeLimit ( limit ) ;
1915 }
2016
2117 push ( value ) {
22- if ( this . count === this . limit ) this . pop ( ) ;
18+ if ( this . count === this . errorQueue . limit ) this . pop ( ) ;
2319 this . errorQueue . errors . push ( value ) ;
2420 this . count ++ ;
2521 this . updateErrorQueueInStorage ( ) ;
@@ -30,7 +26,12 @@ class ApiLogger {
3026 }
3127
3228 updateErrorQueueInStorage ( ) {
33- localStorage . setItem ( 'errorQueue' , JSON . stringify ( this . errorQueue ) ) ;
29+ try {
30+ localStorage . setItem ( 'errorQueue' , JSON . stringify ( this . errorQueue ) ) ;
31+ } catch ( e ) {
32+ console . error ( e ) ;
33+ console . log ( 'Error while updating localstorage.' ) ;
34+ }
3435 }
3536
3637 pop ( ) {
@@ -39,31 +40,48 @@ class ApiLogger {
3940 this . count -- ;
4041 }
4142
43+ getBody ( body ) {
44+ let newBody = null ;
45+ try {
46+ newBody = JSON . parse ( body ) ;
47+ } catch ( err ) {
48+ newBody = body ;
49+ }
50+ return newBody ;
51+ }
52+
4253 wrappedFetch ( ) {
4354 const store = this ;
4455 return new Promise ( ( resolve , reject ) => {
45- const [ url , { method, body } ] = arguments ;
56+ let [ url , options ] = arguments ;
57+ let { body, method } = options || { } ;
58+
4659 this . originalFetch . apply ( window , arguments )
4760 . then ( ( response ) => {
4861 if ( response && response . status >= 400 ) {
49- let reqData = null ;
50- if ( typeof body === 'object' ) {
51- reqData = body && this . truncateString ( JSON . stringify ( body ) , 50 ) ;
52- } else if ( typeof body === 'string' ) {
53- reqData = this . truncateString ( body , 50 ) ;
62+ body = body && this . getBody ( body ) ;
63+ const contentType = response . headers . get ( "content-type" ) ;
64+ const logData = {
65+ id : new Date ( ) . getTime ( ) ,
66+ url,
67+ method,
68+ body,
69+ status : response . status ,
70+ contentType : response . headers . get ( "content-type" )
5471 }
55- response . text ( ) . then ( ( text ) => {
56- store . push ( {
57- id : new Date ( ) . getTime ( ) ,
58- url,
59- method,
60- body : reqData ,
61- response : this . truncateString ( text , 50 ) ,
62- status : response . status ,
63- contentType : response . headers . get ( "content-type" )
72+ if ( contentType && contentType . includes ( "application/json" ) ) {
73+ return response . json ( ) . then ( ( json ) => {
74+ logData . response = json
75+ store . push ( logData ) ;
76+ resolve ( response ) ;
77+ } ) ;
78+ } else {
79+ response . text ( ) . then ( ( text ) => {
80+ logData . response = text ;
81+ store . push ( logData ) ;
82+ resolve ( response ) ;
6483 } ) ;
65- resolve ( response ) ;
66- } ) ;
84+ }
6785 } else {
6886 resolve ( response ) ;
6987 }
@@ -84,8 +102,10 @@ class ApiLogger {
84102 }
85103
86104 changeLimit ( limit ) {
105+ if ( this . errorQueue . errors . length > limit ) {
106+ this . errorQueue . errors = this . errorQueue . errors . slice ( this . errorQueue . errors . length - limit )
107+ }
87108 this . errorQueue . limit = limit ;
88- this . limit = limit ;
89109 this . updateErrorQueueInStorage ( ) ;
90110 }
91111}
0 commit comments