Skip to content

Commit eace10f

Browse files
committed
Merge pull request #439 from flovilmart/flovilmart.cloud-code-request-params
Adds ability to pass qs params to cloud code functions
2 parents 5856ed0 + 8296d77 commit eace10f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

spec/ParseAPI.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,35 @@ describe('miscellaneous', function() {
587587
done();
588588
});
589589
});
590+
591+
it('test cloud function query parameters', (done) => {
592+
Parse.Cloud.define('echoParams', (req, res) => {
593+
res.success(req.params);
594+
});
595+
var headers = {
596+
'Content-Type': 'application/json',
597+
'X-Parse-Application-Id': 'test',
598+
'X-Parse-Javascript-Key': 'test'
599+
};
600+
request.post({
601+
headers: headers,
602+
url: 'http://localhost:8378/1/functions/echoParams', //?option=1&other=2
603+
qs: {
604+
option: 1,
605+
other: 2
606+
},
607+
body: '{"foo":"bar", "other": 1}'
608+
}, (error, response, body) => {
609+
expect(error).toBe(null);
610+
var res = JSON.parse(body).result;
611+
expect(res.option).toEqual('1');
612+
// Make sure query string params override body params
613+
expect(res.other).toEqual('2');
614+
expect(res.foo).toEqual("bar");
615+
delete Parse.Cloud.Functions['echoParams'];
616+
done();
617+
});
618+
});
590619

591620
it('test cloud function parameter validation success', (done) => {
592621
// Register a function with validation

src/functions.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ var router = new PromiseRouter();
99

1010
function handleCloudFunction(req) {
1111
if (Parse.Cloud.Functions[req.params.functionName]) {
12+
13+
const params = Object.assign({}, req.body, req.query);
14+
1215
if (Parse.Cloud.Validators[req.params.functionName]) {
13-
var result = Parse.Cloud.Validators[req.params.functionName](req.body || {});
16+
var result = Parse.Cloud.Validators[req.params.functionName](params);
1417
if (!result) {
1518
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Validation failed.');
1619
}
@@ -19,7 +22,7 @@ function handleCloudFunction(req) {
1922
return new Promise(function (resolve, reject) {
2023
var response = createResponseObject(resolve, reject);
2124
var request = {
22-
params: req.body || {},
25+
params: params,
2326
master: req.auth && req.auth.isMaster,
2427
user: req.auth && req.auth.user,
2528
installationId: req.info.installationId

0 commit comments

Comments
 (0)