trello.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var https = require('https');
  2. class Trello {
  3. constructor() {
  4. this.BASEURL = "https://trello.com/1/";
  5. }
  6. reportBug(bugmessage, callback) {
  7. var bugData = {
  8. "idList": config.trelloBugListId,
  9. "name": bugmessage
  10. };
  11. this.makeRequest("cards", "post", bugData, callback)
  12. }
  13. requestFeature(featuremessage, callback) {
  14. var featureData = {
  15. "idList": config.trelloFeatureListId,
  16. "name": featuremessage
  17. };
  18. this.makeRequest("cards", "post", featureData, callback)
  19. }
  20. requestBugList(callback) {
  21. this.makeRequest("lists/" + config.trelloBugListId + "/cards?fields=id,pos,name", "get", null, callback);
  22. }
  23. requestFeatures(callback) {
  24. this.makeRequest("lists/" + config.trelloFeatureListId + "/cards?fields=id,pos,name", "get", null, callback);
  25. }
  26. addAuth(url) {
  27. if(typeof url == "object") {
  28. url["key"] = config.trelloApiKey;
  29. url["token"] = config.trelloAccessToken;
  30. return url;
  31. }
  32. if(url.indexOf("?") != -1) {
  33. return url + "&key="+config.trelloApiKey + "&token=" + config.trelloAccessToken;
  34. }
  35. return url + "?key="+config.trelloApiKey + "&token=" + config.trelloAccessToken;
  36. }
  37. makeRequest(path, method, data, callback) {
  38. var postData = "";
  39. if(data != null) {
  40. postData = [];
  41. for(var i in data) {
  42. postData.push(i + "=" + data[i]);
  43. }
  44. postData = this.addAuth(postData);
  45. postData = postData.join("&");
  46. } else {
  47. path = this.addAuth(path);
  48. }
  49. var options = {
  50. hostname: 'trello.com',
  51. port: 443,
  52. path: '/1/' + path,
  53. method: method.toUpperCase(),
  54. headers: {
  55. 'Content-Type': 'application/x-www-form-urlencoded',
  56. 'Content-Length': postData.length
  57. }
  58. };
  59. var req = https.request(options, (res) => {
  60. var buffer = "";
  61. res.on('data', (chunk) => {
  62. buffer += chunk;
  63. });
  64. res.on('end', () => {
  65. try {
  66. callback("success", JSON.parse(buffer));
  67. } catch(error) {
  68. console.error("Trello response is invalid JSON", buffer);
  69. callback("error", {"error": buffer});
  70. }
  71. });
  72. res.on('error', () => {
  73. console.error("Trello request failed.");
  74. });
  75. });
  76. req.on('error', (e) => {
  77. console.error(e);
  78. });
  79. console.log(postData);
  80. req.write(postData);
  81. req.end();
  82. }
  83. }
  84. module.exports = new Trello();