feat: add the support for chained route and pass-through addition methods
This commit is contained in:
parent
80ec67d096
commit
1af92e8281
3 changed files with 77 additions and 40 deletions
|
@ -51,6 +51,8 @@ Parameters:
|
|||
- _path_ - the route path, for which the route applies. The route paths are process via the [`path-to-regexp` library](https://www.npmjs.com/package/path-to-regexp) (_String_)
|
||||
- _callback_ - the SVR.JS mod callback applied for the route (_Function_)
|
||||
|
||||
Returns: the SVRouter router (so that you can chain the methods for adding routes or pass-throughs)
|
||||
|
||||
The function adds a route to the SVRouter router.
|
||||
|
||||
If the _method_ parameter is `"*"`, the route will apply to all the methods
|
||||
|
@ -72,6 +74,8 @@ Parameters:
|
|||
- _path_ - the path, for which the route applies. (optional, _String_)
|
||||
- _callback_ - the SVR.JS mod callback, which the SVRouter router will pass to (_Function_)
|
||||
|
||||
Returns: the SVRouter router (so that you can chain the methods for routes or pass-throughs)
|
||||
|
||||
The function adds middleware to the SVRouter router. The middleware can be an another SVRouter router (the absolute request URLs need to be provided for the _router.route_ function in the SVRouter router).
|
||||
|
||||
The _callback_ parameter has these arguments of the SVR.JS mod callback:
|
||||
|
|
84
src/index.js
84
src/index.js
|
@ -4,46 +4,6 @@ const http = require("http");
|
|||
function svrouter() {
|
||||
const routes = [];
|
||||
|
||||
const addRoute = (method, path, callback) => {
|
||||
if (typeof method !== "string") {
|
||||
throw new Error("The HTTP method must be a string.");
|
||||
} else if (typeof path !== "string") {
|
||||
throw new Error("The route path must be a string.");
|
||||
} else if (typeof callback !== "function") {
|
||||
throw new Error("The route callback must be a function.");
|
||||
}
|
||||
|
||||
routes.push({
|
||||
method: method === "*" ? null : method.toUpperCase(),
|
||||
pathFunction: match(path),
|
||||
callback: callback
|
||||
});
|
||||
};
|
||||
|
||||
const passRoute = (path, callback) => {
|
||||
const realCallback = callback ? callback : path;
|
||||
if (typeof realCallback !== "function") {
|
||||
throw new Error("The passed callback must be a function.");
|
||||
} else if (callback && typeof path !== "string") {
|
||||
throw new Error("The path must be a function");
|
||||
}
|
||||
|
||||
routes.push({
|
||||
method: null,
|
||||
pathFunction: callback
|
||||
? (checkedPath) =>
|
||||
checkedPath == path ||
|
||||
checkedPath.substring(0, path.length + 1) == path + "/"
|
||||
? {
|
||||
path: checkedPath,
|
||||
params: null
|
||||
}
|
||||
: false
|
||||
: () => true,
|
||||
callback: realCallback
|
||||
});
|
||||
};
|
||||
|
||||
const router = (req, res, logFacilities, config, next) => {
|
||||
let index = 0;
|
||||
let previousReqParams = req.params;
|
||||
|
@ -87,6 +47,50 @@ function svrouter() {
|
|||
nextRoute();
|
||||
};
|
||||
|
||||
const addRoute = (method, path, callback) => {
|
||||
if (typeof method !== "string") {
|
||||
throw new Error("The HTTP method must be a string.");
|
||||
} else if (typeof path !== "string") {
|
||||
throw new Error("The route path must be a string.");
|
||||
} else if (typeof callback !== "function") {
|
||||
throw new Error("The route callback must be a function.");
|
||||
}
|
||||
|
||||
routes.push({
|
||||
method: method === "*" ? null : method.toUpperCase(),
|
||||
pathFunction: match(path),
|
||||
callback: callback
|
||||
});
|
||||
|
||||
return router;
|
||||
};
|
||||
|
||||
const passRoute = (path, callback) => {
|
||||
const realCallback = callback ? callback : path;
|
||||
if (typeof realCallback !== "function") {
|
||||
throw new Error("The passed callback must be a function.");
|
||||
} else if (callback && typeof path !== "string") {
|
||||
throw new Error("The path must be a function");
|
||||
}
|
||||
|
||||
routes.push({
|
||||
method: null,
|
||||
pathFunction: callback
|
||||
? (checkedPath) =>
|
||||
checkedPath == path ||
|
||||
checkedPath.substring(0, path.length + 1) == path + "/"
|
||||
? {
|
||||
path: checkedPath,
|
||||
params: null
|
||||
}
|
||||
: false
|
||||
: () => true,
|
||||
callback: realCallback
|
||||
});
|
||||
|
||||
return router;
|
||||
};
|
||||
|
||||
const methods = http.METHODS
|
||||
? http.METHODS
|
||||
: ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"];
|
||||
|
|
|
@ -150,6 +150,35 @@ describe("SVRouter", () => {
|
|||
expect(res.end).toHaveBeenCalledWith("Pass-through matched");
|
||||
});
|
||||
|
||||
test("should work with chained adding of routes and pass-throughs", () => {
|
||||
const req = {
|
||||
method: "GET",
|
||||
parsedURL: { pathname: "/anything" },
|
||||
params: null
|
||||
};
|
||||
const res = {};
|
||||
|
||||
router
|
||||
.get("/anything", (req, res, logFacilities, config, next) => {
|
||||
res.passedThroughGet = true;
|
||||
next();
|
||||
})
|
||||
.pass((req, res, logFacilities, config, next) => {
|
||||
res.passedThroughPass = true;
|
||||
next();
|
||||
})
|
||||
.pass((req, res, logFacilities, config, next) => {
|
||||
res.passedThroughPass2 = true;
|
||||
next();
|
||||
});
|
||||
|
||||
router(req, res, null, null, () => {});
|
||||
|
||||
expect(res.passedThroughGet).toBe(true);
|
||||
expect(res.passedThroughPass).toBe(true);
|
||||
expect(res.passedThroughPass2).toBe(true);
|
||||
});
|
||||
|
||||
test("should throw an error if method is not a string in route", () => {
|
||||
expect(() => {
|
||||
router.route(123, "/path", () => {});
|
||||
|
|
Loading…
Reference in a new issue