Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save albertosouza/87a36f2673efbfdd4cb276a1d3462238 to your computer and use it in GitHub Desktop.
Save albertosouza/87a36f2673efbfdd4cb276a1d3462238 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