2016-02-05 14:38:09 -05:00
|
|
|
// FunctionsRouter.js
|
2016-02-19 23:47:44 -05:00
|
|
|
|
|
|
|
|
var express = require('express'),
|
2016-02-05 14:38:09 -05:00
|
|
|
Parse = require('parse/node').Parse,
|
|
|
|
|
triggers = require('../triggers');
|
2016-02-19 23:47:44 -05:00
|
|
|
|
|
|
|
|
import PromiseRouter from '../PromiseRouter';
|
|
|
|
|
|
|
|
|
|
export class FunctionsRouter extends PromiseRouter {
|
|
|
|
|
|
|
|
|
|
mountRoutes() {
|
|
|
|
|
this.route('POST', '/functions/:functionName', FunctionsRouter.handleCloudFunction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static createResponseObject(resolve, reject) {
|
|
|
|
|
return {
|
|
|
|
|
success: function(result) {
|
|
|
|
|
resolve({
|
|
|
|
|
response: {
|
|
|
|
|
result: Parse._encode(result)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
error: function(error) {
|
|
|
|
|
reject(new Parse.Error(Parse.Error.SCRIPT_FAILED, error));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static handleCloudFunction(req) {
|
2016-02-05 14:38:09 -05:00
|
|
|
var applicationId = req.config.applicationId;
|
|
|
|
|
var theFunction = triggers.getFunction(req.params.functionName, applicationId);
|
|
|
|
|
var theValidator = triggers.getValidator(req.params.functionName, applicationId);
|
|
|
|
|
if (theFunction) {
|
2016-02-19 23:47:44 -05:00
|
|
|
|
2016-02-05 14:38:09 -05:00
|
|
|
const params = Object.assign({}, req.body, req.query);
|
2016-02-19 23:47:44 -05:00
|
|
|
var request = {
|
2016-02-05 14:38:09 -05:00
|
|
|
params: params,
|
2016-02-19 23:47:44 -05:00
|
|
|
master: req.auth && req.auth.isMaster,
|
|
|
|
|
user: req.auth && req.auth.user,
|
|
|
|
|
installationId: req.info.installationId
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-05 14:38:09 -05:00
|
|
|
if (theValidator && typeof theValidator === "function") {
|
|
|
|
|
var result = theValidator(request);
|
2016-02-19 23:47:44 -05:00
|
|
|
if (!result) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Validation failed.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
var response = FunctionsRouter.createResponseObject(resolve, reject);
|
2016-02-05 14:38:09 -05:00
|
|
|
// Force the keys before the function calls.
|
|
|
|
|
Parse.applicationId = req.config.applicationId;
|
|
|
|
|
Parse.javascriptKey = req.config.javascriptKey;
|
|
|
|
|
Parse.masterKey = req.config.masterKey;
|
|
|
|
|
theFunction(request, response);
|
2016-02-19 23:47:44 -05:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Invalid function.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|