* @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) {
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));