UNPKG

json-object-editor

Version:

JOE the Json Object Editor | Platform Edition

64 lines (52 loc) 2.09 kB
function PluginUtils(){ var self = this; function getPluginList(){ const plugins = {}; for (var plug in JOE.Apps.plugins){ if (!Object.prototype.hasOwnProperty.call(JOE.Apps.plugins, plug)) { continue; } let plugin = JOE.Apps.plugins[plug]; if (!plugin) { continue; } const asyncNames = Object.keys(plugin.async || {}); const protectedNames = Array.isArray(plugin.protected) ? plugin.protected : []; // Discover top-level function methods on the plugin object. We skip: // - internal metadata fields like 'async' and 'protected' // - properties starting with '_' (conventionally private) // - non-functions const methodNames = Object.keys(plugin).filter(function (k){ if (k === 'async' || k === 'protected') { return false; } if (k[0] === '_') { return false; } if (typeof plugin[k] !== 'function') { return false; } return true; }); plugins[plug] = { name: plug, protected: protectedNames, async: asyncNames, methods: methodNames, _pathname: plugin._pathname, _override: plugin._override }; } return plugins; } this.default = function(data,req,res){ // list the plugins that are available const plugins = getPluginList(); try{ var payload = { params:req.params, data:data, plugins:plugins } }catch(e){ return {errors:'plugin error: '+e,failedat:'plugin'}; } return payload; }; this.html = function(data,req,res){ return JSON.stringify(self.default(data,req),'','\t\r\n <br/>'); } this.protected = []; return self; } module.exports = new PluginUtils();