Cloud.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.getJobStatus = getJobStatus;
  8. exports.getJobsData = getJobsData;
  9. exports.run = run;
  10. exports.startJob = startJob;
  11. var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
  12. var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
  13. var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/typeof"));
  14. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  15. var _decode = _interopRequireDefault(require("./decode"));
  16. var _encode = _interopRequireDefault(require("./encode"));
  17. var _ParseError = _interopRequireDefault(require("./ParseError"));
  18. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  19. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  20. /**
  21. * Copyright (c) 2015-present, Parse, LLC.
  22. * All rights reserved.
  23. *
  24. * This source code is licensed under the BSD-style license found in the
  25. * LICENSE file in the root directory of this source tree. An additional grant
  26. * of patent rights can be found in the PATENTS file in the same directory.
  27. *
  28. * @flow
  29. */
  30. /**
  31. * Contains functions for calling and declaring
  32. * <a href="/docs/cloud_code_guide#functions">cloud functions</a>.
  33. * <p><strong><em>
  34. * Some functions are only available from Cloud Code.
  35. * </em></strong></p>
  36. *
  37. * @class Parse.Cloud
  38. * @static
  39. * @hideconstructor
  40. */
  41. /**
  42. * Makes a call to a cloud function.
  43. *
  44. * @function run
  45. * @name Parse.Cloud.run
  46. * @param {string} name The function name.
  47. * @param {object} data The parameters to send to the cloud function.
  48. * @param {object} options
  49. * @returns {Promise} A promise that will be resolved with the result
  50. * of the function.
  51. */
  52. function run(name
  53. /*: string*/
  54. , data
  55. /*: mixed*/
  56. , options
  57. /*: RequestOptions*/
  58. )
  59. /*: Promise<mixed>*/
  60. {
  61. options = options || {};
  62. if (typeof name !== 'string' || name.length === 0) {
  63. throw new TypeError('Cloud function name must be a string.');
  64. }
  65. var requestOptions = {};
  66. if (options.useMasterKey) {
  67. requestOptions.useMasterKey = options.useMasterKey;
  68. }
  69. if (options.sessionToken) {
  70. requestOptions.sessionToken = options.sessionToken;
  71. }
  72. if (options.context && (0, _typeof2.default)(options.context) === 'object') {
  73. requestOptions.context = options.context;
  74. }
  75. return _CoreManager.default.getCloudController().run(name, data, requestOptions);
  76. }
  77. /**
  78. * Gets data for the current set of cloud jobs.
  79. *
  80. * @function getJobsData
  81. * @name Parse.Cloud.getJobsData
  82. * @returns {Promise} A promise that will be resolved with the result
  83. * of the function.
  84. */
  85. function getJobsData()
  86. /*: Promise<Object>*/
  87. {
  88. return _CoreManager.default.getCloudController().getJobsData({
  89. useMasterKey: true
  90. });
  91. }
  92. /**
  93. * Starts a given cloud job, which will process asynchronously.
  94. *
  95. * @function startJob
  96. * @name Parse.Cloud.startJob
  97. * @param {string} name The function name.
  98. * @param {object} data The parameters to send to the cloud function.
  99. * @returns {Promise} A promise that will be resolved with the jobStatusId
  100. * of the job.
  101. */
  102. function startJob(name
  103. /*: string*/
  104. , data
  105. /*: mixed*/
  106. )
  107. /*: Promise<string>*/
  108. {
  109. if (typeof name !== 'string' || name.length === 0) {
  110. throw new TypeError('Cloud job name must be a string.');
  111. }
  112. return _CoreManager.default.getCloudController().startJob(name, data, {
  113. useMasterKey: true
  114. });
  115. }
  116. /**
  117. * Gets job status by Id
  118. *
  119. * @function getJobStatus
  120. * @name Parse.Cloud.getJobStatus
  121. * @param {string} jobStatusId The Id of Job Status.
  122. * @returns {Parse.Object} Status of Job.
  123. */
  124. function getJobStatus(jobStatusId
  125. /*: string*/
  126. )
  127. /*: Promise<ParseObject>*/
  128. {
  129. var query = new _ParseQuery.default('_JobStatus');
  130. return query.get(jobStatusId, {
  131. useMasterKey: true
  132. });
  133. }
  134. var DefaultController = {
  135. run: function (name, data, options
  136. /*: RequestOptions*/
  137. ) {
  138. var RESTController = _CoreManager.default.getRESTController();
  139. var payload = (0, _encode.default)(data, true);
  140. var request = RESTController.request('POST', "functions/".concat(name), payload, options);
  141. return request.then(function (res) {
  142. if ((0, _typeof2.default)(res) === 'object' && (0, _keys.default)(res).length > 0 && !res.hasOwnProperty('result')) {
  143. throw new _ParseError.default(_ParseError.default.INVALID_JSON, 'The server returned an invalid response.');
  144. }
  145. var decoded = (0, _decode.default)(res);
  146. if (decoded && decoded.hasOwnProperty('result')) {
  147. return _promise.default.resolve(decoded.result);
  148. }
  149. return _promise.default.resolve(undefined);
  150. });
  151. },
  152. getJobsData: function (options
  153. /*: RequestOptions*/
  154. ) {
  155. var RESTController = _CoreManager.default.getRESTController();
  156. return RESTController.request('GET', 'cloud_code/jobs/data', null, options);
  157. },
  158. startJob: function (name, data, options
  159. /*: RequestOptions*/
  160. ) {
  161. var RESTController = _CoreManager.default.getRESTController();
  162. var payload = (0, _encode.default)(data, true);
  163. return RESTController.request('POST', "jobs/".concat(name), payload, options);
  164. }
  165. };
  166. _CoreManager.default.setCloudController(DefaultController);