Permalink
Browse files

close #33 by adding a routes() method for displaying routes

  • Loading branch information...
jeremydaly committed May 16, 2018
1 parent b674c15 commit b5a48ceb328658e929317b732a2986d5fa4c13cb
Showing with 179 additions and 3 deletions.
  1. +21 −2 index.js
  2. +23 −0 lib/prettyPrint.js
  3. +15 −0 lib/utils.js
  4. +54 −0 test/routes.js
  5. +66 −1 test/utils.js
@@ -9,6 +9,8 @@

const REQUEST = require('./lib/request.js') // Resquest object
const RESPONSE = require('./lib/response.js') // Response object
const UTILS = require('./lib/utils.js') // Require utils library
const prettyPrint = require('./lib/prettyPrint') // Pretty print for debugging

// Create the API class
class API {
@@ -102,7 +104,13 @@ class API {
// Add the route to the global _routes
this.setRoute(
this._routes,
(i === route.length-1 ? { ['__'+method.toUpperCase()]: { vars: pathVars, handler: handler, route: '/'+parsedPath.join('/') } } : {}),
(i === route.length-1 ? {
['__'+method.toUpperCase()]: {
vars: pathVars,
handler: handler,
route: '/'+parsedPath.join('/'),
path: '/'+this._prefix.concat(parsedPath).join('/') }
} : {}),
route.slice(0,i+1)
)

@@ -292,8 +300,19 @@ class API {
} // end register


} // end API class
// prettyPrint debugger
routes(format) {
// Parse the routes
let routes = UTILS.extractRoutes(this._routes)

if (format) {
prettyPrint(routes)
} else {
return routes
}
}

} // end API class

// Export the API class as a new instance
module.exports = opts => new API(opts)
@@ -0,0 +1,23 @@
'use strict'

/**
* Lightweight web framework for your serverless applications
* @author Jeremy Daly <jeremy@jeremydaly.com>
* @license MIT
*/

module.exports = routes => {
// Calculate column widths
let widths = routes.reduce((acc,row) => {
return [Math.max(acc[0],row[0].length),Math.max(acc[1],row[1].length)]
},[0,0])

console.log('╔══' + ''.padEnd(widths[0],'') + '══╤══' + ''.padEnd(widths[1],'') + '══╗')
console.log('' + "\u001b[1m" + 'METHOD'.padEnd(widths[0]) + "\u001b[0m" + '' + "\u001b[1m" + 'ROUTE'.padEnd(widths[1]) + "\u001b[0m" + '')
console.log('╟──' + ''.padEnd(widths[0],'') + '──┼──' + ''.padEnd(widths[1],'') + '──╢')
routes.forEach((route,i) => {
console.log('' + route[0].padEnd(widths[0]) + '' + route[1].padEnd(widths[1]) + '')
if (i < routes.length-1) { console.log('╟──' + ''.padEnd(widths[0],'') + '──┼──' + ''.padEnd(widths[1],'') + '──╢') }
})
console.log('╚══' + ''.padEnd(widths[0],'') + '══╧══' + ''.padEnd(widths[1],'') + '══╝')
}
@@ -83,3 +83,18 @@ module.exports.mimeLookup = (input,custom={}) => {
return mime ? mime : false
}
}


// Parses routes into readable array
const extractRoutes = (routes,table=[]) => {
for (let route in routes) {
if (/^__[A-Z]+$/.test(route)) {
table.push([route.replace(/^__/,''),routes[route].path])
} else {
extractRoutes(routes[route],table)
}
}
return table
}

module.exports.extractRoutes = extractRoutes
@@ -600,5 +600,59 @@ describe('Route Tests:', function() {

}) // end method tests

describe('routes() (debug method)', function() {
it('Extract routes', async function() {
expect(api.routes()).to.deep.equal([
[ 'GET', '/' ],
[ 'OPTIONS', '/test' ],
[ 'PUT', '/test' ],
[ 'POST', '/test' ],
[ 'GET', '/test' ],
[ 'OPTIONS', '/test/:test' ],
[ 'DELETE', '/test/:test' ],
[ 'PUT', '/test/:test' ],
[ 'POST', '/test/:test' ],
[ 'GET', '/test/:test' ],
[ 'DELETE', '/test/:test/:test2' ],
[ 'OPTIONS', '/test/:test/query' ],
[ 'PUT', '/test/:test/query' ],
[ 'POST', '/test/:test/query' ],
[ 'GET', '/test/:test/query' ],
[ 'OPTIONS', '/test/:test/query/:test2' ],
[ 'PUT', '/test/:test/query/:test2' ],
[ 'POST', '/test/:test/query/:test2' ],
[ 'GET', '/test/:test/query/:test2' ],
[ 'TEST', '/test/:param1/queryx' ],
[ 'PUT', '/test/json' ],
[ 'POST', '/test/json' ],
[ 'PUT', '/test/form' ],
[ 'POST', '/test/form' ],
[ 'GET', '/test_options' ],
[ 'GET', '/test_options2/:test' ],
[ 'TEST', '/test_options2/:param1/test' ],
[ 'OPTIONS', '/test_options2/:param1/*' ],
[ 'OPTIONS', '/test_options2/*' ],
[ 'OPTIONS', '/*' ],
[ 'HEAD', '/override/head/request' ],
[ 'GET', '/override/head/request' ]
])
})

it('Sample routes', function() {
// Create an api instance
let api2 = require('../index')()
api2.get('/', (req,res) => {})
api2.post('/test', (req,res) => {})
api2.put('/test/put', (req,res) => {})
api2.delete('/test/:var/delete', (req,res) => {})

expect(api2.routes()).to.deep.equal([
[ 'GET', '/' ],
[ 'POST', '/test' ],
[ 'PUT', '/test/put' ],
[ 'DELETE', '/test/:var/delete' ]
])
}) // end it
}) // end routes() test

}) // end ROUTE tests
@@ -189,8 +189,73 @@ describe('Utility Function Tests:', function() {
expect(utils.mimeLookup('.mpeg', { mpeg: 'video/mpeg' })).to.equal('video/mpeg')
}) // end it


}) // end encodeBody tests


describe('extractRoutes:', function() {

it('Sample routes', function() {
// Create an api instance
let api = require('../index')()
api.get('/', (req,res) => {})
api.post('/test', (req,res) => {})
api.put('/test/put', (req,res) => {})
api.delete('/test/:var/delete', (req,res) => {})

expect(utils.extractRoutes(api._routes)).to.deep.equal([
[ 'GET', '/' ],
[ 'POST', '/test' ],
[ 'PUT', '/test/put' ],
[ 'DELETE', '/test/:var/delete' ]
])
}) // end it

it('No routes', function() {
// Create an api instance
let api = require('../index')()

expect(utils.extractRoutes(api._routes)).to.deep.equal([])
}) // end it

it('Prefixed routes', function() {
// Create an api instance
let api = require('../index')()

api.register((apix,opts) => {
apix.get('/', (req,res) => {})
apix.post('/test', (req,res) => {})
}, { prefix: '/v1' })
api.get('/', (req,res) => {})
api.post('/test', (req,res) => {})
api.put('/test/put', (req,res) => {})
api.delete('/test/:var/delete', (req,res) => {})

expect(utils.extractRoutes(api._routes)).to.deep.equal([
[ 'GET', '/v1' ],
[ 'POST', '/v1/test' ],
[ 'GET', '/' ],
[ 'POST', '/test' ],
[ 'PUT', '/test/put' ],
[ 'DELETE', '/test/:var/delete' ]
])
}) // end it

it('Base routes', function() {
// Create an api instance
let api = require('../index')({ base: 'v2' })
api.get('/', (req,res) => {})
api.post('/test', (req,res) => {})
api.put('/test/put', (req,res) => {})
api.delete('/test/:var/delete', (req,res) => {})

expect(utils.extractRoutes(api._routes)).to.deep.equal([
[ 'GET', '/v2' ],
[ 'POST', '/v2/test' ],
[ 'PUT', '/v2/test/put' ],
[ 'DELETE', '/v2/test/:var/delete' ]
])
}) // end it

}) // end extractRoutes

}) // end UTILITY tests

0 comments on commit b5a48ce

Please sign in to comment.