forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
43 lines (36 loc) · 1.04 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// functions.js
var express = require('express'),
Parse = require('parse/node').Parse,
PromiseRouter = require('./PromiseRouter'),
rest = require('./rest');
var router = new PromiseRouter();
function handleCloudFunction(req) {
// TODO: set user from req.auth
if (Parse.Cloud.Functions[req.params.functionName]) {
return new Promise(function (resolve, reject) {
var response = createResponseObject(resolve, reject);
var request = {
params: req.body || {}
};
Parse.Cloud.Functions[req.params.functionName](request, response);
});
} else {
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Invalid function.');
}
}
function createResponseObject(resolve, reject) {
return {
success: function(result) {
resolve({
response: {
result: result
}
});
},
error: function(error) {
reject(new Parse.Error(Parse.Error.SCRIPT_FAILED, error));
}
}
}
router.route('POST', '/functions/:functionName', handleCloudFunction);
module.exports = router;