Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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