UNPKG

soap-next

Version:
168 lines (167 loc) 7.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const http_1 = require("http"); class SoapRequest { constructor(url, params, wsdl) { this.url = url; this.clientParams = params; if (typeof this.clientParams.httpHeaders !== 'undefined') { this.clientParams.httpHeaders['Content-Type'] = 'text/xml; charset=utf-8'; } else { this.clientParams.httpHeaders = { 'Content-Type': 'text/xml; charset=utf-8', }; } this.wsdl = wsdl; this.requestAgent = wsdl.requestAgent; } getRequestHeadParams() { return __awaiter(this, void 0, void 0, function* () { if (!this.clientParams.soapHeaders) { return null; } if (!Array.isArray(this.clientParams.soapHeaders) && this.clientParams.soapHeaders === Object(this.clientParams.soapHeaders)) { this.clientParams.soapHeaders = Object.keys(this.clientParams.soapHeaders).map((key) => ({ name: key, value: this.clientParams.soapHeaders[key], })); } return this.clientParams.soapHeaders .map((item, index) => `<cns${index}:${item.name}>${item.value}</cns${index}:${item.name}>`); }); } getRequestEnvelopeParams() { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const namespaces = yield this.wsdl.getNamespaces(); const namespaceFiltering = namespaces.filter((namespace) => (namespace === null || namespace === void 0 ? void 0 : namespace.short) !== 'xmlns'); const xsd = namespaces.find((namespace) => namespace.short === 'xsd'); if (((_b = (_a = this.clientParams) === null || _a === void 0 ? void 0 : _a.soapHeaders) === null || _b === void 0 ? void 0 : _b.length) > 0) { this.clientParams.soapHeaders.forEach((header, index) => { namespaceFiltering.push({ short: `cns${index}`, full: header.value, }); }); } return { soapEnv: 'http://schemas.xmlsoap.org/soap/envelope/', xml_schema: (xsd === null || xsd === void 0 ? void 0 : xsd.full) || 'http://www.w3.org/2001/XMLSchema', namespaces: namespaceFiltering, }; }); } static getTagAttributes(attributes = {}) { const attributeKeys = Object.keys(attributes); if (attributeKeys.length === 0) { return ''; } return ` ${attributeKeys .map((key) => `${key}="${attributes[key]}"`) .join(' ')}`; } getParamAsString(key, value, attributes) { let val = value; if (Array.isArray(val)) { return val .map((valueItem) => this.getParamAsString(key, valueItem, attributes)) .join(''); } if (Object(val) === val) { val = Object.keys(val) .map((valueKey) => __awaiter(this, void 0, void 0, function* () { return this.getParamAsString(valueKey, val[valueKey], attributes); })) .join(''); } let attributesString = ''; if (attributes) { attributesString = SoapRequest.getTagAttributes(attributes); } return `<${key}${attributesString}>${val}</${key}>`; } getMethodParamRequestString(requestParams, paramKey, bodyParams) { let methodRequestParams = {}; requestParams.forEach((requestParamsAttributes) => { if (requestParamsAttributes.params) { methodRequestParams = requestParamsAttributes.params .find((requestParamItem) => requestParamItem.name === paramKey || requestParamItem.element === paramKey); } }); const paramValue = bodyParams[paramKey]; return this.getParamAsString(paramKey, paramValue, methodRequestParams); } getRequestParamsAsString(method, bodyParams, attributes) { return __awaiter(this, void 0, void 0, function* () { const methodParams = yield this.wsdl.getMethodParamsByName(method); const requestParams = methodParams.request; const responseArray = []; Object.keys(bodyParams).forEach((paramKey) => responseArray.push(this.getMethodParamRequestString(requestParams, paramKey, bodyParams))); let methodNameWithNameSpace = method; if (requestParams.length > 0 && requestParams[0].namespace) { methodNameWithNameSpace = `${requestParams[0].namespace}:${methodNameWithNameSpace}`; } return this.getParamAsString(methodNameWithNameSpace, responseArray.join(''), attributes); }); } getRequestXml(method, bodyParams, attributes) { return __awaiter(this, void 0, void 0, function* () { const headers = yield this.getRequestHeadParams(); const envelope = yield this.getRequestEnvelopeParams(); const body = yield this.getRequestParamsAsString(method, bodyParams, attributes); const namespaces = envelope.namespaces.map((namespace) => `xmlns:${namespace.short}="${namespace.full}"`); const namespacesAsString = namespaces.join(' '); const headersEnvelope = (headers !== null) ? `<SOAP-ENV:Header>${headers.join('')}</SOAP-ENV:Header>` : ''; const bodyEnvelope = `<SOAP-ENV:Body>${body}</SOAP-ENV:Body>`; const soapEnvelope = `<SOAP-ENV:Envelope xmlns:SOAP-ENV="${envelope.soapEnv}" ${namespacesAsString}> ${headersEnvelope} ${bodyEnvelope} </SOAP-ENV:Envelope>`; return `<?xml version="1.0" encoding="UTF-8"?>${soapEnvelope}`; }); } request(body) { return __awaiter(this, void 0, void 0, function* () { const urlWithoutParams = this.url.split('?')[0]; const axiosConfig = { url: urlWithoutParams, method: 'POST', headers: this.clientParams.httpHeaders, responseType: 'text', data: body, }; if (this.requestAgent instanceof http_1.Agent) { axiosConfig.httpAgent = this.requestAgent; } else { axiosConfig.httpsAgent = this.requestAgent; } const result = yield (0, axios_1.default)(axiosConfig); return result.data; }); } call(method, bodyParams, attributes) { return __awaiter(this, void 0, void 0, function* () { const requestXml = yield this.getRequestXml(method, bodyParams, attributes); const result = yield this.request(requestXml); return result; }); } } exports.default = SoapRequest;