Skip to content

Commit

Permalink
Merge pull request #1 from fabrix-app/v1.1
Browse files Browse the repository at this point in the history
V1.1
  • Loading branch information
scott-wyatt committed Jul 19, 2018
2 parents e1c4c37 + de4b4b3 commit 0084835
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 30 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ $ npm install @fabrix/spool-tapestries --save
## Configure

```js
// config/main.js
module.exports = {
// config/main.ts
export const main {
spools: [
// ... other spools
require('@fabrix/spool-tapestries').TapestriesSpool
Expand All @@ -36,8 +36,8 @@ module.exports = {
```

```js
// config/tapestries.js
module.exports = {
// config/tapestries.ts
export const tapestries = {
/**
* Generate routes for controller handlers.
* You can set controllers to true/false to enable/disable
Expand Down
4 changes: 4 additions & 0 deletions lib/TapestriesSpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Spool } from '@fabrix/fabrix/dist/common'
import { union } from 'lodash'

import { Utils } from './utils'
import { validateConfig } from './validator'

import * as config from './config/index'
import * as pkg from '../package.json'
Expand Down Expand Up @@ -34,6 +35,9 @@ export class TapestriesSpool extends Spool {
this.log.warn('spool-tapestries is installed, but TapestryController is not provided')
this.modelTapestries = false
}
return Promise.all([
validateConfig.validateConfig(this.app.config.get('tapestries'))
])
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/config/tapestries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const tapestries = {

prefix: '',
prefix: null,

controllers: false,

Expand Down
1 change: 1 addition & 0 deletions lib/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { tapestries } from './tapestries'
7 changes: 7 additions & 0 deletions lib/schemas/tapestries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as joi from 'joi'

export const tapestries = joi.object().keys({
prefix: joi.string().allow('', null),
controllers: joi.any(),
models: joi.any()
})
4 changes: 3 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const Utils = {
* Get the Controllers to be Ignored by Tapestries
*/
getControllerIgnore(app: FabrixApp): string[] {
return Object.values(app.config.get('tapestries.controllers.ignore') || [])
const configIgnore: string[] = Object.values(app.config.get('tapestries.controllers.ignore') || [])
const defaultIgnore = ['TapestryController']
return [...configIgnore, ...defaultIgnore]
},

/**
Expand Down
1 change: 1 addition & 0 deletions lib/validator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { validateConfig } from './validateConfig'
15 changes: 15 additions & 0 deletions lib/validator/validateConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as joi from 'joi'
import { tapestries } from '../schemas/tapestries'

export const validateConfig = {
validateConfig (config) {
return new Promise((resolve, reject) => {
joi.validate(config, tapestries, (err, value) => {
if (err) {
return reject(new TypeError('config.tapestries: ' + err))
}
return resolve(value)
})
})
}
}
14 changes: 7 additions & 7 deletions package-lock.json

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

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-tapestries",
"version": "1.1.0",
"version": "1.1.2",
"description": "Spool - Tapestries, Easy RESTful Services",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down Expand Up @@ -49,8 +49,8 @@
"lodash": "^4.17.10"
},
"devDependencies": {
"@fabrix/fabrix": "^1.1.0",
"@fabrix/spool-router": "^1.1.0",
"@fabrix/fabrix": "^1.1.1",
"@fabrix/spool-router": "^1.1.2",
"@fabrix/lint": "^1.0.0-alpha.3",
"@types/lodash": "^4.14.109",
"@types/node": "~10.3.4",
Expand All @@ -64,8 +64,8 @@
"typescript": "~2.8.1"
},
"peerDependencies": {
"@fabrix/fabrix": "^1.1.0",
"@fabrix/spool-router": "^1.1.0"
"@fabrix/fabrix": "^1.1.1",
"@fabrix/spool-router": "^1.1.2"
},
"license": "MIT",
"bugs": {
Expand Down
4 changes: 1 addition & 3 deletions test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ module.exports = {
prefix: '/api/v1',
controllers: {
method: '*',
ignore: [
'TapestryController'
]
ignore: []
}
},
main: {
Expand Down
18 changes: 9 additions & 9 deletions test/integration/spool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ describe('Router Spool', () => {
it('should include tapestry routes (Controllers) in app.routes', () => {
const routes = global.app.routes

assert.equal(Object.keys(routes).length, 5)
assert(routes[global.app.config.get('tapestries.prefix') + '/test/testHandler'])
assert.equal(routes.size, 5)
assert(routes.get(global.app.config.get('tapestries.prefix') + '/test/testHandler'))
})
it('should include tapestry routes (Models) in app.routes', () => {
const routes = global.app.routes

assert.equal(Object.keys(routes).length, 5)
assert(routes[global.app.config.get('tapestries.prefix') + '/{model}'])
assert.equal(routes.size, 5)
assert(routes.get(global.app.config.get('tapestries.prefix') + '/{model}'))
})
it('should bind route handler to controller method', () => {
const routes = global.app.routes
assert(_.isFunction(routes[global.app.config.get('tapestries.prefix') + '/{model}/{id?}'].GET))
assert(_.isFunction(routes[global.app.config.get('tapestries.prefix') + '/{model}/{id?}'].PUT))
assert(_.isFunction(routes[global.app.config.get('tapestries.prefix') + '/{model}/{id?}'].PATCH))
assert(_.isFunction(routes.get(global.app.config.get('tapestries.prefix') + '/{model}/{id?}').GET.handler))
assert(_.isFunction(routes.get(global.app.config.get('tapestries.prefix') + '/{model}/{id?}').PUT.handler))
assert(_.isFunction(routes.get(global.app.config.get('tapestries.prefix') + '/{model}/{id?}').PATCH.handler))
})
it('should attach prerequisite methods', () => {
const configRoute = global.app.routes[global.app.config.get('tapestries.prefix') + '/test/testHandler']
assert(_.isFunction(configRoute.config.pre[0]))
const configRoute = global.app.routes.get(global.app.config.get('tapestries.prefix') + '/test/testHandler')
assert(_.isFunction(configRoute.GET.config.pre[0]))
})
})
})

0 comments on commit 0084835

Please sign in to comment.