Commit de198bc0 authored by Panagiotis Skarvelis's avatar Panagiotis Skarvelis
Browse files

hackish way to make soap to work on both esm/cjs

parent 92a8f01b
import thesoap from 'soap';
let soap = thesoap;
try {
soap = require('soap');
}
catch (error) {
//my hackish way to make soap work on both esm and cjs
//theshoap on esm is undefined
//On esm require is not defined, however on cjs require can be used.
//So we try to use require and if it fails we use the thesoap module
}
/**
* SOAP client for getNncIdentity
* @class Soap
* @description SOAP client for getNncIdentity
* @param {string} wsdl - The URL of the SOAP service
* @param {string} username
* @param {string} password
* @param {AuditRecord} auditRecord
*/
class Soap {
_client;
_wsdl;
_username;
_password;
_auditRecord;
constructor(wsdl, username, password, auditRecord) {
this._wsdl = wsdl;
this._username = username;
this._password = password;
this._auditRecord = auditRecord;
}
async init() {
try {
const client = await soap.createClientAsync(this._wsdl, {
wsdl_headers: {
'Authorization': 'Basic ' + Buffer.from(`${this._username}:${this._password}`).toString('base64'),
},
});
this._client = client;
return client;
}
catch (e) {
throw e;
}
}
async getIdentity(vat) {
try {
const client = await this.init();
var options = {
hasNonce: true,
actor: 'actor'
};
var wsSecurity = new soap.WSSecurity(this._username, this._password, options);
client.setSecurity(wsSecurity);
const auditRecord = this._auditRecord;
const args = {
auditRecord: auditRecord,
getNncIdentityInputRecord: {
afm: vat
}
};
const result = await client.getNncIdentityAsync(args);
return result[0].getNncIdentityOutputRecord;
}
catch (e) {
throw e;
}
}
}
export default Soap;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment