diff --git a/lib/TapestriesSpool.ts b/lib/TapestriesSpool.ts index ef21830..86254ec 100644 --- a/lib/TapestriesSpool.ts +++ b/lib/TapestriesSpool.ts @@ -66,10 +66,11 @@ export class TapestriesSpool extends Spool { const controllerTapestries = Util.getControllerTapestries(this.app) || [] const modelTapestries = this.modelTapestries ? Util.getModelTapestries(this.app) : [] const tapestryRoutes = union(controllerTapestries, modelTapestries) || [] + const configRoutes = Object.values(this.app.config.get('routes') || []) this.app.config.set('routes', [ ...tapestryRoutes, - ...(this.app.config.get('routes') || []) + ...configRoutes ]) } } diff --git a/lib/util.ts b/lib/util.ts index 70b4b2b..994cb93 100755 --- a/lib/util.ts +++ b/lib/util.ts @@ -1,27 +1,53 @@ import { FabrixApp } from '@fabrix/fabrix' import { join } from 'path' -import { get, map, flatten, omit } from 'lodash' +import { map, flatten, omit } from 'lodash' import { Routes } from './routes' export const Util = { + /** + * Get the controller method(s) + */ + getControllerMethods(app: FabrixApp) { + let configMethod = app.config.get('tapestries.controllers.method') || ['GET', 'POST'] + if (configMethod instanceof Object) { + configMethod = Object.values(configMethod) + } + return configMethod + }, + /** + * Get the Controllers to be Ignored by Tapestries + */ + getControllerIgnore(app: FabrixApp): string[] { + return Object.values(app.config.get('tapestries.controllers.ignore') || []) + }, + /** + * Get either the configured spool-tapestries prefix, or use the one set by spool-router if any. + */ + getPrefix(app: FabrixApp) { + let prefix = app.config.get('tapestries.prefix') + if (!prefix) { + prefix = app.config.get('router.prefix') || '' + } + return prefix.toString() + }, /** * Compile controller handlers into route objects */ getControllerTapestries (app: FabrixApp): {method: string, path: string, handler: string}[] { if (app.config.get('tapestries.controllers')) { + // Returns an object of available Controllers for Tapestries const controllers = omit( app.controllers || {}, - app.config.get('tapestries.controllers.ignore') || [] + Util.getControllerIgnore(app) ) - const prefix = (app.config.get('tapestries.prefix') || '').toString() + const prefix = Util.getPrefix(app) return flatten(map(controllers, (controller, controllerName: string) => { - return map(controller.methods, handlerName => { return { - method: app.config.get('tapestries.controllers.method') || ['GET', 'POST'], + method: Util.getControllerMethods(app), path: Util.getHandlerPath(app, prefix, controller.id, handlerName), handler: Util.getControllerHandler(controllerName, handlerName) } @@ -37,8 +63,8 @@ export const Util = { * Select the model tapestry routes that the configuration allows. */ getModelTapestries (app: FabrixApp): any[] { - const actionsConfig = app.config.get('tapestries.models.actions') || [] - const prefix = (app.config.get('tapestries.prefix') || '').toString() + const actionsConfig = app.config.get('tapestries.models.actions') || {} + const prefix = Util.getPrefix(app) return Routes .filter(route => { diff --git a/package-lock.json b/package-lock.json index b3ea726..6575f62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-tapestries", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -114,9 +114,9 @@ } }, "@fabrix/fabrix": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@fabrix/fabrix/-/fabrix-1.0.1.tgz", - "integrity": "sha512-MIwsYaj54P6+3subkdkLxVWxz1v1+0Px4m2q2zhtm0T3yiam0JGEEb5MnNjSCTSPxraXU2zCMHEjhkKBd32Ewg==", + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/@fabrix/fabrix/-/fabrix-1.0.8.tgz", + "integrity": "sha512-yV0lhaVcTtAYMLuql0YZbJVIPXbwvDsnlzELNteeds9UTxWdU8hZJwMdLIhk459JZdtHNe0Q1cYBp8ECddO2mQ==", "dev": true, "requires": { "lodash": "4.17.10", @@ -130,9 +130,9 @@ "dev": true }, "@fabrix/spool-router": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@fabrix/spool-router/-/spool-router-1.0.0.tgz", - "integrity": "sha512-PvpXScTVpvhAFvJXG9HjyMxnGa5t1E5xfoiFHCySk22oIORyzE04I8kuwav4+7w9h+vUdjvRDRLVmJkTY17Yqg==", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@fabrix/spool-router/-/spool-router-1.0.1.tgz", + "integrity": "sha512-jiNcRCNZRjRz+jyoNEIE5wEDbCA+8vX2pvq1gA48IUCg6L152eyNq2vdo1/zYmKgwvHRG8odvBgnqD/h28SHuw==", "dev": true, "requires": { "call": "5.0.1", @@ -417,9 +417,9 @@ } }, "isemail": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/isemail/-/isemail-3.1.2.tgz", - "integrity": "sha512-zfRhJn9rFSGhzU5tGZqepRSAj3+g6oTOHxMGGriWNJZzyLPUK8H7VHpqKntegnW8KLyGA9zwuNaCoopl40LTpg==", + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/isemail/-/isemail-3.1.3.tgz", + "integrity": "sha512-5xbsG5wYADIcB+mfLsd+nst1V/D+I7EU7LEZPo2GOIMu4JzfcRs5yQoypP4avA7QtUqgxYLKBYNv4IdzBmbhdw==", "dev": true, "requires": { "punycode": "2.1.1" @@ -453,7 +453,7 @@ "dev": true, "requires": { "hoek": "5.0.3", - "isemail": "3.1.2", + "isemail": "3.1.3", "topo": "3.0.0" } }, diff --git a/package.json b/package.json index 15689cd..56a8742 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-tapestries", - "version": "1.0.0", + "version": "1.0.1", "description": "Spool - Tapestries, Easy RESTful Services", "scripts": { "build": "tsc -p ./lib/tsconfig.release.json", @@ -49,8 +49,8 @@ "lodash": "^4.17.10" }, "devDependencies": { - "@fabrix/fabrix": "^1.0.1", - "@fabrix/spool-router": "^1.0.0", + "@fabrix/fabrix": "^1.0.8", + "@fabrix/spool-router": "^1.0.1", "@fabrix/lint": "^1.0.0-alpha.3", "@types/lodash": "^4.14.109", "@types/node": "~10.3.4", @@ -64,7 +64,7 @@ "typescript": "~2.8.1" }, "peerDependencies": { - "@fabrix/fabrix": "^1.0.1", + "@fabrix/fabrix": "^1.0.8", "@fabrix/spool-router": "^1.0.0" }, "license": "MIT", diff --git a/test/integration/trailpack.test.js b/test/integration/spool.test.js similarity index 90% rename from test/integration/trailpack.test.js rename to test/integration/spool.test.js index eeeca22..fb22cb7 100755 --- a/test/integration/trailpack.test.js +++ b/test/integration/spool.test.js @@ -5,7 +5,7 @@ describe('Router Spool', () => { describe('#configure', () => { it('should have a prefix', () => { - console.log(global.app.config.entries()) + // console.log(global.app.config.entries()) assert.equal(global.app.config.get('tapestries.prefix'), '/api/v1') }) }) @@ -37,10 +37,6 @@ describe('Router Spool', () => { r.method === 'GET' ) }) - - // console.log('configRoute', configRoute) - // console.log('app.routes', global.app.routes) - assert(_.isFunction(configRoute.config.pre[0])) }) })