11/* eslint-disable camelcase */ 
2+ const  pako  =  require ( 'pako' ) ; 
23const  axiosInstance  =  require ( './axiosInstance' ) ; 
34const  UTILS  =  require ( './utils' ) ; 
45const  CONST  =  require ( './const' ) ; 
@@ -38,9 +39,10 @@ function getResultWithDataVersion(response) {
3839 * @param  {object } payload data to be transmitted to endpoint 
3940 * @param  {string } key optional basic auth string to be passed 
4041 * @param  {object } customHeaders the unique reqID 
42+  * @param  {boolean } compress If true, compress the data with gzip if its size is bigger than 1024 
4143 */ 
4244// eslint-disable-next-line max-len 
43- function  DispatchRequest ( url ,  action ,  payload ,  local_auth ,  remote_auth  =  null ,  customHeaders  =  null ,  getDataVersion  =  false )  { 
45+ function  DispatchRequest ( url ,  action ,  payload ,  local_auth ,  remote_auth  =  null ,  customHeaders  =  null ,  getDataVersion  =  false ,   compress   =   false )  { 
4446  /* 
4547     *CORS is only required when trying to fetch data from a browser, 
4648     *as browsers by default will block requests to different origins 
@@ -102,7 +104,7 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
102104  } 
103105
104106  switch  ( action )  { 
105-     case  CONST . DELETE :
107+     case  CONST . DELETE :  { 
106108      if  ( payload )  { 
107109        options . headers  =  options . headers  ? options . headers  : { } ; 
108110        options . headers [ 'Content-Type' ]  =  'application/json; charset=utf-8' ; 
@@ -116,7 +118,8 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
116118          if  ( err . response  &&  err . response . data )  e . data  =  err . response . data ; 
117119          throw  e ; 
118120        } ) ; 
119-     case  CONST . HEAD :
121+     } 
122+     case  CONST . HEAD : { 
120123      return  axiosInstance 
121124        . head ( url ,  options ) 
122125        . then ( ( response )  =>  ( getDataVersion  ? getResultWithDataVersion ( response )  : response . data ) ) 
@@ -127,7 +130,8 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
127130          } 
128131          throw  e ; 
129132        } ) ; 
130-     case  CONST . GET :
133+     } 
134+     case  CONST . GET : { 
131135      if  ( payload )  { 
132136        const  ext  =  UTILS . URIEncodePayload ( payload ) ; 
133137        // eslint-disable-next-line no-param-reassign 
@@ -143,8 +147,9 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
143147          } 
144148          throw  e ; 
145149        } ) ; 
150+     } 
146151    case  CONST . ADD_CSV :
147-     case  CONST . INSERT_TRIPLES :
152+     case  CONST . INSERT_TRIPLES :  { 
148153      options . headers  =  options . headers  ? options . headers  : { } ; 
149154      options . headers [ 'Content-Type' ]  =  'application/form-data; charset=utf-8' ; 
150155      return  axiosInstance 
@@ -155,32 +160,49 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
155160          if  ( err . response  &&  err . response . data )  e . data  =  err . response . data ; 
156161          throw  e ; 
157162        } ) ; 
158-     case  CONST . PUT :
163+     } 
164+     case  CONST . PUT : { 
159165      options . headers  =  options . headers  ? options . headers  : { } ; 
160166      options . headers [ 'Content-Type' ]  =  'application/json; charset=utf-8' ; 
167+       let  compressedContent  =  null ; 
168+       const  jsonString  =  JSON . stringify ( payload ) ; 
169+ 
170+       if  ( jsonString . length  >  1024  &&  compress )  { 
171+         options . headers [ 'Content-Encoding' ]  =  'gzip' ; 
172+         compressedContent  =  pako . gzip ( jsonString ) ; 
173+       } 
161174      return  axiosInstance 
162-         . put ( url ,  payload ,  options ) 
175+         . put ( url ,  compressedContent   ||   payload ,  options ) 
163176        . then ( ( response )  =>  ( getDataVersion  ? getResultWithDataVersion ( response )  : response . data ) ) 
164177        . catch ( ( err )  =>  { 
165178          const  e  =  new  Error ( ErrorMessage . getAPIErrorMessage ( url ,  options ,  err ) ) ; 
166179          if  ( err . response  &&  err . response . data )  e . data  =  err . response . data ; 
167180          throw  e ; 
168181        } ) ; 
169-     case  CONST . QUERY_DOCUMENT :
182+     } 
183+     case  CONST . QUERY_DOCUMENT : { 
170184      options . headers  =  options . headers  ? options . headers  : { } ; 
171185      options . headers [ 'X-HTTP-Method-Override' ]  =  'GET' ; 
172-     // eslint-disable-next-line no-fallthrough 
173-     default :
186+       // eslint-disable-next-line no-fallthrough 
187+     } 
188+     default : { 
189+       let  compressedContentPost  =  null ; 
174190      options . headers  =  options . headers  ? options . headers  : { } ; 
175191      options . headers [ 'Content-Type' ]  =  'application/json; charset=utf-8' ; 
192+       const  jsonStringPost  =  JSON . stringify ( payload ) ; 
193+       if  ( jsonStringPost . length  >  1024  &&  compress )  { 
194+         options . headers [ 'Content-Encoding' ]  =  'gzip' ; 
195+         compressedContentPost  =  pako . gzip ( jsonStringPost ) ; 
196+       } 
176197      return  axiosInstance 
177-         . post ( url ,  payload ,  options ) 
198+         . post ( url ,  compressedContentPost   ||   payload ,  options ) 
178199        . then ( ( response )  =>  ( getDataVersion  ? getResultWithDataVersion ( response )  : response . data ) ) 
179200        . catch ( ( err )  =>  { 
180201          const  e  =  new  Error ( ErrorMessage . getAPIErrorMessage ( url ,  options ,  err ) ) ; 
181202          if  ( err . response  &&  err . response . data )  e . data  =  err . response . data ; 
182203          throw  e ; 
183204        } ) ; 
205+     } 
184206  } 
185207} 
186208
0 commit comments