Skip to content

Commit

Permalink
Target Node.js 18, switch to ky (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Feb 26, 2024
1 parent 0ea3f07 commit 7a5c6c5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 33 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 21
- 20
- 18
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ import npmUser from 'npm-user';
console.log(await npmUser('sindresorhus'));
// {
// name: 'Sindre Sorhus',
// avatar: 'https://gravatar.com/avatar/d36a92237c75c5337c17b60d90686bf9?size=496',
// email: 'sindresorhus@gmail.com'
// avatar: 'https://www.npmjs.com/npm-avatar/…',
// email: 'sindresorhus@gmail.com',
// github: 'sindresorhus',
// twitter: 'sindresorhus'
// }
```
*/
Expand Down
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import got from 'got';
import cheerio from 'cheerio';
import ky from 'ky';
import {load as cheerioLoad} from 'cheerio';
import npmEmail from 'npm-email';

export default async function npmUser(username) {
Expand All @@ -9,10 +9,12 @@ export default async function npmUser(username) {

const url = `https://www.npmjs.com/~${username}`;
try {
const [profile, email] = await Promise.all([got(url), npmEmail(username)]);
const $ = cheerio.load(profile.body);
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 avatar = $('img[src^="/npm-avatar"]')?.attr('src') || undefined;
const $sidebar = $('[class^="_73a8e6f0"]');

return {
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": ">=14.16"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -36,13 +38,13 @@
"profile"
],
"dependencies": {
"cheerio": "^0.22.0",
"got": "^12.5.3",
"npm-email": "^4.0.1"
"cheerio": "1.0.0-rc.12",
"ky": "^1.2.1",
"npm-email": "^5.0.0"
},
"devDependencies": {
"ava": "^5.2.0",
"tsd": "^0.25.0",
"xo": "^0.53.1"
"ava": "^6.1.1",
"tsd": "^0.30.7",
"xo": "^0.57.0"
}
}
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ console.log(await npmUser('sindresorhus'));
/*
{
name: 'Sindre Sorhus',
avatar: 'https://gravatar.com/avatar/d36a92237c75c5337c17b60d90686bf9?size=496',
email: 'sindresorhus@gmail.com'
avatar: 'https://www.npmjs.com/npm-avatar/…',
email: 'sindresorhus@gmail.com',
github: 'sindresorhus',
twitter: 'sindresorhus'
}
*/
```
Expand Down
38 changes: 27 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
import test from 'ava';
import npmUser from './index.js';

const avatarRegex = /^https:\/\/www\.npmjs\.com\/npm-avatar\//m;

test('user: sindresorhus', async t => {
const user = await npmUser('sindresorhus');
t.is(user.name, 'Sindre Sorhus');
t.regex(user.avatar, /npm-avatar/);
t.is(user.email, 'sindresorhus@gmail.com');

t.like(user, {
name: 'Sindre Sorhus',
email: 'sindresorhus@gmail.com',
github: 'sindresorhus',
twitter: 'sindresorhus',
});

t.regex(user.avatar, avatarRegex);
});

test('user: npm', async t => {
const user = await npmUser('npm');
t.is(user.name, 'No Problem, Meatbag');
t.regex(user.avatar, /npm-avatar/);
t.is(user.email, 'npm@npmjs.com');

t.like(user, {
name: 'No Problem, Meatbag',
email: 'npm@npmjs.com',
});

t.regex(user.avatar, avatarRegex);
});

test('user: tj', async t => {
const user = await npmUser('tj');
t.is(user.name, undefined);
t.regex(user.avatar, /npm-avatar/);
t.is(user.email, 'tj@vision-media.ca');
t.is(user.github, undefined);
t.is(user.twitter, undefined);

t.like(user, {
name: undefined,
email: 'tj@vision-media.ca',
github: undefined,
twitter: undefined,
});

t.regex(user.avatar, avatarRegex);
});

0 comments on commit 7a5c6c5

Please sign in to comment.