Skip to content

Commit

Permalink
Require Node.js 14 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 17, 2022
1 parent a61e47e commit 6f2f7de
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 85 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
35 changes: 12 additions & 23 deletions index.d.ts
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>;
37 changes: 18 additions & 19 deletions index.js
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;
}
4 changes: 2 additions & 2 deletions index.test-d.ts
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'));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Get the email of an npm user",
"license": "MIT",
"repository": "sindresorhus/npm-email",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^14.17.0 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -30,12 +33,11 @@
"get"
],
"dependencies": {
"got": "^9.6.0",
"registry-url": "^5.0.0"
"got": "^12.0.1"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^4.0.1",
"tsd": "^0.19.1",
"xo": "^0.48.0"
}
}
25 changes: 7 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,36 @@

> Get the email of an npm user

## Install

```
$ npm install npm-email
```sh
npm install npm-email
```


## Usage

```js
const npmEmail = require('npm-email');
import npmEmail from 'npm-email';

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


## API

### npmEmail(username)

Returns a promise for the user's email address.
Returns a promise for the user's email address, or `undefined` if the user does not exist or the email could not be found.

#### username

Type: `string`

npm username to look up.

The npm username to look up.

## Related

- [npm-email-cli](https://github.com/sindresorhus/npm-email-cli) - CLI for this module
- [npm-keyword](https://github.com/sindresorhus/npm-keyword) - Get a list of npm packages with a certain keyword
- [package-json](https://github.com/sindresorhus/package-json) - Get the package.json of a package from the npm registry
- [npm-user](https://github.com/sindresorhus/npm-user) - Get user info of an npm user


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
14 changes: 3 additions & 11 deletions test.js
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');
});

0 comments on commit 6f2f7de

Please sign in to comment.