-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
187 lines (174 loc) · 4.58 KB
/
index.js
File metadata and controls
187 lines (174 loc) · 4.58 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict';
var debug = require('debug')('bed');
var methods = require('methods');
var request = require('superagent');
module.exports = Bed;
function Bed() {
this.baseUrl = '';
this.headers = {};
this.qs = {};
}
Bed.prototype.base = function(url) {
this.baseUrl = url;
return this;
};
Bed.prototype.auth = function(type, token) {
if (type === 'basic' && arguments.length === 3) {
token = new Buffer([arguments[1], arguments[2]].join(':'), 'utf8').toString('base64');
}
return this.set('Authorization', [type, token].join(' '));
};
Bed.prototype.type = function(type) {
return this.set('Content-Type', type);
};
Bed.prototype.accept = function(accept) {
return this.set('Accept', accept);
};
Bed.prototype.set = addTo('headers');
Bed.prototype.query = addTo('qs');
methods.forEach(function(method) {
Bed.prototype[method] = function(path) {
return new Builder(this, method, path);
};
});
function Builder(bed, method, path) {
this.bed = bed;
this.path = new Path(path);
this.method = method;
this.qs = {};
this.accept = null;
this.headers = {};
this.formatter = function(res) { return res; };
}
Builder.prototype.type = function(type) {
return this.set('Content-Type', type);
};
Builder.prototype.accept = function(accept) {
return this.set('Accept', accept);
};
Builder.prototype.set = addTo('headers');
Builder.prototype.query = addTo('qs');
Builder.prototype.format = function(fn) {
this.formatter = fn;
return this;
};
Builder.prototype.body = function(body) {
this.withBody = body;
return this;
};
Builder.prototype.make = function() {
var builder = this;
return function(/* params..., body, query */) {
var args = [].slice.call(arguments);
var path = builder.path.path;
var params = [];
var body = builder.withBody;
var query = {};
// query is the last option
var last = args[args.length - 1];
if (typeof last === 'object') {
query = args.pop();
}
// params must be the first arguments
if (builder.path.has.params) {
params = args.splice(0, builder.path.has.params);
path = builder.path.populate(params);
}
// body is the rest, unless already set...
if (!body && args.length) {
body = args.length === 1 ? args.pop() : args;
}
// reject if it doesn't have all the required arguments
var missing = params.slice(0, builder.path.has.required);
if (missing.length < builder.path.has.required) {
var e = new Error('missing required params ' + missing.join(', '));
return Promise.reject(e);
}
debug('%s(%j, %j)', builder.method, path, params);
return new Promise(function(resolve, reject) {
var req = request[builder.method](builder.bed.baseUrl + path);
req.query(merge(builder.bed.qs, builder.qs, query));
req.set(merge(builder.bed.headers, builder.headers));
req.send(body);
req.end(function(err, res) {
if (err) {
reject(err);
} else {
try {
res = builder.formatter(res);
} catch(e) {
reject(e);
}
resolve(res);
}
});
});
};
};
function Path(path) {
this.path = path;
this.regexp = null;
this.fields = [];
this.has = {
params: 0,
required: 0,
optional: 0
};
this.parse();
}
Path.prototype.parse = function() {
// required
var has = this.has;
var fields = [];
var position = 0;
var regexp = this.path.replace(/<([^>]+)>|\[([^\]]+)\]/g, function(match, r, o, i, path){
fields.push({part: path.slice(position, i)});
fields.push({name: r || o});
has.required += r ? 1 : 0;
has.optional += o ? 1 : 0;
has.params += 1;
position = i + match.length;
return r ? '(<[^>]+>)' : '(\\[[^\\]]+\\])';
});
fields.push({part: this.path.slice(position)});
this.regexp = new RegExp('^' + regexp.replace(/\//g, '\\/') + '$');
this.fields = fields;
};
Path.prototype.valid = function(path) {
return this.regexp.test(path);
};
Path.prototype.populate = function(params) {
debug('populate(%j)', params);
var i = 0;
return this.fields.map(function(field) {
if (field.part) {
return field.part;
} else {
return params[i++];
}
}).join('');
};
function merge() {
var o = {};
for(var i = 0; i < arguments.length; i++) {
var a = arguments[i];
if (a) {
for(var k in a) {
o[k] = a[k];
}
}
}
return o;
}
function addTo(name) {
return function add(key, value) {
if (typeof key === 'object') {
for(var k in key) {
add.call(this, k, key[k]);
}
} else {
this[name][key] = value;
}
return this;
};
}