From 7a5c6c5e8a87157c7cd9f73a7d046453efcaf061 Mon Sep 17 00:00:00 2001 From: Tommy Date: Mon, 26 Feb 2024 11:22:50 -0600 Subject: [PATCH] Target Node.js 18, switch to `ky` (#21) --- .github/workflows/main.yml | 8 ++++---- index.d.ts | 6 ++++-- index.js | 12 +++++++----- package.json | 20 +++++++++++--------- readme.md | 6 ++++-- test.js | 38 +++++++++++++++++++++++++++----------- 6 files changed, 57 insertions(+), 33 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d50ada6..a768beb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index ca2c38c..470e55d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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' // } ``` */ diff --git a/index.js b/index.js index 757b5c5..9caee65 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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 { diff --git a/package.json b/package.json index 1dc829d..a661c70 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/readme.md b/readme.md index 88f68c4..43ba77a 100644 --- a/readme.md +++ b/readme.md @@ -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' } */ ``` diff --git a/test.js b/test.js index e1b9ce1..b81096a 100644 --- a/test.js +++ b/test.js @@ -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); });