Skip to content

Instantly share code, notes, and snippets.

@circa10a
Last active May 24, 2021 23:02
Show Gist options
  • Save circa10a/8f0d2a676476f9668a3aeb19b6d047eb to your computer and use it in GitHub Desktop.
Save circa10a/8f0d2a676476f9668a3aeb19b6d047eb to your computer and use it in GitHub Desktop.
easy-soap-request-index.js
const axios = require('axios-https-proxy-fix');
/**
* @author Caleb Lemoine
* @param {object} opts easy-soap-request options
* @param {string} opts.url endpoint URL
* @param {object} opts.headers HTTP headers, can be string or object
* @param {string} opts.xml SOAP envelope, can be read from file or passed as string
* @param {int} opts.timeout Milliseconds before timing out request
* @param {object} opts.proxy Object with proxy configuration
* @promise response
* @reject {error}
* @fulfill {body,statusCode}
* @returns {Promise.response{body,statusCode}}
*/
module.exports = function soapRequest(opts = {
url: '',
headers: {},
xml: '',
timeout: 10000,
proxy: false,
}) {
const {
url,
headers,
xml,
timeout,
proxy,
} = opts;
return new Promise((resolve, reject) => {
axios({
method: 'post',
url,
headers,
data: xml,
timeout,
proxy,
}).then((response) => {
resolve({
response: {
headers: response.headers,
body: response.data,
statusCode: response.status,
},
});
}).catch((error) => {
if (error.response) {
console.error(`SOAP FAIL: ${error}`);
reject(error.response.data);
} else {
console.error(`SOAP FAIL: ${error}`);
reject(error);
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment