-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
67 lines (60 loc) · 2.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const _ = require('lodash')
const Trailpack = require('trailpack')
const lib = require('./lib')
module.exports = class FootprintsTrailpack extends Trailpack {
/**
* In order to activate footprints, a FootprintController and
* FootprintService must be provided by other trailpacks. Ensure that
* these are available and appear valid.
*/
validate () {
this.modelFootprints = true
if (!this.app.api.services.FootprintService) {
this.log.warn('trailpack-footprints is installed, but FootprintService is not provided')
this.modelFootprints = false
}
if (!this.app.api.controllers.FootprintController) {
this.log.warn('trailpack-footprints is installed, but FootprintController is not provided')
this.modelFootprints = false
}
}
/**
* Compile routes for controller handlers, and filter model actions based
* on the footprints config.
*
* 1. ETL controller handlers into the standard (hapijs-based) route format.
* For example, the controller handler ExampleController.exampleHandler will
* generate a hapijs route object of the following form:
* {
* method: '*',
* path: '/example/exampleHandler',
* handler: 'ExampleController.exampleHandler'
* }
*
* 2. Create CRUD Route definition which maps to api.controllers.FootprintController
*
* Operation | Method | Path | Footprint Handler
* ----------+--------+----------------------------+-------------------
* Create | POST | /model | FootprintController.create
* Create | POST | /model/{id}/{child} | FootprintController.createAssociation
* Read | GET | /model/{id?} | FootprintController.find
* Read | GET | /model/{id}/{child}/{id?} | FootprintController.findAssociation
* Update | PUT | /model/{id?} | FootprintController.update
* Update | PUT | /model/{id}/{child}/{id?} | FootprintController.updateAssociation
* Delete | DELETE | /model/{id?} | FootprintController.destroy
* Delete | DELETE | /model/{id}/{child}/{id?} | FootprintController.destroyAssociation
*/
configure () {
const controllerFootprints = lib.Util.getControllerFootprints(this.app)
const modelFootprints = this.modelFootprints ? lib.Util.getModelFootprints(this.app) : []
const footprintRoutes = _.union(controllerFootprints, modelFootprints)
this.app.config.set('routes', [ ...footprintRoutes, ...this.app.config.get('routes') ])
}
constructor (app) {
super(app, {
config: require('./config'),
api: require('./api'),
pkg: require('./package')
})
}
}