twitch-easy
Advanced tools
Comparing version 0.0.3 to 0.0.4
import TwitchAPI from './twitchAPI'; | ||
export = TwitchAPI; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -1,25 +0,25 @@ | ||
declare const _default: { | ||
new (clientId: string, clientSecret: string): { | ||
CLIENT_ID: string; | ||
CLIENT_SECRET: string; | ||
twitchAouth2: string; | ||
token: { | ||
access_token: string; | ||
expires_in: number; | ||
time: number; | ||
token_type: string; | ||
}; | ||
twitch: { | ||
GET_CHANNEL: string; | ||
GET_STREAM: string; | ||
}; | ||
getToken(): Promise<void>; | ||
checkToken(): Promise<Boolean>; | ||
getStreamersByName(name: string, quantity?: number, paginator?: string | undefined): Promise<ChannelSearchName | null>; | ||
getStreamerByName(name: string): Promise<StreamerByName | null>; | ||
getStreamersOnline(id: string, quantity?: number, paginator?: string | undefined): Promise<StreamerSearchOnline | null>; | ||
getStreamerOnline(id: string): Promise<StreamerOnline | null>; | ||
}; | ||
}; | ||
export = _default; | ||
import { ITwitchAPI, ChannelSearchName, StreamerByName, StreamerOnline, StreamerSearchOnline } from './types/twitchAPI'; | ||
export default class TwitchAPI implements ITwitchAPI { | ||
private CLIENT_ID; | ||
private CLIENT_SECRET; | ||
private ratelimit_reset?; | ||
private token; | ||
constructor(clientId: string, clientSecret: string); | ||
getToken(): Promise<void>; | ||
checkToken(): Promise<Boolean>; | ||
private updateRateReset; | ||
private createHeader; | ||
getStreamersByName({ name, quantity, paginator, }: { | ||
name: string; | ||
quantity: number; | ||
paginator?: string; | ||
}): Promise<ChannelSearchName | null>; | ||
getStreamerByName(name: string): Promise<StreamerByName | null>; | ||
getStreamersOnline({ id, quantity, paginator, }: { | ||
id: string; | ||
quantity: number; | ||
paginator?: string; | ||
}): Promise<StreamerSearchOnline | null>; | ||
getStreamerOnline(id: string): Promise<StreamerOnline | null>; | ||
} | ||
//# sourceMappingURL=twitchAPI.d.ts.map |
@@ -14,12 +14,9 @@ "use strict"; | ||
}; | ||
const node_fetch_1 = __importDefault(require("node-fetch")); | ||
module.exports = class TwitchAPI { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const axios_1 = __importDefault(require("axios")); | ||
const defaults_1 = require("./defaults"); | ||
class TwitchAPI { | ||
constructor(clientId, clientSecret) { | ||
this.CLIENT_ID = clientId || ''; | ||
this.CLIENT_SECRET = clientSecret || ''; | ||
this.twitchAouth2 = 'https://id.twitch.tv/oauth2/token'; | ||
this.twitch = { | ||
GET_CHANNEL: 'https://api.twitch.tv/helix/search/channels', | ||
GET_STREAM: 'https://api.twitch.tv/helix/streams', | ||
}; | ||
this.CLIENT_ID = clientId; | ||
this.CLIENT_SECRET = clientSecret; | ||
this.token = { | ||
@@ -41,7 +38,8 @@ access_token: '', | ||
}; | ||
const token = yield node_fetch_1.default(this.twitchAouth2, { | ||
const token = yield (0, axios_1.default)({ | ||
url: defaults_1.twitchAouth2, | ||
method: 'post', | ||
body: JSON.stringify(body), | ||
headers: { 'Content-type': 'application/json' }, | ||
}).then((res) => res.json()); | ||
data: body, | ||
}).then((res) => res.data); | ||
this.token.access_token = token.access_token; | ||
@@ -62,3 +60,16 @@ this.token.expires_in = token.expires_in; | ||
} | ||
getStreamersByName(name, quantity = 20, paginator) { | ||
updateRateReset(rate) { | ||
if (!rate) | ||
return; | ||
this.ratelimit_reset = new Date(parseInt(rate, 10) * 1000); | ||
} | ||
createHeader() { | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
'Client-ID': this.CLIENT_ID, | ||
Authorization: `Bearer ${this.token.access_token}`, | ||
}; | ||
return headers; | ||
} | ||
getStreamersByName({ name, quantity = 20, paginator, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -68,17 +79,19 @@ if (!name) | ||
yield this.getToken(); | ||
const headers = this.createHeader(); | ||
const url = paginator | ||
? `${this.twitch.GET_CHANNEL}?first=${quantity}&query=${name}&after=${paginator}` | ||
: `${this.twitch.GET_CHANNEL}?first=${quantity}&query=${name}`; | ||
const streamers = yield node_fetch_1.default(url, { | ||
? `${defaults_1.GET_CHANNEL}?first=${quantity}&query=${name}&after=${paginator}` | ||
: `${defaults_1.GET_CHANNEL}?first=${quantity}&query=${name}`; | ||
const streamers = yield (0, axios_1.default)({ | ||
url, | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Client-ID': this.CLIENT_ID, | ||
Authorization: `Bearer ${this.token.access_token}`, | ||
}, | ||
headers, | ||
}) | ||
.then((res) => { | ||
if (res.status === 200 && res.ok) { | ||
return res.json(); | ||
this.updateRateReset(res.headers['ratelimit-reset']); | ||
if (res.status === 200) { | ||
return res.data; | ||
} | ||
if (res.status === 429) { | ||
throw new Error(`Excess rate limit, will be reset at ${this.ratelimit_reset}`); | ||
} | ||
return null; | ||
@@ -102,5 +115,5 @@ }) | ||
if (!cursor) | ||
streamers = yield this.getStreamersByName(name, 100); | ||
streamers = yield this.getStreamersByName({ name, quantity: 100 }); | ||
else if (cursor) | ||
streamers = yield this.getStreamersByName(name, 100, cursor); | ||
streamers = yield this.getStreamersByName({ name, quantity: 100, paginator: cursor }); | ||
if (!streamers) | ||
@@ -125,3 +138,3 @@ finish = true; | ||
} | ||
getStreamersOnline(id, quantity = 20, paginator) { | ||
getStreamersOnline({ id, quantity = 20, paginator, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -131,17 +144,17 @@ if (!id) | ||
yield this.getToken(); | ||
const url = paginator | ||
? `${this.twitch.GET_STREAM}?first=${quantity}&user_id=${id}&after=${paginator}` | ||
: `${this.twitch.GET_STREAM}?first=${quantity}&user_id=${id}`; | ||
const streamers = yield node_fetch_1.default(url, { | ||
const headers = this.createHeader(); | ||
const url = paginator ? `${defaults_1.GET_STREAM}?first=${quantity}&user_id=${id}&after=${paginator}` : `${defaults_1.GET_STREAM}?first=${quantity}&user_id=${id}`; | ||
const streamers = yield (0, axios_1.default)({ | ||
url, | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Client-ID': this.CLIENT_ID, | ||
Authorization: `Bearer ${this.token.access_token}`, | ||
}, | ||
headers, | ||
}) | ||
.then((res) => { | ||
if (res.status === 200 && res.ok) { | ||
return res.json(); | ||
this.updateRateReset(res.headers['ratelimit-reset']); | ||
if (res.status === 200) { | ||
return res.data; | ||
} | ||
if (res.status === 429) { | ||
throw new Error(`Excess rate limit, will be reset at ${this.ratelimit_reset}`); | ||
} | ||
return null; | ||
@@ -165,5 +178,5 @@ }) | ||
if (!cursor) | ||
streamers = yield this.getStreamersOnline(id, 100); | ||
streamers = yield this.getStreamersOnline({ id, quantity: 100 }); | ||
else if (cursor) | ||
streamers = yield this.getStreamersOnline(id, 100, cursor); | ||
streamers = yield this.getStreamersOnline({ id, quantity: 100, paginator: cursor }); | ||
if (!streamers) | ||
@@ -188,3 +201,4 @@ finish = true; | ||
} | ||
}; | ||
} | ||
exports.default = TwitchAPI; | ||
//# sourceMappingURL=twitchAPI.js.map |
{ | ||
"name": "twitch-easy", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Easy twitch api wrapper", | ||
@@ -9,3 +9,4 @@ "main": "lib/index.js", | ||
"pack": "npm pack", | ||
"build": "tsc -p ." | ||
"build": "tsc -p .", | ||
"start": "tsc && node ." | ||
}, | ||
@@ -18,24 +19,16 @@ "author": { | ||
"dependencies": { | ||
"node-fetch": "^2.6.1" | ||
"axios": "^0.26.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.17.6", | ||
"@types/node-fetch": "^2.5.12", | ||
"@typescript-eslint/eslint-plugin": "^4.28.5", | ||
"@typescript-eslint/parser": "^4.28.5", | ||
"eslint": "^7.31.0", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.23.4", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"prettier": "^2.3.2", | ||
"typescript": "^4.3.5" | ||
"@types/node": "^17.0.21", | ||
"@typescript-eslint/eslint-plugin": "^5.13.0", | ||
"@typescript-eslint/parser": "^5.13.0", | ||
"eslint": "^8.10.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-prettier": "^8.4.0", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"prettier": "^2.5.1", | ||
"typescript": "^4.5.5" | ||
}, | ||
"prettier": { | ||
"trailingComma": "es5", | ||
"tabWidth": 4, | ||
"printWidth": 150, | ||
"semi": true, | ||
"singleQuote": true | ||
}, | ||
"repository": { | ||
@@ -42,0 +35,0 @@ "type": "git", |
@@ -46,3 +46,3 @@ <p align="center"> | ||
async function getStreamer() { | ||
const getStreamer = () => { | ||
const streamer = await api.getStreamerByName('streamer_name'); | ||
@@ -58,8 +58,8 @@ console.log(streamer); | ||
`getStreamersByName (name, qnt, paginator?)` - Returns a array list referring to the searched streamer name | ||
`getStreamersByName ({ name, qnt, paginator? })` - Returns a array list referring to the searched streamer name | ||
`getStreamerByName (name)` - Returns object referring to the searched streamer name | ||
`getStreamersOnline (name, qnt, paginator?)` - Returns a array list referring to the searched online streamer id | ||
`getStreamersOnline ({ name, qnt, paginator? })` - Returns a array list referring to the searched online streamer id | ||
`getStreamerOnline (id)` - Returns object referring to the searched online streamer id |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
31874
10
20
395
+ Addedaxios@^0.26.0
+ Addedaxios@0.26.1(transitive)
+ Addedfollow-redirects@1.15.6(transitive)
- Removednode-fetch@^2.6.1
- Removednode-fetch@2.7.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)