github-username
Advanced tools
Comparing version 5.0.1 to 6.0.0
/** | ||
Get a GitHub username from an email address. | ||
Get the GitHub username from an email address if the email can be found in any commits on GitHub. | ||
@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`. | ||
@returns The username for the `email` or `undefined` if it cannot be found. | ||
@@ -18,4 +18,4 @@ @example | ||
*/ | ||
declare function githubUsername(email: string, token?: string): Promise<string>; | ||
declare function githubUsername(email: string, token?: string): Promise<string | undefined>; | ||
export = githubUsername; |
40
index.js
'use strict'; | ||
const ghGot = require('gh-got'); | ||
const {Octokit} = require('@octokit/rest'); | ||
async function searchCommits(email, token) { | ||
const result = await ghGot('search/commits', { | ||
token, | ||
query: { | ||
q: `author-email:${email}`, | ||
sort: 'author-date', | ||
// eslint-disable-next-line camelcase | ||
per_page: 1 | ||
}, | ||
headers: { | ||
accept: 'application/vnd.github.cloak-preview', | ||
'user-agent': 'https://github.com/sindresorhus/github-username' | ||
} | ||
async function searchCommits(octokit, email) { | ||
const {data} = await octokit.search.commits({ | ||
q: `author-email:${email}`, | ||
sort: 'author-date', | ||
// eslint-disable-next-line camelcase | ||
per_page: 1 | ||
}); | ||
const {body: data} = result; | ||
if (data.total_count === 0) { | ||
@@ -33,16 +24,13 @@ throw new Error(`Couldn't find username for \`${email}\``); | ||
const result = await ghGot('search/users', { | ||
token, | ||
query: { | ||
q: `${email} in:email` | ||
}, | ||
headers: { | ||
'user-agent': 'https://github.com/sindresorhus/github-username' | ||
} | ||
const octokit = new Octokit({ | ||
auth: token, | ||
userAgent: 'https://github.com/sindresorhus/github-username' | ||
}); | ||
const {body: data} = result; | ||
const {data} = await octokit.search.users({ | ||
q: `${email} in:email` | ||
}); | ||
if (data.total_count === 0) { | ||
return searchCommits(email, token); | ||
return searchCommits(octokit, email); | ||
} | ||
@@ -49,0 +37,0 @@ |
{ | ||
"name": "github-username", | ||
"version": "5.0.1", | ||
"version": "6.0.0", | ||
"description": "Get a GitHub username from an email address", | ||
"license": "MIT", | ||
"repository": "sindresorhus/github-username", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -32,9 +33,9 @@ "scripts": { | ||
"dependencies": { | ||
"gh-got": "^8.1.0" | ||
"@octokit/rest": "^18.0.6" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"ava": "^2.4.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.36.1" | ||
} | ||
} |
@@ -1,6 +0,5 @@ | ||
# github-username [](https://travis-ci.org/sindresorhus/github-username) | ||
# github-username | ||
> Get a GitHub username from an email address | ||
## Install | ||
@@ -12,3 +11,2 @@ | ||
## Usage | ||
@@ -25,9 +23,10 @@ | ||
## API | ||
### githubUsername(email, [token]) | ||
### githubUsername(email, token?) | ||
Returns a `Promise<string>` with the username. | ||
Get the GitHub username from an email address if the email can be found in any commits on GitHub. | ||
Returns a `Promise<string?>` with the username. | ||
@@ -45,10 +44,4 @@ | ||
## Related | ||
- [github-username-cli](https://github.com/sindresorhus/github-username-cli) - CLI for this module | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
4092
46
45
+ Added@octokit/rest@^18.0.6
+ Added@octokit/auth-token@2.5.0(transitive)
+ Added@octokit/core@3.6.0(transitive)
+ Added@octokit/endpoint@6.0.12(transitive)
+ Added@octokit/graphql@4.8.0(transitive)
+ Added@octokit/openapi-types@12.11.0(transitive)
+ Added@octokit/plugin-paginate-rest@2.21.3(transitive)
+ Added@octokit/plugin-request-log@1.0.4(transitive)
+ Added@octokit/plugin-rest-endpoint-methods@5.16.2(transitive)
+ Added@octokit/request@5.6.3(transitive)
+ Added@octokit/request-error@2.1.0(transitive)
+ Added@octokit/rest@18.12.0(transitive)
+ Added@octokit/types@6.41.0(transitive)
+ Addedbefore-after-hook@2.2.3(transitive)
+ Addeddeprecation@2.3.1(transitive)
+ Addedis-plain-object@5.0.0(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addeduniversal-user-agent@6.0.1(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
- Removedgh-got@^8.1.0
- Removed@sindresorhus/is@0.14.0(transitive)
- Removed@szmarczak/http-timer@1.1.2(transitive)
- Removedcacheable-request@6.1.0(transitive)
- Removedclone-response@1.0.3(transitive)
- Removeddecompress-response@3.3.0(transitive)
- Removeddefer-to-connect@1.1.3(transitive)
- Removedduplexer3@0.1.5(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedget-stream@4.1.05.2.0(transitive)
- Removedgh-got@8.1.0(transitive)
- Removedgot@9.6.0(transitive)
- Removedhttp-cache-semantics@4.1.1(transitive)
- Removedjson-buffer@3.0.0(transitive)
- Removedkeyv@3.1.0(transitive)
- Removedlowercase-keys@1.0.12.0.0(transitive)
- Removedmimic-response@1.0.1(transitive)
- Removednormalize-url@4.5.1(transitive)
- Removedp-cancelable@1.1.0(transitive)
- Removedprepend-http@2.0.0(transitive)
- Removedpump@3.0.2(transitive)
- Removedresponselike@1.0.2(transitive)
- Removedto-readable-stream@1.0.0(transitive)
- Removedurl-parse-lax@3.0.0(transitive)