File tree 4 files changed +19
-22
lines changed
4 files changed +19
-22
lines changed Original file line number Diff line number Diff line change 1
- export interface UserData {
1
+ export interface UserInfo {
2
2
/**
3
3
* User's name.
4
4
*/
@@ -20,7 +20,7 @@ export interface UserData {
20
20
github : string | null ;
21
21
22
22
/**
23
- * User's associated twitter account.
23
+ * User's associated Twitter account.
24
24
*/
25
25
twitter : string | null ;
26
26
}
@@ -30,4 +30,4 @@ export interface UserData {
30
30
*
31
31
* @param name - User's username on npm.
32
32
*/
33
- export default function npmUser ( name : string ) : Promise < UserData > ;
33
+ export default function npmUser ( name : string ) : Promise < UserInfo > ;
Original file line number Diff line number Diff line change @@ -5,16 +5,13 @@ const npmEmail = require('npm-email');
5
5
6
6
const npmUser = async username => {
7
7
if ( typeof username !== 'string' ) {
8
- return Promise . reject ( new Error ( 'Username required' ) ) ;
8
+ throw new TypeError ( 'Username required' ) ;
9
9
}
10
10
11
11
const url = `https://www.npmjs.com/~${ username } ` ;
12
12
try {
13
- const values = await Promise . all ( [ got ( url ) , npmEmail ( username ) ] ) ;
14
-
15
- const res = values [ 0 ] ;
16
- const email = values [ 1 ] ;
17
- const $ = cheerio . load ( res . body ) ;
13
+ const [ profile , email ] = await Promise . all ( [ got ( url ) , npmEmail ( username ) ] ) ;
14
+ const $ = cheerio . load ( profile . body ) ;
18
15
19
16
let avatar = $ ( 'img[src^="https://s.gravatar.com"]' ) . attr ( 'src' ) ;
20
17
avatar = avatar ? avatar . replace ( / ^ ( h t t p s : \/ \/ ) s \. / , '$1' ) . replace ( / & d e f a u l t = r e t r o $ / , '' ) : null ;
Original file line number Diff line number Diff line change 1
1
import { expectType } from 'tsd-check' ;
2
- import npmUser , { UserData } from '.' ;
2
+ import npmUser , { UserInfo } from '.' ;
3
3
4
- const userDataPromise = npmUser ( 'sindresorhus' ) ;
5
- expectType < Promise < UserData > > ( userDataPromise ) ;
4
+ const userInfoPromise = npmUser ( 'sindresorhus' ) ;
5
+ expectType < Promise < UserInfo > > ( userInfoPromise ) ;
6
6
7
- const userData = await userDataPromise ;
8
- expectType < string | null > ( userData . name ) ;
9
- expectType < string | null > ( userData . avatar ) ;
10
- expectType < string | null > ( userData . email ) ;
11
- expectType < string | null > ( userData . github ) ;
12
- expectType < string | null > ( userData . twitter ) ;
7
+ const userInfo = await userInfoPromise ;
8
+ expectType < string | null > ( userInfo . name ) ;
9
+ expectType < string | null > ( userInfo . avatar ) ;
10
+ expectType < string | null > ( userInfo . email ) ;
11
+ expectType < string | null > ( userInfo . github ) ;
12
+ expectType < string | null > ( userInfo . twitter ) ;
Original file line number Diff line number Diff line change 4
4
5
5
Since npm has no API for this we're forced to scrape the [ profile page] ( https://www.npmjs.com/~sindresorhus ) .
6
6
7
- * Use the faster [ npm-email] ( https://github.com/sindresorhus/npm-email ) if you only need the email.*
7
+ * Use the faster [ npm-email] ( https://github.com/sindresorhus/npm-email ) package if you only need the email.*
8
8
9
9
10
10
## Install
@@ -19,8 +19,8 @@ $ npm install npm-user
19
19
``` js
20
20
const npmUser = require (' npm-user' );
21
21
22
- npmUser ( ' sindresorhus ' ). then ( user => {
23
- console .log (user );
22
+ ( async () => {
23
+ console .log (await npmUser ( ' sindresorhus ' ) );
24
24
/*
25
25
{
26
26
name: 'Sindre Sorhus',
@@ -30,7 +30,7 @@ npmUser('sindresorhus').then(user => {
30
30
twitter: 'sindresorhus'
31
31
}
32
32
*/
33
- });
33
+ })()
34
34
```
35
35
36
36
* Values will be ` null ` if they're not set in the npm profile.*
You can’t perform that action at this time.
0 commit comments