55 * able to render reports or do crud using jaydata context.
66 */
77
8- const request = require ( 'request ' )
8+ const axios = require ( 'axios ' )
99const url = require ( 'url' )
1010const concat = require ( 'concat-stream' )
1111const mimicResponse = require ( 'mimic-response' )
@@ -28,12 +28,13 @@ class Client {
2828 const password = this . password
2929
3030 const optionsToUse = Object . assign ( {
31- uri : url . resolve ( rootUrl , 'api/report' ) ,
32- body : JSON . stringify ( req ) ,
33- strictSSL : false ,
31+ method : 'post' ,
32+ url : url . resolve ( rootUrl , 'api/report' ) ,
33+ data : JSON . stringify ( req ) ,
3434 headers : {
3535 'Content-Type' : 'application/json'
36- }
36+ } ,
37+ responseType : 'stream'
3738 } , options )
3839
3940 if ( username ) {
@@ -43,10 +44,14 @@ class Client {
4344 }
4445 }
4546
46- return new Promise ( ( resolve , reject ) => {
47- const responseStream = request . post ( optionsToUse )
47+ let response
4848
49- responseStream . on ( 'error' , ( err ) => {
49+ try {
50+ response = await axios ( optionsToUse )
51+ } catch ( err ) {
52+ if ( err . response ) {
53+ response = err . response
54+ } else {
5055 const error = new Error ( 'Error while executing request to remote server' )
5156
5257 addStack ( error , err . stack , {
@@ -55,58 +60,57 @@ class Client {
5560
5661 error . message = `${ error . message } . ${ err . message } `
5762
58- reject ( err )
59- } )
63+ throw error
64+ }
65+ }
6066
61- responseStream . on ( 'response' , ( response ) => {
62- if ( response . statusCode !== 200 ) {
63- return responseToBuffer ( response , ( err , data ) => {
64- if ( err ) {
65- return reject ( err )
66- }
67-
68- try {
69- const errorMessage = JSON . parse ( data . toString ( ) )
70- const error = new Error ( errorMessage . message )
71-
72- addStack ( error , errorMessage . stack , {
73- stripMessage : true ,
74- stackPrefix : 'Remote stack: '
75- } )
76-
77- error . remoteStack = errorMessage . stack
78- reject ( error )
79- } catch ( e ) {
80- const err = new Error ( `Error while executing request to remote server: Unknown error, status code ${ response . statusCode } ` )
81-
82- addStack ( err , e . stack , {
83- stackPrefix : 'Parsing Error stack: '
84- } )
85-
86- err . response = response
87- reject ( err )
88- }
89- } )
90- }
67+ const responseStream = response . data
9168
92- // when working with streams and promises we should be extra-careful,
93- // promises resolves in next ticks so there is a chance that a stream
94- // can loose some data because consumer does not took the chance to process
95- // the data when it was ready.
96- // to solve this we create a transform stream that starts paused (any stream starts paused)
97- // and flow the data from response to it, since the stream is paused it won't loose data
98- // and will start emiting it when consumer calls `.body()` or any other stream method like `.pipe`
99- const newResponseStream = new PassThrough ( )
69+ let bodyData
10070
101- mimicResponse ( response , newResponseStream )
71+ if ( response . status !== 200 ) {
72+ bodyData = await responseToBuffer ( responseStream )
73+ let error
10274
103- newResponseStream . body = ( ) => responseToBuffer ( newResponseStream )
75+ try {
76+ const errorMessage = JSON . parse ( bodyData . toString ( ) )
77+ error = new Error ( errorMessage . message )
10478
105- response . pipe ( newResponseStream )
79+ addStack ( error , errorMessage . stack , {
80+ stripMessage : true ,
81+ stackPrefix : 'Remote stack: '
82+ } )
10683
107- resolve ( newResponseStream )
108- } )
109- } )
84+ error . remoteStack = errorMessage . stack
85+ } catch ( e ) {
86+ error = new Error ( `Error while executing request to remote server: Unknown error, status code ${ response . status } ` )
87+
88+ addStack ( error , e . stack , {
89+ stackPrefix : 'Parsing Error stack: '
90+ } )
91+
92+ error . response = response
93+ }
94+
95+ throw error
96+ }
97+
98+ // when working with streams and promises we should be extra-careful,
99+ // promises resolves in next ticks so there is a chance that a stream
100+ // can loose some data because consumer does not took the chance to process
101+ // the data when it was ready.
102+ // to solve this we create a transform stream that starts paused (any stream starts paused)
103+ // and flow the data from response to it, since the stream is paused it won't loose data
104+ // and will start emiting it when consumer calls `.body()` or any other stream method like `.pipe`
105+ const newResponseStream = new PassThrough ( )
106+
107+ mimicResponse ( responseStream , newResponseStream )
108+
109+ newResponseStream . body = ( ) => responseToBuffer ( newResponseStream )
110+
111+ responseStream . pipe ( newResponseStream )
112+
113+ return newResponseStream
110114 }
111115}
112116
0 commit comments