Cloud.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getJobStatus = getJobStatus;
  6. exports.getJobsData = getJobsData;
  7. exports.run = run;
  8. exports.startJob = startJob;
  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. * @flow
  22. */
  23. /*:: import type { RequestOptions } from './RESTController';*/
  24. /**
  25. * Contains functions for calling and declaring
  26. * <a href="/docs/cloud_code_guide#functions">cloud functions</a>.
  27. * <p><strong><em>
  28. * Some functions are only available from Cloud Code.
  29. * </em></strong></p>
  30. *
  31. * @class Parse.Cloud
  32. * @static
  33. * @hideconstructor
  34. */
  35. /**
  36. * Makes a call to a cloud function.
  37. *
  38. * @function run
  39. * @name Parse.Cloud.run
  40. * @param {string} name The function name.
  41. * @param {object} data The parameters to send to the cloud function.
  42. * @param {object} options
  43. * @returns {Promise} A promise that will be resolved with the result
  44. * of the function.
  45. */
  46. function run(name /*: string*/, data /*: mixed*/, options /*: RequestOptions*/) /*: Promise<mixed>*/{
  47. options = options || {};
  48. if (typeof name !== 'string' || name.length === 0) {
  49. throw new TypeError('Cloud function name must be a string.');
  50. }
  51. const requestOptions = {};
  52. if (options.useMasterKey) {
  53. requestOptions.useMasterKey = options.useMasterKey;
  54. }
  55. if (options.sessionToken) {
  56. requestOptions.sessionToken = options.sessionToken;
  57. }
  58. if (options.context && typeof options.context === 'object') {
  59. requestOptions.context = options.context;
  60. }
  61. return _CoreManager.default.getCloudController().run(name, data, requestOptions);
  62. }
  63. /**
  64. * Gets data for the current set of cloud jobs.
  65. *
  66. * @function getJobsData
  67. * @name Parse.Cloud.getJobsData
  68. * @returns {Promise} A promise that will be resolved with the result
  69. * of the function.
  70. */
  71. function getJobsData() /*: Promise<Object>*/{
  72. return _CoreManager.default.getCloudController().getJobsData({
  73. useMasterKey: true
  74. });
  75. }
  76. /**
  77. * Starts a given cloud job, which will process asynchronously.
  78. *
  79. * @function startJob
  80. * @name Parse.Cloud.startJob
  81. * @param {string} name The function name.
  82. * @param {object} data The parameters to send to the cloud function.
  83. * @returns {Promise} A promise that will be resolved with the jobStatusId
  84. * of the job.
  85. */
  86. function startJob(name /*: string*/, data /*: mixed*/) /*: Promise<string>*/{
  87. if (typeof name !== 'string' || name.length === 0) {
  88. throw new TypeError('Cloud job name must be a string.');
  89. }
  90. return _CoreManager.default.getCloudController().startJob(name, data, {
  91. useMasterKey: true
  92. });
  93. }
  94. /**
  95. * Gets job status by Id
  96. *
  97. * @function getJobStatus
  98. * @name Parse.Cloud.getJobStatus
  99. * @param {string} jobStatusId The Id of Job Status.
  100. * @returns {Parse.Object} Status of Job.
  101. */
  102. function getJobStatus(jobStatusId /*: string*/) /*: Promise<ParseObject>*/{
  103. const query = new _ParseQuery.default('_JobStatus');
  104. return query.get(jobStatusId, {
  105. useMasterKey: true
  106. });
  107. }
  108. const DefaultController = {
  109. run(name, data, options /*: RequestOptions*/) {
  110. const RESTController = _CoreManager.default.getRESTController();
  111. const payload = (0, _encode.default)(data, true);
  112. const request = RESTController.request('POST', 'functions/' + name, payload, options);
  113. return request.then(res => {
  114. if (typeof res === 'object' && Object.keys(res).length > 0 && !res.hasOwnProperty('result')) {
  115. throw new _ParseError.default(_ParseError.default.INVALID_JSON, 'The server returned an invalid response.');
  116. }
  117. const decoded = (0, _decode.default)(res);
  118. if (decoded && decoded.hasOwnProperty('result')) {
  119. return Promise.resolve(decoded.result);
  120. }
  121. return Promise.resolve(undefined);
  122. });
  123. },
  124. getJobsData(options /*: RequestOptions*/) {
  125. const RESTController = _CoreManager.default.getRESTController();
  126. return RESTController.request('GET', 'cloud_code/jobs/data', null, options);
  127. },
  128. startJob(name, data, options /*: RequestOptions*/) {
  129. const RESTController = _CoreManager.default.getRESTController();
  130. const payload = (0, _encode.default)(data, true);
  131. return RESTController.request('POST', 'jobs/' + name, payload, options);
  132. }
  133. };
  134. _CoreManager.default.setCloudController(DefaultController);