chore: rename SVRouter to SVRRouter due to npm publishing issues
This commit is contained in:
parent
10ddecc775
commit
272fda4780
6 changed files with 47 additions and 47 deletions
2
.github/workflows/main.yml
vendored
2
.github/workflows/main.yml
vendored
|
@ -14,6 +14,6 @@ jobs:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- uses: spyoungtech/mirror-action@v0.5.1
|
- uses: spyoungtech/mirror-action@v0.5.1
|
||||||
with:
|
with:
|
||||||
REMOTE: "https://git.svrjs.org/svrjs/svrouter.git"
|
REMOTE: "https://git.svrjs.org/svrjs/svrrouter.git"
|
||||||
GIT_USERNAME: github-mirror
|
GIT_USERNAME: github-mirror
|
||||||
GIT_PASSWORD: ${{ secrets.GIT_PASSWORD }}
|
GIT_PASSWORD: ${{ secrets.GIT_PASSWORD }}
|
||||||
|
|
38
README.md
38
README.md
|
@ -1,12 +1,12 @@
|
||||||
# SVRouter
|
# SVRRouter
|
||||||
|
|
||||||
SVRouter is a router library built for use in building web applications as SVR.JS 4.x mods.
|
SVRRouter is a router library built for use in building web applications as SVR.JS 4.x mods.
|
||||||
|
|
||||||
Example SVR.JS mod that uses SVRouter (you will need to install the `svrouter` npm package either in the SVR.JS installation root or globally):
|
Example SVR.JS mod that uses SVRRouter (you will need to install the `svrrouter` npm package either in the SVR.JS installation root or globally):
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const svrouter = require("svrouter");
|
const svrrouter = require("svrrouter");
|
||||||
const router = svrouter();
|
const router = svrrouter();
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
router.get("/hello", (req, res, logFacilities, config, next) => {
|
router.get("/hello", (req, res, logFacilities, config, next) => {
|
||||||
|
@ -20,18 +20,18 @@ router.get("/hello", (req, res, logFacilities, config, next) => {
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
||||||
module.exports.modInfo = {
|
module.exports.modInfo = {
|
||||||
name: "Example mod with SVRouter",
|
name: "Example mod with SVRRouter",
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## Methods (exported by the `svrouter` package)
|
## Methods (exported by the `svrrouter` package)
|
||||||
|
|
||||||
### _svrouter()_
|
### _svrrouter()_
|
||||||
|
|
||||||
Returns: the SVRouter router, which is a SVR.JS mod function with additional functions for adding routes and middleware.
|
Returns: the SVRRouter router, which is a SVR.JS mod function with additional functions for adding routes and middleware.
|
||||||
|
|
||||||
## Methods (provided by the SVRouter router)
|
## Methods (provided by the SVRRouter router)
|
||||||
|
|
||||||
### _router(req, res, logFacilities, config, next)_
|
### _router(req, res, logFacilities, config, next)_
|
||||||
|
|
||||||
|
@ -51,9 +51,9 @@ Parameters:
|
||||||
- _path_ - the route path (begins with "/"), 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_)
|
- _path_ - the route path (begins with "/"), 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_)
|
- _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)
|
Returns: the SVRRouter router (so that you can chain the methods for adding routes or pass-throughs)
|
||||||
|
|
||||||
The function adds a route to the SVRouter router.
|
The function adds a route to the SVRRouter router.
|
||||||
|
|
||||||
If the _method_ parameter is `"*"`, the route will apply to all the methods
|
If the _method_ parameter is `"*"`, the route will apply to all the methods
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ The _callback_ parameter has these arguments of the SVR.JS mod callback:
|
||||||
|
|
||||||
The _req_ object has an additional _params_ property, which contains request parameters, for example if the request URL is `/api/task/1`, and the route path is `/api/task/:id`, then the _req.params_ object is a `null` prototype object with the `id` property set to `"1"`.
|
The _req_ object has an additional _params_ property, which contains request parameters, for example if the request URL is `/api/task/1`, and the route path is `/api/task/:id`, then the _req.params_ object is a `null` prototype object with the `id` property set to `"1"`.
|
||||||
|
|
||||||
The _req_ object has another additional _svrouterBase_ property, which contains the route base used internally by a SVRouter router. It's not recommended to override this property, as doing it may result in a SVRouter router behaving erratically.
|
The _req_ object has another additional _svrrouterBase_ property, which contains the route base used internally by a SVRRouter router. It's not recommended to override this property, as doing it may result in a SVRRouter router behaving erratically.
|
||||||
|
|
||||||
You can read more about the SVR.JS mod callbacks in the [SVR.JS mod API documentation](https://svrjs.org/docs/api/svrjs-api).
|
You can read more about the SVR.JS mod callbacks in the [SVR.JS mod API documentation](https://svrjs.org/docs/api/svrjs-api).
|
||||||
|
|
||||||
|
@ -74,11 +74,11 @@ You can read more about the SVR.JS mod callbacks in the [SVR.JS mod API document
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
- _path_ - the path (begins with "/"), for which the route applies. (optional, _String_)
|
- _path_ - the path (begins with "/"), for which the route applies. (optional, _String_)
|
||||||
- _callback_ - the SVR.JS mod callback, which the SVRouter router will pass to (_Function_)
|
- _callback_ - the SVR.JS mod callback, which the SVRRouter router will pass to (_Function_)
|
||||||
|
|
||||||
Returns: the SVRouter router (so that you can chain the methods for routes or pass-throughs)
|
Returns: the SVRRouter router (so that you can chain the methods for routes or pass-throughs)
|
||||||
|
|
||||||
The function adds a pass-through (can be middleware) to the SVRouter router. The pass-through can be to an another SVRouter router (the request URLs relative to a parent route need to be provided for the _router.route_ function in the SVRouter router).
|
The function adds a pass-through (can be middleware) to the SVRRouter router. The pass-through can be to an another SVRRouter router (the request URLs relative to a parent route need to be provided for the _router.route_ function in the SVRRouter router).
|
||||||
|
|
||||||
The _callback_ parameter has these arguments of the SVR.JS mod callback:
|
The _callback_ parameter has these arguments of the SVR.JS mod callback:
|
||||||
- _req_ - the SVR.JS request object
|
- _req_ - the SVR.JS request object
|
||||||
|
@ -87,7 +87,7 @@ The _callback_ parameter has these arguments of the SVR.JS mod callback:
|
||||||
- _config_ - the SVR.JS configuration object
|
- _config_ - the SVR.JS configuration object
|
||||||
- _next_ - the callback which passes the execution to other routes, SVR.JS mods and SVR.JS internal handlers.
|
- _next_ - the callback which passes the execution to other routes, SVR.JS mods and SVR.JS internal handlers.
|
||||||
|
|
||||||
The _req_ object has an additional _svrouterBase_ property, which contains the route base used internally by a SVRouter router. It's not recommended to override this property, as doing it may result in a SVRouter router behaving erratically.
|
The _req_ object has an additional _svrrouterBase_ property, which contains the route base used internally by a SVRRouter router. It's not recommended to override this property, as doing it may result in a SVRRouter router behaving erratically.
|
||||||
|
|
||||||
You can read more about the SVR.JS mod callbacks in the [SVR.JS mod API documentation](https://svrjs.org/docs/api/svrjs-api).
|
You can read more about the SVR.JS mod callbacks in the [SVR.JS mod API documentation](https://svrjs.org/docs/api/svrjs-api).
|
||||||
|
|
||||||
|
@ -97,9 +97,9 @@ Parameters:
|
||||||
- _path_ - the path (begins with "/"), for which the route applies. (optional, _String_)
|
- _path_ - the path (begins with "/"), for which the route applies. (optional, _String_)
|
||||||
- _middleware_ - the middleware compatible with the `router` library (_Function_)
|
- _middleware_ - the middleware compatible with the `router` library (_Function_)
|
||||||
|
|
||||||
Returns: the SVRouter router (so that you can chain the methods for routes or pass-throughs)
|
Returns: the SVRRouter router (so that you can chain the methods for routes or pass-throughs)
|
||||||
|
|
||||||
The function adds middleware compatible with the `router` library to the SVRouter router. Middleware that depends on Express's request and response properties may not work.
|
The function adds middleware compatible with the `router` library to the SVRRouter router. Middleware that depends on Express's request and response properties may not work.
|
||||||
|
|
||||||
The _middleware_ parameter has these arguments of middleware compatible with the `router` library:
|
The _middleware_ parameter has these arguments of middleware compatible with the `router` library:
|
||||||
- _req_ - the request object
|
- _req_ - the request object
|
||||||
|
|
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,11 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "svrouter",
|
"name": "svrrouter",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "svrouter",
|
"name": "svrrouter",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "svrouter",
|
"name": "svrrouter",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"description": "A router library for SVR.JS 4.x mods",
|
"description": "A router library for SVR.JS 4.x mods",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -14,7 +14,7 @@
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.svrjs.org/svrjs/svrouter.git"
|
"url": "https://git.svrjs.org/svrjs/svrrouter.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"routing",
|
"routing",
|
||||||
|
|
30
src/index.js
30
src/index.js
|
@ -1,27 +1,27 @@
|
||||||
const { match } = require("path-to-regexp");
|
const { match } = require("path-to-regexp");
|
||||||
const http = require("http");
|
const http = require("http");
|
||||||
|
|
||||||
function svrouter() {
|
function svrrouter() {
|
||||||
const routes = [];
|
const routes = [];
|
||||||
const router = (req, res, logFacilities, config, next) => {
|
const router = (req, res, logFacilities, config, next) => {
|
||||||
let index = 0;
|
let index = 0;
|
||||||
let previousReqParams = req.params;
|
let previousReqParams = req.params;
|
||||||
let previousReqSvrouterBase = req.svrouterBase;
|
let previousReqSvrouterBase = req.svrrouterBase;
|
||||||
let paramsPresent = false;
|
let paramsPresent = false;
|
||||||
let passPathPresent = false;
|
let passPathPresent = false;
|
||||||
|
|
||||||
const nextRoute = () => {
|
const nextRoute = () => {
|
||||||
if (paramsPresent) req.params = previousReqParams;
|
if (paramsPresent) req.params = previousReqParams;
|
||||||
if (passPathPresent) req.svrouterBase = previousReqSvrouterBase;
|
if (passPathPresent) req.svrrouterBase = previousReqSvrouterBase;
|
||||||
let currentRoute = routes[index++];
|
let currentRoute = routes[index++];
|
||||||
let currentMatch =
|
let currentMatch =
|
||||||
currentRoute && currentRoute.pathFunction
|
currentRoute && currentRoute.pathFunction
|
||||||
? currentRoute.pathFunction(
|
? currentRoute.pathFunction(
|
||||||
req.parsedURL.pathname.substring(
|
req.parsedURL.pathname.substring(
|
||||||
(req.svrouterBase ? req.svrouterBase : "").length
|
(req.svrrouterBase ? req.svrrouterBase : "").length
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
: false; // Let's assume the request URL begins with the contents of the req.svrouterBase property.
|
: false; // Let's assume the request URL begins with the contents of the req.svrrouterBase property.
|
||||||
while (
|
while (
|
||||||
currentRoute &&
|
currentRoute &&
|
||||||
((currentRoute.method && req.method != currentRoute.method) ||
|
((currentRoute.method && req.method != currentRoute.method) ||
|
||||||
|
@ -32,7 +32,7 @@ function svrouter() {
|
||||||
currentRoute && currentRoute.pathFunction
|
currentRoute && currentRoute.pathFunction
|
||||||
? currentRoute.pathFunction(
|
? currentRoute.pathFunction(
|
||||||
req.parsedURL.pathname.substring(
|
req.parsedURL.pathname.substring(
|
||||||
(req.svrouterBase ? req.svrouterBase : "").length
|
(req.svrrouterBase ? req.svrrouterBase : "").length
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
: false;
|
: false;
|
||||||
|
@ -47,8 +47,8 @@ function svrouter() {
|
||||||
? currentMatch.params
|
? currentMatch.params
|
||||||
: Object.create(null);
|
: Object.create(null);
|
||||||
if (passPathPresent)
|
if (passPathPresent)
|
||||||
req.svrouterBase =
|
req.svrrouterBase =
|
||||||
(req.svrouterBase ? req.svrouterBase : "") +
|
(req.svrrouterBase ? req.svrrouterBase : "") +
|
||||||
currentRoute.passPath;
|
currentRoute.passPath;
|
||||||
currentRoute.callback(req, res, logFacilities, config, nextRoute);
|
currentRoute.callback(req, res, logFacilities, config, nextRoute);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -122,20 +122,20 @@ function svrouter() {
|
||||||
const previousReqBaseUrl = req.baseUrl;
|
const previousReqBaseUrl = req.baseUrl;
|
||||||
const previousReqUrl = req.url;
|
const previousReqUrl = req.url;
|
||||||
const previousReqOriginalUrl = req.originalUrl;
|
const previousReqOriginalUrl = req.originalUrl;
|
||||||
const previousReqSvrouterBase = req.svrouterBase;
|
const previousReqSvrouterBase = req.svrrouterBase;
|
||||||
|
|
||||||
const svrouterBase = req.svrouterBase ? req.svrouterBase : "";
|
const svrrouterBase = req.svrrouterBase ? req.svrrouterBase : "";
|
||||||
req.baseUrl = svrouterBase;
|
req.baseUrl = svrrouterBase;
|
||||||
req.originalUrl = req.url;
|
req.originalUrl = req.url;
|
||||||
req.url = req.url.substr(svrouterBase.length); // Let's assume the request URL begins with the contents of svrouterBase variable.
|
req.url = req.url.substr(svrrouterBase.length); // Let's assume the request URL begins with the contents of svrrouterBase variable.
|
||||||
if (!req.url) req.url = "/";
|
if (!req.url) req.url = "/";
|
||||||
req.svrouterBase = undefined;
|
req.svrrouterBase = undefined;
|
||||||
|
|
||||||
const nextCallback = () => {
|
const nextCallback = () => {
|
||||||
req.baseUrl = previousReqBaseUrl;
|
req.baseUrl = previousReqBaseUrl;
|
||||||
req.url = previousReqUrl;
|
req.url = previousReqUrl;
|
||||||
req.originalUrl = previousReqOriginalUrl;
|
req.originalUrl = previousReqOriginalUrl;
|
||||||
req.svrouterBase = previousReqSvrouterBase;
|
req.svrrouterBase = previousReqSvrouterBase;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -162,4 +162,4 @@ function svrouter() {
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = svrouter;
|
module.exports = svrrouter;
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const svrouter = require("../src/index.js");
|
const svrrouter = require("../src/index.js");
|
||||||
|
|
||||||
describe("SVRouter", () => {
|
describe("SVRRouter", () => {
|
||||||
let router;
|
let router;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
router = svrouter();
|
router = svrrouter();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("should add and handle a GET route", () => {
|
test("should add and handle a GET route", () => {
|
||||||
|
@ -313,7 +313,7 @@ describe("SVRouter", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("should handle nested routers correctly", () => {
|
test("should handle nested routers correctly", () => {
|
||||||
const childRouter = svrouter();
|
const childRouter = svrrouter();
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
@ -338,7 +338,7 @@ describe("SVRouter", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("should fallback to parent router if no route matches in child router", () => {
|
test("should fallback to parent router if no route matches in child router", () => {
|
||||||
const childRouter = svrouter();
|
const childRouter = svrrouter();
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
@ -364,7 +364,7 @@ describe("SVRouter", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("should handle parameterized routes in nested routers", () => {
|
test("should handle parameterized routes in nested routers", () => {
|
||||||
const childRouter = svrouter();
|
const childRouter = svrrouter();
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
@ -389,8 +389,8 @@ describe("SVRouter", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("should handle multiple nested routers", () => {
|
test("should handle multiple nested routers", () => {
|
||||||
const childRouter1 = svrouter();
|
const childRouter1 = svrrouter();
|
||||||
const childRouter2 = svrouter();
|
const childRouter2 = svrrouter();
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
|
Loading…
Reference in a new issue