Cloud.js 4.7 KB

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