-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a61e47e
commit 6f2f7de
Showing
8 changed files
with
54 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,15 @@ | ||
declare const npmEmail: { | ||
/** | ||
Get the email of an npm user. | ||
/** | ||
Get the email of an npm user. | ||
@param username - npm username to look up. | ||
@returns The user's email address. | ||
@param username - The npm username to look up. | ||
@returns The user's email address, or `undefined` if the user does not exist or the email could not be found. | ||
@example | ||
``` | ||
import npmEmail = require('npm-email'); | ||
@example | ||
``` | ||
import npmEmail from npm-email'; | ||
(async () => { | ||
console.log(await npmEmail('sindresorhus')); | ||
//=> 'sindresorhus@gmail.com' | ||
})(); | ||
``` | ||
*/ | ||
(username: string): Promise<string>; | ||
|
||
// TODO: Remove this for the next major release, refactor the whole definition to: | ||
// declare function npmEmail(username: string): Promise<string>; | ||
// export = npmEmail; | ||
default: typeof npmEmail; | ||
}; | ||
|
||
export = npmEmail; | ||
console.log(await npmEmail('sindresorhus')); | ||
//=> 'sindresorhus@gmail.com' | ||
``` | ||
*/ | ||
export default function npmEmail(username: string): Promise<string | undefined>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,33 @@ | ||
'use strict'; | ||
const {URL} = require('url'); | ||
const got = require('got'); | ||
const getRegistryUrl = require('registry-url'); | ||
import got from 'got'; | ||
|
||
const npmEmail = async username => { | ||
export default async function npmEmail(username) { | ||
if (typeof username !== 'string') { | ||
throw new TypeError('Username required'); | ||
} | ||
|
||
let registryUrl = getRegistryUrl(); | ||
const url = new URL(`https://api.npms.io/v2/search?q=maintainer:${encodeURIComponent(username)}`); | ||
|
||
// `npmjs.com` no longer supports the endpoint we need, so we use a mirror. | ||
if (registryUrl.trim().replace(/\/$/, '') === 'https://registry.npmjs.org') { | ||
registryUrl = 'https://r.cnpmjs.org/'; | ||
} | ||
try { | ||
const {results} = await got(url).json(); | ||
|
||
const url = new URL(`${registryUrl}-/user/org.couchdb.user:${username}`); | ||
for (const {package: package_} of results) { | ||
const users = [ | ||
package_.author, | ||
package_.publisher, | ||
...package_.maintainers, | ||
]; | ||
|
||
try { | ||
const {body} = await got(url, {json: true}); | ||
return body.email; | ||
for (const user of users) { | ||
if (user?.username === username && user?.email && typeof user.email === 'string') { | ||
return user.email; | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
if (error && error.statusCode === 404) { | ||
throw new Error(`User ${username} doesn't exist`); | ||
} | ||
|
||
throw error; | ||
} | ||
}; | ||
|
||
module.exports = npmEmail; | ||
// TODO: Remove this for the next major release | ||
module.exports.default = npmEmail; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import {expectType} from 'tsd'; | ||
import npmEmail = require('.'); | ||
import npmEmail from './index.js'; | ||
|
||
expectType<Promise<string>>(npmEmail('sindresorhus')); | ||
expectType<Promise<string | undefined>>(npmEmail('sindresorhus')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,11 @@ | ||
import test from 'ava'; | ||
import npmEmail from '.'; | ||
import npmEmail from './index.js'; | ||
|
||
test('invalid input', async t => { | ||
await t.throwsAsync(npmEmail(1), 'Username required'); | ||
}); | ||
|
||
test('unknown username', async t => { | ||
const randomName = `asdasfgrgafadsgaf${Math.random().toString().slice(2)}`; | ||
await t.throwsAsync(npmEmail(randomName), `User ${randomName} doesn't exist`); | ||
await t.throwsAsync(npmEmail(1), {message: 'Username required'}); | ||
}); | ||
|
||
test('valid username', async t => { | ||
t.is(await npmEmail('sindresorhus'), 'sindresorhus@gmail.com'); | ||
}); | ||
|
||
test('valid username with special character', async t => { | ||
t.is(await npmEmail('lukeramsden\''), 'lukeramsden8@gmail.com'); | ||
t.is(await npmEmail('vdemedes'), 'vdemedes@gmail.com'); | ||
}); |