simple-helix-api
Advanced tools
Comparing version 1.0.6 to 1.0.8
64
index.js
@@ -9,3 +9,7 @@ const request = require("request"); | ||
if(err) throw reject(err); | ||
try { return resolve(JSON.parse(body)); } catch(e) { return resolve(body); } | ||
try { | ||
const json = JSON.parse(body); | ||
if(json.status === 429) return reject({ error: "Too Many Requests" }); | ||
else return resolve(json); | ||
} catch(e) { return resolve(body); } | ||
}); | ||
@@ -27,11 +31,32 @@ }); | ||
if(params.access_token) Helix.prototype.access_token = params.access_token; | ||
else | ||
if(params.access_token) { | ||
Helix.prototype.access_token = params.access_token; | ||
Helix.prototype.auth = { | ||
OAuth: `OAuth ${params.access_token}`, | ||
Bearer: `Bearer ${params.access_token}` | ||
}; | ||
if(params.increaseRate) { | ||
Helix.prototype.increaseRate = true; | ||
Helix.prototype.headers = Object.assign(Helix.prototype.headers, { "Authorization": Helix.prototype.auth.Bearer }); | ||
} else Helix.prototype.headers = Object.assign(Helix.prototype.headers, { "Authorization": Helix.prototype.auth.OAuth }); | ||
} | ||
else { | ||
if(!params.disableWarns) | ||
console.warn("You not set up access token"); | ||
if(params.increaseRate) | ||
console.warn("To increase the rate you need to provide access_token"); | ||
} | ||
}; | ||
handleError(error) { throw error; } | ||
oauth() { | ||
let headers = this.headers; | ||
headers["Authorization"] = this.auth.OAuth; | ||
return headers; | ||
} | ||
async getUser(user) { | ||
const url = `https://api.twitch.tv/helix/users?${Number(user) ? "id" : "login"}=${user}`; | ||
const response = await syncRequest({ url, headers: this.headers }); | ||
const response = await syncRequest({ url, headers: this.headers }).catch(this.handleError); | ||
return response.data[0]; | ||
@@ -42,3 +67,3 @@ } | ||
const url = `https://api.twitch.tv/kraken/channels/${user_id}`; | ||
return await syncRequest({ url, headers: this.headers }); | ||
return await syncRequest({ url, headers: this.headers }).catch(this.handleError);; | ||
} | ||
@@ -48,3 +73,3 @@ | ||
const url = `https://api.twitch.tv/helix/streams?${Number(user) ? "user_id" : "user_login"}=${user}`; | ||
const response = await syncRequest({ url, headers: this.headers }); | ||
const response = await syncRequest({ url, headers: this.headers }).catch(this.handleError); | ||
return response.data[0] ? response.data[0] : { error: "You must start stream to get stream data or wait for Twitch to announce you online" }; | ||
@@ -55,3 +80,3 @@ } | ||
const url = `https://api.twitch.tv/helix/streams/metadata?${Number(user) ? "user_id" : "user_login"}=${user}`; | ||
const response = await syncRequest({ url, headers: this.headers }); | ||
const response = await syncRequest({ url, headers: this.headers }).catch(this.handleError); | ||
return response.data[0] ? response.data[0] : { error: "You must start stream to get stream data or wait for Twitch to announce you online" }; | ||
@@ -62,3 +87,3 @@ } | ||
const url = `https://api.twitch.tv/helix/games?${Number(game) ? "game_id" : "name"}=${game}`; | ||
const response = await syncRequest({ url, headers: this.headers }); | ||
const response = await syncRequest({ url, headers: this.headers }).catch(this.handleError); | ||
return response.data[0]; | ||
@@ -68,2 +93,3 @@ } | ||
async getFollowers(user_id, count = 20, after = "") { | ||
if(count > 100) return console.error("You can't fetch more than 100 followers per request"); | ||
const url = `https://api.twitch.tv/helix/users/follows?to_id=${user_id}&first=${count}&after=${after}`; | ||
@@ -87,3 +113,3 @@ return await syncRequest({ url, headers: this.headers }); | ||
get(); | ||
return get(); | ||
}); | ||
@@ -95,3 +121,3 @@ | ||
const url = `https://api.twitch.tv/helix/users/follows?to_id=${user_id}`; | ||
const { total } = await syncRequest({ url, headers: this.headers }); | ||
const { total } = await syncRequest({ url, headers: this.headers }).catch(this.handleError); | ||
return total; | ||
@@ -102,3 +128,3 @@ } | ||
user = user.toLowerCase(); | ||
return await syncRequest(`https://tmi.twitch.tv/group/user/${user}/chatters`); | ||
return await syncRequest(`https://tmi.twitch.tv/group/user/${user}/chatters`).catch(this.handleError); | ||
} | ||
@@ -117,4 +143,4 @@ | ||
}, | ||
headers: Object.assign(this.headers, { "Authorization": `OAuth ${this.access_token}` }) | ||
}); | ||
headers: this.oauth() | ||
}).catch(this.handleError); | ||
return { success: response.status == title && response.game == game }; | ||
@@ -129,4 +155,4 @@ } | ||
json: { user_id: id, description }, | ||
headers: Object.assign(this.headers, { "Authorization": `OAuth ${this.access_token}` }) | ||
}); | ||
headers: this.oauth() | ||
}).catch(this.handleError); | ||
if(response.error) return { status: "error", error: response.error }; | ||
@@ -142,4 +168,4 @@ return Object.assign({ status: "success" }, response); | ||
json: { user_id: id, video_id }, | ||
headers: Object.assign(this.headers, { "Authorization": `OAuth ${this.access_token}` }) | ||
}); | ||
headers: this.oauth() | ||
}).catch(this.handleError); | ||
} | ||
@@ -149,4 +175,4 @@ | ||
const url = `https://api.twitch.tv/helix/games/top?first=${count}`; | ||
const response = await syncRequest({ url, headers: this.headers }); | ||
return response.data; | ||
const { data } = await syncRequest({ url, headers: this.headers }).catch(this.handleError); | ||
return data; | ||
} | ||
@@ -153,0 +179,0 @@ |
{ | ||
"name": "simple-helix-api", | ||
"version": "1.0.6", | ||
"version": "1.0.8", | ||
"description": "The Simple Helix API allows developers to easily develop applications for Twitch", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -32,2 +32,3 @@ # About | ||
| client_id | true | null | Client ID of application | | ||
| increaseRate | false | false | Use Bearer instead of OAuth to increase Rate Limit | | ||
| disableWarns | false | false | Disabled warnings in console | | ||
@@ -41,2 +42,8 @@ | ||
## Increase Rate | ||
This option uses Bearer authorization instead of OAuth, which allows you to increase the number of requests per minute to 800 instead of 30. | ||
If you want to use this, you need to know that methods like ```updateStream(), createMarker()``` force OAuth authorization. | ||
## Common methods | ||
@@ -109,3 +116,3 @@ | ||
Get viewers splitted by categories (broadcaster, admins, staff, moderators, vips, viewers). | ||
Get viewers splitted by categories (broadcaster, admins, staff, moderators, vips, viewers). | ||
Attention! This method used username instead of user ID | ||
@@ -112,0 +119,0 @@ |
13996
152
209
3