twitch-new-api
Advanced tools
Comparing version 1.0.1 to 1.1.0
191
index.js
const fetch = require("node-fetch"); | ||
let temp = { | ||
followers: [], | ||
pagination: "" | ||
}; | ||
/** | ||
@@ -23,7 +28,8 @@ * @param clientId | ||
getStream: `https://api.twitch.tv/helix/streams`, | ||
getGames: `https://api.twitch.tv/helix/games` | ||
getGames: `https://api.twitch.tv/helix/games`, | ||
getFollowers: `https://api.twitch.tv/helix/users/follows` | ||
}; | ||
} | ||
/** | ||
* @param login login (username) of the user, Example: 'twitch' | ||
* @param {String} login login (username) of the user, Example: 'twitch' | ||
*/ | ||
@@ -45,3 +51,3 @@ async getUserByLogin(login) { | ||
/** | ||
* @param id id of the user, Example: '12826' | ||
* @param {Number} id id of the user, Example: '12826' | ||
*/ | ||
@@ -57,3 +63,4 @@ async getUserById(id) { | ||
}); | ||
return await fetchResponse.json(); | ||
let fetchData = await fetchResponse.json(); | ||
return fetchData.data[0]; | ||
} catch (err) { | ||
@@ -64,3 +71,3 @@ console.log(err); | ||
/** | ||
* @param id id of the user, Example: '12826' | ||
* @param {Number} id id of the user, Example: '12826' | ||
*/ | ||
@@ -82,3 +89,3 @@ async getStreamById(id) { | ||
/** | ||
* @param login login (username) of the user, Example: 'twitch' | ||
* @param {String} login login (username) of the user, Example: 'twitch' | ||
*/ | ||
@@ -103,3 +110,3 @@ async getStreamByLogin(login) { | ||
/** | ||
* @param gameId id of the game, Example: 'twitch' | ||
* @param {Number} gameId id of the game, Example: 'twitch' | ||
*/ | ||
@@ -120,2 +127,172 @@ async getGameById(gameId) { | ||
} | ||
/** | ||
* @param {Number} id id of the user, Example: '12826' | ||
*/ | ||
async getAllFollowersToId(id) { | ||
try { | ||
temp.followers = []; | ||
await isTokenValid(this); | ||
let fetchResponse = await fetch( | ||
`${this.url.getFollowers}?to_id=${id}&first=100`, | ||
{ | ||
method: `GET`, | ||
headers: { | ||
Authorization: `${this.data.tokenType} ${this.data.accessToken}` | ||
} | ||
} | ||
); | ||
let fetchData = await fetchResponse.json(); | ||
if (fetchData.total > 100) { | ||
let counter = Math.floor(fetchData.total / 100); | ||
temp.pagination = fetchData.pagination.cursor; | ||
for (const element of fetchData.data) { | ||
temp.followers.push(element); | ||
} | ||
for (let i = 0; i < counter; i++) { | ||
let fetchResponse = await fetch( | ||
`${this.url.getFollowers}?to_id=${id}&first=100&after=${temp.pagination}`, | ||
{ | ||
method: `GET`, | ||
headers: { | ||
Authorization: `${this.data.tokenType} ${this.data.accessToken}` | ||
} | ||
} | ||
); | ||
let fetchData = await fetchResponse.json(); | ||
temp.pagination = fetchData.pagination.cursor; | ||
if (fetchData.data && fetchData.data.length) { | ||
for (const element of fetchData.data) { | ||
temp.followers.push(element); | ||
} | ||
} | ||
} | ||
} else if (fetchData.data && fetchData.data.length) { | ||
for (const element of fetchData.data) { | ||
temp.followers.push(element); | ||
} | ||
} else if (fetchData.error) { | ||
return fetchData; | ||
} | ||
return temp.followers; | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
/** | ||
* @param {Number} id id of the user, Example: '12826' | ||
* @param {Number} number number of how many followers you want, Example: '20' | ||
*/ | ||
async getFollowersToId(id, number) { | ||
try { | ||
if (number <= 0 || number > 100) { | ||
throw new Error( | ||
`number must be a Number and greater than 0 and smaller than 100` | ||
); | ||
} | ||
temp.followers = []; | ||
await isTokenValid(this); | ||
let fetchResponse = await fetch( | ||
`${this.url.getFollowers}?to_id=${id}&first=${number}`, | ||
{ | ||
method: `GET`, | ||
headers: { | ||
Authorization: `${this.data.tokenType} ${this.data.accessToken}` | ||
} | ||
} | ||
); | ||
let fetchData = await fetchResponse.json(); | ||
if (fetchData.error) { | ||
return fetchData; | ||
} | ||
return fetchData.data; | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
/** | ||
* @param {Number} id id of the user, Example: '12826' | ||
*/ | ||
async getAllFollowersFromId(id) { | ||
try { | ||
temp.followers = []; | ||
await isTokenValid(this); | ||
let fetchResponse = await fetch( | ||
`${this.url.getFollowers}?from_id=${id}&first=100`, | ||
{ | ||
method: `GET`, | ||
headers: { | ||
Authorization: `${this.data.tokenType} ${this.data.accessToken}` | ||
} | ||
} | ||
); | ||
let fetchData = await fetchResponse.json(); | ||
if (fetchData.total > 100) { | ||
let counter = Math.floor(fetchData.total / 100); | ||
temp.pagination = fetchData.pagination.cursor; | ||
for (const element of fetchData.data) { | ||
temp.followers.push(element); | ||
} | ||
for (let i = 0; i < counter; i++) { | ||
let fetchResponse = await fetch( | ||
`${this.url.getFollowers}?to_id=${id}&first=100&after=${temp.pagination}`, | ||
{ | ||
method: `GET`, | ||
headers: { | ||
Authorization: `${this.data.tokenType} ${this.data.accessToken}` | ||
} | ||
} | ||
); | ||
let fetchData = await fetchResponse.json(); | ||
temp.pagination = fetchData.pagination.cursor; | ||
if (fetchData.data && fetchData.data.length) { | ||
for (const element of fetchData.data) { | ||
temp.followers.push(element); | ||
} | ||
} | ||
} | ||
} else if (fetchData.data && fetchData.data.length) { | ||
for (const element of fetchData.data) { | ||
temp.followers.push(element); | ||
} | ||
} else if (fetchData.error) { | ||
return fetchData; | ||
} | ||
return temp.followers; | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
/** | ||
* @param {Number} id id of the user, Example: '12826' | ||
* @param {Number} number number of how many followers you want, Example: '20' | ||
*/ | ||
async getFollowersFromId(id, number) { | ||
try { | ||
if (number <= 0 || number > 100) { | ||
throw new Error( | ||
`number must be a Number and greater than 0 and smaller than 100` | ||
); | ||
} | ||
temp.followers = []; | ||
await isTokenValid(this); | ||
let fetchResponse = await fetch( | ||
`${this.url.getFollowers}?to_id=${id}&first=${number}`, | ||
{ | ||
method: `GET`, | ||
headers: { | ||
Authorization: `${this.data.tokenType} ${this.data.accessToken}` | ||
} | ||
} | ||
); | ||
let fetchData = await fetchResponse.json(); | ||
if (fetchData.error) { | ||
return fetchData; | ||
} | ||
return fetchData.data; | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
} | ||
@@ -122,0 +299,0 @@ |
{ | ||
"name": "twitch-new-api", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Twitch New Api", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,4 +31,8 @@ # Introduction | ||
TwitchClient.getUserByLogin(userLogin); // gets user data from User Login - Example: twitch | ||
TwitchClient.getAllFollowerToId(userId); // gets all of followers from User ID - Example: 12826 | ||
TwitchClient.getFollowerToId(userId, number); // gets a number followers from User ID - Example: 12826, 20 | ||
TwitchClient.getAllFollowerFromId(userId); // gets all of followings from User ID - Example: 12826 | ||
TwitchClient.getFollowerFromId(userId, number); // gets a number of followings from User ID - Example: 12826, 20 | ||
``` | ||
# License - MIT |
11460
320
38
12