55 * able to render reports or do crud using jaydata context.
66 */
77
8- var request = require ( "request" ) ,
9- url = require ( "url" ) ,
10- concat = require ( 'concat-stream' ) ;
8+ var request = require ( 'request' )
9+ var url = require ( 'url' )
10+ var concat = require ( 'concat-stream' )
11+ var assign = require ( 'object-assign' )
1112
1213module . exports = function ( url , username , password ) {
13- return new Client ( url , username , password ) ;
14- } ;
14+ return new Client ( url , username , password )
15+ }
1516
1617var Client = function ( url , username , password ) {
17- this . url = url ;
18- this . username = username ;
19- this . password = password ;
18+ this . url = url
19+ this . username = username
20+ this . password = password
2021}
2122
2223/**
2324 * Render report in temore server and return response
2425 * @returns object containing header property with response headers and body property with response body
2526 */
2627Client . prototype . render = function ( req , options , cb ) {
27-
28- if ( typeof cb == 'undefined' ) {
29- cb = options ;
30- options = { }
28+ if ( typeof cb === 'undefined' ) {
29+ cb = options
30+ options = { }
31+ }
32+
33+ options = assign ( {
34+ uri : url . resolve ( this . url , 'api/report' ) ,
35+ body : JSON . stringify ( req ) ,
36+ strictSSL : false ,
37+ headers : {
38+ 'Content-Type' : 'application/json'
3139 }
40+ } , options )
3241
33-
34- options . uri = options . uri || url . resolve ( this . url , 'api/report' ) ;
35- options . body = options . body || JSON . stringify ( req ) ;
36- options . strictSSL = options . strictSSL || false ;
37- options . encoding = options . encoding || null ;
38- options . headers = options . headers || {
39- 'Content-Type' : 'application/json'
40- } ;
41-
42-
43- if ( this . username ) {
44- options . auth = {
45- username : this . username ,
46- password : this . password
47- }
42+ if ( this . username ) {
43+ options . auth = {
44+ username : this . username ,
45+ password : this . password
4846 }
49-
50- var responseStream = request . post ( options ) ;
51-
52- responseStream . on ( "error" , function ( err ) {
53- cb ( err ) ;
54- } ) ;
55-
56- responseStream . on ( "response" , function ( response ) {
57- response . on ( "error" , function ( err ) {
58- cb ( err )
59- } )
60-
61- if ( response . statusCode !== 200 ) {
62- return responseToBuffer ( response , function ( data ) {
63- try {
64- var errorMessage = JSON . parse ( data . toString ( ) ) ;
65- var error = new Error ( errorMessage . message ) ;
66- error . remoteStack = errorMessage . stack ;
67- cb ( error )
68- }
69- catch ( e ) {
70- cb ( new Error ( data ) ) ;
71- }
72- } ) ;
47+ }
48+
49+ var responseStream = request . post ( options )
50+
51+ responseStream . on ( 'error' , function ( err ) {
52+ cb ( err )
53+ } )
54+
55+ responseStream . on ( 'response' , function ( response ) {
56+ response . on ( 'error' , function ( err ) {
57+ cb ( err )
58+ } )
59+
60+ if ( response . statusCode !== 200 ) {
61+ return responseToBuffer ( response , function ( data ) {
62+ try {
63+ var errorMessage = JSON . parse ( data . toString ( ) )
64+ var error = new Error ( errorMessage . message )
65+ error . remoteStack = errorMessage . stack
66+ cb ( error )
67+ } catch ( e ) {
68+ var err = new Error ( 'Unknown error, status code ' + response . statusCode )
69+ err . response = response
70+ cb ( err )
7371 }
72+ } )
73+ }
7474
75- response . body = function ( cb ) { responseToBuffer ( response , cb ) ; }
76- cb ( null , response ) ;
77- } ) ;
78- } ;
79-
75+ response . body = function ( cb ) { responseToBuffer ( response , cb ) }
76+ cb ( null , response )
77+ } )
78+ }
8079
81- function responseToBuffer ( response , cb ) {
82- var writeStream = concat ( cb ) ;
83- response . pipe ( writeStream ) ;
84- }
80+ function responseToBuffer ( response , cb ) {
81+ var writeStream = concat ( cb )
82+ response . pipe ( writeStream )
83+ }
0 commit comments