Skip to content

Commit fe861db

Browse files
BendingBendersindresorhus
authored andcommittedApr 15, 2019
Require Node.js 8, add TypeScript definition (#9)
1 parent d2c8fc6 commit fe861db

8 files changed

+94
-71
lines changed
 

‎.gitattributes

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

‎.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: node_js
22
node_js:
3+
- '10'
34
- '8'
4-
- '6'
5-
- '4'

‎index.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
Get a GitHub username from an email address.
3+
4+
@param email - Email address for the user of whom you want the username.
5+
@param token - GitHub [personal access token](https://github.com/settings/tokens/new).
6+
@returns The username for the `email`.
7+
8+
@example
9+
```
10+
import githubUsername = require('github-username');
11+
12+
(async () => {
13+
console.log(await githubUsername('sindresorhus@gmail.com'));
14+
//=> 'sindresorhus'
15+
})();
16+
```
17+
*/
18+
declare function githubUsername(email: string, token: string): Promise<string>;
19+
20+
export = githubUsername;

‎index.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22
const ghGot = require('gh-got');
33

4-
function searchCommits(email, token) {
5-
return ghGot('search/commits', {
4+
async function searchCommits(email, token) {
5+
const result = await ghGot('search/commits', {
66
token,
77
query: {
88
q: `author-email:${email}`,
@@ -14,37 +14,37 @@ function searchCommits(email, token) {
1414
accept: 'application/vnd.github.cloak-preview',
1515
'user-agent': 'https://github.com/sindresorhus/github-username'
1616
}
17-
}).then(result => {
18-
const data = result.body;
17+
});
1918

20-
if (data.total_count === 0) {
21-
throw new Error(`Couldn't find username for \`${email}\``);
22-
}
19+
const {body: data} = result;
2320

24-
return data.items[0].author.login;
25-
});
21+
if (data.total_count === 0) {
22+
throw new Error(`Couldn't find username for \`${email}\``);
23+
}
24+
25+
return data.items[0].author.login;
2626
}
2727

28-
module.exports = (email, token) => {
28+
module.exports = async (email, token) => {
2929
if (!(typeof email === 'string' && email.includes('@'))) {
30-
return Promise.reject(new Error('Email required'));
30+
throw new Error('Email required');
3131
}
3232

33-
return ghGot('search/users', {
33+
const result = await ghGot('search/users', {
3434
token,
3535
query: {
3636
q: `${email} in:email`
3737
},
3838
headers: {
3939
'user-agent': 'https://github.com/sindresorhus/github-username'
4040
}
41-
}).then(result => {
42-
const data = result.body;
41+
});
4342

44-
if (data.total_count === 0) {
45-
return searchCommits(email, token);
46-
}
43+
const {body: data} = result;
4744

48-
return data.items[0].login;
49-
});
45+
if (data.total_count === 0) {
46+
return searchCommits(email, token);
47+
}
48+
49+
return data.items[0].login;
5050
};

‎index.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {expectType} from 'tsd';
2+
import githubUsername = require('.');
3+
4+
expectType<Promise<string>>(
5+
githubUsername('sindresorhus@gmail.com', 'deadbeef')
6+
);

‎package.json

+37-38
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
{
2-
"name": "github-username",
3-
"version": "4.1.0",
4-
"description": "Get a GitHub username from an email address",
5-
"license": "MIT",
6-
"repository": "sindresorhus/github-username",
7-
"author": {
8-
"name": "Sindre Sorhus",
9-
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11-
},
12-
"engines": {
13-
"node": ">=4"
14-
},
15-
"scripts": {
16-
"test": "xo && ava"
17-
},
18-
"files": [
19-
"index.js"
20-
],
21-
"keywords": [
22-
"github",
23-
"user",
24-
"username",
25-
"email",
26-
"address",
27-
"gh",
28-
"git"
29-
],
30-
"dependencies": {
31-
"gh-got": "^6.0.0"
32-
},
33-
"devDependencies": {
34-
"ava": "*",
35-
"xo": "*"
36-
},
37-
"xo": {
38-
"esnext": true
39-
}
2+
"name": "github-username",
3+
"version": "4.1.0",
4+
"description": "Get a GitHub username from an email address",
5+
"license": "MIT",
6+
"repository": "sindresorhus/github-username",
7+
"author": {
8+
"name": "Sindre Sorhus",
9+
"email": "sindresorhus@gmail.com",
10+
"url": "sindresorhus.com"
11+
},
12+
"engines": {
13+
"node": ">=8"
14+
},
15+
"scripts": {
16+
"test": "xo && ava && tsd"
17+
},
18+
"files": [
19+
"index.js",
20+
"index.d.ts"
21+
],
22+
"keywords": [
23+
"github",
24+
"user",
25+
"username",
26+
"email",
27+
"address",
28+
"gh",
29+
"git"
30+
],
31+
"dependencies": {
32+
"gh-got": "^8.1.0"
33+
},
34+
"devDependencies": {
35+
"ava": "^1.4.1",
36+
"tsd": "^0.7.2",
37+
"xo": "^0.24.0"
38+
}
4039
}

‎readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ $ npm install github-username
1515
```js
1616
const githubUsername = require('github-username');
1717

18-
githubUsername('sindresorhus@gmail.com').then(username => {
19-
console.log(username);
18+
(async () => {
19+
console.log(await githubUsername('sindresorhus@gmail.com'));
2020
//=> 'sindresorhus'
21-
});
21+
})();
2222
```
2323

2424

‎test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import test from 'ava';
2-
import m from './';
2+
import githubUsername from '.';
33

44
test('gets GitHub username from email', async t => {
5-
t.is(await m('sindresorhus@gmail.com'), 'sindresorhus');
5+
t.is(await githubUsername('sindresorhus@gmail.com'), 'sindresorhus');
66
});
77

88
test('gets GitHub username from email using Commit Search API', async t => {
9-
t.is(await m('markdotto@gmail.com'), 'mdo');
9+
t.is(await githubUsername('markdotto@gmail.com'), 'mdo');
1010
});
1111

1212
test('rejects when GitHub has no user for the email', async t => {
13-
await t.throws(m('nogithubaccount@example.com'));
13+
await t.throwsAsync(githubUsername('nogithubaccount@example.com'));
1414
});
1515

1616
test('rejects when email is missing', async t => {
17-
await t.throws(m());
17+
await t.throwsAsync(githubUsername());
1818
});
1919

2020
test('rejects when email is invalid', async t => {
21-
await t.throws(m('sindresorhus_gmail.com'));
21+
await t.throwsAsync(githubUsername('sindresorhus_gmail.com'));
2222
});
2323

2424
test('rejects when email is not a string', async t => {
25-
await t.throws(m(() => 'sindresorhus_gmail.com'));
25+
await t.throwsAsync(githubUsername(() => 'sindresorhus_gmail.com'));
2626
});

0 commit comments

Comments
 (0)
Please sign in to comment.