Skip to content

Commit

Permalink
Add TypeScript definition, update dependencies (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 12, 2019
1 parent 32bb674 commit de325d8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: node_js
node_js:
- '10'
- '8'
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Get the email of an npm user.
*
* @param username - npm username to look up.
* @returns A promise for the user's email address.
*/
export default function npmEmail(username: string): Promise<string>;
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

const {URL} = require('url');
const got = require('got');
const registryUrl = require('registry-url');

module.exports = async username => {
const npmEmail = async username => {
if (typeof username !== 'string') {
throw new TypeError('Username required');
}
Expand All @@ -13,11 +14,14 @@ module.exports = async username => {
try {
const {body} = await got(url, {json: true});
return body.email;
} catch (err) {
if (err && err.statusCode === 404) {
} catch (error) {
if (error && error.statusCode === 404) {
throw new Error(`User ${username} doesn't exist`);
}

throw err;
throw error;
}
};

module.exports = npmEmail;
module.exports.default = npmEmail;
4 changes: 4 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from 'tsd-check';
import npmEmail from '.';

expectType<Promise<string>>(npmEmail('sindresorhus'));
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"npm",
Expand All @@ -29,11 +30,12 @@
"get"
],
"dependencies": {
"got": "^8.0.0",
"registry-url": "^3.0.0"
"got": "^9.6.0",
"registry-url": "^4.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.3.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import test from 'ava';
import m from '.';
import npmEmail from '.';

test('invalid input', async t => {
await t.throws(m(1), 'Username required');
await t.throwsAsync(npmEmail(1), 'Username required');
});

test('unknown username', async t => {
const randomName = `asdasfgrgafadsgaf${Math.random().toString().slice(2)}`;
await t.throws(m(randomName), `User ${randomName} doesn't exist`);
await t.throwsAsync(npmEmail(randomName), `User ${randomName} doesn't exist`);
});

test('valid username', async t => {
t.is(await m('sindresorhus'), 'sindresorhus@gmail.com');
t.is(await npmEmail('sindresorhus'), 'sindresorhus@gmail.com');
});

test('valid username with special character', async t => {
t.is(await m(`lukeramsden'`), 'lukeramsden8@gmail.com');
t.is(await npmEmail('lukeramsden\''), 'lukeramsden8@gmail.com');
});

0 comments on commit de325d8

Please sign in to comment.