Files
shields/services/twitch/twitch.service.js
2021-02-14 19:41:02 +00:00

68 lines
1.5 KiB
JavaScript

'use strict'
const Joi = require('joi')
const TwitchBase = require('./twitch-base')
const helixSchema = Joi.object({
data: Joi.array().required(),
})
module.exports = class TwitchStatus extends TwitchBase {
static category = 'social'
static route = {
base: 'twitch/status',
pattern: ':user',
}
static examples = [
{
title: 'Twitch Status',
namedParams: {
user: 'andyonthewings',
},
queryParams: { style: 'social' },
staticPreview: {
message: 'live',
color: 'red',
style: 'social',
},
},
]
static defaultBadgeData = {
label: 'twitch',
namedLogo: 'twitch',
}
static render({ user, isLive }) {
return {
message: isLive ? 'live' : 'offline',
color: isLive ? 'red' : 'lightgrey',
link: `https://www.twitch.tv/${user}`,
}
}
async fetch({ user }) {
// If `user` does not exist on Twitch,
// https://api.twitch.tv/helix/streams returns an empty array,
// which is the same as when a user is offline.
// Checking for whether a user exists needs another API call,
// which we consider not worth it.
const streams = this._requestJson({
schema: helixSchema,
url: `https://api.twitch.tv/helix/streams`,
options: {
qs: { user_login: user },
},
})
return streams
}
async handle({ user }) {
const data = await this.fetch({ user })
return this.constructor.render({ user, isLive: data.data.length > 0 })
}
}