Cloud.js 4.9 KB

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