frame.soap
Version:
SOAP client library for Frame services
176 lines • 7.56 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SoapRequests = void 0;
/* eslint-disable import/prefer-default-export */
// const soapRequest = require('easy-soap-request')
const easy_soap_request_1 = __importDefault(require("easy-soap-request"));
const electron_log_1 = __importDefault(require("electron-log"));
const xml2js = __importStar(require("xml2js"));
const SoapError_1 = require("./SoapError");
const frame_constants_1 = require("frame.constants");
const enum_1 = require("./enum");
class SoapRequests {
constructor(url, soapVersion) {
this.version1Header = `xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"`;
this.version1ContentType = { 'Content-Type': 'text/xml' };
this.version2Header = `xmlns:soap="http://www.w3.org/2003/05/soap-envelope"`;
this.version2ContentType = {
'Content-Type': 'application/soap+xml',
};
this.url = url;
this.namespace = 'namespace';
this.soapVersion = soapVersion;
if (soapVersion === enum_1.Soap.Version1) {
this.soapHeader = this.version1Header;
this.headers = this.version1ContentType;
}
else {
this.soapHeader = this.version2Header;
this.headers = this.version2ContentType;
}
}
get SoapVersion() {
return this.soapVersion;
}
static addXmlTag(attribute, start = true) {
const open = '<';
const close = '>';
return `${open}${start ? '' : '/'}${attribute}${close}`;
}
static addXmlAttribute(attribute, value, cr = false) {
return `<${attribute}>${value}${cr ? `\n` : ''}</${attribute}>`;
}
static addXmlAttr(attribute, value) {
const tag1 = SoapRequests.addXmlTag(attribute);
const tag2 = SoapRequests.addXmlTag(attribute, false);
return `${tag1}${value ?? ''}${tag2}`;
}
addXmlAction(action, values) {
const valueString = values.join('');
return SoapRequests.addXmlAttribute(`${this.namespace}:${action}`, valueString);
}
prefix(service, version) {
const prefixEnvelope = '<soap:Envelope ' +
(version === enum_1.Soap.Version1 ? this.version1Header : this.version2Header) +
` xmlns:${this.namespace}="${service}">`;
const prefixHeader = '<soap:Header/>';
const prefixBody = `<soap:Body>`;
return `${prefixEnvelope}${prefixHeader}${prefixBody}`;
}
get Suffix() {
const suffixBody = `</soap:Body>`;
const suffixEnvelope = `</soap:Envelope>`;
return `${suffixBody}${suffixEnvelope}`;
}
async runRequest(xmlString, service,
// parseFunc?: (result: any, assignFunc?: (result: any) => string) => string,
assignFunc, logging = false) {
const xml = `${this.prefix(service, this.soapVersion)}${xmlString}${this.Suffix}`;
if (logging) {
electron_log_1.default.debug(`SoapRequest.runRequest: ---------------- SoapVersion ${this.soapVersion} ----------------`);
// log.debug(`prefix: ${prefix}`);
electron_log_1.default.debug(frame_constants_1.green +
this.prefix(service, this.soapVersion) +
xmlString +
this.Suffix +
frame_constants_1.reset);
// log.debug(`suffix: ${suffix}`);
// log.debug(`headers: ${JSON.stringify(this.headers, null, 2)}`);
// log.debug(`xml: ${xml}`);
electron_log_1.default.debug(`-----------------------------------------------------------------------------`);
}
let result;
try {
const { response } = await (0, easy_soap_request_1.default)({
url: this.url,
headers: this.headers,
xml,
timeout: 5000,
});
const { body } = response;
result = body;
}
catch (error) {
let xmlResultR;
let xmlCodeR = '0';
let xmlIndicatorR = 'X';
let xmlResultL;
let xmlCodeL = '0';
let xmlIndicatorL = 'X';
const parser = new xml2js.Parser({ explicitArray: false, trim: true });
let xmlString = error.response ? error.response.data : '';
// Ensure all newline characters are removed
const xmlString2 = xmlString.replace(/\n/g, '');
parser.parseString(xmlString2, (_error, result) => {
const xmlPath = result['soap:Envelope']['soap:Body']['soap:Fault']['detail']['ns2:WebServiceFault']['faults']['fault'];
xmlResultR = xmlPath[0]['message'];
xmlCodeR = xmlPath[0]['code'];
xmlIndicatorR = xmlPath[0]['RLIndicator'];
xmlResultL = xmlPath[1]['message'];
xmlCodeL = xmlPath[1]['code'];
xmlIndicatorL = xmlPath[1]['RLIndicator'];
});
throw new SoapError_1.SoapError('runRequest', {
indicator: xmlIndicatorR === 'R' ? frame_constants_1.SIDE.Right : frame_constants_1.SIDE.Left,
message: xmlResultR,
code: parseInt(xmlCodeR),
}, {
indicator: xmlIndicatorL === 'R' ? frame_constants_1.SIDE.Right : frame_constants_1.SIDE.Left,
message: xmlResultL,
code: parseInt(xmlCodeL),
});
}
let xmlResult;
if (assignFunc) {
const parser = new xml2js.Parser({ explicitArray: false, trim: true });
try {
parser.parseString(result, (_error, result) => {
xmlResult = assignFunc(result);
});
}
catch (error) {
throw new Error(`SoapRequests.runRequest: assignFunc parseString error: ${error}`);
}
result = xmlResult;
}
return result;
}
}
exports.SoapRequests = SoapRequests;
//# sourceMappingURL=SoapRequests.js.map