Skip to content

Commit 4ff5a5a

Browse files
authored
Merge pull request #5 from fabrix-app/v1.1
[chore] cleanup
2 parents b882115 + bace812 commit 4ff5a5a

File tree

12 files changed

+195
-52
lines changed

12 files changed

+195
-52
lines changed

lib/RouterSpool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as pkg from '../package.json'
1818
* @see https://github.com/fabrix-app/spool-hapi
1919
*/
2020
export class RouterSpool extends SystemSpool {
21-
private _routes = {}
21+
private _routes: Map<string, {[key: string]: any}> // = new Map
2222

2323
constructor (app) {
2424
super(app, {
@@ -63,7 +63,7 @@ export class RouterSpool extends SystemSpool {
6363
/**
6464
* Get's the routes from spool-router
6565
*/
66-
get routes(): {[key: string]: any} {
66+
get routes(): Map<string, {[key: string]: any}> {
6767
return this._routes
6868
}
6969

lib/config/router.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const router = {
2-
prefix: '',
3-
sortOrder: 'asc'
2+
prefix: null,
3+
sortOrder: 'asc',
4+
debug: false
45
}

lib/interfaces/IRoute.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface IRoute {
2-
[key: string]: { [key: string]: any },
2+
[key: string]: any,
3+
_orgPath: string,
34
config?: {
45
pre?: any,
56
[key: string]: { [key: string]: any }

lib/schemas/router.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import * as joi from 'joi'
22

33
export const routerSchema = joi.object().keys({
44
sortOrder: joi.string().allow('asc', 'desc').required(),
5-
prefix: joi.string().allow('').required()
5+
prefix: joi.string().allow('', null).required(),
6+
debug: joi.any()
67
})

lib/utils.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FabrixApp } from '@fabrix/fabrix'
2-
import { get } from 'lodash'
2+
import { get, omit } from 'lodash'
33
import { Router } from 'call'
44
import { IRoute } from './interfaces/IRoute'
55

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

27-
path = Utils.getPathFromRoute(app, path, route)
28-
Utils.getHandlerFromString(app, route)
27+
if (app.config.get('router.debug')) {
28+
orgRoute._orgPath = path
29+
}
30+
31+
path = Utils.getPathFromRoute(app, path, orgRoute)
32+
33+
if (app.config.get('router.debug')) {
34+
orgRoute._newPath = path
35+
}
2936

30-
route.config.pre = route.config.pre
37+
Utils.getHandlerFromString(app, orgRoute)
38+
39+
orgRoute.config.pre = orgRoute.config.pre
3140
.map(pre => Utils.getHandlerFromPrerequisite(app, pre))
3241
.filter(handler => !!handler)
3342

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

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

42-
routeHandlers.forEach(method => {
43-
if (route[method]) {
44-
route[method].config = route[method].config || route.config
45-
route[method].config.pre = route[method].config.pre || route.config.pre
46-
route[method].config.pre = route[method].config.pre
51+
orgRouteHandlers.forEach(method => {
52+
if (orgRoute[method]) {
53+
orgRoute[method].config = orgRoute[method].config || orgRoute.config
54+
orgRoute[method].config.pre = orgRoute[method].config.pre || orgRoute.config.pre
55+
orgRoute[method].config.pre = orgRoute[method].config.pre
4756
.map(pre => Utils.getHandlerFromPrerequisite(app, pre))
4857
.filter(handler => !!handler)
4958
}
5059
})
5160

61+
const route = omit(orgRoute, 'config')
62+
5263
return { path, route }
5364
},
5465

@@ -180,10 +191,10 @@ export const Utils = {
180191
* Sort a route collection by object key
181192
*/
182193
sortRoutes(routes, order) {
183-
const toReturn = {}
194+
const toReturn = new Map
184195
const sorted = Object.keys(routes).sort(Utils.createSpecificityComparator({ order: order }))
185196
sorted.forEach((r, i) => {
186-
toReturn[r] = routes[r]
197+
toReturn.set(r, routes[r])
187198
})
188199
return toReturn
189200
},
@@ -212,19 +223,19 @@ export const Utils = {
212223
|| routeA === catchAllRoute
213224
) {
214225
return asc ? 1 : -1
215-
// Also push index route down to end, but not past the default
216226
}
227+
// Also push index route down to end, but not past the default
217228
else if (
218229
routeB === defaultRoute
219230
|| routeB === catchAllRoute
220231
) {
221232
return asc ? -1 : 1
222-
// Also push index route down to end, but not past the default
223233
}
234+
// Also push index route down to end, but not past the default
224235
else if (/^\/$/.test(routeA) && (routeB !== defaultRoute && routeB !== catchAllRoute)) {
225236
return asc ? 1 : -1
226-
// Otherwise, sort based on either depth or free variable priority
227237
}
238+
// Otherwise, sort based on either depth or free variable priority
228239
else {
229240
const slicedA = routeA.split('/') // .normalize('/' + routeA + '/').split('/').join('/')
230241
const slicedB = routeB.split('/') // .normalize('/' + routeB + '/').split('/').join('/')

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fabrix/spool-router",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Spool - Router for Fabrix",
55
"scripts": {
66
"build": "tsc -p ./lib/tsconfig.release.json",
@@ -49,7 +49,7 @@
4949
"lodash": "^4.17.10"
5050
},
5151
"devDependencies": {
52-
"@fabrix/fabrix": "^1.1.0",
52+
"@fabrix/fabrix": "^1.1.1",
5353
"@fabrix/lint": "^1.0.0-alpha.3",
5454
"@types/lodash": "^4.14.109",
5555
"@types/node": "~10.3.4",
@@ -64,7 +64,7 @@
6464
"typescript": "~2.8.1"
6565
},
6666
"peerDependencies": {
67-
"@fabrix/fabrix": "^1.1.0"
67+
"@fabrix/fabrix": "^1.1.1"
6868
},
6969
"license": "MIT",
7070
"bugs": {

test/fixtures/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ module.exports = {
3434
customPrefixer: {
3535
prefix: '/prefix'
3636
},
37+
router: {
38+
debug: true
39+
},
3740
routes: {
3841
'/test/foo': {
3942
'GET': 'TestController.foo'

test/integration/lib/util.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ describe('lib.Util', () => {
3636
}
3737
})
3838

39-
assert.equal(route.config.pre[0], global.app.policies.FooPolicy.bar)
39+
assert.equal(route.GET.config.pre[0], global.app.policies.FooPolicy.bar)
40+
assert.equal(route.HEAD.config.pre[0], global.app.policies.FooPolicy.bar)
41+
assert.equal(route.POST.config.pre[0], global.app.policies.FooPolicy.bar)
42+
assert.equal(route.PUT.config.pre[0], global.app.policies.FooPolicy.bar)
43+
assert.equal(route.DELETE.config.pre[0], global.app.policies.FooPolicy.bar)
44+
assert.equal(route.CONNECT.config.pre[0], global.app.policies.FooPolicy.bar)
45+
assert.equal(route.OPTIONS.config.pre[0], global.app.policies.FooPolicy.bar)
46+
assert.equal(route.TRACE.config.pre[0], global.app.policies.FooPolicy.bar)
47+
assert.equal(route.PATCH.config.pre[0], global.app.policies.FooPolicy.bar)
4048
})
4149
it('should resolve the prerequisite handler (object) to the correct policy method', () => {
4250
const { path, route} = lib.Utils.buildRoute(global.app, '/foo/bar', {
@@ -49,8 +57,6 @@ describe('lib.Util', () => {
4957
]
5058
}
5159
})
52-
53-
assert.equal(route.config.pre[0], global.app.policies.FooPolicy.bar)
5460
assert.equal(route.GET.config.pre[0], global.app.policies.FooPolicy.bar)
5561
})
5662
it('should resolve the prerequisite handler (string) to the correct policy method', () => {

test/integration/spool.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ describe('Router Spool', () => {
1515
// assert(_.isFunction(routes[2].handler))
1616
// assert(_.isPlainObject(routes[3].handler))
1717

18-
assert(global.app.routes['/test/foo1'])
19-
assert(global.app.routes['/test/foo2'])
20-
assert(global.app.routes['/prefix/test/custom/prefix'])
18+
assert(global.app.routes.get('/test/foo1'))
19+
assert(global.app.routes.get('/test/foo2'))
20+
assert(global.app.routes.get('/prefix/test/custom/prefix'))
2121

22-
assert.equal(Object.keys(global.app.routes).length, 9)
22+
assert.equal(global.app.routes.size, 9)
2323
})
2424
})
2525

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

2828
it('tags could be an array', () => {
2929
const routes = global.app.routes
30-
const route = routes['/test/foo/tags']
31-
assert(_.isObject(route.config))
32-
assert(_.isArray(route.config.tags))
33-
assert(_.includes(route.config.tags, 'test', 'other'))
30+
const route = routes.get('/test/foo/tags')
31+
assert(_.isObject(route.GET.config))
32+
assert(_.isArray(route.GET.config.tags))
33+
assert(_.includes(route.GET.config.tags, 'test', 'other'))
3434
})
3535
})
3636
})

0 commit comments

Comments
 (0)