Cloud.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. import CoreManager from './CoreManager';
  12. import decode from './decode';
  13. import encode from './encode';
  14. import ParseError from './ParseError';
  15. import ParseQuery from './ParseQuery';
  16. import ParseObject from './ParseObject';
  17. /*:: import type { RequestOptions } from './RESTController';*/
  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. * @method run
  32. * @name Parse.Cloud.run
  33. * @param {String} name The function name.
  34. * @param {Object} data The parameters to send to the cloud function.
  35. * @param {Object} options
  36. * @return {Promise} A promise that will be resolved with the result
  37. * of the function.
  38. */
  39. export function run(name
  40. /*: string*/
  41. , data
  42. /*: mixed*/
  43. , options
  44. /*: RequestOptions*/
  45. )
  46. /*: Promise<mixed>*/
  47. {
  48. options = options || {};
  49. if (typeof name !== 'string' || name.length === 0) {
  50. throw new TypeError('Cloud function name must be a string.');
  51. }
  52. const requestOptions = {};
  53. if (options.useMasterKey) {
  54. requestOptions.useMasterKey = options.useMasterKey;
  55. }
  56. if (options.sessionToken) {
  57. requestOptions.sessionToken = options.sessionToken;
  58. }
  59. return CoreManager.getCloudController().run(name, data, requestOptions);
  60. }
  61. /**
  62. * Gets data for the current set of cloud jobs.
  63. * @method getJobsData
  64. * @name Parse.Cloud.getJobsData
  65. * @return {Promise} A promise that will be resolved with the result
  66. * of the function.
  67. */
  68. export function getJobsData()
  69. /*: Promise<Object>*/
  70. {
  71. return CoreManager.getCloudController().getJobsData({
  72. useMasterKey: true
  73. });
  74. }
  75. /**
  76. * Starts a given cloud job, which will process asynchronously.
  77. * @method 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. * @return {Promise} A promise that will be resolved with the jobStatusId
  82. * of the job.
  83. */
  84. export function startJob(name
  85. /*: string*/
  86. , data
  87. /*: mixed*/
  88. )
  89. /*: Promise<string>*/
  90. {
  91. if (typeof name !== 'string' || name.length === 0) {
  92. throw new TypeError('Cloud job name must be a string.');
  93. }
  94. return CoreManager.getCloudController().startJob(name, data, {
  95. useMasterKey: true
  96. });
  97. }
  98. /**
  99. * Gets job status by Id
  100. * @method getJobStatus
  101. * @name Parse.Cloud.getJobStatus
  102. * @param {String} jobStatusId The Id of Job Status.
  103. * @return {Parse.Object} Status of Job.
  104. */
  105. export function getJobStatus(jobStatusId
  106. /*: string*/
  107. )
  108. /*: Promise<ParseObject>*/
  109. {
  110. const query = new ParseQuery('_JobStatus');
  111. return query.get(jobStatusId, {
  112. useMasterKey: true
  113. });
  114. }
  115. const DefaultController = {
  116. run(name, data, options
  117. /*: RequestOptions*/
  118. ) {
  119. const RESTController = CoreManager.getRESTController();
  120. const payload = encode(data, true);
  121. const request = RESTController.request('POST', 'functions/' + name, payload, options);
  122. return request.then(res => {
  123. if (typeof res === 'object' && Object.keys(res).length > 0 && !res.hasOwnProperty('result')) {
  124. throw new ParseError(ParseError.INVALID_JSON, 'The server returned an invalid response.');
  125. }
  126. const decoded = decode(res);
  127. if (decoded && decoded.hasOwnProperty('result')) {
  128. return Promise.resolve(decoded.result);
  129. }
  130. return Promise.resolve(undefined);
  131. });
  132. },
  133. getJobsData(options
  134. /*: RequestOptions*/
  135. ) {
  136. const RESTController = CoreManager.getRESTController();
  137. return RESTController.request('GET', 'cloud_code/jobs/data', null, options);
  138. },
  139. startJob(name, data, options
  140. /*: RequestOptions*/
  141. ) {
  142. const RESTController = CoreManager.getRESTController();
  143. const payload = encode(data, true);
  144. return RESTController.request('POST', 'jobs/' + name, payload, options);
  145. }
  146. };
  147. CoreManager.setCloudController(DefaultController);