Skip to content

Commit a7941d1

Browse files
committed
support promise and thunk. fixed #6
1 parent 8956049 commit a7941d1

File tree

10 files changed

+170
-17
lines changed

10 files changed

+170
-17
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ Makefile
66
logo.png
77
.jshint*
88
coverage/
9+
logo.png
10+
*.log

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ Require [co](https://github.com/visionmedia/co) and node >= `0.11.12`:
5555

5656
```js
5757
var co = require('co');
58+
var gitlab = require('node-gitlab');
59+
60+
var client = gitlab.createThunk({
61+
api: 'https://gitlab.com/api/v3',
62+
privateToken: 'your private token'
63+
});
5864

5965
co(function* () {
6066
var milestones = yield client.milestones.list({id: 1});
@@ -63,6 +69,8 @@ co(function* () {
6369

6470
### Promise way
6571

72+
Require node >= `0.11.13` or [bluebird](https://github.com/petkaantonov/bluebird):
73+
6674
```js
6775
var gitlab = require('node-gitlab');
6876

lib/gitlab.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ Gitlab.prototype.setAuthentication = function (req) {
5252
Gitlab.create = function (options) {
5353
return new Gitlab(options);
5454
};
55+
56+
Gitlab.createPromise = function (options) {
57+
var client = Gitlab.create(options);
58+
return require('./promisify')(client);
59+
};
60+
61+
Gitlab.createThunk = function (options) {
62+
var client = Gitlab.create(options);
63+
return require('./thunkify')(client);
64+
};

lib/promisify.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**!
2+
* gitlab - lib/promisify.js
3+
*
4+
* Copyright(c) fengmk2 and other contributors.
5+
* MIT Licensed
6+
*
7+
* Authors:
8+
* fengmk2 <[email protected]> (http://fengmk2.github.com)
9+
*/
10+
11+
'use strict';
12+
13+
/**
14+
* Module dependencies.
15+
*/
16+
17+
/* jshint ignore:start */
18+
var Promise = require('native-or-bluebird');
19+
/* jshint ignore:end */
20+
var properties = require('./properties');
21+
22+
module.exports = promisifyAll;
23+
24+
function promisifyAll(source) {
25+
var target = {
26+
request: promisify('request', source.request.bind(source))
27+
};
28+
Object.keys(properties).forEach(function (name) {
29+
var methods = properties[name];
30+
target[name] = {};
31+
methods.forEach(function (method) {
32+
target[name][method] = promisify(method, source[name][method].bind(source[name]));
33+
});
34+
});
35+
return target;
36+
}
37+
38+
function promisify(name, fn) {
39+
/* jshint evil:true */
40+
return eval('(function ' + name + '() {\n'
41+
+ ' var len = arguments.length;\n'
42+
+ ' var args = new Array(len + 1);\n'
43+
+ ' for (var i = 0; i < len; ++i) { args[i] = arguments[i]; }\n'
44+
+ ' var lastIndex = i;\n'
45+
+ ' return new Promise(function (resolve, reject) {\n'
46+
+ ' args[lastIndex] = makeCallback(resolve, reject);\n'
47+
+ ' fn.apply(null, args);\n'
48+
+ ' });\n'
49+
+ '});');
50+
}
51+
52+
function makeCallback(resolve, reject) {
53+
return function (err, result) {
54+
if (err) {
55+
reject(err);
56+
} else {
57+
resolve(result);
58+
}
59+
};
60+
}

lib/methods.js renamed to lib/properties.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**!
2-
* gitlab - lib/methods.js
2+
* gitlab - lib/properties.js
33
*
44
* Copyright(c) fengmk2 and other contributors.
55
* MIT Licensed
@@ -16,7 +16,7 @@
1616

1717
var defaultMethods = ['get', 'list', 'create', 'update', 'remove'];
1818

19-
var methods = {
19+
var properties = {
2020
milestones: [],
2121
members: [],
2222
hooks: [],
@@ -46,8 +46,9 @@ var methods = {
4646
groupMembers: [],
4747
};
4848

49-
methods.forEach(function (apis) {
50-
apis = defaultMethods.concat(apis);
51-
});
49+
for (var key in properties) {
50+
var methods = properties[key];
51+
properties[key] = defaultMethods.concat(methods);
52+
}
5253

53-
module.exports = methods;
54+
module.exports = properties;

lib/thunkify.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**!
2+
* gitlab - lib/thunkify.js
3+
*
4+
* Copyright(c) fengmk2 and other contributors.
5+
* MIT Licensed
6+
*
7+
* Authors:
8+
* fengmk2 <[email protected]> (http://fengmk2.github.com)
9+
*/
10+
11+
'use strict';
12+
13+
/**
14+
* Module dependencies.
15+
*/
16+
17+
var thunkify = require('thunkify-wrap');
18+
var properties = require('./properties');
19+
20+
module.exports = function (client) {
21+
thunkify(client, 'request');
22+
for (var key in properties) {
23+
var methods = properties[key];
24+
thunkify(client[key], methods);
25+
}
26+
return client;
27+
};

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44
"description": "Gitlab API nodejs client.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha -R spec -t 20000 test/*.test.js",
8-
"test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -t 20000 test/*.test.js",
9-
"test-travis": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- -t 20000 test/*.test.js",
7+
"test": "mocha --harmony -R spec -r co-mocha -t 20000 test/*.test.js",
8+
"test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -r co-mocha -t 20000 test/*.test.js",
9+
"test-travis": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- -r co-mocha -t 20000 test/*.test.js",
1010
"jshint": "jshint .",
1111
"autod": "autod -w --prefix '~'",
1212
"cnpm": "npm install --registry=https://registry.npm.taobao.org",
1313
"contributors": "contributors -f plain -o AUTHORS"
1414
},
1515
"dependencies": {
1616
"debug": "~2.0.0",
17-
"restful-client": "~1.0.0"
17+
"native-or-bluebird": "~1.1.1",
18+
"restful-client": "~1.0.0",
19+
"thunkify-wrap": "~1.0.2"
1820
},
1921
"devDependencies": {
2022
"autod": "*",
2123
"contributors": "*",
22-
"istanbul": "*",
24+
"istanbul-harmony": "*",
2325
"mocha": "*",
26+
"co-mocha": "*",
2427
"pedding": "~1.0.0",
2528
"should": "~4.0.4"
2629
},

test/client.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616

1717
var gitlab = require('../');
18+
var config = require('./config');
1819

19-
var client = gitlab.create(require('./config'));
20+
var client = gitlab.create(config);
2021

2122
client.createProject = function (callback) {
2223
client._list(function (err) {
@@ -63,4 +64,7 @@ client.removeProject = function (callback) {
6364
}, callback);
6465
};
6566

67+
client.promise = gitlab.createPromise(config);
68+
client.thunk = gitlab.createThunk(config);
69+
6670
module.exports = client;

test/gitlab.test.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
* Module dependencies.
1515
*/
1616

17-
var client = require('./client');
1817
var should = require('should');
18+
var client = require('./client');
1919

2020
describe('gitlab.test.js', function () {
21-
2221
describe('Client.request()', function () {
2322
it('should request success', function (done) {
2423
client.request('get', '/projects', {}, function (err, projects) {
@@ -28,6 +27,20 @@ describe('gitlab.test.js', function () {
2827
});
2928
});
3029

30+
it('should request with promise way success', function (done) {
31+
client.promise.request('get', '/projects', {})
32+
.then(function (projects) {
33+
projects.length.should.above(0);
34+
done();
35+
})
36+
.catch(done);
37+
});
38+
39+
it('should request with thunk way success', function* () {
40+
var projects = yield client.thunk.request('get', '/projects', {});
41+
projects.length.should.above(0);
42+
});
43+
3144
it('should request 404 error', function (done) {
3245
client.request('get', '/projects/:id/milestones', {id: 99999999}, function (err, milestones) {
3346
should.exists(err);
@@ -58,7 +71,5 @@ describe('gitlab.test.js', function () {
5871
done();
5972
});
6073
});
61-
6274
});
63-
6475
});

test/issue.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('issue.test.js', function () {
3333
});
3434

3535
after(client.removeProject);
36+
3637
describe('client.issues.get()', function () {
3738
it('should return a issue', function (done) {
3839
client.issues.get({id: client.id, issue_id: issueId}, function (err, row) {
@@ -44,6 +45,26 @@ describe('issue.test.js', function () {
4445
done();
4546
});
4647
});
48+
49+
it('should return issue with promise way', function (done) {
50+
client.promise.issues.get({id: client.id, issue_id: issueId})
51+
.then(function (row) {
52+
row.id.should.equal(issueId);
53+
row.should.have.keys('id', 'iid', 'project_id', 'title', 'description', 'labels',
54+
'milestone', 'assignee', 'author', 'state',
55+
'updated_at', 'created_at');
56+
done();
57+
})
58+
.catch(done);
59+
});
60+
61+
it('should return issue with thunk way', function* () {
62+
var row = yield client.thunk.issues.get({id: client.id, issue_id: issueId});
63+
row.id.should.equal(issueId);
64+
row.should.have.keys('id', 'iid', 'project_id', 'title', 'description', 'labels',
65+
'milestone', 'assignee', 'author', 'state',
66+
'updated_at', 'created_at');
67+
});
4768
});
4869

4970
describe('client.issues.list()', function () {
@@ -110,7 +131,6 @@ describe('issue.test.js', function () {
110131
});
111132
});
112133
});
113-
114134
});
115135

116136
describe('client.issues.listNotes()', function () {
@@ -123,6 +143,13 @@ describe('issue.test.js', function () {
123143
done();
124144
});
125145
});
146+
147+
it('should return issue\'s notes in thunk way', function* () {
148+
var rows = yield client.thunk.issues.listNotes({id: client.id, issue_id: issueId});
149+
rows.length.should.above(0);
150+
var row = rows[0];
151+
row.should.have.keys('id', 'body', 'author', 'created_at', 'attachment');
152+
});
126153
});
127154

128155
describe('client.issues.createNote()', function () {

0 commit comments

Comments
 (0)