Adds Hooks API

Adds Parse.Hooks.js in src/cloud-code/Parse.Hooks.js
Moves Cloud code related functions in src/cloud-code
This commit is contained in:
Florent Vilmart
2016-02-05 14:38:09 -05:00
parent a7262dafd8
commit 9ac7a52e40
19 changed files with 1121 additions and 104 deletions

View File

@@ -1,4 +1,3 @@
import PromiseRouter from '../PromiseRouter';
import express from 'express';
import BodyParser from 'body-parser';
import * as Middlewares from '../middlewares';

View File

@@ -1,7 +1,8 @@
// functions.js
// FunctionsRouter.js
var express = require('express'),
Parse = require('parse/node').Parse;
Parse = require('parse/node').Parse,
triggers = require('../triggers');
import PromiseRouter from '../PromiseRouter';
@@ -27,17 +28,21 @@ export class FunctionsRouter extends PromiseRouter {
}
static handleCloudFunction(req) {
if (Parse.Cloud.Functions[req.params.functionName]) {
var applicationId = req.config.applicationId;
var theFunction = triggers.getFunction(req.params.functionName, applicationId);
var theValidator = triggers.getValidator(req.params.functionName, applicationId);
if (theFunction) {
const params = Object.assign({}, req.body, req.query);
var request = {
params: Object.assign({}, req.body, req.query),
params: params,
master: req.auth && req.auth.isMaster,
user: req.auth && req.auth.user,
installationId: req.info.installationId
};
if (Parse.Cloud.Validators[req.params.functionName]) {
var result = Parse.Cloud.Validators[req.params.functionName](request);
if (theValidator && typeof theValidator === "function") {
var result = theValidator(request);
if (!result) {
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Validation failed.');
}
@@ -45,7 +50,11 @@ export class FunctionsRouter extends PromiseRouter {
return new Promise(function (resolve, reject) {
var response = FunctionsRouter.createResponseObject(resolve, reject);
Parse.Cloud.Functions[req.params.functionName](request, response);
// 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);
});
} else {
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Invalid function.');

107
src/Routers/HooksRouter.js Normal file
View File

@@ -0,0 +1,107 @@
import { Parse } from 'parse/node';
import PromiseRouter from '../PromiseRouter';
import { HooksController } from '../Controllers/HooksController';
function enforceMasterKeyAccess(req) {
if (!req.auth.isMaster) {
throw new Parse.Error(403, "unauthorized: master key is required");
}
}
export class HooksRouter extends PromiseRouter {
createHook(aHook, config) {
return config.hooksController.createHook(aHook).then( (hook) => ({response: hook}));
};
updateHook(aHook, config) {
return config.hooksController.updateHook(aHook).then((hook) => ({response: hook}));
};
handlePost(req) {
return this.createHook(req.body, req.config);
};
handleGetFunctions(req) {
var hooksController = req.config.hooksController;
if (req.params.functionName) {
return hooksController.getFunction(req.params.functionName).then( (foundFunction) => {
if (!foundFunction) {
throw new Parse.Error(143, `no function named: ${req.params.functionName} is defined`);
}
return Promise.resolve({response: foundFunction});
});
}
return hooksController.getFunctions().then((functions) => {
return { response: functions || [] };
}, (err) => {
throw err;
});
}
handleGetTriggers(req) {
var hooksController = req.config.hooksController;
if (req.params.className && req.params.triggerName) {
return hooksController.getTrigger(req.params.className, req.params.triggerName).then((foundTrigger) => {
if (!foundTrigger) {
throw new Parse.Error(143,`class ${req.params.className} does not exist`);
}
return Promise.resolve({response: foundTrigger});
});
}
return hooksController.getTriggers().then((triggers) => ({ response: triggers || [] }));
}
handleDelete(req) {
var hooksController = req.config.hooksController;
if (req.params.functionName) {
return hooksController.deleteFunction(req.params.functionName).then(() => ({response: {}}))
} else if (req.params.className && req.params.triggerName) {
return hooksController.deleteTrigger(req.params.className, req.params.triggerName).then(() => ({response: {}}))
}
return Promise.resolve({response: {}});
}
handleUpdate(req) {
var hook;
if (req.params.functionName && req.body.url) {
hook = {}
hook.functionName = req.params.functionName;
hook.url = req.body.url;
} else if (req.params.className && req.params.triggerName && req.body.url) {
hook = {}
hook.className = req.params.className;
hook.triggerName = req.params.triggerName;
hook.url = req.body.url
} else {
throw new Parse.Error(143, "invalid hook declaration");
}
return this.updateHook(hook, req.config);
}
handlePut(req) {
var body = req.body;
if (body.__op == "Delete") {
return this.handleDelete(req);
} else {
return this.handleUpdate(req);
}
}
mountRoutes() {
this.route('GET', '/hooks/functions', enforceMasterKeyAccess, this.handleGetFunctions.bind(this));
this.route('GET', '/hooks/triggers', enforceMasterKeyAccess, this.handleGetTriggers.bind(this));
this.route('GET', '/hooks/functions/:functionName', enforceMasterKeyAccess, this.handleGetFunctions.bind(this));
this.route('GET', '/hooks/triggers/:className/:triggerName', enforceMasterKeyAccess, this.handleGetTriggers.bind(this));
this.route('POST', '/hooks/functions', enforceMasterKeyAccess, this.handlePost.bind(this));
this.route('POST', '/hooks/triggers', enforceMasterKeyAccess, this.handlePost.bind(this));
this.route('PUT', '/hooks/functions/:functionName', enforceMasterKeyAccess, this.handlePut.bind(this));
this.route('PUT', '/hooks/triggers/:className/:triggerName', enforceMasterKeyAccess, this.handlePut.bind(this));
}
}
export default HooksRouter;