11'use strict'
2- var request = require ( 'request' )
2+ import fetch from 'node-fetch'
33
44const gitHubAPIURLs = {
55 getUser : 'https://api.github.com/users/%username%' ,
66 getPullRequests :
77 'https://api.github.com/search/issues?per_page=1000&q=-label:invalid+created:%year%-09-30T00:00:00-12:00..%year%-10-31T23:59:59-12:00+type:pr+is:public+author:%username%'
88}
99
10- const getMinPullRequests = year => {
10+ const getMinPullRequests = ( year ) => {
1111 switch ( year ) {
1212 case 2018 :
1313 return 5
@@ -16,7 +16,7 @@ const getMinPullRequests = year => {
1616 }
1717}
1818
19- const _checkForValidYear = year => {
19+ const _checkForValidYear = ( year ) => {
2020 let currentYear = new Date ( ) . getFullYear ( )
2121 if ( year > currentYear ) {
2222 throw new Error ( 'Invalid year provided. The year must be less than or equal to the current year' )
@@ -26,39 +26,38 @@ const _checkForValidYear = year => {
2626 }
2727}
2828
29- const _query = url =>
30- new Promise ( ( resolve , reject ) => {
31- request . get (
32- {
33- url : url ,
34- headers : { 'User-Agent' : 'request' }
35- } ,
36- ( err , res , data ) => {
37- if ( ! err && res . statusCode == 200 ) resolve ( data )
38- reject ( err )
39- }
40- )
29+ const _query = async ( url ) => {
30+ const response = await fetch ( url , {
31+ headers : { 'User-Agent' : 'request' }
4132 } )
33+
34+ if ( ! response . ok ) {
35+ throw new Error ( `HTTP error! status: ${ response . status } ` )
36+ }
37+
38+ return response . text ( )
39+ }
4240
4341const _processResult = ( url , callback , transform ) => {
4442 if ( callback ) {
4543 _query ( url )
4644 . then ( jsonPipe )
47- . then ( data => ( transform ? transform ( data ) : data ) )
48- . then ( result => callback ( result ) )
49- . catch ( err => {
45+ . then ( ( data ) => ( transform ? transform ( data ) : data ) )
46+ . then ( ( result ) => callback ( result ) )
47+ . catch ( ( err ) => {
5048 throw new Error (
5149 'There was a problem retrieving information for this account. Error Message: ' + ( err ? err . message : '' )
5250 )
5351 } )
5452 } else {
5553 return _query ( url )
5654 . then ( jsonPipe )
57- . then ( data => ( transform ? transform ( data ) : data ) )
55+ . then ( ( data ) => ( transform ? transform ( data ) : data ) )
5856 }
5957}
6058
61- const jsonPipe = body => JSON . parse ( body )
59+ const jsonPipe = ( body ) => JSON . parse ( body )
60+
6261
6362/**
6463 * Returns user information
@@ -71,12 +70,12 @@ const getUserInfo = (username, callback) => {
7170 return _processResult ( url , callback )
7271}
7372
74- const _transformHacktoberfestResult = minPullRequest => statsInfo => ( {
73+ const _transformHacktoberfestResult = ( minPullRequest ) => ( statsInfo ) => ( {
7574 completed : ! ! ( statsInfo . total_count >= minPullRequest ) ,
7675 current : statsInfo . total_count ,
7776 required : minPullRequest ,
7877 progress : statsInfo . total_count + '/' + minPullRequest ,
79- contributions : statsInfo . items . map ( repo => repo . repository_url )
78+ contributions : statsInfo . items . map ( ( repo ) => repo . repository_url )
8079} )
8180
8281/**
@@ -106,5 +105,4 @@ const getHacktoberfestStats = (username, year, callback) => {
106105 return _processResult ( url , callback , _transformHacktoberfestResult ( minPullRequest ) )
107106}
108107
109- exports . getHacktoberfestStats = getHacktoberfestStats
110- exports . getUserInfo = getUserInfo
108+ export { getUserInfo , getHacktoberfestStats }
0 commit comments