Sample JavaScript Library

The following is a sample JavaScript library that can be used to send Extend API requests and handle the response.


/** * Send a Switchvox API request * @param {string} method - Switchvox API method name e.g. "switchvox.users.getMyInfo" * @param {object} parameters - parameters as required by that API. Defaults to {} * @example * SV.api.send('switchvox.users.getMyInfo', {'type' : 'EXTERNAL'}) * .then(function (res) { console.log(res); }); * @returns {Promise} */ module.exports.send = function (method, parameters) { var url = "/json"; return new Promise(function (resolve, reject) { var request = { 'request' : { 'method' : method, 'parameters' : parameters || {} } }; var req = new XMLHttpRequest(); req.addEventListener('load', function () { handleResponse(req.responseText, resolve, reject); }); req.addEventListener('error', reject); req.open(reqMethod, url, true); req.setRequestHeader('Content-Type', 'application/json'); req.send(JSON.stringify(request)); }); };function handleResponse (responseText, resolve, reject) { if (!responseText) { reject(new ExtendApiError('Did not receive a response')); } else { var res; try { res = JSON.parse(responseText); } catch(e) { reject(new ExtendApiError('Problem parsing JSON response')); } if (!res) { reject(new ExtendApiError('Parsed an undefined response')); } else if (res.response.errors) { var errors = forceArray(res.response.errors.error); var msg = (errors.length) ? errors[0].message : 'Extend Api Error'; reject(new ExtendApiError(msg, errors)); } else if (res.response.result) { resolve(res.response.result); } } }
Unable to render {include} The included page could not be found.