123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import CoreManager from './CoreManager';
- import decode from './decode';
- import ParseError from './ParseError';
- export function getFunctions() {
- return CoreManager.getHooksController().get("functions");
- }
- export function getTriggers() {
- return CoreManager.getHooksController().get("triggers");
- }
- export function getFunction(name) {
- return CoreManager.getHooksController().get("functions", name);
- }
- export function getTrigger(className, triggerName) {
- return CoreManager.getHooksController().get("triggers", className, triggerName);
- }
- export function createFunction(functionName, url) {
- return create({
- functionName: functionName,
- url: url
- });
- }
- export function createTrigger(className, triggerName, url) {
- return create({
- className: className,
- triggerName: triggerName,
- url: url
- });
- }
- export function create(hook) {
- return CoreManager.getHooksController().create(hook);
- }
- export function updateFunction(functionName, url) {
- return update({
- functionName: functionName,
- url: url
- });
- }
- export function updateTrigger(className, triggerName, url) {
- return update({
- className: className,
- triggerName: triggerName,
- url: url
- });
- }
- export function update(hook) {
- return CoreManager.getHooksController().update(hook);
- }
- export function removeFunction(functionName) {
- return remove({
- functionName: functionName
- });
- }
- export function removeTrigger(className, triggerName) {
- return remove({
- className: className,
- triggerName: triggerName
- });
- }
- export function remove(hook) {
- return CoreManager.getHooksController().remove(hook);
- }
- const DefaultController = {
- get(type, functionName, triggerName) {
- let url = "/hooks/" + type;
- if (functionName) {
- url += "/" + functionName;
- if (triggerName) {
- url += "/" + triggerName;
- }
- }
- return this.sendRequest("GET", url);
- },
- create(hook) {
- let url;
- if (hook.functionName && hook.url) {
- url = "/hooks/functions";
- } else if (hook.className && hook.triggerName && hook.url) {
- url = "/hooks/triggers";
- } else {
- return Promise.reject({
- error: 'invalid hook declaration',
- code: 143
- });
- }
- return this.sendRequest("POST", url, hook);
- },
- remove(hook) {
- let url;
- if (hook.functionName) {
- url = "/hooks/functions/" + hook.functionName;
- delete hook.functionName;
- } else if (hook.className && hook.triggerName) {
- url = "/hooks/triggers/" + hook.className + "/" + hook.triggerName;
- delete hook.className;
- delete hook.triggerName;
- } else {
- return Promise.reject({
- error: 'invalid hook declaration',
- code: 143
- });
- }
- return this.sendRequest("PUT", url, {
- "__op": "Delete"
- });
- },
- update(hook) {
- let url;
- if (hook.functionName && hook.url) {
- url = "/hooks/functions/" + hook.functionName;
- delete hook.functionName;
- } else if (hook.className && hook.triggerName && hook.url) {
- url = "/hooks/triggers/" + hook.className + "/" + hook.triggerName;
- delete hook.className;
- delete hook.triggerName;
- } else {
- return Promise.reject({
- error: 'invalid hook declaration',
- code: 143
- });
- }
- return this.sendRequest('PUT', url, hook);
- },
- sendRequest(method, url, body) {
- return CoreManager.getRESTController().request(method, url, body, {
- useMasterKey: true
- }).then(res => {
- const decoded = decode(res);
- if (decoded) {
- return Promise.resolve(decoded);
- }
- return Promise.reject(new ParseError(ParseError.INVALID_JSON, 'The server returned an invalid response.'));
- });
- }
- };
- CoreManager.setHooksController(DefaultController);
|