Cloud.js 4.5 KB

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