-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
80 lines (61 loc) · 1.39 KB
/
index.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
var request = require('request');
var noop = function() {};
var merge = function() {
var result = {};
for (var i = 0; i < arguments.length; i++) {
var obj = arguments[i];
for (var j in obj) {
result[j] = obj[j];
}
}
return result;
};
var get = function(url, options, callback) {
if(!callback) {
callback = options;
options = {};
}
var options = merge(options,{url:url},{method:'GET'});
delete options.uri;
request(options, callback);
};
exports.get = get;
var post = function(url, body, options, callback) {
if(!callback && typeof options === 'function') {
callback = options;
options = {};
}
callback = callback || noop;
var options = merge(options,{
method: 'POST',
url: url,
body: body
});
delete options.uri;
request(options, callback);
};
exports.post = post;
var getJSON = function(url, options, callback) {
if(!callback) {
callback = options;
options = {};
}
get(url, options, function(err, response, body) {
if(err) {
callback(err, null, null);
return;
}
callback(null, response, JSON.parse(body))
});
};
exports.getJSON = getJSON;
var postJSON = function(url, data, options, callback) {
if(!callback) {
callback = options;
options = {};
}
var options = merge(options, {'content-type': 'application/json'});
delete options.uri;
post(url, JSON.stringify(data), options, callback);
};
exports.postJSON = postJSON;