From 7a5c6c5e8a87157c7cd9f73a7d046453efcaf061 Mon Sep 17 00:00:00 2001 From: Tommy Date: Mon, 26 Feb 2024 11:22:50 -0600 Subject: [PATCH 1/9] 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); }); From 685237bfea944c8d103987ead92483268a7f770b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 27 Feb 2024 00:23:21 +0700 Subject: [PATCH 2/9] Meta tweaks --- package.json | 1 + readme.md | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a661c70..8e3863b 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "types": "./index.d.ts", "default": "./index.js" }, + "sideEffects": false, "engines": { "node": ">=18" }, diff --git a/readme.md b/readme.md index 43ba77a..9232a8b 100644 --- a/readme.md +++ b/readme.md @@ -37,4 +37,3 @@ console.log(await npmUser('sindresorhus')); - [npm-email](https://github.com/sindresorhus/npm-email) - Get the email of an npm user - [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-packages](https://github.com/kevva/npm-user-packages) - Get packages by an npm user From aa590a42e89cbb7fa7bc82b57a97759d875fe2c5 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 27 Feb 2024 00:36:24 +0700 Subject: [PATCH 3/9] 6.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8e3863b..1e29f95 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "npm-user", - "version": "5.0.1", + "version": "6.0.0", "description": "Get user info of an npm user", "license": "MIT", "repository": "sindresorhus/npm-user", From f8e0a28a7cf706bb0dc212a857c993f767701910 Mon Sep 17 00:00:00 2001 From: Tommy Date: Mon, 26 Feb 2024 23:25:06 -0600 Subject: [PATCH 4/9] Fix 404 error handling (#22) --- index.js | 2 +- test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9caee65..78c8ea5 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ export default async function npmUser(username) { twitter: $sidebar.find('a[href^="https://twitter.com/"]').text().slice(1) || undefined, }; } catch (error) { - if (error.statusCode === 404) { + if (error.response.status === 404) { error.message = 'User doesn\'t exist'; } diff --git a/test.js b/test.js index b81096a..6a379a9 100644 --- a/test.js +++ b/test.js @@ -39,3 +39,10 @@ test('user: tj', async t => { t.regex(user.avatar, avatarRegex); }); + +test('handles non-existent user', async t => { + await t.throwsAsync( + npmUser('sindresorhus123'), + {message: 'User doesn\'t exist'}, + ); +}); From 85206326649f0bf17adddc1ff8c7f83be0929f8b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 27 Feb 2024 12:26:02 +0700 Subject: [PATCH 5/9] 6.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1e29f95..882c129 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "npm-user", - "version": "6.0.0", + "version": "6.0.1", "description": "Get user info of an npm user", "license": "MIT", "repository": "sindresorhus/npm-user", From b01900149cbd1320dcd9c9760f4b3923053c0a0b Mon Sep 17 00:00:00 2001 From: Tommy Date: Tue, 27 Feb 2024 21:52:00 -0600 Subject: [PATCH 6/9] Improve 404 error (#23) --- index.js | 4 +++- test.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 78c8ea5..c968ba4 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,9 @@ export default async function npmUser(username) { }; } catch (error) { if (error.response.status === 404) { - error.message = 'User doesn\'t exist'; + const notFoundError = new Error(`User \`${username}\` could not be found`, {cause: error}); + notFoundError.code = 'ERR_NO_NPM_USER'; + throw notFoundError; } throw error; diff --git a/test.js b/test.js index 6a379a9..8fed3a5 100644 --- a/test.js +++ b/test.js @@ -42,7 +42,7 @@ test('user: tj', async t => { test('handles non-existent user', async t => { await t.throwsAsync( - npmUser('sindresorhus123'), - {message: 'User doesn\'t exist'}, + npmUser('nnnope'), + {message: 'User `nnnope` could not be found', code: 'ERR_NO_NPM_USER'}, ); }); From 38a7c48bd5f2df488b7d62bba374cf37c08d5560 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 28 Feb 2024 11:04:44 +0700 Subject: [PATCH 7/9] 6.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 882c129..25fac53 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "npm-user", - "version": "6.0.1", + "version": "6.1.0", "description": "Get user info of an npm user", "license": "MIT", "repository": "sindresorhus/npm-user", From d63f45d5461bdfa476aa06d8379f9239a48f66fc Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 29 Feb 2024 02:59:16 +0700 Subject: [PATCH 8/9] Improve the error handling --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c968ba4..8485bb4 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ export default async function npmUser(username) { twitter: $sidebar.find('a[href^="https://twitter.com/"]').text().slice(1) || undefined, }; } catch (error) { - if (error.response.status === 404) { + 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; From 3b398f43215aee1865c518b94af3492ba3aeabde Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 29 Feb 2024 03:00:39 +0700 Subject: [PATCH 9/9] 6.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 25fac53..9e7b338 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "npm-user", - "version": "6.1.0", + "version": "6.1.1", "description": "Get user info of an npm user", "license": "MIT", "repository": "sindresorhus/npm-user",