-
Notifications
You must be signed in to change notification settings - Fork 133
/
chainer.js
47 lines (40 loc) · 1.36 KB
/
chainer.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
const plus = require('./plus')
// Daisy-Chainer
// ===============================
//
// Generates the functions so `octo.repos(...).issues.comments.fetch()` works.
// Constructs a URL for the verb methods (like `.fetch` and `.create`).
module.exports = class Chainer {
constructor (_verbMethods) {
this._verbMethods = _verbMethods
}
chain (path, name, contextTree, fn) {
if (typeof fn === 'undefined' || fn === null) {
fn = (...args) => {
if (!args.length) { throw new Error('BUG! must be called with at least one argument') }
let separator = '/'
// Special-case compare because its args turn into '...' instead of the usual '/'
if (name === 'compare') {
separator = '...'
}
return this.chain(`${path}/${args.join(separator)}`, name, contextTree)
}
}
this._verbMethods.injectVerbMethods(path, fn)
if (typeof fn === 'function' || typeof fn === 'object') {
for (name in contextTree || {}) {
(name => {
// Delete the key if it already exists
delete fn[plus.camelize(name)]
return Object.defineProperty(fn, plus.camelize(name), {
configurable: true,
enumerable: true,
get: () => this.chain(`${path}/${name}`, name, contextTree[name])
}
)
})(name)
}
}
return fn
}
}