1
+ /* eslint-disable require-jsdoc */
1
2
/***
2
3
* @module api/utils/countly-request
3
4
*/
4
5
5
6
const got = require ( 'got' ) ;
6
7
const FormData = require ( 'form-data' ) ;
7
- const plugins = require ( '../../../plugins/pluginManager.js' ) ;
8
8
const { HttpsProxyAgent, HttpProxyAgent} = require ( 'hpagent' ) ;
9
9
10
- var initParams = function ( uri , options , callback ) {
10
+ var initParams = function ( uri , options , callback , countlyConfig ) {
11
11
12
12
if ( typeof options === 'function' ) {
13
13
callback = options ;
@@ -32,7 +32,7 @@ var initParams = function(uri, options, callback) {
32
32
33
33
params . callback = callback || params . callback ;
34
34
35
- var config = plugins . getConfig ( "security" ) ;
35
+ var config = countlyConfig ;
36
36
37
37
if ( config && config . proxy_hostname ) {
38
38
if ( ! params . options ) {
@@ -150,39 +150,84 @@ var convertOptionsToGot = function(options) {
150
150
} ;
151
151
152
152
153
- module . exports = function ( uri , options , callback ) {
153
+ // Factory function to initialize with config
154
+ module . exports = function ( countlyConfig ) {
155
+ // Return the request function
156
+ // eslint-disable-next-line require-jsdoc
157
+ function requestFunction ( uri , options , callback ) {
158
+ if ( typeof uri === 'undefined' ) {
159
+ throw new Error ( 'undefined is not a valid uri or options object.' ) ;
160
+ }
161
+
162
+ // Initialize params with the provided config
163
+ const params = initParams ( uri , options , callback , countlyConfig ) ;
154
164
155
- if ( typeof uri === 'undefined' ) {
156
- throw new Error ( 'undefined is not a valid uri or options object.' ) ;
165
+ // Request logic follows, unchanged from your provided code
166
+ if ( params . options && ( params . options . url || params . options . uri ) ) {
167
+ got ( params . options )
168
+ . then ( response => {
169
+ params . callback ( null , response , response . body ) ;
170
+ } )
171
+ . catch ( error => {
172
+ params . callback ( error ) ;
173
+ } ) ;
174
+ }
175
+ else {
176
+ got ( params . uri , params . options )
177
+ . then ( response => {
178
+ params . callback ( null , response , response . body ) ;
179
+ } )
180
+ . catch ( error => {
181
+ params . callback ( error ) ;
182
+ } ) ;
183
+ }
157
184
}
158
185
186
+ // eslint-disable-next-line require-jsdoc
187
+ function post ( uri , options , callback , config ) {
188
+ var params = initParams ( uri , options , callback , config ) ;
189
+ if ( params . options && ( params . options . url || params . options . uri ) ) {
190
+ if ( params . options . form && params . options . form . fileStream && params . options . form . fileField ) {
191
+ // If options include a form, use uploadFormFile
192
+ const { url, form } = params . options ;
193
+ uploadFormFile ( url || params . options . uri , form , params . callback ) ;
194
+ }
195
+ else {
196
+ // Make the request using got
197
+ got . post ( params . options )
198
+ . then ( response => {
199
+ // Call the callback with the response data
200
+ params . callback ( null , response , response . body ) ;
201
+ } )
202
+ . catch ( error => {
203
+ // Call the callback with the error
204
+ params . callback ( error ) ;
205
+ } ) ;
206
+ }
207
+ }
208
+ else {
209
+ // Make the request using got
210
+ got . post ( params . uri , params . options )
211
+ . then ( response => {
212
+ params . callback ( null , response , response . body ) ;
213
+ } )
214
+ . catch ( error => {
215
+ // Call the callback with the error
216
+ params . callback ( error ) ;
217
+ } ) ;
218
+ }
219
+ }
159
220
160
- const params = initParams ( uri , options , callback ) ;
161
221
162
- if ( params . options && ( params . options . url || params . options . uri ) ) {
163
- // Make the request using got
164
- got ( params . options )
165
- . then ( response => {
166
- // Call the callback with the response data
167
- params . callback ( null , response , response . body ) ;
168
- } )
169
- . catch ( error => {
170
- // Call the callback with the error
171
- params . callback ( error ) ;
172
- } ) ;
173
- }
174
- else {
175
- // Make the request using got
176
- got ( params . uri , params . options )
177
- . then ( response => {
178
- params . callback ( null , response , response . body ) ;
179
- } )
180
- . catch ( error => {
181
- // Call the callback with the error
182
- params . callback ( error ) ;
183
- } ) ;
222
+ function get ( uri , options , callback , config ) {
223
+ module . exports ( uri , options , callback , config ) ;
184
224
}
185
225
226
+ requestFunction . post = post ;
227
+ requestFunction . get = get ;
228
+ requestFunction . convertOptionsToGot = convertOptionsToGot ;
229
+
230
+ return requestFunction ;
186
231
187
232
} ;
188
233
@@ -211,44 +256,7 @@ async function uploadFormFile(url, fileData, callback) {
211
256
}
212
257
}
213
258
214
- // Add a post method to the request object
215
- module . exports . post = function ( uri , options , callback ) {
216
- var params = initParams ( uri , options , callback ) ;
217
- if ( params . options && ( params . options . url || params . options . uri ) ) {
218
- if ( params . options . form && params . options . form . fileStream && params . options . form . fileField ) {
219
- // If options include a form, use uploadFormFile
220
- const { url, form } = params . options ;
221
- uploadFormFile ( url || params . options . uri , form , params . callback ) ;
222
- }
223
- else {
224
- // Make the request using got
225
- got . post ( params . options )
226
- . then ( response => {
227
- // Call the callback with the response data
228
- params . callback ( null , response , response . body ) ;
229
- } )
230
- . catch ( error => {
231
- // Call the callback with the error
232
- params . callback ( error ) ;
233
- } ) ;
234
- }
235
- }
236
- else {
237
- // Make the request using got
238
- got . post ( params . uri , params . options )
239
- . then ( response => {
240
- params . callback ( null , response , response . body ) ;
241
- } )
242
- . catch ( error => {
243
- // Call the callback with the error
244
- params . callback ( error ) ;
245
- } ) ;
246
- }
247
- } ;
248
259
249
- //Add a get method to the request object
250
- module . exports . get = function ( uri , options , callback ) {
251
- module . exports ( uri , options , callback ) ;
252
- } ;
260
+
253
261
254
262
module . exports . convertOptionsToGot = convertOptionsToGot ;
0 commit comments