express-list-routes
Advanced tools
Comparing version 1.1.3 to 1.1.4
17
index.js
@@ -6,2 +6,4 @@ const path = require('path'); | ||
spacer: 7, | ||
logger: console.info, | ||
color: true, | ||
}; | ||
@@ -41,3 +43,8 @@ | ||
function getPathFromRegex(regexp) { | ||
return regexp.toString().replace('/^', '').replace('?(?=\\/|$)/i', '').replace(/\\\//g, '/'); | ||
return regexp | ||
.toString() | ||
.replace('/^', '') | ||
.replace('?(?=\\/|$)/i', '') | ||
.replace(/\\\//g, '/') | ||
.replace('(?:/(?=$))', ''); | ||
} | ||
@@ -83,2 +90,3 @@ | ||
const options = { ...defaultOptions, ...opts }; | ||
const paths = []; | ||
@@ -92,3 +100,3 @@ if (stacks) { | ||
if (!routeLogged[method] && method) { | ||
const stackMethod = colorMethod(method); | ||
const stackMethod = options.color ? colorMethod(method) : method; | ||
const stackSpace = spacer(options.spacer - method.length); | ||
@@ -98,3 +106,4 @@ const stackPath = path.resolve( | ||
); | ||
console.info(stackMethod, stackSpace, stackPath); | ||
options.logger(stackMethod, stackSpace, stackPath); | ||
paths.push({ method, path: stackPath }); | ||
routeLogged[method] = true; | ||
@@ -106,2 +115,4 @@ } | ||
} | ||
return paths; | ||
}; |
@@ -7,4 +7,2 @@ const express3 = require('express3'); | ||
jest.spyOn(global.console, 'info'); | ||
function handler(req, res) { | ||
@@ -16,14 +14,26 @@ res.send('handled'); | ||
it('prints out all routes', () => { | ||
const logger = jest.fn(); | ||
const app = express3(); | ||
app.get('/test', handler); | ||
app.get('/user', handler); | ||
app.post('/user', handler); | ||
app.get('/user', handler); | ||
app.patch('/user', handler); | ||
app.delete('/user', handler); | ||
expressListRoutes(app); | ||
const paths = expressListRoutes(app, { logger }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(paths).toEqual([ | ||
{ method: 'GET', path: '/test' }, | ||
{ method: 'GET', path: '/user' }, | ||
{ method: 'POST', path: '/user' }, | ||
{ method: 'PATCH', path: '/user' }, | ||
{ method: 'DELETE', path: '/user' }, | ||
]); | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[32mGET\u001b[39m', ' ', '/test'], | ||
['\u001b[32mGET\u001b[39m', ' ', '/user'], | ||
['\u001b[33mPOST\u001b[39m', ' ', '/user'], | ||
['\u001b[90mPATCH\u001b[39m', ' ', '/user'], | ||
['\u001b[31mDELETE\u001b[39m', ' ', '/user'], | ||
]); | ||
@@ -33,2 +43,3 @@ }); | ||
it('works with prefix and dividers', () => { | ||
const logger = jest.fn(); | ||
const app = express3(); | ||
@@ -40,5 +51,10 @@ | ||
expressListRoutes(app, { prefix: '/api/v1' }); | ||
const paths = expressListRoutes(app, { logger, prefix: '/api/v1' }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(paths).toEqual([ | ||
{ method: 'GET', path: '/api/v1/test' }, | ||
{ method: 'GET', path: '/api/v1/user' }, | ||
{ method: 'POST', path: '/api/v1/user' }, | ||
]); | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/test'], | ||
@@ -53,2 +69,3 @@ ['\u001b[32mGET\u001b[39m', ' ', '/api/v1/user'], | ||
it('works with top level routes', () => { | ||
const logger = jest.fn(); | ||
const app = express4(); | ||
@@ -58,5 +75,9 @@ app.get('/user', handler); | ||
expressListRoutes(app); | ||
const paths = expressListRoutes(app, { logger }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(paths).toEqual([ | ||
{ method: 'GET', path: '/user' }, | ||
{ method: 'POST', path: '/user' }, | ||
]); | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[32mGET\u001b[39m', ' ', '/user'], | ||
@@ -68,2 +89,3 @@ ['\u001b[33mPOST\u001b[39m', ' ', '/user'], | ||
it('works with express.Router', () => { | ||
const logger = jest.fn(); | ||
const app = express4(); | ||
@@ -78,5 +100,10 @@ const router = express4.Router(); | ||
expressListRoutes(router); | ||
const paths = expressListRoutes(router, { logger }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(paths).toEqual([ | ||
{ method: 'POST', path: '/user' }, | ||
{ method: 'GET', path: '/user' }, | ||
{ method: 'PUT', path: '/user' }, | ||
]); | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[33mPOST\u001b[39m', ' ', '/user'], | ||
@@ -89,2 +116,3 @@ ['\u001b[32mGET\u001b[39m', ' ', '/user'], | ||
it('handles nested routers', () => { | ||
const logger = jest.fn(); | ||
const app = express4(); | ||
@@ -99,5 +127,11 @@ const router = express4.Router(); | ||
expressListRoutes(app, { prefix: '/api/v1' }); | ||
const paths = expressListRoutes(app, { logger, prefix: '/api/v1' }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(paths).toEqual([ | ||
{ method: 'GET', path: '/api/v1/test' }, | ||
{ method: 'POST', path: '/api/v1/admin/user' }, | ||
{ method: 'GET', path: '/api/v1/admin/user' }, | ||
{ method: 'PUT', path: '/api/v1/admin/user' }, | ||
]); | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/test'], | ||
@@ -111,2 +145,3 @@ ['\u001b[33mPOST\u001b[39m', ' ', '/api/v1/admin/user'], | ||
it('works with options', () => { | ||
const logger = jest.fn(); | ||
const app = express4(); | ||
@@ -116,5 +151,9 @@ app.get('/user', handler); | ||
expressListRoutes(app, { prefix: '/api/v1', spacer: 3 }); | ||
const paths = expressListRoutes(app, { logger, prefix: '/api/v1', spacer: 3 }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(paths).toEqual([ | ||
{ method: 'GET', path: '/api/v1/user' }, | ||
{ method: 'POST', path: '/api/v1/user' }, | ||
]); | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[32mGET\u001b[39m', '', '/api/v1/user'], | ||
@@ -128,2 +167,3 @@ ['\u001b[33mPOST\u001b[39m', '', '/api/v1/user'], | ||
it('works with top level routes', () => { | ||
const logger = jest.fn(); | ||
const app = express5(); | ||
@@ -133,5 +173,9 @@ app.get('/user', handler); | ||
expressListRoutes(app); | ||
const paths = expressListRoutes(app, { logger }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(paths).toEqual([ | ||
{ method: 'GET', path: '/user' }, | ||
{ method: 'POST', path: '/user' }, | ||
]); | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[32mGET\u001b[39m', ' ', '/user'], | ||
@@ -143,2 +187,3 @@ ['\u001b[33mPOST\u001b[39m', ' ', '/user'], | ||
it('works with express.Router', () => { | ||
const logger = jest.fn(); | ||
const app = express5(); | ||
@@ -153,5 +198,10 @@ const router = express5.Router(); | ||
expressListRoutes(router); | ||
const paths = expressListRoutes(router, { logger }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(paths).toEqual([ | ||
{ method: 'POST', path: '/user' }, | ||
{ method: 'GET', path: '/user' }, | ||
{ method: 'PUT', path: '/user' }, | ||
]); | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[33mPOST\u001b[39m', ' ', '/user'], | ||
@@ -164,2 +214,3 @@ ['\u001b[32mGET\u001b[39m', ' ', '/user'], | ||
it('handles nested routers', () => { | ||
const logger = jest.fn(); | ||
const app = express5(); | ||
@@ -174,5 +225,5 @@ const router = express5.Router(); | ||
expressListRoutes(app, { prefix: '/api/v1' }); | ||
expressListRoutes(app, { logger, prefix: '/api/v1' }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[32mGET\u001b[39m', ' ', '/api/v1/test'], | ||
@@ -186,2 +237,3 @@ ['\u001b[33mPOST\u001b[39m', ' ', '/api/v1/admin/user'], | ||
it('works with options', () => { | ||
const logger = jest.fn(); | ||
const app = express5(); | ||
@@ -191,5 +243,5 @@ app.get('/user', handler); | ||
expressListRoutes(app, { prefix: '/api/v1', spacer: 3 }); | ||
expressListRoutes(app, { logger, prefix: '/api/v1', spacer: 3 }); | ||
expect(global.console.info.mock.calls).toEqual([ | ||
expect(logger.mock.calls).toEqual([ | ||
['\u001b[32mGET\u001b[39m', '', '/api/v1/user'], | ||
@@ -200,1 +252,36 @@ ['\u001b[33mPOST\u001b[39m', '', '/api/v1/user'], | ||
}); | ||
describe('unknown app', () => { | ||
it('works if the app passed in does not have stacks/routes', () => { | ||
const logger = jest.fn(); | ||
const paths = expressListRoutes({}, { logger }); | ||
expect(paths).toEqual([]); | ||
expect(logger.mock.calls).toEqual([]); | ||
}); | ||
it('handles "other" methods', () => { | ||
const logger = jest.fn(); | ||
const app = express3(); | ||
app.trace('/test', handler); | ||
const paths = expressListRoutes(app, { logger }); | ||
expect(paths).toEqual([{ method: 'TRACE', path: '/test' }]); | ||
expect(logger.mock.calls).toEqual([['TRACE', ' ', '/test']]); | ||
}); | ||
it('disable coloring method names if option is false', () => { | ||
const logger = jest.fn(); | ||
const app = express3(); | ||
app.get('/test', handler); | ||
const paths = expressListRoutes(app, { logger, color: false }); | ||
expect(paths).toEqual([{ method: 'GET', path: '/test' }]); | ||
expect(logger.mock.calls).toEqual([['GET', ' ', '/test']]); | ||
}); | ||
}); |
{ | ||
"name": "express-list-routes", | ||
"version": "1.1.3", | ||
"description": "List routes for Express 4", | ||
"version": "1.1.4", | ||
"description": "List routes for Express", | ||
"keywords": [ | ||
@@ -9,2 +9,3 @@ "express", | ||
"express4", | ||
"express5", | ||
"routes", | ||
@@ -16,4 +17,5 @@ "list", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "jest" | ||
"test": "jest --coverage" | ||
}, | ||
@@ -28,7 +30,7 @@ "repository": { | ||
"devDependencies": { | ||
"jest": "^26.6.3", | ||
"express3": "npm:express@^3.13.0", | ||
"express4": "npm:express@^4.17.0", | ||
"jest": "^29", | ||
"express3": "npm:express@^3", | ||
"express4": "npm:express@^4", | ||
"express5": "npm:express@^5.0.0-alpha.8", | ||
"prettier": "^2.2.1" | ||
"prettier": "^2" | ||
}, | ||
@@ -35,0 +37,0 @@ "jest": { |
# express-list-routes | ||
List all routes used in Express. | ||
List all routes used in Express[3,4,5] | ||
@@ -55,2 +55,4 @@ [![NPM Version][npm-image]][npm-url] | ||
spacer: 7 // Spacer between router Method and Path | ||
logger: console.info // A custom logger function | ||
color: true // If the console log should color the method name | ||
} | ||
@@ -57,0 +59,0 @@ ``` |
18742
14
329
80