11'use strict' ;
22
3- const $request = require ( 'request ' ) ;
3+ const rock = require ( 'rock-req ' ) ;
44
55const constants = require ( './consts' ) ;
66const Keys = require ( './keymanagers/Keys' ) ;
@@ -52,7 +52,6 @@ const stringifyKeys = (keys) => {
5252 * @property {string } storageLocation - Location where to store the files that are downloaded. This can be a network share for example.
5353 */
5454
55-
5655module . exports = class Client {
5756 /**
5857 *Creates an instance of Client.
@@ -71,18 +70,17 @@ module.exports = class Client {
7170 languageCode,
7271 storageLocation,
7372 } ) {
74- if ( ! url )
75- throw new Error ( 'EBICS URL is required' ) ;
76- if ( ! partnerId )
77- throw new Error ( 'partnerId is required' ) ;
78- if ( ! userId )
79- throw new Error ( 'userId is required' ) ;
80- if ( ! hostId )
81- throw new Error ( 'hostId is required' ) ;
82- if ( ! passphrase )
83- throw new Error ( 'passphrase is required' ) ;
84-
85- if ( ! keyStorage || typeof keyStorage . read !== 'function' || typeof keyStorage . write !== 'function' )
73+ if ( ! url ) throw new Error ( 'EBICS URL is required' ) ;
74+ if ( ! partnerId ) throw new Error ( 'partnerId is required' ) ;
75+ if ( ! userId ) throw new Error ( 'userId is required' ) ;
76+ if ( ! hostId ) throw new Error ( 'hostId is required' ) ;
77+ if ( ! passphrase ) throw new Error ( 'passphrase is required' ) ;
78+
79+ if (
80+ ! keyStorage
81+ || typeof keyStorage . read !== 'function'
82+ || typeof keyStorage . write !== 'function'
83+ )
8684 throw new Error ( 'keyStorage implementation missing or wrong' ) ;
8785
8886 this . url = url ;
@@ -99,17 +97,25 @@ module.exports = class Client {
9997 }
10098
10199 async send ( order ) {
102- const isInObject = ( 'operation' in order ) ;
100+ const isInObject = 'operation' in order ;
103101
104102 if ( ! isInObject ) throw new Error ( 'Operation for the order needed' ) ;
105103
106- if ( order . operation . toUpperCase ( ) === constants . orderOperations . ini ) return this . initialization ( order ) ;
104+ if ( order . operation . toUpperCase ( ) === constants . orderOperations . ini )
105+ return this . initialization ( order ) ;
107106
108107 const keys = await this . keys ( ) ;
109- if ( keys === null ) throw new Error ( 'No keys provided. Can not send the order or any other order for that matter.' ) ;
110-
111- if ( order . operation . toUpperCase ( ) === constants . orderOperations . upload ) return this . upload ( order ) ;
112- if ( order . operation . toUpperCase ( ) === constants . orderOperations . download ) return this . download ( order ) ;
108+ if ( keys === null )
109+ throw new Error (
110+ 'No keys provided. Can not send the order or any other order for that matter.' ,
111+ ) ;
112+
113+ if ( order . operation . toUpperCase ( ) === constants . orderOperations . upload )
114+ return this . upload ( order ) ;
115+ if (
116+ order . operation . toUpperCase ( ) === constants . orderOperations . download
117+ )
118+ return this . download ( order ) ;
113119
114120 throw new Error ( 'Wrong order operation provided' ) ;
115121 }
@@ -118,8 +124,7 @@ module.exports = class Client {
118124 const keys = await this . keys ( ) ;
119125 if ( keys === null ) await this . _generateKeys ( ) ;
120126
121- if ( this . tracesStorage )
122- this . tracesStorage . new ( ) . ofType ( 'ORDER.INI' ) ;
127+ if ( this . tracesStorage ) this . tracesStorage . new ( ) . ofType ( 'ORDER.INI' ) ;
123128 const res = await this . ebicsRequest ( order ) ;
124129 const xml = res . orderData ( ) ;
125130
@@ -132,7 +137,9 @@ module.exports = class Client {
132137
133138 technicalCode : returnedTechnicalCode ,
134139 technicalCodeSymbol : res . technicalSymbol ( ) ,
135- technicalCodeShortText : res . technicalShortText ( returnedTechnicalCode ) ,
140+ technicalCodeShortText : res . technicalShortText (
141+ returnedTechnicalCode ,
142+ ) ,
136143 technicalCodeMeaning : res . technicalMeaning ( returnedTechnicalCode ) ,
137144
138145 businessCode : returnedBusinessCode ,
@@ -167,7 +174,9 @@ module.exports = class Client {
167174
168175 technicalCode : returnedTechnicalCode ,
169176 technicalCodeSymbol : res . technicalSymbol ( ) ,
170- technicalCodeShortText : res . technicalShortText ( returnedTechnicalCode ) ,
177+ technicalCodeShortText : res . technicalShortText (
178+ returnedTechnicalCode ,
179+ ) ,
171180 technicalCodeMeaning : res . technicalMeaning ( returnedTechnicalCode ) ,
172181
173182 businessCode : returnedBusinessCode ,
@@ -178,8 +187,7 @@ module.exports = class Client {
178187 }
179188
180189 async upload ( order ) {
181- if ( this . tracesStorage )
182- this . tracesStorage . new ( ) . ofType ( 'ORDER.UPLOAD' ) ;
190+ if ( this . tracesStorage ) this . tracesStorage . new ( ) . ofType ( 'ORDER.UPLOAD' ) ;
183191 let res = await this . ebicsRequest ( order ) ;
184192 const transactionId = res . transactionId ( ) ;
185193 const orderId = res . orderId ( ) ;
@@ -197,32 +205,46 @@ module.exports = class Client {
197205 return new Promise ( async ( resolve , reject ) => {
198206 const { version } = order ;
199207 const keys = await this . keys ( ) ;
200- const r = signer . version ( version ) . sign ( ( await serializer . use ( order , this ) ) . toXML ( ) , keys . x ( ) ) ;
208+ const r = signer
209+ . version ( version )
210+ . sign ( ( await serializer . use ( order , this ) ) . toXML ( ) , keys . x ( ) ) ;
201211
202212 if ( this . tracesStorage )
203- this . tracesStorage . label ( `REQUEST.${ order . orderDetails . OrderType } ` ) . data ( r ) . persist ( ) ;
204-
205- $request . post ( {
206- url : this . url ,
207- body : r ,
208- headers : { 'content-type' : 'text/xml;charset=UTF-8' } ,
209- } , ( err , res , data ) => {
210- if ( err ) reject ( err ) ;
211-
212- const ebicsResponse = response . version ( version ) ( data , keys ) ;
213-
214- if ( this . tracesStorage )
215- this . tracesStorage . label ( `RESPONSE.${ order . orderDetails . OrderType } ` ) . connect ( ) . data ( ebicsResponse . toXML ( ) ) . persist ( ) ;
216-
217- resolve ( ebicsResponse ) ;
218- } ) ;
213+ this . tracesStorage
214+ . label ( `REQUEST.${ order . orderDetails . OrderType } ` )
215+ . data ( r )
216+ . persist ( ) ;
217+
218+ rock . post (
219+ this . url ,
220+ {
221+ body : r ,
222+ headers : { 'content-type' : 'text/xml;charset=UTF-8' } ,
223+ } ,
224+ ( err , res , data ) => {
225+ if ( err ) reject ( err ) ;
226+
227+ const ebicsResponse = response . version ( version ) ( data , keys ) ;
228+
229+ if ( this . tracesStorage )
230+ this . tracesStorage
231+ . label ( `RESPONSE.${ order . orderDetails . OrderType } ` )
232+ . connect ( )
233+ . data ( ebicsResponse . toXML ( ) )
234+ . persist ( ) ;
235+
236+ resolve ( ebicsResponse ) ;
237+ } ,
238+ ) ;
219239 } ) ;
220240 }
221241
222242 async signOrder ( order ) {
223243 const { version } = order ;
224244 const keys = await this . keys ( ) ;
225- return signer . version ( version ) . sign ( ( await serializer . use ( order , this ) ) . toXML ( ) , keys . x ( ) ) ;
245+ return signer
246+ . version ( version )
247+ . sign ( ( await serializer . use ( order , this ) ) . toXML ( ) , keys . x ( ) ) ;
226248 }
227249
228250 async keys ( ) {
@@ -253,6 +275,8 @@ module.exports = class Client {
253275 }
254276
255277 _writeKeys ( keysObject ) {
256- return this . keyStorage . write ( this . keyEncryptor . encrypt ( stringifyKeys ( keysObject . keys ) ) ) ;
278+ return this . keyStorage . write (
279+ this . keyEncryptor . encrypt ( stringifyKeys ( keysObject . keys ) ) ,
280+ ) ;
257281 }
258282} ;
0 commit comments