Cloud.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.run = run;
  6. exports.getJobsData = getJobsData;
  7. exports.startJob = startJob;
  8. exports.getJobStatus = getJobStatus;
  9. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  10. var _decode = _interopRequireDefault(require("./decode"));
  11. var _encode = _interopRequireDefault(require("./encode"));
  12. var _ParseError = _interopRequireDefault(require("./ParseError"));
  13. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  14. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  15. function _interopRequireDefault(obj) {
  16. return obj && obj.__esModule ? obj : {
  17. default: obj
  18. };
  19. }
  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. * @method run
  44. * @name Parse.Cloud.run
  45. * @param {String} name The function name.
  46. * @param {Object} data The parameters to send to the cloud function.
  47. * @param {Object} options
  48. * @return {Promise} A promise that will be resolved with the result
  49. * of the function.
  50. */
  51. function run(name
  52. /*: string*/
  53. , data
  54. /*: mixed*/
  55. , options
  56. /*: RequestOptions*/
  57. )
  58. /*: Promise<mixed>*/
  59. {
  60. options = options || {};
  61. if (typeof name !== 'string' || name.length === 0) {
  62. throw new TypeError('Cloud function name must be a string.');
  63. }
  64. const requestOptions = {};
  65. if (options.useMasterKey) {
  66. requestOptions.useMasterKey = options.useMasterKey;
  67. }
  68. if (options.sessionToken) {
  69. requestOptions.sessionToken = options.sessionToken;
  70. }
  71. return _CoreManager.default.getCloudController().run(name, data, requestOptions);
  72. }
  73. /**
  74. * Gets data for the current set of cloud jobs.
  75. * @method getJobsData
  76. * @name Parse.Cloud.getJobsData
  77. * @return {Promise} A promise that will be resolved with the result
  78. * of the function.
  79. */
  80. function getJobsData()
  81. /*: Promise<Object>*/
  82. {
  83. return _CoreManager.default.getCloudController().getJobsData({
  84. useMasterKey: true
  85. });
  86. }
  87. /**
  88. * Starts a given cloud job, which will process asynchronously.
  89. * @method startJob
  90. * @name Parse.Cloud.startJob
  91. * @param {String} name The function name.
  92. * @param {Object} data The parameters to send to the cloud function.
  93. * @return {Promise} A promise that will be resolved with the jobStatusId
  94. * of the job.
  95. */
  96. function startJob(name
  97. /*: string*/
  98. , data
  99. /*: mixed*/
  100. )
  101. /*: Promise<string>*/
  102. {
  103. if (typeof name !== 'string' || name.length === 0) {
  104. throw new TypeError('Cloud job name must be a string.');
  105. }
  106. return _CoreManager.default.getCloudController().startJob(name, data, {
  107. useMasterKey: true
  108. });
  109. }
  110. /**
  111. * Gets job status by Id
  112. * @method getJobStatus
  113. * @name Parse.Cloud.getJobStatus
  114. * @param {String} jobStatusId The Id of Job Status.
  115. * @return {Parse.Object} Status of Job.
  116. */
  117. function getJobStatus(jobStatusId
  118. /*: string*/
  119. )
  120. /*: Promise<ParseObject>*/
  121. {
  122. const query = new _ParseQuery.default('_JobStatus');
  123. return query.get(jobStatusId, {
  124. useMasterKey: true
  125. });
  126. }
  127. const DefaultController = {
  128. run(name, data, options
  129. /*: RequestOptions*/
  130. ) {
  131. const RESTController = _CoreManager.default.getRESTController();
  132. const payload = (0, _encode.default)(data, true);
  133. const request = RESTController.request('POST', 'functions/' + name, payload, options);
  134. return request.then(res => {
  135. if (typeof res === 'object' && Object.keys(res).length > 0 && !res.hasOwnProperty('result')) {
  136. throw new _ParseError.default(_ParseError.default.INVALID_JSON, 'The server returned an invalid response.');
  137. }
  138. const decoded = (0, _decode.default)(res);
  139. if (decoded && decoded.hasOwnProperty('result')) {
  140. return Promise.resolve(decoded.result);
  141. }
  142. return Promise.resolve(undefined);
  143. });
  144. },
  145. getJobsData(options
  146. /*: RequestOptions*/
  147. ) {
  148. const RESTController = _CoreManager.default.getRESTController();
  149. return RESTController.request('GET', 'cloud_code/jobs/data', null, options);
  150. },
  151. startJob(name, data, options
  152. /*: RequestOptions*/
  153. ) {
  154. const RESTController = _CoreManager.default.getRESTController();
  155. const payload = (0, _encode.default)(data, true);
  156. return RESTController.request('POST', 'jobs/' + name, payload, options);
  157. }
  158. };
  159. _CoreManager.default.setCloudController(DefaultController);