Created
February 6, 2017 17:53
-
-
Save AlcaDesign/358d601665b99f7d676bb6fe6323c57b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'), | |
_kraken = request.defaults({ | |
baseUrl: 'https://api.twitch.tv/kraken/', | |
headers: { | |
'Client-ID': 'CLIENT ID HERE', | |
Accept: 'Accept: application/vnd.twitchtv.v5+json' | |
}, | |
json: true | |
}); | |
function kraken(options) { | |
return opts => new Promise((resolve, reject) => | |
_kraken(opts, (err, res, body) => { | |
if(err) { | |
return reject({ err, res }); | |
} | |
resolve({ res, body }); | |
}) | |
); | |
} | |
// Get a user object from a username. | |
function userByName(login = '') { | |
return kraken({ | |
url: 'users', | |
qs: { login } | |
}) | |
.then(({ req, body: { _total, users: [ user ] } }) => { | |
if(_total) { | |
return user; | |
} | |
return null; | |
}); | |
} | |
// Get a user object from a user ID. | |
function user(id = '') { | |
return kraken({ | |
url: `users/${id}` | |
}); | |
} | |
// Get a channel object from a user ID. | |
function channel(id = '') { | |
return kraken({ | |
url: `channels/${id}` | |
}); | |
} | |
module.exports = { | |
channel, | |
user, | |
userByName | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment