Commit 7b4bc843 authored by Jarrod's avatar Jarrod 💬

Allow route-specific middleware config in userland `config/routes.js`

parent cd77118d
Pipeline #1328 passed with stages
in 49 seconds
......@@ -38,29 +38,49 @@ module.exports = class SailsPluggable {
}
childLoaded (hook) {
if (Object.values(this.subHooks).filter(loaded => loaded).length === Object.values(this.subHooks).length) {
// Allow hook models to extend other models
_.each(sails.config.orm.moduleDefinitions.models, (def, extender) => {
if (def.extend) {
_.each(def.extend, (attributes, name) => {
sails.log.info(`Ahoy: Extending ${name}`, attributes)
const model = sails.config.orm.moduleDefinitions.models[name] // || sails.models[name]
if (!model || !model.attributes) {
sails.log.error(`Unable to extend model ${name} from ${extender}`)
}
_.each(attributes, (opt, field) => {
sails.config.orm.moduleDefinitions.models[name].attributes[field] = opt
})
const allChildrenLoaded = Object.values(this.subHooks).filter(loaded => loaded).length === Object.values(this.subHooks).length
if (!allChildrenLoaded) {
return false
}
// Allow hook models to extend other models
_.each(sails.config.orm.moduleDefinitions.models, (def, extender) => {
if (def.extend) {
_.each(def.extend, (attributes, name) => {
sails.log.info(`Ahoy: Extending ${name}`, attributes)
const model = sails.config.orm.moduleDefinitions.models[name] // || sails.models[name]
if (!model || !model.attributes) {
sails.log.error(`Unable to extend model ${name} from ${extender}`)
}
_.each(attributes, (opt, field) => {
sails.config.orm.moduleDefinitions.models[name].attributes[field] = opt
})
})
}
})
// Reload the Sails ORM
sails.hooks.orm.reload((err) => {
if (err) { sails.log.error(`Unable to reload sails ORM`, err.stack) }
sails.emit('ahoy:ready')
})
// Retrofit our route-level middlewares
sails.after('router:after', () => {
_.each(sails.config.routes, (def, path) => {
if (!def.action) { return }
if (def.auth || def.permission) {
sails.registerActionMiddleware(permissionsMiddleware, key)
}
})
// Reload the Sails ORM
sails.hooks.orm.reload((err) => {
if (err) { sails.log.error(`Unable to reload sails ORM`, err.stack) }
sails.emit('ahoy:ready')
if (def.cache) {
sails.log.verbose('Initializing cache middleare for ', path)
sails.registerActionMiddleware(cacheMiddleware, def.action)
}
})
}
sails.router.flush()
})
}
async registerActions () {
......@@ -73,14 +93,6 @@ module.exports = class SailsPluggable {
this.log(` - Registering action ${key}`)
sails.registerAction(require(path.join(this.opt.dir, `actions`, config.action)), key)
if (config.auth || config.permission) {
sails.registerActionMiddleware(permissionsMiddleware, key)
}
if (config.cache) {
sails.registerActionMiddleware(cacheMiddleware, key)
}
if (this.opt.apiBase) {
reqPath = reqPath.replace(/\//, `/${this.opt.apiBase}/`)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment