Skip to content
This repository has been archived by the owner on Aug 7, 2022. It is now read-only.

Add Promise compatibility #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ t.get("/1/members/me", { cards: "open" }, function(err, data) {
if (err) throw err;
console.log(data);
});

// Use it as a promise
t.get("/1/members/me", { cards: "open" })
.then(function(data) { console.log(data); });
```

### Uploading attachments to a card
Expand Down
25 changes: 16 additions & 9 deletions lib/node-trello.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,22 @@ Trello.prototype.request = function (method, uri, argsOrCallback, callback) {
options.json = this.addAuthArgs(this.parseQuery(uri, args));
}

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];
}

callback(err, body);
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)
}
});
});
};

Expand Down
55 changes: 37 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"devDependencies": {
"blanket": "^1.2.3",
"mocha": "^3.4.2",
"should": "^5.2.0",
"should": "^12.0.0",
"travis-cov": "^0.2.5"
},
"config": {
Expand Down
5 changes: 5 additions & 0 deletions test/trello.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ describe("Trello", function () {

request.get = method;
});

it("should return a promise", function () {
const promise = this.trello.request("VERB", "/test");
promise.should.be.Promise();
});
});
});
});