Skip to content

Commit 01b9ab4

Browse files
committed
add get(), auth(), list()
1 parent c0cc702 commit 01b9ab4

File tree

10 files changed

+237
-125
lines changed

10 files changed

+237
-125
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
33
- '0.11'
4-
script: "make test-travis"
4+
script: "npm run test-travis"
55
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
This software is licensed under the MIT License.
22

3-
Copyright (c) 2014 fengmk2 <fengmk2@gmail.com> and other contributors
3+
Copyright (c) 2015 fengmk2 <fengmk2@gmail.com> and other contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 0 additions & 51 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npm-user-service
1818
[david-image]: https://img.shields.io/david/cnpm/npm-user-service.svg?style=flat
1919
[david-url]: https://david-dm.org/cnpm/npm-user-service
2020

21-
npm user service for [cnpmjs.org](https://github.com/cnpm/cnpmjs.org/wiki/Use-Your-Own-User-Authorization)
21+
[npm] user service for [cnpmjs.org](https://github.com/cnpm/cnpmjs.org/wiki/Use-Your-Own-User-Authorization)
2222

2323
## Install
2424

@@ -28,6 +28,8 @@ $ npm install npm-user-service --save
2828

2929
## Usage
3030

31+
Set `userService` on [cnpmjs.org/config/config.js](https://github.com/cnpm/cnpmjs.org/blob/master/config/index.js)
32+
3133
```js
3234
var NpmUserService = require('npm-user-service');
3335

@@ -36,25 +38,7 @@ config.userService = new NpmUserService();
3638

3739
## License
3840

39-
(The MIT License)
40-
41-
Copyright (c) 2014 fengmk2 &lt;fengmk2@gmail.com&gt; and other contributors
42-
43-
Permission is hereby granted, free of charge, to any person obtaining
44-
a copy of this software and associated documentation files (the
45-
'Software'), to deal in the Software without restriction, including
46-
without limitation the rights to use, copy, modify, merge, publish,
47-
distribute, sublicense, and/or sell copies of the Software, and to
48-
permit persons to whom the Software is furnished to do so, subject to
49-
the following conditions:
41+
MIT
5042

51-
The above copyright notice and this permission notice shall be
52-
included in all copies or substantial portions of the Software.
5343

54-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
55-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
57-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
58-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
59-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
60-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44+
[npm]: https://npmjs.org

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('./lib/npm-user-service');
1+
module.exports = require('./lib/user');

lib/npm-user-service.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/user.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**!
2+
* npm-user-service - lib/user.js
3+
*
4+
* Copyright(c) 2014 fengmk2 and other contributors.
5+
* MIT Licensed
6+
*
7+
* Authors:
8+
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
9+
*/
10+
11+
"use strict";
12+
13+
/**
14+
* Module dependencies.
15+
*/
16+
17+
var urllib = require('urllib');
18+
19+
module.exports = UserService;
20+
21+
function UserService(options) {
22+
options = options || {};
23+
this.registry = options.registry || 'https://registry.npmjs.com';
24+
}
25+
26+
var proto = UserService.prototype;
27+
28+
/**
29+
* Auth user with login name and password
30+
* @param {String} login login name
31+
* @param {String} password login password
32+
* @return {User}
33+
*/
34+
proto.auth = function* (login, password) {
35+
// GET https://registry.npmjs.com/-/user/org.couchdb.user:npm-user-service-testuser?write=true
36+
var url = '/-/user/org.couchdb.user:' + login + '?write=true';
37+
var user = yield this._request('GET', url, {
38+
username: login,
39+
password: password
40+
});
41+
return formatUser(user);
42+
};
43+
44+
/**
45+
* Get user by login name
46+
* @param {String} login login name
47+
* @return {User}
48+
*/
49+
proto.get = function* (login) {
50+
var url = '/-/user/org.couchdb.user:' + login;
51+
var user = yield this._request('GET', url);
52+
return formatUser(user);
53+
};
54+
55+
/**
56+
* List users
57+
* @param {Array<String>} logins login names
58+
* @return {Array<User>}
59+
*/
60+
proto.list = function* (logins) {
61+
var that = this;
62+
var users = yield logins.map(function (name) {
63+
return that.get(name);
64+
});
65+
return users.filter(function (user) {
66+
return !!user;
67+
});
68+
};
69+
70+
/**
71+
* Search users
72+
* @param {String} query query keyword
73+
* @param {Object} [options] optional query params
74+
* - {Number} limit match users count, default is `20`
75+
* @return {Array<User>}
76+
*/
77+
proto.search = function* (query, options) {
78+
79+
};
80+
81+
proto._request = function* (method, url, auth) {
82+
var baseauth;
83+
if (auth) {
84+
baseauth = auth.username + ':' + auth.password;
85+
}
86+
url = this.registry + url;
87+
var result = yield urllib.request(url, {
88+
method: method,
89+
dataType: 'json',
90+
auth: baseauth,
91+
});
92+
var status = result.status || -1;
93+
if (status >= 200 && status < 300) {
94+
return result.data;
95+
}
96+
97+
if (status === 404) {
98+
return null;
99+
}
100+
101+
var data = result.data || {};
102+
var message = data.reason || 'status ' + status;
103+
var err = new Error(message);
104+
err.status = status;
105+
err.headers = result.headers;
106+
err.raw = data;
107+
if (status >= 400 && status < 500) {
108+
err.name = 'NpmClientError';
109+
} else {
110+
err.name = 'NpmServerError';
111+
}
112+
throw err;
113+
};
114+
115+
function formatUser(data) {
116+
if (!data) {
117+
return data;
118+
}
119+
120+
return {
121+
login: data.name,
122+
email: data.email,
123+
name: data.fullname || data.name,
124+
html_url: data.homepage || 'https://npmjs.org/~' + data.name,
125+
avatar_url: data.avatar || '',
126+
im_url: '',
127+
// site_admin: data.site_admin,
128+
// scopes: [],
129+
_raw: data
130+
};
131+
}

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
"description": "npm user service for cnpmjs.org",
55
"main": "index.js",
66
"scripts": {
7-
"test": "make test-all"
8-
},
9-
"config": {
10-
"cov": {
11-
"threshold": 100
12-
}
7+
"test": "mocha --harmony --check-leaks -R spec -t 5000 -r co-mocha test/*.test.js",
8+
"test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- --check-leaks -t 5000 -r co-mocha test/*.test.js",
9+
"test-travis": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- --check-leaks -t 5000 -r co-mocha test/*.test.js",
10+
"jshint": "jshint .",
11+
"autod": "autod -w --prefix '~'",
12+
"cnpm": "npm install --registry=https://registry.npm.taobao.org",
13+
"contributors": "contributors -f plain -o AUTHORS"
1314
},
1415
"dependencies": {
15-
16+
"urllib": "~2.2.1"
1617
},
1718
"devDependencies": {
1819
"autod": "*",
20+
"co-mocha": "*",
1921
"contributors": "*",
20-
"should": "*",
21-
"jshint": "*",
2222
"cov": "*",
2323
"istanbul-harmony": "*",
24-
"mocha": "*"
24+
"jshint": "*",
25+
"should": "~4.4.2"
2526
},
2627
"homepage": "https://github.com/cnpm/npm-user-service",
2728
"repository": {
@@ -37,7 +38,7 @@
3738
"npm-user-service", "user-service", "cnpm"
3839
],
3940
"engines": {
40-
"node": ">= 0.10.0"
41+
"node": ">= 0.11.14"
4142
},
4243
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)",
4344
"license": "MIT"

test/npm-user-service.test.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)