Files
shields/services/twitch/twitch.service.js
Andy Li 4b9053d2d5 add [twitch] user status badge (#3683)
* add /twitch/status/:user badge

* update comments

* use a proper schema for the Twitch API calls

* use a token to make Twitch api calls

* fix handling of rate-limit error and bad token error

* [twitch] get a token as soon as creating a Twitch service

* [twitch] start both requests to users and stream before awaiting

* [twitch] set a timeout to replace the token before it expires

* [twitch] use authHelper

* [twitch] skip tests when no credentials

* [twitch] add one more status test

* twitch: do not check whether a user exists
2019-09-09 16:41:32 -05:00

78 lines
1.6 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const TwitchBase = require('./twitch-base')
const helixSchema = Joi.object({
data: Joi.array().required(),
})
class TwitchStatus extends TwitchBase {
static get category() {
return 'activity'
}
static get route() {
return {
base: 'twitch/status',
pattern: ':user',
}
}
static get examples() {
const preview = this.render({
user: 'andyonthewings',
isLive: true,
})
// link[] is not allowed in examples
delete preview.link
return [
{
title: 'Twitch Status',
namedParams: {
user: 'andyonthewings',
},
staticPreview: preview,
},
]
}
static get defaultBadgeData() {
return {
label: '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 })
}
}
module.exports = [TwitchStatus]