1
1
/* eslint-disable camelcase */
2
+ const pako = require ( 'pako' ) ;
2
3
const axiosInstance = require ( './axiosInstance' ) ;
3
4
const UTILS = require ( './utils' ) ;
4
5
const CONST = require ( './const' ) ;
@@ -38,9 +39,10 @@ function getResultWithDataVersion(response) {
38
39
* @param {object } payload data to be transmitted to endpoint
39
40
* @param {string } key optional basic auth string to be passed
40
41
* @param {object } customHeaders the unique reqID
42
+ * @param {boolean } compress If true, compress the data with gzip if its size is bigger than 1024
41
43
*/
42
44
// 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 ) {
44
46
/*
45
47
*CORS is only required when trying to fetch data from a browser,
46
48
*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
102
104
}
103
105
104
106
switch ( action ) {
105
- case CONST . DELETE :
107
+ case CONST . DELETE : {
106
108
if ( payload ) {
107
109
options . headers = options . headers ? options . headers : { } ;
108
110
options . headers [ 'Content-Type' ] = 'application/json; charset=utf-8' ;
@@ -116,7 +118,8 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
116
118
if ( err . response && err . response . data ) e . data = err . response . data ;
117
119
throw e ;
118
120
} ) ;
119
- case CONST . HEAD :
121
+ }
122
+ case CONST . HEAD : {
120
123
return axiosInstance
121
124
. head ( url , options )
122
125
. then ( ( response ) => ( getDataVersion ? getResultWithDataVersion ( response ) : response . data ) )
@@ -127,7 +130,8 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
127
130
}
128
131
throw e ;
129
132
} ) ;
130
- case CONST . GET :
133
+ }
134
+ case CONST . GET : {
131
135
if ( payload ) {
132
136
const ext = UTILS . URIEncodePayload ( payload ) ;
133
137
// eslint-disable-next-line no-param-reassign
@@ -143,8 +147,9 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
143
147
}
144
148
throw e ;
145
149
} ) ;
150
+ }
146
151
case CONST . ADD_CSV :
147
- case CONST . INSERT_TRIPLES :
152
+ case CONST . INSERT_TRIPLES : {
148
153
options . headers = options . headers ? options . headers : { } ;
149
154
options . headers [ 'Content-Type' ] = 'application/form-data; charset=utf-8' ;
150
155
return axiosInstance
@@ -155,32 +160,49 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
155
160
if ( err . response && err . response . data ) e . data = err . response . data ;
156
161
throw e ;
157
162
} ) ;
158
- case CONST . PUT :
163
+ }
164
+ case CONST . PUT : {
159
165
options . headers = options . headers ? options . headers : { } ;
160
166
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
+ }
161
174
return axiosInstance
162
- . put ( url , payload , options )
175
+ . put ( url , compressedContent || payload , options )
163
176
. then ( ( response ) => ( getDataVersion ? getResultWithDataVersion ( response ) : response . data ) )
164
177
. catch ( ( err ) => {
165
178
const e = new Error ( ErrorMessage . getAPIErrorMessage ( url , options , err ) ) ;
166
179
if ( err . response && err . response . data ) e . data = err . response . data ;
167
180
throw e ;
168
181
} ) ;
169
- case CONST . QUERY_DOCUMENT :
182
+ }
183
+ case CONST . QUERY_DOCUMENT : {
170
184
options . headers = options . headers ? options . headers : { } ;
171
185
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 ;
174
190
options . headers = options . headers ? options . headers : { } ;
175
191
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
+ }
176
197
return axiosInstance
177
- . post ( url , payload , options )
198
+ . post ( url , compressedContentPost || payload , options )
178
199
. then ( ( response ) => ( getDataVersion ? getResultWithDataVersion ( response ) : response . data ) )
179
200
. catch ( ( err ) => {
180
201
const e = new Error ( ErrorMessage . getAPIErrorMessage ( url , options , err ) ) ;
181
202
if ( err . response && err . response . data ) e . data = err . response . data ;
182
203
throw e ;
183
204
} ) ;
205
+ }
184
206
}
185
207
}
186
208
0 commit comments