CloudCode.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Defines a Cloud Function.
  3. *
  4. * **Available in Cloud Code only.**
  5. *
  6. * @method define
  7. * @name Parse.Cloud.define
  8. * @param {String} name The name of the Cloud Function
  9. * @param {Function} data The Cloud Function to register. This function should take one parameter {@link Parse.Cloud.FunctionRequest}
  10. */
  11. /**
  12. * Registers an after delete function.
  13. *
  14. * **Available in Cloud Code only.**
  15. *
  16. * If you want to use afterDelete for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1.
  17. * ```
  18. * Parse.Cloud.afterDelete('MyCustomClass', (request) => {
  19. * // code here
  20. * })
  21. *
  22. * Parse.Cloud.afterDelete(Parse.User, (request) => {
  23. * // code here
  24. * })
  25. *```
  26. *
  27. * @method afterDelete
  28. * @name Parse.Cloud.afterDelete
  29. * @param {(String|Parse.Object)} arg1 The Parse.Object subclass to register the after delete function for. This can instead be a String that is the className of the subclass.
  30. * @param {Function} func The function to run after a delete. This function should take just one parameter, {@link Parse.Cloud.TriggerRequest}.
  31. */
  32. /**
  33. *
  34. * Registers an after save function.
  35. *
  36. * **Available in Cloud Code only.**
  37. *
  38. * If you want to use afterSave for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1.
  39. *
  40. * ```
  41. * Parse.Cloud.afterSave('MyCustomClass', function(request) {
  42. * // code here
  43. * })
  44. *
  45. * Parse.Cloud.afterSave(Parse.User, function(request) {
  46. * // code here
  47. * })
  48. * ```
  49. *
  50. * @method afterSave
  51. * @name Parse.Cloud.afterSave
  52. * @param {(String|Parse.Object)} arg1 The Parse.Object subclass to register the after save function for. This can instead be a String that is the className of the subclass.
  53. * @param {Function} func The function to run after a save. This function should take just one parameter, {@link Parse.Cloud.TriggerRequest}.
  54. */
  55. /**
  56. * Registers an before delete function.
  57. *
  58. * **Available in Cloud Code only.**
  59. *
  60. * If you want to use beforeDelete for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1.
  61. * ```
  62. * Parse.Cloud.beforeDelete('MyCustomClass', (request, response) => {
  63. * // code here
  64. * })
  65. *
  66. * Parse.Cloud.beforeDelete(Parse.User, (request, response) => {
  67. * // code here
  68. * })
  69. *```
  70. *
  71. * @method beforeDelete
  72. * @name Parse.Cloud.beforeDelete
  73. * @param {(String|Parse.Object)} arg1 The Parse.Object subclass to register the before delete function for. This can instead be a String that is the className of the subclass.
  74. * @param {Function} func The function to run before a delete. This function should take two parameters a {@link Parse.Cloud.TriggerRequest} and a {@link Parse.Cloud.BeforeDeleteResponse}.
  75. */
  76. /**
  77. *
  78. * Registers an before save function.
  79. *
  80. * **Available in Cloud Code only.**
  81. *
  82. * If you want to use beforeSave for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1.
  83. *
  84. * ```
  85. * Parse.Cloud.beforeSave('MyCustomClass', (request, response) => {
  86. * // code here
  87. * })
  88. *
  89. * Parse.Cloud.beforeSave(Parse.User, (request, response) => {
  90. * // code here
  91. * })
  92. * ```
  93. *
  94. * @method beforeSave
  95. * @name Parse.Cloud.beforeSave
  96. * @param {(String|Parse.Object)} arg1 The Parse.Object subclass to register the after save function for. This can instead be a String that is the className of the subclass.
  97. * @param {Function} func The function to run before a save. This function should take two parameters a {@link Parse.Cloud.TriggerRequest} and a {@link Parse.Cloud.BeforeSaveResponse}.
  98. */
  99. /**
  100. * Makes an HTTP Request.
  101. *
  102. * **Available in Cloud Code only.**
  103. *
  104. * By default, Parse.Cloud.httpRequest does not follow redirects caused by HTTP 3xx response codes. You can use the followRedirects option in the {@link Parse.Cloud.HTTPOptions} object to change this behavior.
  105. *
  106. * Sample request:
  107. * ```
  108. * Parse.Cloud.httpRequest({
  109. * url: 'http://www.parse.com/'
  110. * }).then(function(httpResponse) {
  111. * // success
  112. * console.log(httpResponse.text);
  113. * },function(httpResponse) {
  114. * // error
  115. * console.error('Request failed with response code ' + httpResponse.status);
  116. * });
  117. * ```
  118. *
  119. * @method httpRequest
  120. * @name Parse.Cloud.httpRequest
  121. * @param {Parse.Cloud.HTTPOptions} options The Parse.Cloud.HTTPOptions object that makes the request.
  122. * @return {Promise<Parse.Cloud.HTTPResponse>} A promise that will be resolved with a {@link Parse.Cloud.HTTPResponse} object when the request completes.
  123. */
  124. /**
  125. * Defines a Background Job.
  126. *
  127. * **Available in Cloud Code only.**
  128. *
  129. * @method job
  130. * @name Parse.Cloud.job
  131. * @param {String} name The name of the Background Job
  132. * @param {Function} func The Background Job to register. This function should take two parameters a {@link Parse.Cloud.JobRequest} and a {@link Parse.Cloud.JobStatus}
  133. *
  134. */
  135. /**
  136. * @typedef Parse.Cloud.TriggerRequest
  137. * @property {String} installationId If set, the installationId triggering the request.
  138. * @property {Boolean} master If true, means the master key was used.
  139. * @property {Parse.User} user If set, the user that made the request.
  140. * @property {Parse.Object} object The object triggering the hook.
  141. * @property {String} ip The IP address of the client making the request.
  142. * @property {Object} headers The original HTTP headers for the request.
  143. * @property {String} triggerName The name of the trigger (`beforeSave`, `afterSave`, ...)
  144. * @property {Object} log The current logger inside Parse Server.
  145. * @property {Parse.Object} original If set, the object, as currently stored.
  146. */
  147. /**
  148. * @typedef Parse.Cloud.FunctionRequest
  149. * @property {String} installationId If set, the installationId triggering the request.
  150. * @property {Boolean} master If true, means the master key was used.
  151. * @property {Parse.User} user If set, the user that made the request.
  152. * @property {Object} params The params passed to the cloud function.
  153. */
  154. /**
  155. * @typedef Parse.Cloud.JobRequest
  156. * @property {Object} params The params passed to the background job.
  157. */
  158. /**
  159. * @typedef Parse.Cloud.JobStatus
  160. * @property {function} error If error is called, will end the job unsuccessfully with an optional completion message to be stored in the job status.
  161. * @property {function} message If message is called with a string argument, will update the current message to be stored in the job status.
  162. * @property {function} success If success is called, will end the job successfullly with the optional completion message to be stored in the job status.
  163. */
  164. /**
  165. * @typedef Parse.Cloud.BeforeSaveResponse
  166. * @property {function} success If called, will allow the save to happen. If a Parse.Object is passed in, then the passed in object will be saved instead.
  167. * @property {function} error If called, will reject the save. An optional error message may be passed in.
  168. */
  169. /**
  170. * @typedef Parse.Cloud.BeforeDeleteResponse
  171. * @property {function} success If called, will allow the delete to happen.
  172. * @property {function} error If called, will reject the save. An optional error message may be passed in.
  173. */
  174. /**
  175. * @typedef Parse.Cloud.FunctionResponse
  176. * @property {function} success If success is called, will return a successful response with the optional argument to the caller.
  177. * @property {function} error If error is called, will return an error response with an optionally passed message.
  178. */
  179. /**
  180. * @typedef Parse.Cloud.HTTPOptions
  181. * @property {String|Object} body The body of the request. If it is a JSON object, then the Content-Type set in the headers must be application/x-www-form-urlencoded or application/json. You can also set this to a {@link Buffer} object to send raw bytes. If you use a Buffer, you should also set the Content-Type header explicitly to describe what these bytes represent.
  182. * @property {function} error The function that is called when the request fails. It will be passed a Parse.Cloud.HTTPResponse object.
  183. * @property {Boolean} followRedirects Whether to follow redirects caused by HTTP 3xx responses. Defaults to false.
  184. * @property {Object} headers The headers for the request.
  185. * @property {String} method The method of the request. GET, POST, PUT, DELETE, HEAD, and OPTIONS are supported. Will default to GET if not specified.
  186. * @property {String|Object} params The query portion of the url. You can pass a JSON object of key value pairs like params: {q : 'Sean Plott'} or a raw string like params:q=Sean Plott.
  187. * @property {function} success The function that is called when the request successfully completes. It will be passed a Parse.Cloud.HTTPResponse object.
  188. * @property {string} url The url to send the request to.
  189. */
  190. /**
  191. * @typedef Parse.Cloud.HTTPResponse
  192. * @property {Buffer} buffer The raw byte representation of the response body. Use this to receive binary data. See Buffer for more details.
  193. * @property {Object} cookies The cookies sent by the server. The keys in this object are the names of the cookies. The values are Parse.Cloud.Cookie objects.
  194. * @property {Object} data The parsed response body as a JavaScript object. This is only available when the response Content-Type is application/x-www-form-urlencoded or application/json.
  195. * @property {Object} headers The headers sent by the server. The keys in this object are the names of the headers. We do not support multiple response headers with the same name. In the common case of Set-Cookie headers, please use the cookies field instead.
  196. * @property {Number} status The status code.
  197. * @property {String} text The raw text representation of the response body.
  198. */