Skip to content

Instantly share code, notes, and snippets.

@qetr1ck-op
Last active November 2, 2015 18:40
Show Gist options
  • Save qetr1ck-op/5361eaecf2f41b9c872a to your computer and use it in GitHub Desktop.
Save qetr1ck-op/5361eaecf2f41b9c872a to your computer and use it in GitHub Desktop.
/**
* [main module]
* @return {[object]}
* [with methods: createRequest, createField, getResponseNode, getErrorNode, getFieldValue]
*/
export default function soap() {
return {
/**
* [request with xml attributes]
* @param {[string]} urn [name of webservice]
* @param {[strting]} name [method]
* @param {[object]} pExt [body data to server]
* @return {[object]} [JavaScript soap envelope]
*/
createRequest: function(urn, name, body) {
const soapObj = {
'#attributes': {
'soapenv:encodingStyle': 'http://schemas.xmlsoap.org/soap/encoding/'
},
'return': {
'#attributes': {
'xsi:type': name + 'Request'
}
}
};
if (body){
for (let vPrt in body){
soapObj['return'][vPrt] = body[vPrt];
}
}
return getWrappedRequest(urn, soapObj);
},
/**
* [create soap field]
* @param {[string]} type [type of field]
* @param {[string/number]} value [name of value]
* @return {[type]} [description]
*/
createField: function(type, value) {
return {
'#attributes': {
'xsi:type': 'xsd:' + type
},
'#text': value
};
},
getResponseNode: function(ns, data, type) {
return getNode(ns, data, type)['response'];
},
getErrorNode: function(ns, data, type) {
return getNode(ns, data, type)['error'];
},
getFieldValue: function(data, name) {
return name ? data[name]['#text'] : data['#text'];
}
};
function getWrappedRequest(urn, body, head) {
const wrappedRequest = {
'soapenv:Envelope': {
'#attributes': {
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:xsd': 'http://www.w3.org/2001/XMLSchema',
'xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
'xmlns:urn': urn
},
'soapenv:Header': {},
'soapenv:Body': {}
}
};
if (head){
wrappedRequest['soapenv:Envelope']['soapenv:Header'] = head;
}
if (body){
wrappedRequest['soapenv:Envelope']['soapenv:Body'] = body;
}
return wrappedRequest;
}
/**
* [getNode]
* @param {[string]} ns [namespace]
* @param {[object]} data [response]
* @param {[string]} type []
* @return {[object]} []
*/
function getNode(ns, data, type) {
return data['SOAP-ENV:Envelope']['SOAP-ENV:Body']['ns1:Webservices' + ns + '.' + type + 'Response']['return'];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment