/**
* @file Manages Salesforce Apex REST endpoint calls
* @author Shinichi Tomita <shinichi.tomita@gmail.com>
*/
/**
* API class for Apex REST endpoint call
*
* @class
* @param {Connection} conn Connection
*/
var Apex = function(conn) {
this._conn = conn;
};
/**
* @private
*/
Apex.prototype._baseUrl = function() {
return this._conn.instanceUrl + "/services/apexrest";
};
/**
* @private
*/
Apex.prototype._createRequestParams = function(method, path, body) {
var params = {
method: method,
url: this._baseUrl() + path
};
if (!/^(GET|DELETE)$/i.test(method)) {
params.headers = {
"Content-Type" : "application/json"
};
}
if (body) {
params.body = JSON.stringify(body);
}
return params;
};
/**
* Call Apex REST service in GET request
*
* @param {String} path - URL path to Apex REST service
* @param {Callback.<Object>} [callback] - Callback function
* @returns {Promise.<Object>}
*/
Apex.prototype.get = function(path, callback) {
return this._conn._request(this._createRequestParams('GET', path)).thenCall(callback);
};
/**
* Call Apex REST service in POST request
*
* @param {String} path - URL path to Apex REST service
* @param {Object} [body] - Request body
* @param {Callback.<Object>} [callback] - Callback function
* @returns {Promise.<Object>}
*/
Apex.prototype.post = function(path, body, callback) {
if (typeof body === 'function') {
callback = body;
body = undefined;
}
var params = this._createRequestParams('POST', path, body);
return this._conn._request(params).thenCall(callback);
};
/**
* Call Apex REST service in PUT request
*
* @param {String} path - URL path to Apex REST service
* @param {Object} [body] - Request body
* @param {Callback.<Object>} [callback] - Callback function
* @returns {Promise.<Object>}
*/
Apex.prototype.put = function(path, body, callback) {
if (typeof body === 'function') {
callback = body;
body = undefined;
}
var params = this._createRequestParams('PUT', path, body);
return this._conn._request(params).thenCall(callback);
};
/**
* Call Apex REST service in PATCH request
*
* @param {String} path - URL path to Apex REST service
* @param {Object} [body] - Request body
* @param {Callback.<Object>} [callback] - Callback function
* @returns {Promise.<Object>}
*/
Apex.prototype.patch = function(path, body, callback) {
if (typeof body === 'function') {
callback = body;
body = undefined;
}
var params = this._createRequestParams('PATCH', path, body);
return this._conn._request(params).thenCall(callback);
};
/**
* Synonym of Apex#delete()
*
* @method Apex#del
*
* @param {String} path - URL path to Apex REST service
* @param {Object} [body] - Request body
* @param {Callback.<Object>} [callback] - Callback function
* @returns {Promise.<Object>}
*/
/**
* Call Apex REST service in DELETE request
*
* @method Apex#delete
*
* @param {String} path - URL path to Apex REST service
* @param {Object} [body] - Request body
* @param {Callback.<Object>} [callback] - Callback function
* @returns {Promise.<Object>}
*/
Apex.prototype.del =
Apex.prototype["delete"] = function(path, callback) {
return this._conn._request(this._createRequestParams('DELETE', path)).thenCall(callback);
};
module.exports = Apex;