-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (30 loc) · 1.08 KB
/
index.js
File metadata and controls
36 lines (30 loc) · 1.08 KB
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
import ky from 'ky';
import {load as cheerioLoad} from 'cheerio';
import npmEmail from 'npm-email';
export default async function npmUser(username) {
if (typeof username !== 'string') {
throw new TypeError('Username required');
}
const url = `https://www.npmjs.com/~${username}`;
try {
const [profile, email] = await Promise.all([ky(url).text(), npmEmail(username)]);
const $ = cheerioLoad(profile);
let avatar = $('img[src^="/npm-avatar"]')?.attr('src') || undefined;
avatar &&= `https://www.npmjs.com${avatar}`;
const $sidebar = $('[class^="_73a8e6f0"]');
return {
name: $sidebar.find('.eaac77a6.mv2').text() || undefined,
avatar,
email,
github: $sidebar.find('a[href^="https://github.com/"]').text().slice(1) || undefined,
twitter: $sidebar.find('a[href^="https://twitter.com/"]').text().slice(1) || undefined,
};
} 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;
}
}