-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
41 lines (33 loc) · 1008 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import ky from 'ky';
export default async function npmEmail(username) {
if (typeof username !== 'string') {
throw new TypeError('Username required');
}
const url = new URL(`https://registry.npmjs.com/-/v1/search?&text=maintainer:${encodeURIComponent(username)}`);
try {
const {objects: results, total} = await ky(url).json();
if (total === 0) {
return;
}
results.sort((a, b) => b.package.date.localeCompare(a.package.date));
for (const {package: package_} of results) {
const users = [
package_.author,
package_.publisher,
...package_.maintainers,
];
for (const user of users) {
if (user?.username === username && typeof user?.email === 'string' && user?.email !== '') {
return user.email;
}
}
}
} catch (error) {
if (error?.response?.status === 404) {
const notFoundError = new Error(`User \`${username}\` could not be found`, {cause: error});
notFoundError.code = 'ERR_NO_NPM_USER';
throw notFoundError;
}
throw error;
}
}