Source: api/apex.js

  1. /**
  2. * @file Manages Salesforce Apex REST endpoint calls
  3. * @author Shinichi Tomita <shinichi.tomita@gmail.com>
  4. */
  5. 'use strict';
  6. var jsforce = require('../core');
  7. /**
  8. * API class for Apex REST endpoint call
  9. *
  10. * @class
  11. * @param {Connection} conn Connection
  12. */
  13. var Apex = function(conn) {
  14. this._conn = conn;
  15. };
  16. /**
  17. * @private
  18. */
  19. Apex.prototype._baseUrl = function() {
  20. return this._conn.instanceUrl + "/services/apexrest";
  21. };
  22. /**
  23. * @private
  24. */
  25. Apex.prototype._createRequestParams = function(method, path, body, options) {
  26. var params = {
  27. method: method,
  28. url: this._baseUrl() + path
  29. },
  30. _headers = {};
  31. if(options && 'object' === typeof options['headers']){
  32. _headers = options['headers'];
  33. }
  34. if (!/^(GET|DELETE)$/i.test(method)) {
  35. _headers["Content-Type"] = "application/json";
  36. }
  37. params.headers = _headers;
  38. if (body) {
  39. params.body = JSON.stringify(body);
  40. }
  41. return params;
  42. };
  43. /**
  44. * Call Apex REST service in GET request
  45. *
  46. * @param {String} path - URL path to Apex REST service
  47. * @param {Object} options - Holds headers and other meta data for the request.
  48. * @param {Callback.<Object>} [callback] - Callback function
  49. * @returns {Promise.<Object>}
  50. */
  51. Apex.prototype.get = function(path, options, callback) {
  52. if (typeof options === 'function') {
  53. callback = options;
  54. options = undefined;
  55. }
  56. return this._conn.request(this._createRequestParams('GET', path, undefined, options)).thenCall(callback);
  57. };
  58. /**
  59. * Call Apex REST service in POST request
  60. *
  61. * @param {String} path - URL path to Apex REST service
  62. * @param {Object} [body] - Request body
  63. * @param {Object} options - Holds headers and other meta data for the request.
  64. * @param {Callback.<Object>} [callback] - Callback function
  65. * @returns {Promise.<Object>}
  66. */
  67. Apex.prototype.post = function(path, body, options, callback) {
  68. if (typeof body === 'function') {
  69. callback = body;
  70. body = undefined;
  71. options = undefined;
  72. }
  73. if (typeof options === 'function') {
  74. callback = options;
  75. options = undefined;
  76. }
  77. var params = this._createRequestParams('POST', path, body, options);
  78. return this._conn.request(params).thenCall(callback);
  79. };
  80. /**
  81. * Call Apex REST service in PUT request
  82. *
  83. * @param {String} path - URL path to Apex REST service
  84. * @param {Object} [body] - Request body
  85. * @param {Object} [options] - Holds headers and other meta data for the request.
  86. * @param {Callback.<Object>} [callback] - Callback function
  87. * @returns {Promise.<Object>}
  88. */
  89. Apex.prototype.put = function(path, body, options, callback) {
  90. if (typeof body === 'function') {
  91. callback = body;
  92. body = undefined;
  93. options = undefined;
  94. }
  95. if (typeof options === 'function') {
  96. callback = options;
  97. options = undefined;
  98. }
  99. var params = this._createRequestParams('PUT', path, body, options);
  100. return this._conn.request(params).thenCall(callback);
  101. };
  102. /**
  103. * Call Apex REST service in PATCH request
  104. *
  105. * @param {String} path - URL path to Apex REST service
  106. * @param {Object} [body] - Request body
  107. * @param {Object} [options] - Holds headers and other meta data for the request.
  108. * @param {Callback.<Object>} [callback] - Callback function
  109. * @returns {Promise.<Object>}
  110. */
  111. Apex.prototype.patch = function(path, body, options, callback) {
  112. if (typeof body === 'function') {
  113. callback = body;
  114. body = undefined;
  115. options = undefined;
  116. }
  117. if (typeof options === 'function') {
  118. callback = options;
  119. options = undefined;
  120. }
  121. var params = this._createRequestParams('PATCH', path, body, options);
  122. return this._conn.request(params).thenCall(callback);
  123. };
  124. /**
  125. * Synonym of Apex#delete()
  126. *
  127. * @method Apex#del
  128. *
  129. * @param {String} path - URL path to Apex REST service
  130. * @param {Object} [body] - Request body
  131. * @param {Callback.<Object>} [callback] - Callback function
  132. * @returns {Promise.<Object>}
  133. */
  134. /**
  135. * Call Apex REST service in DELETE request
  136. *
  137. * @method Apex#delete
  138. *
  139. * @param {String} path - URL path to Apex REST service
  140. * @param {Object} [body] - Request body
  141. * @param {Object} [options] - Holds headers and other meta data for the request.
  142. * @param {Callback.<Object>} [callback] - Callback function
  143. * @returns {Promise.<Object>}
  144. */
  145. Apex.prototype.del =
  146. Apex.prototype["delete"] = function(path, options, callback) {
  147. if (typeof options === 'function') {
  148. callback = options;
  149. options = undefined;
  150. }
  151. return this._conn.request(this._createRequestParams('DELETE', path, undefined, options)).thenCall(callback);
  152. };
  153. /*--------------------------------------------*/
  154. /*
  155. * Register hook in connection instantiation for dynamically adding this API module features
  156. */
  157. jsforce.on('connection:new', function(conn) {
  158. conn.apex = new Apex(conn);
  159. });
  160. module.exports = Apex;