From 02030f72c2167bfac6f6e6fc206ec7af76412eed Mon Sep 17 00:00:00 2001 From: dblythy Date: Wed, 21 Oct 2020 09:08:53 +1100 Subject: [PATCH] Replace response.success / response.error (#774) --- _includes/arduino/cloud-code.md | 4 ++-- _includes/common/errors.md | 2 -- _includes/common/performance.md | 23 +++++++++-------------- _includes/common/security.md | 18 +++++------------- _includes/common/sessions.md | 16 +++++++++------- _includes/embedded_c/cloud-code.md | 4 ++-- _includes/rest/sessions.md | 16 +++++++++------- 7 files changed, 36 insertions(+), 47 deletions(-) diff --git a/_includes/arduino/cloud-code.md b/_includes/arduino/cloud-code.md index 5b6fed760..44d80fd5f 100644 --- a/_includes/arduino/cloud-code.md +++ b/_includes/arduino/cloud-code.md @@ -7,8 +7,8 @@ You write your Cloud Code in JavaScript using the Parse JavaScript SDK. We provi For example, you define a Cloud Function as below. ```javascript -Parse.Cloud.define("hello", function(request, response) { - response.success(request.body); +Parse.Cloud.define("hello", request => { + return request.body; }); ``` diff --git a/_includes/common/errors.md b/_includes/common/errors.md index ded95da4c..6ac7ae174 100644 --- a/_includes/common/errors.md +++ b/_includes/common/errors.md @@ -32,8 +32,6 @@ The following is a list of all the error codes that can be returned by the Parse | `ScriptFailed` | 141 | Cloud Code script failed. Usually points to a JavaScript error. Check error message for more details. | | `FunctionNotFound` | 141 | Cloud function not found. Check that the specified Cloud function is present in your Cloud Code script and has been deployed. | | `JobNotFound` | 141 | Background job not found. Check that the specified job is present in your Cloud Code script and has been deployed. | -| `SuccessErrorNotCalled` | 141 | success/error was not called. A cloud function will return once response.success() or response.error() is called. A background job will similarly finish execution once status.success() or status.error() is called. If a function or job never reaches either of the success/error methods, this error will be returned. This may happen when a function does not handle an error response correctly, preventing code execution from reaching the success() method call. | -| `MultupleSuccessErrorCalls` | 141 | Can't call success/error multiple times. A cloud function will return once response.success() or response.error() is called. A background job will similarly finish execution once status.success() or status.error() is called. If a function or job calls success() and/or error() more than once in a single execution path, this error will be returned. | | `ValidationFailed` | 142 | Cloud Code validation failed. | | `WebhookError` | 143 | Webhook error. | | `InvalidImageData` | 150 | Invalid image data. | diff --git a/_includes/common/performance.md b/_includes/common/performance.md index 816252fc2..18c35ce6f 100644 --- a/_includes/common/performance.md +++ b/_includes/common/performance.md @@ -1008,19 +1008,15 @@ You can use this to offload processing to the Parse servers thus increasing your We saw examples of limiting the data returned by writing restrictive queries. You can also use [Cloud Functions]({{ site.baseUrl }}/cloudcode/guide/#cloud-functions) to help limit the amount of data returned to your app. In the following example, we use a Cloud Function to get a movie's average rating: ```javascript -Parse.Cloud.define("averageStars", function(request, response) { - var Review = Parse.Object.extend("Review"); - var query = new Parse.Query(Review); +Parse.Cloud.define("averageStars", async (request) => { + const query = new Parse.Query("Review"); query.equalTo("movie", request.params.movie); - query.find().then(function(results) { - var sum = 0; - for (var i = 0; i < results.length; ++i) { - sum += results[i].get("stars"); - } - response.success(sum / results.length); - }, function(error) { - response.error("movie lookup failed"); - }); + const results = await query.find(); + let sum = 0; + for (let i = 0; i < results.length; ++i) { + sum += results[i].get("stars"); + } + return sum / results.length; }); ``` @@ -1308,7 +1304,7 @@ Let's walk through an example of how you could build an efficient search. You ca ```javascript var _ = require("underscore"); -Parse.Cloud.beforeSave("Post", function(request, response) { +Parse.Cloud.beforeSave("Post", request => { var post = request.object; var toLowerCase = function(w) { return w.toLowerCase(); }; var words = post.get("text").split(/\b/); @@ -1321,7 +1317,6 @@ Parse.Cloud.beforeSave("Post", function(request, response) { hashtags = _.map(hashtags, toLowerCase); post.set("words", words); post.set("hashtags", hashtags); - response.success(); }); ``` diff --git a/_includes/common/security.md b/_includes/common/security.md index 42468c6b0..cd327b8a8 100644 --- a/_includes/common/security.md +++ b/_includes/common/security.md @@ -516,12 +516,10 @@ One particularly common use case for Cloud Code is preventing invalid data from To create validation functions, Cloud Code allows you to implement a `beforeSave` trigger for your class. These triggers are run whenever an object is saved, and allow you to modify the object or completely reject a save. For example, this is how you create a [Cloud Code beforeSave trigger]({{ site.baseUrl }}/cloudcode/guide/#beforesave-triggers) to make sure every user has an email address set: ```js -Parse.Cloud.beforeSave(Parse.User, function(request, response) { - var user = request.object; +Parse.Cloud.beforeSave(Parse.User, request => { + const user = request.object; if (!user.get("email")) { - response.error("Every user must have an email address."); - } else { - response.success(); + throw "Every user must have an email address."; } }); ``` @@ -548,17 +546,11 @@ Say you want to allow a user to "like" a `Post` object without giving them full The master key should be used carefully. setting `useMasterKey` to `true` only in the individual API function calls that need that security override: ```js -Parse.Cloud.define("like", function(request, response) { +Parse.Cloud.define("like", async request => { var post = new Parse.Object("Post"); post.id = request.params.postId; post.increment("likes"); - post.save(null, { useMasterKey: true }).then(function() { - // If I choose to do something else here, it won't be using - // the master key and I'll be subject to ordinary security measures. - response.success(); - }, function(error) { - response.error(error); - }); + await post.save(null, { useMasterKey: true }) }); ``` diff --git a/_includes/common/sessions.md b/_includes/common/sessions.md index a25f41b7f..f01c415a8 100644 --- a/_includes/common/sessions.md +++ b/_includes/common/sessions.md @@ -301,13 +301,15 @@ Restricted sessions are prohibited from creating, modifying, or deleting any dat If you want to prevent restricted Sessions from modifying classes other than `User`, `Session`, or `Role`, you can write a Cloud Code `beforeSave` handler for that class: ```js -Parse.Cloud.beforeSave("MyClass", function(request, response) { - Parse.Session.current().then(function(session) { - if (session.get('restricted')) { - response.error('write operation not allowed'); - } - response.success(); - }); +Parse.Cloud.beforeSave("MyClass", async request => { + const user = request.user; + const token = user.getSessionToken(); + const query = new Parse.Query(Parse.Session); + query.equalTo('sessionToken', token); + const session = await q.first({ useMasterKey: true }); + if (session.get('restricted')) { + throw 'write operation not allowed'; + } }); ``` You can configure Class-Level Permissions (CLPs) for the Session class just like other classes on Parse. CLPs restrict reading/writing of sessions via the `Session` API, but do not restrict Parse Server's automatic session creation/deletion when users log in, sign up, and log out. We recommend that you disable all CLPs not needed by your app. Here are some common use cases for Session CLPs: diff --git a/_includes/embedded_c/cloud-code.md b/_includes/embedded_c/cloud-code.md index d0ea10c98..877a6f249 100644 --- a/_includes/embedded_c/cloud-code.md +++ b/_includes/embedded_c/cloud-code.md @@ -7,8 +7,8 @@ You write your Cloud Code in JavaScript using the Parse JavaScript SDK. See our For example, you define a Cloud Function as below. ```cpp -Parse.Cloud.define("hello", function(request, response) { - response.success(request.body); +Parse.Cloud.define("hello", request => { + return request.body; }); ``` diff --git a/_includes/rest/sessions.md b/_includes/rest/sessions.md index 4ffbb2001..062b5ad9b 100644 --- a/_includes/rest/sessions.md +++ b/_includes/rest/sessions.md @@ -286,13 +286,15 @@ Restricted sessions are prohibited from creating, modifying, or deleting any dat If you want to prevent restricted Sessions from modifying classes other than `User`, `Session`, or `Role`, you can write a Cloud Code `beforeSave` handler for that class:

-Parse.Cloud.beforeSave("MyClass", function(request, response) {
-  Parse.Session.current().then(function(session) {
-    if (session.get('restricted')) {
-      response.error('write operation not allowed');
-    }
-    response.success();
-  });
+Parse.Cloud.beforeSave("MyClass", async request => {
+  const user = request.user;
+  const token = user.getSessionToken(); 
+  const query = new Parse.Query(Parse.Session);
+  query.equalTo('sessionToken', token);
+  const session = await q.first({ useMasterKey: true });
+  if (session.get('restricted')) {
+      throw 'write operation not allowed';
+  }
 });