express-list-routes
Advanced tools
Comparing version 0.1.4 to 1.0.0
116
index.js
@@ -1,49 +0,81 @@ | ||
var _ = require('lodash'), | ||
colors = require('colors'), | ||
options = { | ||
prefix: '', | ||
spacer: 7 | ||
}; | ||
const defaultOptions = { | ||
prefix: '', | ||
spacer: 7, | ||
}; | ||
function spacer(x) { | ||
var res = ''; | ||
while(x--) res += ' '; | ||
return res; | ||
} | ||
const COLORS = { | ||
yellow: 33, | ||
green: 32, | ||
blue: 34, | ||
red: 31, | ||
grey: 90, | ||
magenta: 35, | ||
clear: 39, | ||
}; | ||
const spacer = (x) => [...new Array(x)].map(() => ' ').join(''); | ||
const colorText = (color, string) => `\u001b[${color}m${string}\u001b[${COLORS.clear}m`; | ||
function colorMethod(method) { | ||
switch(method){ | ||
case('POST'): return method.yellow; break; | ||
case('GET'): return method.green; break; | ||
case('PUT'): return method.blue; break; | ||
case('DELETE'): return method.red; break; | ||
case('PATCH'): return method.grey; break; | ||
default: return method; | ||
} | ||
switch (method) { | ||
case 'POST': | ||
return colorText(COLORS.yellow, method); | ||
case 'GET': | ||
return colorText(COLORS.green, method); | ||
case 'PUT': | ||
return colorText(COLORS.blue, method); | ||
case 'DELETE': | ||
return colorText(COLORS.red, method); | ||
case 'PATCH': | ||
return colorText(COLORS.grey, method); | ||
default: | ||
return method; | ||
} | ||
} | ||
module.exports = function() { | ||
_.each(arguments, function(arg){ | ||
if (_.isString(arg)) { | ||
console.info(arg.magenta); | ||
} else if (_.isObject(arg)) { | ||
if(!arg.stack) { | ||
_.assign(options, arg); | ||
} else { | ||
_.each(arg.stack, function(stack){ | ||
if (stack.route) { | ||
var route = stack.route, | ||
methodsDone= {}; | ||
_.each(route.stack, function(r){ | ||
var method = r.method ? r.method.toUpperCase() : null; | ||
if(!methodsDone[method] && method){ | ||
console.info(colorMethod(method), spacer(options.spacer - method.length), options.prefix + route.path); | ||
methodsDone[method] = true; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
function getStacks(app) { | ||
// Express 3 | ||
if (app.routes) { | ||
// convert to express 4 | ||
return Object.keys(app.routes) | ||
.reduce((acc, method) => [...acc, ...app.routes[method]], []) | ||
.map((route) => ({ route: { stack: [route] } })); | ||
} | ||
// Express 4 | ||
if (app._router && app._router.stack) { | ||
return app._router.stack.reduce((acc, stack) => { | ||
if (stack.handle.stack) { | ||
return [...acc, ...stack.handle.stack]; | ||
} | ||
return [...acc, stack]; | ||
}, []); | ||
} | ||
return []; | ||
} | ||
module.exports = function expressListRoutes(app, opts) { | ||
const stacks = getStacks(app); | ||
const options = { ...defaultOptions, ...opts }; | ||
if (stacks) { | ||
for (const stack of stacks) { | ||
if (stack.route) { | ||
const routeLogged = {}; | ||
for (const route of stack.route.stack) { | ||
const method = route.method ? route.method.toUpperCase() : null; | ||
if (!routeLogged[method] && method) { | ||
console.info( | ||
colorMethod(method), | ||
spacer(options.spacer - method.length), | ||
[options.prefix, stack.route.path, route.path].filter((s) => !!s).join(''), | ||
); | ||
routeLogged[method] = true; | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
}; |
{ | ||
"name": "express-list-routes", | ||
"version": "0.1.4", | ||
"version": "1.0.0", | ||
"description": "List routes for Express 4", | ||
"keywords": ["express","routes","list","log","paths"], | ||
"keywords": [ | ||
"express", | ||
"routes", | ||
"list", | ||
"log", | ||
"paths" | ||
], | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jest" | ||
}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "https://github.com/labithiotis/express-list-routes.git" | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/labithiotis/express-list-routes.git" | ||
}, | ||
"author": "Darren Labithiotis", | ||
"license": "ISC", | ||
"dependencies": { | ||
"colors": "^1.0.3", | ||
"lodash": "^3.0.0" | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"jest": "^26.6.3", | ||
"express3": "npm:express@^3.13.0", | ||
"express4": "npm:express@^4.17.0", | ||
"prettier": "^2.2.1" | ||
}, | ||
"jest": { | ||
"bail": true, | ||
"clearMocks": true, | ||
"testRegex": "\\.tests?\\.(js|ts)$", | ||
"transformIgnorePatterns": [ | ||
"<rootDir>/node_modules" | ||
] | ||
} | ||
} |
@@ -9,6 +9,7 @@ # express-list-routes | ||
```js | ||
var expressListRoutes = require('express-list-routes'), | ||
express = require('express'), | ||
router = express.Router(); | ||
const expressListRoutes = require('express-list-routes'); | ||
const express = require('express'); | ||
const app = express() | ||
app.use('/api/v1', router); | ||
@@ -21,3 +22,3 @@ | ||
expressListRoutes({ prefix: '/api/v1' }, 'API:', router ); | ||
expressListRoutes(router, { prefix: '/api/v1' }); | ||
@@ -53,3 +54,14 @@ ``` | ||
## Migrations | ||
### 0.1 -> 1.0 | ||
The order of the params have changed, and dropped support for freeform text. | ||
**BEFORE 0.1<** | ||
```expressListRoutes({ prefix: '/api/v1' }, 'API:', router);``` | ||
**AFTER 0.2+** | ||
```expressListRoutes(router, { prefix: '/api/v1' });``` | ||
[npm-image]: https://img.shields.io/npm/v/express-list-routes.svg?style=flat | ||
@@ -56,0 +68,0 @@ [npm-url]: https://npmjs.org/package/express-list-routes |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8155
0
9
141
1
72
4
- Removedcolors@^1.0.3
- Removedlodash@^3.0.0
- Removedcolors@1.4.0(transitive)
- Removedlodash@3.10.1(transitive)