Skip to content
This repository has been archived by the owner on Jan 20, 2021. It is now read-only.

Commit

Permalink
feat: add paramChar option
Browse files Browse the repository at this point in the history
  • Loading branch information
test123456789012345 committed May 13, 2020
1 parent 44f45a9 commit c9778e9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ const mountRoutes = require('./mountRoutes');

module.exports = function(
root,
{ cwd = process.cwd(), viewExtensions = [] } = {}
{ cwd = process.cwd(), viewExtensions = [], paramChar = '$' } = {}
) {
root = path.resolve(cwd, root);
// The base router that'll hold everything
const router = Router();

// First mount the middlewares
const routesFromMiddlewares = mountMiddlewares(router, root, { cwd });
const routesFromMiddlewares = mountMiddlewares(router, root, {
cwd,
paramChar
});

// And then the actual routes
mountRoutes(router, root, { routes: routesFromMiddlewares, viewExtensions });
mountRoutes(router, root, {
routes: routesFromMiddlewares,
viewExtensions,
paramChar
});

return router;
};
4 changes: 2 additions & 2 deletions src/mountMiddlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const toExpressPath = require('./toExpressPath');
const compareRouteDepth = require('./compareRouteDepth');
const compareRouteVariability = require('./compareRouteVariability');

module.exports = function mountMiddlewares(router, root) {
module.exports = function mountMiddlewares(router, root, { paramChar }) {
// Extra routes to configure
const routesFromMiddlewares = [];
// First let's list the routes file
Expand All @@ -19,7 +19,7 @@ module.exports = function mountMiddlewares(router, root) {
return {
// We need the express routePath here so that
// variables are properly detected when sorting
routePath: toExpressPath(path.dirname(filePath)),
routePath: toExpressPath(path.dirname(filePath), { paramChar }),
config
};
})
Expand Down
8 changes: 4 additions & 4 deletions src/mountRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const compareRouteVariability = require('./compareRouteVariability');
module.exports = function mountRoutes(
router,
root,
{ routes = [], viewExtensions = [] }
{ routes = [], viewExtensions = [], paramChar }
) {
routes
.concat(discoverRoutes(root, { viewExtensions }))
.concat(discoverRoutes(root, { viewExtensions, paramChar }))
.sort((routeA, routeB) => {
return (
-1 * compareRouteDepth(routeA.routePath, routeB.routePath) ||
Expand All @@ -25,7 +25,7 @@ module.exports = function mountRoutes(
});
};

function discoverRoutes(root, { viewExtensions }) {
function discoverRoutes(root, { viewExtensions, paramChar }) {
return fastGlob
.sync(
`**/{*.,}(${HTTP_METHODS.join('|')}).(${['js', ...viewExtensions].join(
Expand All @@ -42,7 +42,7 @@ function discoverRoutes(root, { viewExtensions }) {
const handler = getHandler(fullPath, extension);
return {
fullPath,
routePath: toExpressPath(routePath),
routePath: toExpressPath(routePath, { paramChar }),
method,
extension,
handler
Expand Down
6 changes: 4 additions & 2 deletions src/toExpressPath.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = function toExpressPath(routePath) {
module.exports = function toExpressPath(routePath, { paramChar }) {
if (routePath) {
return routePath.replace(/\$/g, ':').replace(/__/g, '/');
return routePath
.replace(new RegExp(`\\${paramChar}`, 'g'), ':')
.replace(/__/g, '/');
} else {
return '/';
}
Expand Down

0 comments on commit c9778e9

Please sign in to comment.