projects
/
squeep-api-dingus
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
allow additional arguments to be passed to handler functions
[squeep-api-dingus]
/
lib
/
router.js
diff --git
a/lib/router.js
b/lib/router.js
index 92a85006458d4544a0336e532c11649e1b596946..61a8d11cb3e55e2f4d434940166a4027f0b2ab51 100644
(file)
--- 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 {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) {
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(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}'`);
}
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
* @param {string} method
* @param {string[]} urlPath
* @param {object} ctx
+ * @returns {object}
*/
lookup(method, urlPath, ctx = {}) {
const pathParts = urlPath.split('/').map((part) => decodeURIComponent(part));
*/
lookup(method, urlPath, ctx = {}) {
const pathParts = urlPath.split('/').map((part) => decodeURIComponent(part));