-
Notifications
You must be signed in to change notification settings - Fork 24
/
purest.js
73 lines (61 loc) · 1.96 KB
/
purest.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
var extend = require('@simov/deep-extend')
var compose = require('./lib/client')
var def = require('./config/methods')
var transform = {
endpoint: require('./lib/endpoint'),
alias: require('./lib/alias'),
method: require('./lib/method'),
url: require('./lib/url'),
auth: require('./lib/auth'),
}
module.exports = function purest (ctor = {}) {
ctor.config = ctor.config || require('./config/providers')
var client = (arg) =>
arg === undefined ? client :
typeof arg === 'string' ? client.endpoint(arg) :
compose(
(req) => transform.endpoint(ctor, req),
transform.alias,
transform.method,
transform.url,
transform.auth,
(req) =>
req.buffer ? compose.buffer(req) :
req.stream ? compose.stream(req) :
compose.client(req),
)(arg)
// same in lib/endpoint.js - probably move it here ..
var methods = Object.assign(
{}, def.methods, def.http, def.url, def.purest, ctor.methods || {})
var exec = ['request', 'buffer', 'stream']
.concat(methods.request)
.concat(methods.buffer)
.concat(methods.stream)
var wrap = (name) => (value, ...rest) => {
if (exec.includes(name)) {
var main = ['request', 'buffer', 'stream']
if (main.includes(name)) {
client._options[name] = true
}
else {
var index = main.findIndex((key) => methods[key].includes(key))
client._options[main[index]] = true
}
var options = extend({}, client._options, value || {})
client._options = {}
return client(options)
}
else if (['auth'].concat(methods.auth).includes(name)) {
client._options.auth = [].concat(value).concat(rest)
return client
}
else {
client._options[name] = value
return client
}
}
client._options = {}
var all = Object.keys(methods).concat(Object.values(methods).reduce((all, aliases) => all.concat(aliases)))
all.forEach((name) => client[name] = wrap(name))
return client
}