From fe861dbe1a9788c5668e0d92ffb1be0a762f98f6 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 15 Apr 2019 02:28:20 +0000 Subject: [PATCH] Require Node.js 8, add TypeScript definition (#9) --- .gitattributes | 3 +- .travis.yml | 3 +- index.d.ts | 20 +++++++++++++ index.js | 38 ++++++++++++------------- index.test-d.ts | 6 ++++ package.json | 75 ++++++++++++++++++++++++------------------------- readme.md | 6 ++-- test.js | 14 ++++----- 8 files changed, 94 insertions(+), 71 deletions(-) create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.travis.yml b/.travis.yml index 7d69d74..f3fa8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ language: node_js node_js: + - '10' - '8' - - '6' - - '4' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e8152d3 --- /dev/null +++ b/index.d.ts @@ -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; + +export = githubUsername; diff --git a/index.js b/index.js index e094f1b..831d55a 100644 --- a/index.js +++ b/index.js @@ -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}`, @@ -14,23 +14,23 @@ 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` @@ -38,13 +38,13 @@ module.exports = (email, token) => { 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; }; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..193bb3a --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,6 @@ +import {expectType} from 'tsd'; +import githubUsername = require('.'); + +expectType>( + githubUsername('sindresorhus@gmail.com', 'deadbeef') +); diff --git a/package.json b/package.json index c7fa61d..109963c 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/readme.md b/readme.md index b4f331e..7be2ecb 100644 --- a/readme.md +++ b/readme.md @@ -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' -}); +})(); ``` diff --git a/test.js b/test.js index 8cf6ee7..2c8c22e 100644 --- a/test.js +++ b/test.js @@ -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')); });