Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 15, 2019
1 parent d2c8fc6 commit fe861db
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 71 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
Get a GitHub username from an email address.
@param email - Email address for the user of whom you want the username.
@param token - GitHub [personal access token](https://github.com/settings/tokens/new).
@returns The username for the `email`.
@example
```
import githubUsername = require('github-username');
(async () => {
console.log(await githubUsername('sindresorhus@gmail.com'));
//=> 'sindresorhus'
})();
```
*/
declare function githubUsername(email: string, token: string): Promise<string>;

export = githubUsername;
38 changes: 19 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
const ghGot = require('gh-got');

function searchCommits(email, token) {
return ghGot('search/commits', {
async function searchCommits(email, token) {
const result = await ghGot('search/commits', {
token,
query: {
q: `author-email:${email}`,
Expand All @@ -14,37 +14,37 @@ function searchCommits(email, token) {
accept: 'application/vnd.github.cloak-preview',
'user-agent': 'https://github.com/sindresorhus/github-username'
}
}).then(result => {
const data = result.body;
});

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

return data.items[0].author.login;
});
if (data.total_count === 0) {
throw new Error(`Couldn't find username for \`${email}\``);
}

return data.items[0].author.login;
}

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

return ghGot('search/users', {
const result = await ghGot('search/users', {
token,
query: {
q: `${email} in:email`
},
headers: {
'user-agent': 'https://github.com/sindresorhus/github-username'
}
}).then(result => {
const data = result.body;
});

if (data.total_count === 0) {
return searchCommits(email, token);
}
const {body: data} = result;

return data.items[0].login;
});
if (data.total_count === 0) {
return searchCommits(email, token);
}

return data.items[0].login;
};
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import githubUsername = require('.');

expectType<Promise<string>>(
githubUsername('sindresorhus@gmail.com', 'deadbeef')
);
75 changes: 37 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
{
"name": "github-username",
"version": "4.1.0",
"description": "Get a GitHub username from an email address",
"license": "MIT",
"repository": "sindresorhus/github-username",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"github",
"user",
"username",
"email",
"address",
"gh",
"git"
],
"dependencies": {
"gh-got": "^6.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
"name": "github-username",
"version": "4.1.0",
"description": "Get a GitHub username from an email address",
"license": "MIT",
"repository": "sindresorhus/github-username",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"github",
"user",
"username",
"email",
"address",
"gh",
"git"
],
"dependencies": {
"gh-got": "^8.1.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ $ npm install github-username
```js
const githubUsername = require('github-username');

githubUsername('sindresorhus@gmail.com').then(username => {
console.log(username);
(async () => {
console.log(await githubUsername('sindresorhus@gmail.com'));
//=> 'sindresorhus'
});
})();
```


Expand Down
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import test from 'ava';
import m from './';
import githubUsername from '.';

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

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

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

test('rejects when email is missing', async t => {
await t.throws(m());
await t.throwsAsync(githubUsername());
});

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

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

0 comments on commit fe861db

Please sign in to comment.