X-Git-Url: http://git.squeep.com/?p=squeep-api-dingus;a=blobdiff_plain;f=lib%2Frouter.js;h=61a8d11cb3e55e2f4d434940166a4027f0b2ab51;hp=92a85006458d4544a0336e532c11649e1b596946;hb=1bfd3f26e768c390a6be543281e79b7ea5c4b9c5;hpb=0f19dc825bb6e74106db319b79950ca463ad40f0 diff --git a/lib/router.js b/lib/router.js index 92a8500..61a8d11 100644 --- a/lib/router.js +++ b/lib/router.js @@ -149,8 +149,9 @@ class Router { * @param {string|string[]} methods * @param {string} urlPath * @param {fn} handler + * @param {*[]} handlerArgs */ - on(methods, urlPath, handler) { + on(methods, urlPath, handler, handlerArgs = []) { const matchParts = this._pathDefinitionToPathMatch(urlPath); let existingPath = this._pathFindExact(matchParts); if (!existingPath) { @@ -163,20 +164,26 @@ class Router { if (!Array.isArray(methods)) { methods = [methods]; } + if (!Array.isArray(handlerArgs)) { + throw new TypeError(`handlerArgs must be an Array, not '${typeof handlerArgs}'`); + } methods.forEach((method) => { if (!httpMethods.includes(method) && method !== '*') { throw new DingusError(`invalid method '${method}'`); } - existingPath[METHODS][method] = handler; + existingPath[METHODS][method] = { handler, handlerArgs }; }); } /** - * Return a matching handler for a request, sets path parameters on context. + * Return an object, which contains a matching handler and any extra + * arguments, for a requested url. + * Also sets path parameters on context. * @param {string} method * @param {string[]} urlPath * @param {object} ctx + * @returns {object} */ lookup(method, urlPath, ctx = {}) { const pathParts = urlPath.split('/').map((part) => decodeURIComponent(part));