Last active
February 1, 2018 18:27
-
-
Save marcus-sa/6d446ea3ac85f429ab8bc8fc394a7a6a to your computer and use it in GitHub Desktop.
Feathers Provider for AdonisJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const _ = require('lodash') | |
const path = require('path') | |
const { ioc } = require('@adonisjs/fold') | |
const feathers = require('@feathersjs/feathers') | |
module.exports = class Feathers { | |
constructor(Config, Env, Helpers) { | |
//this._config = Config.get('rest', { port: 8) | |
this._env = Env.get('NODE_ENV') | |
this._port = Env.get('PORT', 3000) | |
this._helpers = Helpers | |
this._start = require(path.join(Helpers.basePath(), 'start', 'feathers.js')) | |
this._serviceBreakpoint = null | |
this._services = {} | |
this._servicesPath = 'App/Services' | |
this._hooksPath = 'App/Services/Hooks' | |
//this.express = express | |
this.app = feathers()//express(Feathers) | |
} | |
_createClosure(path, name) { | |
return ioc.use(this._helpers.makeNameSpace(path), name) | |
} | |
_createHooks(name, closure) { | |
this._services[this._serviceBreakpoint].hooks[name] = ( | |
typeof closure === 'string' | |
? this._createClosure(this._hooksPath, closure) | |
: closure | |
) | |
this._serviceBreakpoint = null | |
} | |
_validateServiceBreakpoint() { | |
if (!this._serviceBreakpoint) { | |
throw new Error('No service breakpoint') | |
} | |
} | |
_start(adonis) { | |
Object.keys(this._services).forEach(serviceName => { | |
const service = this._services[serviceName] | |
this.app.use(serviceName, service.closure) | |
if (service.hooks) { | |
this.app.service(serviceName).hooks(service.hooks) | |
} | |
}) | |
this.app.listen(this._port) | |
} | |
service(name, closure) { | |
if (!closure) { | |
if (!this._services[name]) { | |
throw new Error('Service doesnt exist yet') | |
} | |
return this.app.service(name) | |
} | |
this._serviceBreakpoint = name | |
if (typeof closure === 'string') { | |
closure = this._createClosure(this._servicesPath, closure) | |
} | |
this._services[name] = { | |
closure, | |
hooks: {} | |
} | |
return this | |
} | |
use(module) { | |
this.app.use(module) | |
} | |
configure(module) { | |
this.app.configure(module) | |
} | |
hooksBefore(closure) { | |
this._validateServiceBreakpoint() | |
this._createHooks('before', closure) | |
return this | |
} | |
hooksAfter(closure) { | |
this._validateServiceBreakpoint() | |
this._createHooks('after', closure) | |
return this | |
} | |
hooks(closure) { | |
this._validateServiceBreakpoint() | |
if (typeof closure === 'string') { | |
closure = this._createClosure(this._hooksPath, closure) | |
} | |
this._services[this._serviceBreakpoint].hooks = closure | |
this._serviceBreakpoint = null | |
return this | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const { ServiceProvider } = require('@adonisjs/fold') | |
const NAMESPACE = 'Adonis/Addons/Feathers' | |
class FeathersProvider extends ServiceProvider { | |
* register() { | |
this.app.singleton(NAMESPACE, (app) => { | |
const Feathers = require('./Feathers') | |
const Config = app.use('Config') | |
const Env = app.use('Env') | |
const Helpers = app.use('Helpers') | |
return new Feathers(Config, Env, Helpers) | |
}) | |
this.app.alias(NAMESPACE, 'Feathers') | |
} | |
* boot() { | |
this.app.use(NAMESPACE)._start(this.app) | |
} | |
} | |
module.exports = FeathersProvider |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment