Skip to content

Commit

Permalink
Merge pull request #5 from fabrix-app/v1.1
Browse files Browse the repository at this point in the history
[chore] cleanup
  • Loading branch information
scott-wyatt authored Jul 19, 2018
2 parents b882115 + bace812 commit 4ff5a5a
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lib/RouterSpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as pkg from '../package.json'
* @see https://github.com/fabrix-app/spool-hapi
*/
export class RouterSpool extends SystemSpool {
private _routes = {}
private _routes: Map<string, {[key: string]: any}> // = new Map

constructor (app) {
super(app, {
Expand Down Expand Up @@ -63,7 +63,7 @@ export class RouterSpool extends SystemSpool {
/**
* Get's the routes from spool-router
*/
get routes(): {[key: string]: any} {
get routes(): Map<string, {[key: string]: any}> {
return this._routes
}

Expand Down
5 changes: 3 additions & 2 deletions lib/config/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const router = {
prefix: '',
sortOrder: 'asc'
prefix: null,
sortOrder: 'asc',
debug: false
}
3 changes: 2 additions & 1 deletion lib/interfaces/IRoute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface IRoute {
[key: string]: { [key: string]: any },
[key: string]: any,
_orgPath: string,
config?: {
pre?: any,
[key: string]: { [key: string]: any }
Expand Down
3 changes: 2 additions & 1 deletion lib/schemas/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import * as joi from 'joi'

export const routerSchema = joi.object().keys({
sortOrder: joi.string().allow('asc', 'desc').required(),
prefix: joi.string().allow('').required()
prefix: joi.string().allow('', null).required(),
debug: joi.any()
})
51 changes: 31 additions & 20 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FabrixApp } from '@fabrix/fabrix'
import { get } from 'lodash'
import { get, omit } from 'lodash'
import { Router } from 'call'
import { IRoute } from './interfaces/IRoute'

Expand All @@ -20,35 +20,46 @@ export const Utils = {
* Build a complete route, with bound handler and attached preconditions
*/
buildRoute (app: FabrixApp, path: string, rawRoute: IRoute) {
const route = Object.assign({ }, rawRoute)
route.config = route.config || (route.config = { })
route.config.pre = route.config.pre || (route.config.pre = [ ])
const orgRoute = Object.assign({ }, rawRoute)
orgRoute.config = orgRoute.config || (orgRoute.config = { })
orgRoute.config.pre = orgRoute.config.pre || (orgRoute.config.pre = [ ])

path = Utils.getPathFromRoute(app, path, route)
Utils.getHandlerFromString(app, route)
if (app.config.get('router.debug')) {
orgRoute._orgPath = path
}

path = Utils.getPathFromRoute(app, path, orgRoute)

if (app.config.get('router.debug')) {
orgRoute._newPath = path
}

route.config.pre = route.config.pre
Utils.getHandlerFromString(app, orgRoute)

orgRoute.config.pre = orgRoute.config.pre
.map(pre => Utils.getHandlerFromPrerequisite(app, pre))
.filter(handler => !!handler)

const routeHandlers = Object.keys(route).filter(value => -1 !== Utils.methods.indexOf(value))
const orgRouteHandlers = Object.keys(orgRoute).filter(value => -1 !== Utils.methods.indexOf(value))

if (!routeHandlers.some(v => Utils.methods.indexOf(v) >= 0 || !!route[v])) {
app.log.error('spool-router: route ', path, ' handler [', routeHandlers.join(', '), ']',
if (!orgRouteHandlers.some(v => Utils.methods.indexOf(v) >= 0 || !!orgRoute[v])) {
app.log.error('spool-orgRouter: orgRoute ', path, ' handler [', orgRouteHandlers.join(', '), ']',
'does not correspond to any defined Controller handler')
return {}
}

routeHandlers.forEach(method => {
if (route[method]) {
route[method].config = route[method].config || route.config
route[method].config.pre = route[method].config.pre || route.config.pre
route[method].config.pre = route[method].config.pre
orgRouteHandlers.forEach(method => {
if (orgRoute[method]) {
orgRoute[method].config = orgRoute[method].config || orgRoute.config
orgRoute[method].config.pre = orgRoute[method].config.pre || orgRoute.config.pre
orgRoute[method].config.pre = orgRoute[method].config.pre
.map(pre => Utils.getHandlerFromPrerequisite(app, pre))
.filter(handler => !!handler)
}
})

const route = omit(orgRoute, 'config')

return { path, route }
},

Expand Down Expand Up @@ -180,10 +191,10 @@ export const Utils = {
* Sort a route collection by object key
*/
sortRoutes(routes, order) {
const toReturn = {}
const toReturn = new Map
const sorted = Object.keys(routes).sort(Utils.createSpecificityComparator({ order: order }))
sorted.forEach((r, i) => {
toReturn[r] = routes[r]
toReturn.set(r, routes[r])
})
return toReturn
},
Expand Down Expand Up @@ -212,19 +223,19 @@ export const Utils = {
|| routeA === catchAllRoute
) {
return asc ? 1 : -1
// Also push index route down to end, but not past the default
}
// Also push index route down to end, but not past the default
else if (
routeB === defaultRoute
|| routeB === catchAllRoute
) {
return asc ? -1 : 1
// Also push index route down to end, but not past the default
}
// Also push index route down to end, but not past the default
else if (/^\/$/.test(routeA) && (routeB !== defaultRoute && routeB !== catchAllRoute)) {
return asc ? 1 : -1
// Otherwise, sort based on either depth or free variable priority
}
// Otherwise, sort based on either depth or free variable priority
else {
const slicedA = routeA.split('/') // .normalize('/' + routeA + '/').split('/').join('/')
const slicedB = routeB.split('/') // .normalize('/' + routeB + '/').split('/').join('/')
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-router",
"version": "1.1.1",
"version": "1.1.2",
"description": "Spool - Router for Fabrix",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down Expand Up @@ -49,7 +49,7 @@
"lodash": "^4.17.10"
},
"devDependencies": {
"@fabrix/fabrix": "^1.1.0",
"@fabrix/fabrix": "^1.1.1",
"@fabrix/lint": "^1.0.0-alpha.3",
"@types/lodash": "^4.14.109",
"@types/node": "~10.3.4",
Expand All @@ -64,7 +64,7 @@
"typescript": "~2.8.1"
},
"peerDependencies": {
"@fabrix/fabrix": "^1.1.0"
"@fabrix/fabrix": "^1.1.1"
},
"license": "MIT",
"bugs": {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module.exports = {
customPrefixer: {
prefix: '/prefix'
},
router: {
debug: true
},
routes: {
'/test/foo': {
'GET': 'TestController.foo'
Expand Down
12 changes: 9 additions & 3 deletions test/integration/lib/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ describe('lib.Util', () => {
}
})

assert.equal(route.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.GET.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.HEAD.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.POST.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.PUT.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.DELETE.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.CONNECT.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.OPTIONS.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.TRACE.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.PATCH.config.pre[0], global.app.policies.FooPolicy.bar)
})
it('should resolve the prerequisite handler (object) to the correct policy method', () => {
const { path, route} = lib.Utils.buildRoute(global.app, '/foo/bar', {
Expand All @@ -49,8 +57,6 @@ describe('lib.Util', () => {
]
}
})

assert.equal(route.config.pre[0], global.app.policies.FooPolicy.bar)
assert.equal(route.GET.config.pre[0], global.app.policies.FooPolicy.bar)
})
it('should resolve the prerequisite handler (string) to the correct policy method', () => {
Expand Down
16 changes: 8 additions & 8 deletions test/integration/spool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ describe('Router Spool', () => {
// assert(_.isFunction(routes[2].handler))
// assert(_.isPlainObject(routes[3].handler))

assert(global.app.routes['/test/foo1'])
assert(global.app.routes['/test/foo2'])
assert(global.app.routes['/prefix/test/custom/prefix'])
assert(global.app.routes.get('/test/foo1'))
assert(global.app.routes.get('/test/foo2'))
assert(global.app.routes.get('/prefix/test/custom/prefix'))

assert.equal(Object.keys(global.app.routes).length, 9)
assert.equal(global.app.routes.size, 9)
})
})

describe('route #config', () => {

it('tags could be an array', () => {
const routes = global.app.routes
const route = routes['/test/foo/tags']
assert(_.isObject(route.config))
assert(_.isArray(route.config.tags))
assert(_.includes(route.config.tags, 'test', 'other'))
const route = routes.get('/test/foo/tags')
assert(_.isObject(route.GET.config))
assert(_.isArray(route.GET.config.tags))
assert(_.includes(route.GET.config.tags, 'test', 'other'))
})
})
})
Loading

0 comments on commit 4ff5a5a

Please sign in to comment.