Skip to content

Commit

Permalink
Merge pull request #19 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[fix] bad pre
  • Loading branch information
scott-wyatt authored Apr 10, 2019
2 parents 104e661 + d531678 commit 1954064
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
70 changes: 56 additions & 14 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,49 @@ export const Utils = {
*/
getControllerPolicy(app: FabrixApp, handler, routeMethod, pre = [ ]): FabrixPolicy[] {
const controller = Utils.getControllerFromHandler(handler)
const method = Utils.getControllerMethodFromHandler(handler)

if (app.config.get('policies.*.*')) {
// console.log(handler, routeMethod,
// 'policies.*.*',
// app.config.get('policies.*.*')
// )
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get('policies.*.*'))])]
}
if (app.config.get(`policies.*.${routeMethod}`)) {
// console.log(handler, routeMethod,
// `policies.*.${routeMethod}`,
// app.config.get(`policies.*.${routeMethod}`)
// )
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.*.${routeMethod}`))])]
}
if (handler && controller && app.config.get(`policies.${controller}.*.*`)) {
if (controller && app.config.get(`policies.${controller}.*.*`)) {
// console.log(handler, routeMethod,
// `policies.${controller}.*.*`,
// app.config.get(`policies.${controller}.*.*`)
// )
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.${controller}.*.*`))])]
}
if (handler && controller && app.config.get(`policies.${controller}.*.${routeMethod}`)) {
if (controller && app.config.get(`policies.${controller}.*.${routeMethod}`)) {
// console.log(handler, routeMethod,
// `policies.${controller}.*.${routeMethod}`,
// app.config.get(`policies.${controller}.*.${routeMethod}`)
// )
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.${controller}.*.${routeMethod}`))])]
}
if (handler && app.config.get(`policies.${handler}.${routeMethod}`)) {
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.${handler}.${routeMethod}`))])]
if (controller && method && app.config.get(`policies.${controller}.${method}.*`)) {
// console.log(handler, routeMethod
// `policies.${controller}.${method}.*`,
// app.config.get(`policies.${controller}.${method}.*`)
// )
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.${controller}.${method}.*`))])]
}
if (controller && method && app.config.get(`policies.${controller}.${method}.${routeMethod}`)) {
// console.log(handler,
// `policies.${controller}.${method}.${routeMethod}`,
// app.config.get(`policies.${controller}.${method}.${routeMethod}`)
// )
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.${controller}.${method}.${routeMethod}`))])]
}
return pre
},
Expand All @@ -295,7 +323,9 @@ export const Utils = {
handler = Utils.getPolicyFromString(app, pre)
}
else if (pre && Array.isArray(pre)) {
handler = pre.map(p => Utils.getPolicyFromString(app, p)).filter(p => p)
handler = pre
.map(p => Utils.getPolicyFromString(app, p))
.filter(p => p)
}
else if (pre && typeof pre.method === 'string') {
handler = Utils.getPolicyFromString(app, pre.method)
Expand Down Expand Up @@ -327,6 +357,10 @@ export const Utils = {
return isString(handler) ? handler.split('.')[0] : handler
},

getControllerMethodFromHandler(handler): string {
return isString(handler) ? handler.split('.')[1] : handler
},

/**
* Get handler method from a controller.method string path
*/
Expand All @@ -341,28 +375,36 @@ export const Utils = {

Utils.methods.forEach(method => {
if (route[method]) {
route.config = route.config || { }
route.config.pre = Utils.getControllerPolicy(app, null, method, route.config.pre)
console.log('\n')
// Clean copy of config
const config = Object.assign({}, route.config)
// Clean copy of config.pre
config.pre = (route.config.pre || []).slice()
// Get Policies
config.pre = Utils.getControllerPolicy(app, null, method, config.pre)

if (typeof route[method] === 'string') {
route.config.pre = Utils.getControllerPolicy(app, route[method], method, route.config.pre)
return route[method] = {
handler: Utils.getControllerFromString(app, route[method]),
config: route.config
const handler = route[method]
route[method] = {
handler: Utils.getControllerFromString(app, handler),
config: config
}
route[method].config.pre = Utils.getControllerPolicy(app, handler, method, route[method].config.pre)
return route[method]
}
else if (route[method] instanceof Object && route[method].hasOwnProperty('handler')) {
route[method].config = route[method].config || route.config
route[method].config.pre = route[method].config.pre || route.config.pre
route[method].config = route[method].config || config
route[method].config.pre = route[method].config.pre || config.pre

if (typeof route[method].handler === 'string') {
route.config.pre = Utils.getControllerPolicy(app, route[method].handler, method, route.config.pre)
route.config.pre = Utils.getControllerPolicy(app, route[method].handler, method, config.pre)
return route[method] = {
...route[method],
handler: Utils.getControllerFromString(app, route[method].handler)
}
}
else {
// route.config.pre = Utils.getControllerPolicy(app, route[method].handler, method, route.config.pre)
return route[method] = {
...route[method],
handler: route[method].handler
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-router",
"version": "1.6.0",
"version": "1.6.1",
"description": "Spool - Router for Fabrix",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down
14 changes: 11 additions & 3 deletions test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ module.exports = {
GetPolicy: class GetPolicy extends Policy {
foo () { }
},
PostPolicy: class PostPolicy extends Policy {
post () { }
},
FooGetPolicy: class FooGetPolicy extends Policy {
foo () { }
},
FooPostPolicy: class FooPostPolicy extends Policy {
post () { }
},
FooWildCardPolicy: class FooWildCardPolicy extends Policy {
foo () { }
}
Expand All @@ -54,20 +60,22 @@ module.exports = {
policies: {
'*': {
'*': ['GlobalPolicy.foo'],
'GET': ['GetPolicy.foo']
'GET': ['GetPolicy.foo'],
'POST': ['PostPolicy.post'],
},
'TestController': {
'*': {
'*': ['FooWildCardPolicy.foo']
},
'foo': {
'GET': ['FooGetPolicy.foo']
'GET': ['FooGetPolicy.foo'],
'POST': ['FooPostPolicy.post']
}
}
},
routes: {
'/test/foo': {
'GET': 'TestController.foo'
'GET': 'TestController.foo',
},
'/test/{foo}': {
'GET': 'TestController.foo'
Expand Down
14 changes: 12 additions & 2 deletions test/unit/lib/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,24 @@ describe('lib.Util', () => {
assert.equal(route.GET.config.pre[1], global.app.policies.GetPolicy.foo)
})

it('should inherit the global policy and the global GET policy and the Controller Specific Get Policy', () => {
it('should inherit the global policy and the global GET/POST policy and the Controller Specific Get/POST Policy', () => {
const [{ path, route}] = lib.Utils.buildRoute(global.app, '/foo/bar', {
'GET': 'TestController.foo'
'GET': 'TestController.foo',
'POST': 'TestController.foo'
})
console.log('BRK 1', route.GET.config)
assert.equal(route.GET.config.pre[0], global.app.policies.GlobalPolicy.foo)
assert.equal(route.GET.config.pre[1], global.app.policies.GetPolicy.foo)
assert.equal(route.GET.config.pre[2], global.app.policies.FooWildCardPolicy.foo)
assert.equal(route.GET.config.pre[3], global.app.policies.FooGetPolicy.foo)
assert.equal(route.GET.config.pre.length, 4)

console.log('BRK 2', route.POST.config)
assert.equal(route.POST.config.pre[0], global.app.policies.GlobalPolicy.foo)
assert.equal(route.POST.config.pre[1], global.app.policies.PostPolicy.post)
assert.equal(route.POST.config.pre[2], global.app.policies.FooWildCardPolicy.foo)
assert.equal(route.POST.config.pre[3], global.app.policies.FooPostPolicy.post)
assert.equal(route.POST.config.pre.length, 4)
})
})
})

0 comments on commit 1954064

Please sign in to comment.