Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 1x 1x 19x 19x 19x 19x 15x 19x 1x 1x 12x 12x 12x 12x 12x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 28x 28x 28x 1x 1x 52x 52x 52x 51x 52x 52x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 19x 19x 19x 19x 19x 19x 17x 16x 16x 177x 176x 16x 19x 19x 9x 9x 19x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 19x 19x 19x 19x 19x 1x 1x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 132x 132x 132x 132x 132x 132x 22x 110x 132x 132x 132x 132x 132x 132x 132x 132x 4x 4x 4x 4x 132x 132x 16x 8x 8x 16x 16x 132x 112x 112x 132x 2x 2x 132x 132x 132x 132x 1x | import { z } from "zod"
import type { TwitchCredentials } from "@workspace/env/twitch"
export const BOT_SCOPES = [
"user:read:chat",
"user:write:chat",
"user:bot",
] as const
export type TwitchFailureCode =
| "timeout"
| "networkUnavailable"
| "apiUnavailable"
| "unauthorized"
| "malformedResponse"
| "wrongClient"
| "wrongBot"
| "missingScope"
| "expiredToken"
| "broadcasterUnavailable"
| "messageDropped"
export class TwitchFailure extends Error {
constructor(
readonly code: TwitchFailureCode,
readonly status?: number
) {
super(`Twitch operation failed: ${code}`)
this.name = "TwitchFailure"
}
}
const tokenSchema = z.object({
access_token: z.string().min(1),
expires_in: z.number().int().positive(),
token_type: z.literal("bearer"),
})
const botTokenSchema = tokenSchema.extend({
refresh_token: z.string().min(1),
scope: z.array(z.string()),
})
const validationSchema = z.object({
client_id: z.string().min(1),
user_id: z.string().nullish(),
scopes: z
.array(z.string())
.nullable()
.transform((scopes) => scopes ?? []),
expires_in: z.number().int().nonnegative(),
})
export type BotToken = z.infer<typeof botTokenSchema>
export class TwitchApiService {
constructor(
private readonly config: Pick<
TwitchCredentials,
| "TWITCH_CLIENT_ID"
| "TWITCH_CLIENT_SECRET"
| "TWITCH_BOT_USER_ID"
| "TWITCH_HTTP_TIMEOUT_MS"
>,
private readonly requestFetch: typeof fetch = fetch,
private readonly shutdownSignal?: AbortSignal
) {}
async acquireAppToken(): Promise<string> {
const token = await this.tokenRequest(tokenSchema, {
grant_type: "client_credentials",
})
await this.validateAppToken(token.access_token)
return token.access_token
}
refreshBotToken(refreshToken: string): Promise<BotToken> {
return this.tokenRequest(botTokenSchema, {
grant_type: "refresh_token",
refresh_token: refreshToken,
})
}
exchangeBotCode(code: string, redirectUri: string): Promise<BotToken> {
return this.tokenRequest(botTokenSchema, {
grant_type: "authorization_code",
code,
redirect_uri: redirectUri,
})
}
async validateAppToken(accessToken: string): Promise<void> {
const value = await this.validate(accessToken)
if (value.user_id) throw new TwitchFailure("wrongClient")
}
async validateBotToken(accessToken: string): Promise<void> {
const value = await this.validate(accessToken)
if (value.user_id !== this.config.TWITCH_BOT_USER_ID)
throw new TwitchFailure("wrongBot")
if (!BOT_SCOPES.every((scope) => value.scopes.includes(scope)))
throw new TwitchFailure("missingScope")
}
async broadcasterExists(
appToken: string,
broadcasterId: string
): Promise<void> {
const result = await this.request(
z.object({ data: z.array(z.object({ id: z.string() })) }),
`https://api.twitch.tv/helix/users?id=${encodeURIComponent(broadcasterId)}`,
this.helixHeaders(appToken)
)
if (!result.data.some((user) => user.id === broadcasterId))
throw new TwitchFailure("broadcasterUnavailable")
}
async sendChatMessage(
botUserToken: string,
broadcasterId: string,
message: string
): Promise<string> {
if (
!message.trim() ||
[...message].length > 500 ||
[...message].some(
(character) =>
character.charCodeAt(0) < 32 ||
(character.charCodeAt(0) >= 127 && character.charCodeAt(0) <= 159)
)
)
throw new Error(
"Message must contain 1-500 characters without control characters."
)
const result = await this.request(
z.object({
data: z.array(
z.object({ message_id: z.string(), is_sent: z.boolean() })
),
}),
"https://api.twitch.tv/helix/chat/messages",
{
...this.helixHeaders(botUserToken),
method: "POST",
body: JSON.stringify({
broadcaster_id: broadcasterId,
sender_id: this.config.TWITCH_BOT_USER_ID,
message,
}),
}
)
const entry = result.data[0]
if (!entry?.is_sent || !entry.message_id)
throw new TwitchFailure("messageDropped")
return entry.message_id
}
private async validate(accessToken: string) {
const value = await this.request(
validationSchema,
"https://id.twitch.tv/oauth2/validate",
{ headers: { Authorization: `OAuth ${accessToken}` } }
)
if (value.client_id !== this.config.TWITCH_CLIENT_ID)
throw new TwitchFailure("wrongClient")
if (value.expires_in === 0) throw new TwitchFailure("expiredToken")
return value
}
private tokenRequest<T>(
schema: z.ZodType<T>,
fields: Record<string, string>
): Promise<T> {
return this.request(schema, "https://id.twitch.tv/oauth2/token", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: this.config.TWITCH_CLIENT_ID,
client_secret: this.config.TWITCH_CLIENT_SECRET,
...fields,
}),
})
}
private helixHeaders(appToken: string): RequestInit {
return {
headers: {
Authorization: `Bearer ${appToken}`,
"Client-Id": this.config.TWITCH_CLIENT_ID,
"Content-Type": "application/json",
},
}
}
private async request<T>(
schema: z.ZodType<T>,
url: string,
init: RequestInit
): Promise<T> {
const timeout = AbortSignal.timeout(this.config.TWITCH_HTTP_TIMEOUT_MS)
const signal = this.shutdownSignal
? AbortSignal.any([timeout, this.shutdownSignal])
: timeout
let response: Response
try {
response = await this.requestFetch(url, {
...init,
signal,
redirect: "error",
})
} catch {
throw new TwitchFailure(
timeout.aborted ? "timeout" : "networkUnavailable"
)
}
if (!response.ok)
throw new TwitchFailure(
response.status === 401 || response.status === 403
? "unauthorized"
: "apiUnavailable",
response.status
)
let body: unknown
try {
body = await response.json()
} catch {
throw new TwitchFailure(timeout.aborted ? "timeout" : "malformedResponse")
}
const parsed = schema.safeParse(body)
if (!parsed.success) throw new TwitchFailure("malformedResponse")
return parsed.data
}
}
|