forked from awesomejerry/node-trello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-trello.js
132 lines (110 loc) · 3.31 KB
/
node-trello.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
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
var request = require("request");
var querystring = require("querystring");
var OAuth = require("./trello-oauth");
// Creates a new Trello request wrapper.
// Syntax: new Trello(applicationApiKey, userToken)
var Trello = module.exports = function (key, token) {
if (!key) {
throw new Error("Application API key is required");
}
this.key = key;
this.token = token;
this.host = "https://api.trello.com";
};
// Make a GET request to Trello.
// Syntax: trello.get(uri, [query], callback)
Trello.prototype.get = function () {
Array.prototype.unshift.call(arguments, "GET");
return this.request.apply(this, arguments);
};
// Make a POST request to Trello.
// Syntax: trello.post(uri, [query], callback)
Trello.prototype.post = function () {
Array.prototype.unshift.call(arguments, "POST");
return this.request.apply(this, arguments);
};
// Make a PUT request to Trello.
// Syntax: trello.put(uri, [query], callback)
Trello.prototype.put = function () {
Array.prototype.unshift.call(arguments, "PUT");
return this.request.apply(this, arguments);
};
// Make a DELETE request to Trello.
// Syntax: trello.del(uri, [query], callback)
Trello.prototype.del = function () {
Array.prototype.unshift.call(arguments, "DELETE");
return this.request.apply(this, arguments);
};
// Make a request to Trello.
// Syntax: trello.request(method, uri, [query], callback)
Trello.prototype.request = function (method, uri, argsOrCallback, callback) {
var args;
//Can be called with
//(method, uri, callback)
//(method, uri, args, callback)
if (arguments.length === 4 || arguments.length === 3 && typeof argsOrCallback === 'function') {
callback = argsOrCallback;
args = {};
}
//and (method, uri, args)
else {
args = argsOrCallback || {};
}
var url = this.host + (uri[0] === "/" ? "" : "/") + uri;
if (method === "GET") {
url += "?" + querystring.stringify(this.addAuthArgs(this.parseQuery(uri, args)));
}
var options = {
url: url,
method: method
};
if (args.attachment) {
options.formData = {
key: this.key,
token: this.token
};
if (typeof args.attachment === "string" || args.attachment instanceof String) {
options.formData.url = args.attachment;
}
else {
options.formData.file = args.attachment;
}
}
else {
options.json = this.addAuthArgs(this.parseQuery(uri, args));
}
return new Promise(function (resolve, reject) {
request[method === 'DELETE' ? 'del' : method.toLowerCase()](options, function (err, response, body) {
if (!err && response.statusCode >= 400) {
err = new Error(body);
err.statusCode = response.statusCode;
err.responseBody = body;
err.statusMessage = require('http').STATUS_CODES[response.statusCode];
}
if (typeof callback === 'function') {
callback(err, body);
} else {
if (err) reject(err);
else resolve(body)
}
});
});
};
Trello.prototype.addAuthArgs = function (args) {
args.key = this.key;
if (this.token) {
args.token = this.token;
}
return args;
};
Trello.prototype.parseQuery = function (uri, args) {
if (uri.indexOf("?") !== -1) {
var ref = querystring.parse(uri.split("?")[1]);
for (var key in ref) {
var value = ref[key];
args[key] = value;
}
}
return args;
};
Trello.OAuth = OAuth;