BVB Source Codes

parse-server Show HooksController.js Source code

Return Download parse-server: download HooksController.js Source code - Download parse-server Source code - Type:.js
  1. /** @flow weak */
  2.  
  3. import * as triggers        from "../triggers";
  4. import * as Parse           from "parse/node";
  5. import * as request         from "request";
  6. import { logger }           from '../logger';
  7.  
  8. const DefaultHooksCollectionName = "_Hooks";
  9.  
  10. export class HooksController {
  11.   _applicationId:string;
  12.   _webhookKey:string;
  13.   database: any;
  14.  
  15.   constructor(applicationId:string, databaseController, webhookKey) {
  16.     this._applicationId = applicationId;
  17.     this._webhookKey = webhookKey;
  18.     this.database = databaseController;
  19.   }
  20.  
  21.   load() {
  22.     return this._getHooks().then(hooks => {
  23.       hooks = hooks || [];
  24.       hooks.forEach((hook) => {
  25.         this.addHookToTriggers(hook);
  26.       });
  27.     });
  28.   }
  29.  
  30.   getFunction(functionName) {
  31.     return this._getHooks({ functionName: functionName }, 1).then(results => results[0]);
  32.   }
  33.  
  34.   getFunctions() {
  35.     return this._getHooks({ functionName: { $exists: true } });
  36.   }
  37.  
  38.   getTrigger(className, triggerName) {
  39.     return this._getHooks({ className: className, triggerName: triggerName }, 1).then(results => results[0]);
  40.   }
  41.  
  42.   getTriggers() {
  43.     return this._getHooks({ className: { $exists: true }, triggerName: { $exists: true } });
  44.   }
  45.  
  46.   deleteFunction(functionName) {
  47.     triggers.removeFunction(functionName, this._applicationId);
  48.     return this._removeHooks({ functionName: functionName });
  49.   }
  50.  
  51.   deleteTrigger(className, triggerName) {
  52.     triggers.removeTrigger(triggerName, className, this._applicationId);
  53.     return this._removeHooks({ className: className, triggerName: triggerName });
  54.   }
  55.  
  56.   _getHooks(query = {}) {
  57.     return this.database.find(DefaultHooksCollectionName, query).then((results) => {
  58.       return results.map((result) => {
  59.         delete result.objectId;
  60.         return result;
  61.       });
  62.     });
  63.   }
  64.  
  65.   _removeHooks(query) {
  66.     return this.database.destroy(DefaultHooksCollectionName, query).then(() => {
  67.       return Promise.resolve({});
  68.     });
  69.   }
  70.  
  71.   saveHook(hook) {
  72.     var query;
  73.     if (hook.functionName && hook.url) {
  74.       query = { functionName: hook.functionName }
  75.     } else if (hook.triggerName && hook.className && hook.url) {
  76.       query = { className: hook.className, triggerName: hook.triggerName }
  77.     } else {
  78.       throw new Parse.Error(143, "invalid hook declaration");
  79.     }
  80.     return this.database.update(DefaultHooksCollectionName, query, hook, {upsert: true}).then(() => {
  81.       return Promise.resolve(hook);
  82.     })
  83.   }
  84.  
  85.   addHookToTriggers(hook) {
  86.     var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey);
  87.     wrappedFunction.url = hook.url;
  88.     if (hook.className) {
  89.       triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId)
  90.     } else {
  91.       triggers.addFunction(hook.functionName, wrappedFunction, null, this._applicationId);
  92.     }
  93.   }
  94.  
  95.   addHook(hook) {
  96.     this.addHookToTriggers(hook);
  97.     return this.saveHook(hook);
  98.   }
  99.  
  100.   createOrUpdateHook(aHook) {
  101.     var hook;
  102.     if (aHook && aHook.functionName && aHook.url) {
  103.       hook = {};
  104.       hook.functionName = aHook.functionName;
  105.       hook.url = aHook.url;
  106.     } else if (aHook && aHook.className && aHook.url && aHook.triggerName && triggers.Types[aHook.triggerName]) {
  107.       hook = {};
  108.       hook.className = aHook.className;
  109.       hook.url = aHook.url;
  110.       hook.triggerName = aHook.triggerName;
  111.  
  112.     } else {
  113.       throw new Parse.Error(143, "invalid hook declaration");
  114.     }
  115.  
  116.     return this.addHook(hook);
  117.   }
  118.  
  119.   createHook(aHook) {
  120.     if (aHook.functionName) {
  121.       return this.getFunction(aHook.functionName).then((result) => {
  122.         if (result) {
  123.           throw new Parse.Error(143, `function name: ${aHook.functionName} already exits`);
  124.         } else {
  125.           return this.createOrUpdateHook(aHook);
  126.         }
  127.       });
  128.     } else if (aHook.className && aHook.triggerName) {
  129.       return this.getTrigger(aHook.className, aHook.triggerName).then((result) => {
  130.         if (result) {
  131.           throw new Parse.Error(143, `class ${aHook.className} already has trigger ${aHook.triggerName}`);
  132.         }
  133.         return this.createOrUpdateHook(aHook);
  134.       });
  135.     }
  136.  
  137.     throw new Parse.Error(143, "invalid hook declaration");
  138.   }
  139.  
  140.   updateHook(aHook) {
  141.     if (aHook.functionName) {
  142.       return this.getFunction(aHook.functionName).then((result) => {
  143.         if (result) {
  144.           return this.createOrUpdateHook(aHook);
  145.         }
  146.         throw new Parse.Error(143, `no function named: ${aHook.functionName} is defined`);
  147.       });
  148.     } else if (aHook.className && aHook.triggerName) {
  149.       return this.getTrigger(aHook.className, aHook.triggerName).then((result) => {
  150.         if (result) {
  151.           return this.createOrUpdateHook(aHook);
  152.         }
  153.         throw new Parse.Error(143, `class ${aHook.className} does not exist`);
  154.       });
  155.     }
  156.     throw new Parse.Error(143, "invalid hook declaration");
  157.   }
  158. }
  159.  
  160. function wrapToHTTPRequest(hook, key) {
  161.   return (req, res) => {
  162.     const jsonBody = {};
  163.     for (var i in req) {
  164.       jsonBody[i] = req[i];
  165.     }
  166.     if (req.object) {
  167.       jsonBody.object = req.object.toJSON();
  168.       jsonBody.object.className = req.object.className;
  169.     }
  170.     if (req.original) {
  171.       jsonBody.original = req.original.toJSON();
  172.       jsonBody.original.className = req.original.className;
  173.     }
  174.     const jsonRequest: any = {
  175.       headers: {
  176.         'Content-Type': 'application/json'
  177.       },
  178.       body: JSON.stringify(jsonBody)
  179.     };
  180.  
  181.     if (key) {
  182.       jsonRequest.headers['X-Parse-Webhook-Key'] = key;
  183.     } else {
  184.       logger.warn('Making outgoing webhook request without webhookKey being set!');
  185.     }
  186.  
  187.     request.post(hook.url, jsonRequest, function (err, httpResponse, body) {
  188.       var result;
  189.       if (body) {
  190.         if (typeof body === "string") {
  191.           try {
  192.             body = JSON.parse(body);
  193.           } catch (e) {
  194.             err = { error: "Malformed response", code: -1 };
  195.           }
  196.         }
  197.         if (!err) {
  198.           result = body.success;
  199.           err = body.error;
  200.         }
  201.       }
  202.  
  203.       if (err) {
  204.         return res.error(err);
  205.       } else if (hook.triggerName === 'beforeSave') {
  206.         if (typeof result === 'object') {
  207.           delete result.createdAt;
  208.           delete result.updatedAt;
  209.         }
  210.         return res.success({object: result});
  211.       } else {
  212.         return res.success(result);
  213.       }
  214.     });
  215.   }
  216. }
  217.  
  218. export default HooksController;
  219.  
downloadHooksController.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top