-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcasterApiClient.js
33 lines (27 loc) · 1016 Bytes
/
broadcasterApiClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const config = require('./config')
const {RefreshingAuthProvider} = require("@twurple/auth");
const TokenService = require("./services/token");
const {ApiClient} = require("@twurple/api");
class BroadcasterApiClient {
constructor() {
this.apiClient = null
}
async getApiClient () {
if (this.apiClient === null) {
const authProvider = new RefreshingAuthProvider(
{
clientId: config.twitch.clientId,
clientSecret: config.twitch.clientSecret
}
)
const tokenData = await TokenService.getToken(config.twitch.roomId)
authProvider.onRefresh(async (userId, newTokenData) => {
await TokenService.updateToken(userId, newTokenData)
})
await authProvider.addUserForToken(tokenData);
this.apiClient = new ApiClient({authProvider})
}
return this.apiClient
}
}
module.exports = new BroadcasterApiClient()