From c332ce82dd3710a0edaab4ab8beff792ad844ac5 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Thu, 9 Jan 2020 20:56:04 +0100 Subject: [PATCH] formatting --- index.js | 143 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 46 deletions(-) diff --git a/index.js b/index.js index 4e5f57d..de82640 100644 --- a/index.js +++ b/index.js @@ -11,23 +11,104 @@ const path = require ('path'); * @property {boolean} [rethrow] rethrow errors (default: true) */ +/** + * @typedef {object} handler_description + * @property {string} module_folder folder the module file is in + * @property {string} file name of the module + * @property {any} opts optional arguments + * @property {boolean} rethrow should errors be rethrown + */ + +/** + * wrap a requestor handler to be compatible with express + * + * @param {handler_description} data handler data + * @returns {Function} requestor handler + */ +function get_handler ({ module_folder, file, opts, rethrow }) { + // eslint-disable-next-line global-require + const handler = require (path.join (process.cwd (), module_folder, file)); + + return (req, res, next) => { + try { + handler (req, res, next, opts); + } + catch (e) { + if (rethrow) + throw e; + else + console.error (e); + } + }; +} + + +/** + * register a handler to the given app + * + * @param {any} app express app + * @param {handler_description} handler_description data for the used handler + * @param {string} method method to respond to + * @param {string} url url to respond to + * @param {boolean} verbose should verbose logging be enabled + */ +function register_handler ( + app, + handler_description, + method, + url, + verbose +) { + const handler = get_handler (handler_description); + + if (verbose) + console.log ( + `[requestor info] redirecting ${url} to ${handler_description.file}` + ); + + switch (method) { + case 'post': + app.post (url, handler); + break; + case 'get': + app.get (url, handler); + break; + case 'put': + app.put (url, handler); + break; + case 'delete': + app.delete (url, handler); + break; + case 'all': + app.all (url, handler); + break; + default: + if (verbose) + console.warn ( + `'${method}' did not match any request method, ignoring` + ); + + break; + } +} + /** * Load all request handlers in the given folder * * @param {any} app express app - * @param {string} modulefolder folder that contains all modules + * @param {string} module_folder folder that contains all modules * @param {options} options additional options */ module.exports = function main ( app, - modulefolder, + module_folder, options = { opts: null, subdir: '', verbose: false, rethrow: true } ) { const { opts, subdir, verbose, rethrow } = options; - for (const f of fs.readdirSync (modulefolder)) { + for (const file of fs.readdirSync (module_folder)) { const regex = /(?.*?)-(?.*?)\.js/u; - const { groups } = regex.exec (f); + const { groups } = regex.exec (file); if (typeof subdir === 'undefined') groups.url = `/${groups.url}/`; @@ -39,47 +120,17 @@ module.exports = function main ( .replace (/\./gu, '/') .replace (/\/+/gu, '/'); - // eslint-disable-next-line global-require - const handler = require (path.join (process.cwd (), modulefolder, f)); - - const requestor_handler = (req, res, next) => { - try { - handler (req, res, next, opts); - } - catch (e) { - if (rethrow) - throw e; - else - console.error (e); - } - }; - - if (verbose) - console.log (`[requestor info] redirecting ${groups.url} to ${f}`); - - switch (groups.method) { - case 'post': - app.post (groups.url, requestor_handler); - break; - case 'get': - app.get (groups.url, requestor_handler); - break; - case 'put': - app.put (groups.url, requestor_handler); - break; - case 'delete': - app.delete (groups.url, requestor_handler); - break; - case 'all': - app.all (groups.url, requestor_handler); - break; - default: - if (verbose) - console.warn ( - `'${groups.method}' did not match any request method, ignoring` - ); - - break; - } + register_handler ( + app, + { + file, + module_folder, + opts, + rethrow + }, + groups.method, + groups.url, + verbose + ); } };