33 lines
788 B
JavaScript
33 lines
788 B
JavaScript
const axios = require('axios')
|
|
const config = require('@config/index')
|
|
const baseURL = `https://api.twitch.tv/helix`
|
|
const client = axios.create({
|
|
baseURL,
|
|
headers: {
|
|
'Client-ID': config.twitch.clientid
|
|
}
|
|
})
|
|
|
|
const tokenHeader = (token) => {
|
|
if (typeof token !== 'string' || token.trim().length === 0) return null
|
|
return {
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
}
|
|
|
|
module.exports.getUserData = async (token = '') => {
|
|
let headers = tokenHeader(token)
|
|
if (headers === null) return null
|
|
let url = '/users'
|
|
|
|
let result = await client({
|
|
method: 'get',
|
|
headers,
|
|
url
|
|
})
|
|
|
|
if (!('data' in result) || !('data' in result.data) || !Array.isArray(result.data.data)) return null
|
|
if (result.data.data.length === 0) return null
|
|
return result.data.data[0]
|
|
}
|