CloudCode.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * Defines a Cloud Function.
  3. *
  4. * **Available in Cloud Code only.**
  5. *
  6. * @function 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. * @function 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. * @function 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) => {
  63. * // code here
  64. * })
  65. *
  66. * Parse.Cloud.beforeDelete(Parse.User, (request) => {
  67. * // code here
  68. * })
  69. *```
  70. *
  71. * @function 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 just one parameter, {@link Parse.Cloud.TriggerRequest}.
  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) => {
  86. * // code here
  87. * })
  88. *
  89. * Parse.Cloud.beforeSave(Parse.User, (request) => {
  90. * // code here
  91. * })
  92. * ```
  93. *
  94. * @function 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 just one parameter, {@link Parse.Cloud.TriggerRequest}.
  98. */
  99. /**
  100. *
  101. * Registers an before save file function. A new Parse.File can be returned to override the file that gets saved.
  102. * If you want to replace the rquesting Parse.File with a Parse.File that is already saved, simply return the already saved Parse.File.
  103. * You can also add metadata to the file that will be stored via whatever file storage solution you're using.
  104. *
  105. * **Available in Cloud Code only.**
  106. *
  107. * Example: Adding metadata and tags
  108. * ```
  109. * Parse.Cloud.beforeSaveFile(({ file, user }) => {
  110. * file.addMetadata('foo', 'bar');
  111. * file.addTag('createdBy', user.id);
  112. * });
  113. *
  114. * ```
  115. *
  116. * Example: replacing file with an already saved file
  117. *
  118. * ```
  119. * Parse.Cloud.beforeSaveFile(({ file, user }) => {
  120. * return user.get('avatar');
  121. * });
  122. *
  123. * ```
  124. *
  125. * Example: replacing file with a different file
  126. *
  127. * ```
  128. * Parse.Cloud.beforeSaveFile(({ file, user }) => {
  129. * const metadata = { foo: 'bar' };
  130. * const tags = { createdBy: user.id };
  131. * const newFile = new Parse.File(file.name(), <some other file data>, 'text/plain', metadata, tags);
  132. * return newFile;
  133. * });
  134. *
  135. * ```
  136. *
  137. * @function beforeSaveFile
  138. * @name Parse.Cloud.beforeSaveFile
  139. * @param {Function} func The function to run before a file saves. This function should take one parameter, a {@link Parse.Cloud.FileTriggerRequest}.
  140. */
  141. /**
  142. *
  143. * Registers an after save file function.
  144. *
  145. * **Available in Cloud Code only.**
  146. *
  147. * Example: creating a new object that references this file in a separate collection
  148. * ```
  149. * Parse.Cloud.afterSaveFile(async ({ file, user }) => {
  150. * const fileObject = new Parse.Object('FileObject');
  151. * fileObject.set('metadata', file.metadata());
  152. * fileObject.set('tags', file.tags());
  153. * fileObject.set('name', file.name());
  154. * fileObject.set('createdBy', user);
  155. * await fileObject.save({ sessionToken: user.getSessionToken() });
  156. * });
  157. *
  158. * @method afterSaveFile
  159. * @name Parse.Cloud.afterSaveFile
  160. * @param {Function} func The function to run after a file saves. This function should take one parameter, a {@link Parse.Cloud.FileTriggerRequest}.
  161. */
  162. /**
  163. * @function beforeConnect
  164. * @name Parse.Cloud.beforeConnect
  165. * @param {Function} func The function to before connection is made. This function can be async and should take just one parameter, {@link Parse.Cloud.ConnectTriggerRequest}.
  166. */
  167. /**
  168. *
  169. * Registers a before connect function.
  170. *
  171. * **Available in Cloud Code only.**
  172. *
  173. * Example: restrict LiveQueries to logged in users.
  174. * ```
  175. * Parse.Cloud.beforeConnect((request) => {
  176. * if (!request.user) {
  177. * throw "Please login before you attempt to connect."
  178. * }
  179. * });
  180. * ```
  181. */
  182. /**
  183. * @function beforeSubscribe
  184. * @name Parse.Cloud.beforeSubscribe
  185. * @param {(string | Parse.Object)} arg1 The Parse.Object subclass to register the before subscription function for. This can instead be a String that is the className of the subclass.
  186. * @param {Function} func The function to run before a subscription. This function can be async and should take one parameter, a {@link Parse.Cloud.TriggerRequest}.
  187. */
  188. /**
  189. *
  190. * Registers a before subscribe function.
  191. *
  192. * **Available in Cloud Code only.**
  193. * Example: restrict subscriptions to MyObject to Admin accounts only.
  194. * ```
  195. * Parse.Cloud.beforeSubscribe('MyObject', (request) => {
  196. * if (!request.user.get('Admin')) {
  197. * throw new Parse.Error(101, 'You are not authorized to subscribe to MyObject.');
  198. * }
  199. * let query = request.query; // the Parse.Query
  200. * query.select("name","year")
  201. * });
  202. * ```
  203. */
  204. /**
  205. * Makes an HTTP Request.
  206. *
  207. * **Available in Cloud Code only.**
  208. *
  209. * 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.
  210. *
  211. * Sample request:
  212. * ```
  213. * Parse.Cloud.httpRequest({
  214. * url: 'http://www.example.com/'
  215. * }).then(function(httpResponse) {
  216. * // success
  217. * console.log(httpResponse.text);
  218. * },function(httpResponse) {
  219. * // error
  220. * console.error('Request failed with response code ' + httpResponse.status);
  221. * });
  222. * ```
  223. *
  224. * @function httpRequest
  225. * @name Parse.Cloud.httpRequest
  226. * @param {Parse.Cloud.HTTPOptions} options The Parse.Cloud.HTTPOptions object that makes the request.
  227. * @returns {Promise<Parse.Cloud.HTTPResponse>} A promise that will be resolved with a {@link Parse.Cloud.HTTPResponse} object when the request completes.
  228. */
  229. /**
  230. * Defines a Background Job.
  231. *
  232. * **Available in Cloud Code only.**
  233. *
  234. * @function job
  235. * @name Parse.Cloud.job
  236. * @param {string} name The name of the Background Job
  237. * @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}
  238. */
  239. /**
  240. * @typedef Parse.Cloud.TriggerRequest
  241. * @property {string} installationId If set, the installationId triggering the request.
  242. * @property {boolean} master If true, means the master key was used.
  243. * @property {Parse.User} user If set, the user that made the request.
  244. * @property {Parse.Object} object The object triggering the hook.
  245. * @property {string} ip The IP address of the client making the request.
  246. * @property {object} headers The original HTTP headers for the request.
  247. * @property {string} triggerName The name of the trigger (`beforeSave`, `afterSave`, ...)
  248. * @property {object} log The current logger inside Parse Server.
  249. * @property {Parse.Object} original If set, the object, as currently stored.
  250. */
  251. /**
  252. * @typedef Parse.Cloud.FileTriggerRequest
  253. * @property {string} installationId If set, the installationId triggering the request.
  254. * @property {boolean} master If true, means the master key was used.
  255. * @property {Parse.User} user If set, the user that made the request.
  256. * @property {Parse.File} file The file triggering the hook.
  257. * @property {string} ip The IP address of the client making the request.
  258. * @property {object} headers The original HTTP headers for the request.
  259. * @property {string} triggerName The name of the trigger (`beforeSaveFile`, `afterSaveFile`, ...)
  260. * @property {object} log The current logger inside Parse Server.
  261. */
  262. /**
  263. * @typedef Parse.Cloud.ConnectTriggerRequest
  264. * @property {string} installationId If set, the installationId triggering the request.
  265. * @property {boolean} useMasterKey If true, means the master key was used.
  266. * @property {Parse.User} user If set, the user that made the request.
  267. * @property {number} clients The number of clients connected.
  268. * @property {number} subscriptions The number of subscriptions connected.
  269. * @property {string} sessionToken If set, the session of the user that made the request.
  270. */
  271. /**
  272. * @typedef Parse.Cloud.FunctionRequest
  273. * @property {string} installationId If set, the installationId triggering the request.
  274. * @property {boolean} master If true, means the master key was used.
  275. * @property {Parse.User} user If set, the user that made the request.
  276. * @property {object} params The params passed to the cloud function.
  277. */
  278. /**
  279. * @typedef Parse.Cloud.JobRequest
  280. * @property {object} params The params passed to the background job.
  281. */
  282. /**
  283. * @typedef Parse.Cloud.JobStatus
  284. * @property {Function} error If error is called, will end the job unsuccessfully with an optional completion message to be stored in the job status.
  285. * @property {Function} message If message is called with a string argument, will update the current message to be stored in the job status.
  286. * @property {Function} success If success is called, will end the job successfullly with the optional completion message to be stored in the job status.
  287. */
  288. /**
  289. * @typedef Parse.Cloud.HTTPOptions
  290. * @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.
  291. * @property {Function} error The function that is called when the request fails. It will be passed a Parse.Cloud.HTTPResponse object.
  292. * @property {boolean} followRedirects Whether to follow redirects caused by HTTP 3xx responses. Defaults to false.
  293. * @property {object} headers The headers for the request.
  294. * @property {string} method The method of the request. GET, POST, PUT, DELETE, HEAD, and OPTIONS are supported. Will default to GET if not specified.
  295. * @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.
  296. * @property {Function} success The function that is called when the request successfully completes. It will be passed a Parse.Cloud.HTTPResponse object.
  297. * @property {string} url The url to send the request to.
  298. */
  299. /**
  300. * @typedef Parse.Cloud.HTTPResponse
  301. * @property {Buffer} buffer The raw byte representation of the response body. Use this to receive binary data. See Buffer for more details.
  302. * @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.
  303. * @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.
  304. * @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.
  305. * @property {number} status The status code.
  306. * @property {string} text The raw text representation of the response body.
  307. */
  308. "use strict";