-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpaloma.js
106 lines (86 loc) · 2.63 KB
/
paloma.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const assert = require('assert')
const path = require('path')
const Bottle = require('bottlejs')
const fnArgs = require('fn-args')
const Koa = require('koa')
const requireDirectory = require('require-directory')
const AJS = require('another-json-schema')
const router = require('./router')
module.exports = class Paloma extends Koa {
constructor () {
super()
this._bottle = new Bottle()
this._controllers = Object.create(null)
}
load (dir) {
if (path.isAbsolute(dir)) {
requireDirectory(module, dir)
} else {
requireDirectory(module, path.join(path.dirname(module.parent.filename), dir))
}
return this
}
route (route) {
assert(typeof route === 'object', '`route` must be a object')
assert(typeof route.method === 'string', '`method` must be a string, like: \'GET\'')
assert(typeof route.path === 'string', '`path` must be a string, like: \'/users/:name\'')
assert(typeof route.controller === 'string' || typeof route.controller === 'function' || Array.isArray(route.controller), '`controller` must be a string or function or array')
this.use(router.call(this, route))
return this
}
controller (name, fn) {
assert(typeof name === 'string', 'controller name must be string.')
if (!fn) {
if (!this._controllers[name]) {
throw new TypeError('controller ' + name + ' is not defined')
}
return this._controllers[name]
}
const _args = fnArgs(fn).slice(2)
this._controllers[name] = (ctx, next) => {
return fn.apply(null, [ctx, next].concat(_args.map(arg => this._bottle.container[arg])))
}
return this
}
service (name, fn) {
assert(typeof name === 'string', 'service name must be string.')
if (!fn) {
const _service = this._bottle.container[name]
if (!_service) {
throw new TypeError('service ' + name + ' is not defined')
}
return _service
}
const _args = fnArgs(fn)
this._bottle.service.apply(this._bottle, [name, fn].concat(_args))
return this
}
factory () {
this._bottle.factory.apply(this._bottle, arguments)
return this
}
provider () {
this._bottle.provider.apply(this._bottle, arguments)
return this
}
constant () {
this._bottle.constant.apply(this._bottle, arguments)
return this
}
value () {
this._bottle.value.apply(this._bottle, arguments)
return this
}
decorator () {
this._bottle.decorator.apply(this._bottle, arguments)
return this
}
middlewares () {
this._bottle.middleware.apply(this._bottle, arguments)
return this
}
get Types () {
return AJS.Types
}
}
module.exports.Types = AJS.Types