@gakr-gakr/twitch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/token.ts ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Twitch access token resolution with environment variable support.
3
+ *
4
+ * Supports reading Twitch OAuth access tokens from config or environment variable.
5
+ * The AUTOBOT_TWITCH_ACCESS_TOKEN env var is only used for the default account.
6
+ *
7
+ * Token resolution priority:
8
+ * 1. Account access token from merged config (accounts.{id} or base-level for default)
9
+ * 2. Environment variable: AUTOBOT_TWITCH_ACCESS_TOKEN (default account only)
10
+ */
11
+
12
+ import {
13
+ DEFAULT_ACCOUNT_ID,
14
+ normalizeAccountId,
15
+ resolveNormalizedAccountEntry,
16
+ } from "autobot/plugin-sdk/account-resolution";
17
+ import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
18
+
19
+ export type TwitchTokenSource = "env" | "config" | "none";
20
+
21
+ export type TwitchTokenResolution = {
22
+ token: string;
23
+ source: TwitchTokenSource;
24
+ };
25
+
26
+ /**
27
+ * Normalize a Twitch OAuth token - ensure it has the oauth: prefix
28
+ */
29
+ function normalizeTwitchToken(raw?: string | null): string | undefined {
30
+ if (!raw) {
31
+ return undefined;
32
+ }
33
+ const trimmed = raw.trim();
34
+ if (!trimmed) {
35
+ return undefined;
36
+ }
37
+ // Twitch tokens should have oauth: prefix
38
+ return trimmed.startsWith("oauth:") ? trimmed : `oauth:${trimmed}`;
39
+ }
40
+
41
+ /**
42
+ * Resolve Twitch access token from config or environment variable.
43
+ *
44
+ * Priority:
45
+ * 1. Account access token (from merged config - base-level for default, or accounts.{accountId})
46
+ * 2. Environment variable: AUTOBOT_TWITCH_ACCESS_TOKEN (default account only)
47
+ *
48
+ * The getAccountConfig function handles merging base-level config with accounts.default,
49
+ * so this logic works for both simplified and multi-account patterns.
50
+ *
51
+ * @param cfg - AutoBot config
52
+ * @param opts - Options including accountId and optional envToken override
53
+ * @returns Token resolution with source
54
+ */
55
+ export function resolveTwitchToken(
56
+ cfg?: AutoBotConfig,
57
+ opts: { accountId?: string | null; envToken?: string | null } = {},
58
+ ): TwitchTokenResolution {
59
+ const accountId = normalizeAccountId(opts.accountId);
60
+
61
+ // Get merged account config (handles both simplified and multi-account patterns)
62
+ const twitchCfg = cfg?.channels?.twitch;
63
+ const accounts = twitchCfg?.accounts as Record<string, Record<string, unknown>> | undefined;
64
+ const accountCfg = resolveNormalizedAccountEntry(accounts, accountId, normalizeAccountId);
65
+
66
+ // For default account, also check base-level config
67
+ let token: string | undefined;
68
+ if (accountId === DEFAULT_ACCOUNT_ID) {
69
+ // Base-level config takes precedence
70
+ token = normalizeTwitchToken(
71
+ (typeof twitchCfg?.accessToken === "string" ? twitchCfg.accessToken : undefined) ||
72
+ (accountCfg?.accessToken as string | undefined),
73
+ );
74
+ } else {
75
+ // Non-default accounts only use accounts object
76
+ token = normalizeTwitchToken(accountCfg?.accessToken as string | undefined);
77
+ }
78
+
79
+ if (token) {
80
+ return { token, source: "config" };
81
+ }
82
+
83
+ // Environment variable (default account only)
84
+ const allowEnv = accountId === DEFAULT_ACCOUNT_ID;
85
+ const envToken = allowEnv
86
+ ? normalizeTwitchToken(opts.envToken ?? process.env.AUTOBOT_TWITCH_ACCESS_TOKEN)
87
+ : undefined;
88
+ if (envToken) {
89
+ return { token: envToken, source: "env" };
90
+ }
91
+
92
+ return { token: "", source: "none" };
93
+ }
@@ -0,0 +1,281 @@
1
+ import { RefreshingAuthProvider, StaticAuthProvider } from "@twurple/auth";
2
+ import { ChatClient, LogLevel } from "@twurple/chat";
3
+ import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
4
+ import { formatErrorMessage } from "autobot/plugin-sdk/error-runtime";
5
+ import { resolveTwitchToken } from "./token.js";
6
+ import type { ChannelLogSink, TwitchAccountConfig, TwitchChatMessage } from "./types.js";
7
+ import { normalizeToken } from "./utils/twitch.js";
8
+
9
+ const TWITCH_CHAT_AUTH_INTENTS = ["chat"];
10
+
11
+ /**
12
+ * Manages Twitch chat client connections
13
+ */
14
+ export class TwitchClientManager {
15
+ private clients = new Map<string, ChatClient>();
16
+ private messageHandlers = new Map<string, (message: TwitchChatMessage) => void>();
17
+
18
+ constructor(private logger: ChannelLogSink) {}
19
+
20
+ /**
21
+ * Create an auth provider for the account.
22
+ */
23
+ private async createAuthProvider(
24
+ account: TwitchAccountConfig,
25
+ normalizedToken: string,
26
+ ): Promise<StaticAuthProvider | RefreshingAuthProvider> {
27
+ if (!account.clientId) {
28
+ throw new Error("Missing Twitch client ID");
29
+ }
30
+
31
+ if (account.clientSecret) {
32
+ const authProvider = new RefreshingAuthProvider({
33
+ clientId: account.clientId,
34
+ clientSecret: account.clientSecret,
35
+ });
36
+
37
+ await authProvider
38
+ .addUserForToken(
39
+ {
40
+ accessToken: normalizedToken,
41
+ refreshToken: account.refreshToken ?? null,
42
+ expiresIn: account.expiresIn ?? null,
43
+ obtainmentTimestamp: account.obtainmentTimestamp ?? Date.now(),
44
+ },
45
+ TWITCH_CHAT_AUTH_INTENTS,
46
+ )
47
+ .then((userId) => {
48
+ this.logger.info(
49
+ `Added user ${userId} to RefreshingAuthProvider for ${account.username}`,
50
+ );
51
+ })
52
+ .catch((err) => {
53
+ this.logger.error(
54
+ `Failed to add user to RefreshingAuthProvider: ${formatErrorMessage(err)}`,
55
+ );
56
+ });
57
+
58
+ authProvider.onRefresh((userId, token) => {
59
+ this.logger.info(
60
+ `Access token refreshed for user ${userId} (expires in ${token.expiresIn ? `${token.expiresIn}s` : "unknown"})`,
61
+ );
62
+ });
63
+
64
+ authProvider.onRefreshFailure((userId, error) => {
65
+ this.logger.error(`Failed to refresh access token for user ${userId}: ${error.message}`);
66
+ });
67
+
68
+ const refreshStatus = account.refreshToken
69
+ ? "automatic token refresh enabled"
70
+ : "token refresh disabled (no refresh token)";
71
+ this.logger.info(`Using RefreshingAuthProvider for ${account.username} (${refreshStatus})`);
72
+
73
+ return authProvider;
74
+ }
75
+
76
+ this.logger.info(`Using StaticAuthProvider for ${account.username} (no clientSecret provided)`);
77
+ return new StaticAuthProvider(account.clientId, normalizedToken);
78
+ }
79
+
80
+ /**
81
+ * Get or create a chat client for an account
82
+ */
83
+ async getClient(
84
+ account: TwitchAccountConfig,
85
+ cfg?: AutoBotConfig,
86
+ accountId?: string,
87
+ ): Promise<ChatClient> {
88
+ const key = this.getAccountKey(account);
89
+
90
+ const existing = this.clients.get(key);
91
+ if (existing) {
92
+ return existing;
93
+ }
94
+
95
+ const tokenResolution = resolveTwitchToken(cfg, {
96
+ accountId,
97
+ });
98
+
99
+ if (!tokenResolution.token) {
100
+ this.logger.error(
101
+ `Missing Twitch token for account ${account.username} (set channels.twitch.accounts.${account.username}.token or AUTOBOT_TWITCH_ACCESS_TOKEN for default)`,
102
+ );
103
+ throw new Error("Missing Twitch token");
104
+ }
105
+
106
+ this.logger.debug?.(`Using ${tokenResolution.source} token source for ${account.username}`);
107
+
108
+ if (!account.clientId) {
109
+ this.logger.error(`Missing Twitch client ID for account ${account.username}`);
110
+ throw new Error("Missing Twitch client ID");
111
+ }
112
+
113
+ const normalizedToken = normalizeToken(tokenResolution.token);
114
+
115
+ const authProvider = await this.createAuthProvider(account, normalizedToken);
116
+
117
+ const client = new ChatClient({
118
+ authProvider,
119
+ channels: [account.channel],
120
+ rejoinChannelsOnReconnect: true,
121
+ requestMembershipEvents: true,
122
+ logger: {
123
+ minLevel: LogLevel.WARNING,
124
+ custom: {
125
+ log: (level, message) => {
126
+ switch (level) {
127
+ case LogLevel.CRITICAL:
128
+ this.logger.error(message);
129
+ break;
130
+ case LogLevel.ERROR:
131
+ this.logger.error(message);
132
+ break;
133
+ case LogLevel.WARNING:
134
+ this.logger.warn(message);
135
+ break;
136
+ case LogLevel.INFO:
137
+ this.logger.info(message);
138
+ break;
139
+ case LogLevel.DEBUG:
140
+ this.logger.debug?.(message);
141
+ break;
142
+ case LogLevel.TRACE:
143
+ this.logger.debug?.(message);
144
+ break;
145
+ }
146
+ },
147
+ },
148
+ },
149
+ });
150
+
151
+ this.setupClientHandlers(client, account);
152
+
153
+ client.connect();
154
+
155
+ this.clients.set(key, client);
156
+ this.logger.info(`Connected to Twitch as ${account.username}`);
157
+
158
+ return client;
159
+ }
160
+
161
+ /**
162
+ * Set up message and event handlers for a client
163
+ */
164
+ private setupClientHandlers(client: ChatClient, account: TwitchAccountConfig): void {
165
+ const key = this.getAccountKey(account);
166
+
167
+ // Handle incoming messages
168
+ client.onMessage((channelName, _user, messageText, msg) => {
169
+ const handler = this.messageHandlers.get(key);
170
+ if (handler) {
171
+ const normalizedChannel = channelName.startsWith("#") ? channelName.slice(1) : channelName;
172
+ const from = `twitch:${msg.userInfo.userName}`;
173
+ const preview = messageText.slice(0, 100).replace(/\n/g, "\\n");
174
+ this.logger.debug?.(
175
+ `twitch inbound: channel=${normalizedChannel} from=${from} len=${messageText.length} preview="${preview}"`,
176
+ );
177
+
178
+ handler({
179
+ username: msg.userInfo.userName,
180
+ displayName: msg.userInfo.displayName,
181
+ userId: msg.userInfo.userId,
182
+ message: messageText,
183
+ channel: normalizedChannel,
184
+ id: msg.id,
185
+ timestamp: new Date(),
186
+ isMod: msg.userInfo.isMod,
187
+ isOwner: msg.userInfo.isBroadcaster,
188
+ isVip: msg.userInfo.isVip,
189
+ isSub: msg.userInfo.isSubscriber,
190
+ chatType: "group",
191
+ });
192
+ }
193
+ });
194
+
195
+ this.logger.info(`Set up handlers for ${key}`);
196
+ }
197
+
198
+ /**
199
+ * Set a message handler for an account
200
+ * @returns A function that removes the handler when called
201
+ */
202
+ onMessage(
203
+ account: TwitchAccountConfig,
204
+ handler: (message: TwitchChatMessage) => void,
205
+ ): () => void {
206
+ const key = this.getAccountKey(account);
207
+ this.messageHandlers.set(key, handler);
208
+ return () => {
209
+ this.messageHandlers.delete(key);
210
+ };
211
+ }
212
+
213
+ /**
214
+ * Disconnect a client
215
+ */
216
+ async disconnect(account: TwitchAccountConfig): Promise<void> {
217
+ const key = this.getAccountKey(account);
218
+ const client = this.clients.get(key);
219
+
220
+ if (client) {
221
+ client.quit();
222
+ this.clients.delete(key);
223
+ this.messageHandlers.delete(key);
224
+ this.logger.info(`Disconnected ${key}`);
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Disconnect all clients
230
+ */
231
+ async disconnectAll(): Promise<void> {
232
+ this.clients.forEach((client) => client.quit());
233
+ this.clients.clear();
234
+ this.messageHandlers.clear();
235
+ this.logger.info(" Disconnected all clients");
236
+ }
237
+
238
+ /**
239
+ * Send a message to a channel
240
+ */
241
+ async sendMessage(
242
+ account: TwitchAccountConfig,
243
+ channel: string,
244
+ message: string,
245
+ cfg?: AutoBotConfig,
246
+ accountId?: string,
247
+ ): Promise<{ ok: boolean; error?: string; messageId?: string }> {
248
+ try {
249
+ const client = await this.getClient(account, cfg, accountId);
250
+
251
+ // Generate a message ID (Twurple's say() doesn't return the message ID, so we generate one)
252
+ const messageId = crypto.randomUUID();
253
+
254
+ // Send message (Twurple handles rate limiting)
255
+ await client.say(channel, message);
256
+
257
+ return { ok: true, messageId };
258
+ } catch (error) {
259
+ this.logger.error(`Failed to send message: ${formatErrorMessage(error)}`);
260
+ return {
261
+ ok: false,
262
+ error: formatErrorMessage(error),
263
+ };
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Generate a unique key for an account
269
+ */
270
+ public getAccountKey(account: TwitchAccountConfig): string {
271
+ return `${account.username}:${account.channel}`;
272
+ }
273
+
274
+ /**
275
+ * Clear all clients and handlers (for testing)
276
+ */
277
+ clearForTest(): void {
278
+ this.clients.clear();
279
+ this.messageHandlers.clear();
280
+ }
281
+ }
package/src/types.ts ADDED
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Twitch channel plugin types.
3
+ *
4
+ * This file defines Twitch-specific types. Generic channel types are imported
5
+ * from AutoBot core.
6
+ */
7
+
8
+ import type {
9
+ ChannelAccountSnapshot,
10
+ ChannelLogSink,
11
+ ChannelMessageActionAdapter,
12
+ ChannelMessageActionContext,
13
+ ChannelOutboundAdapter,
14
+ ChannelOutboundContext,
15
+ ChannelPlugin,
16
+ ChannelResolveKind,
17
+ ChannelResolveResult,
18
+ OutboundDeliveryResult,
19
+ } from "../runtime-api.js";
20
+
21
+ // ============================================================================
22
+ // Twitch-Specific Types
23
+ // ============================================================================
24
+
25
+ /**
26
+ * Twitch user roles that can be allowed to interact with the bot
27
+ */
28
+ export type TwitchRole = "moderator" | "owner" | "vip" | "subscriber" | "all";
29
+
30
+ /**
31
+ * Account configuration for a Twitch channel
32
+ */
33
+ export interface TwitchAccountConfig {
34
+ /** Twitch username */
35
+ username: string;
36
+ /** Twitch OAuth access token (requires chat:read and chat:write scopes) */
37
+ accessToken: string;
38
+ /** Twitch client ID (from Twitch Developer Portal or twitchtokengenerator.com) */
39
+ clientId: string;
40
+ /** Channel name to join (required) */
41
+ channel: string;
42
+ /** Enable this account */
43
+ enabled?: boolean;
44
+ /** Allowlist of Twitch user IDs who can interact with the bot (use IDs for safety, not usernames) */
45
+ allowFrom?: Array<string>;
46
+ /** Roles allowed to interact with the bot (e.g., ["mod", "vip", "sub"]) */
47
+ allowedRoles?: TwitchRole[];
48
+ /** Require @mention to trigger bot responses */
49
+ requireMention?: boolean;
50
+ /** Outbound response prefix override for this channel/account. */
51
+ responsePrefix?: string;
52
+ /** Twitch client secret (required for token refresh via RefreshingAuthProvider) */
53
+ clientSecret?: string;
54
+ /** Refresh token (required for automatic token refresh) */
55
+ refreshToken?: string;
56
+ /** Token expiry time in seconds (optional, for token refresh tracking) */
57
+ expiresIn?: number | null;
58
+ /** Timestamp when token was obtained (optional, for token refresh tracking) */
59
+ obtainmentTimestamp?: number;
60
+ }
61
+
62
+ /**
63
+ * Twitch message from chat
64
+ */
65
+ export interface TwitchChatMessage {
66
+ /** Username of sender */
67
+ username: string;
68
+ /** Twitch user ID of sender (unique, persistent identifier) */
69
+ userId?: string;
70
+ /** Message text */
71
+ message: string;
72
+ /** Channel name */
73
+ channel: string;
74
+ /** Display name (may include special characters) */
75
+ displayName?: string;
76
+ /** Message ID */
77
+ id?: string;
78
+ /** Timestamp */
79
+ timestamp?: Date;
80
+ /** Whether the sender is a moderator */
81
+ isMod?: boolean;
82
+ /** Whether the sender is the channel owner/broadcaster */
83
+ isOwner?: boolean;
84
+ /** Whether the sender is a VIP */
85
+ isVip?: boolean;
86
+ /** Whether the sender is a subscriber */
87
+ isSub?: boolean;
88
+ /** Chat type */
89
+ chatType?: "group";
90
+ }
91
+
92
+ // Re-export core types for convenience
93
+ export type {
94
+ ChannelAccountSnapshot,
95
+ ChannelLogSink,
96
+ ChannelMessageActionAdapter,
97
+ ChannelMessageActionContext,
98
+ ChannelOutboundAdapter,
99
+ ChannelResolveKind,
100
+ ChannelResolveResult,
101
+ ChannelPlugin,
102
+ ChannelOutboundContext,
103
+ OutboundDeliveryResult,
104
+ };
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Markdown utilities for Twitch chat
3
+ *
4
+ * Twitch chat doesn't support markdown formatting, so we strip it before sending.
5
+ * Based on AutoBot's markdownToText in src/agents/tools/web-fetch-utils.ts.
6
+ */
7
+
8
+ /**
9
+ * Strip markdown formatting from text for Twitch compatibility.
10
+ *
11
+ * Removes images, links, bold, italic, strikethrough, code blocks, inline code,
12
+ * headers, and list formatting. Replaces newlines with spaces since Twitch
13
+ * is a single-line chat medium.
14
+ *
15
+ * @param markdown - The markdown text to strip
16
+ * @returns Plain text with markdown removed
17
+ */
18
+ export function stripMarkdownForTwitch(markdown: string): string {
19
+ return (
20
+ markdown
21
+ // Images
22
+ .replace(/!\[[^\]]*]\([^)]+\)/g, "")
23
+ // Links
24
+ .replace(/\[([^\]]+)]\([^)]+\)/g, "$1")
25
+ // Bold (**text**)
26
+ .replace(/\*\*([^*]+)\*\*/g, "$1")
27
+ // Bold (__text__)
28
+ .replace(/__([^_]+)__/g, "$1")
29
+ // Italic (*text*)
30
+ .replace(/\*([^*]+)\*/g, "$1")
31
+ // Italic (_text_)
32
+ .replace(/_([^_]+)_/g, "$1")
33
+ // Strikethrough (~~text~~)
34
+ .replace(/~~([^~]+)~~/g, "$1")
35
+ // Code blocks
36
+ .replace(/```[\s\S]*?```/g, (block) => block.replace(/```[^\n]*\n?/g, "").replace(/```/g, ""))
37
+ // Inline code
38
+ .replace(/`([^`]+)`/g, "$1")
39
+ // Headers
40
+ .replace(/^#{1,6}\s+/gm, "")
41
+ // Lists
42
+ .replace(/^\s*[-*+]\s+/gm, "")
43
+ .replace(/^\s*\d+\.\s+/gm, "")
44
+ // Normalize whitespace
45
+ .replace(/\r/g, "") // Remove carriage returns
46
+ .replace(/[ \t]+\n/g, "\n") // Remove trailing spaces before newlines
47
+ .replace(/\n/g, " ") // Replace newlines with spaces (for Twitch)
48
+ .replace(/[ \t]{2,}/g, " ") // Reduce multiple spaces to single
49
+ .trim()
50
+ );
51
+ }
52
+
53
+ /**
54
+ * Simple word-boundary chunker for Twitch (500 char limit).
55
+ * Strips markdown before chunking to avoid breaking markdown patterns.
56
+ *
57
+ * @param text - The text to chunk
58
+ * @param limit - Maximum characters per chunk (Twitch limit is 500)
59
+ * @returns Array of text chunks
60
+ */
61
+ export function chunkTextForTwitch(text: string, limit: number): string[] {
62
+ // First, strip markdown
63
+ const cleaned = stripMarkdownForTwitch(text);
64
+ if (!cleaned) {
65
+ return [];
66
+ }
67
+ if (limit <= 0) {
68
+ return [cleaned];
69
+ }
70
+ if (cleaned.length <= limit) {
71
+ return [cleaned];
72
+ }
73
+
74
+ const chunks: string[] = [];
75
+ let remaining = cleaned;
76
+
77
+ while (remaining.length > limit) {
78
+ // Find the last space before the limit
79
+ const window = remaining.slice(0, limit);
80
+ const lastSpaceIndex = window.lastIndexOf(" ");
81
+
82
+ if (lastSpaceIndex === -1) {
83
+ // No space found, hard split at limit
84
+ chunks.push(window);
85
+ remaining = remaining.slice(limit);
86
+ } else {
87
+ // Split at the last space
88
+ chunks.push(window.slice(0, lastSpaceIndex));
89
+ remaining = remaining.slice(lastSpaceIndex + 1);
90
+ }
91
+ }
92
+
93
+ if (remaining) {
94
+ chunks.push(remaining);
95
+ }
96
+
97
+ return chunks;
98
+ }
@@ -0,0 +1,81 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { normalizeLowercaseStringOrEmpty } from "autobot/plugin-sdk/string-coerce-runtime";
3
+
4
+ /**
5
+ * Twitch-specific utility functions
6
+ */
7
+
8
+ /**
9
+ * Normalize Twitch channel names.
10
+ *
11
+ * Removes the '#' prefix if present, converts to lowercase, and trims whitespace.
12
+ * Twitch channel names are case-insensitive and don't use the '#' prefix in the API.
13
+ *
14
+ * @param channel - The channel name to normalize
15
+ * @returns Normalized channel name
16
+ *
17
+ * @example
18
+ * normalizeTwitchChannel("#TwitchChannel") // "twitchchannel"
19
+ * normalizeTwitchChannel("MyChannel") // "mychannel"
20
+ */
21
+ export function normalizeTwitchChannel(channel: string): string {
22
+ const trimmed = normalizeLowercaseStringOrEmpty(channel);
23
+ return trimmed.startsWith("#") ? trimmed.slice(1) : trimmed;
24
+ }
25
+
26
+ /**
27
+ * Create a standardized error message for missing target.
28
+ *
29
+ * @param provider - The provider name (e.g., "Twitch")
30
+ * @param hint - Optional hint for how to fix the issue
31
+ * @returns Error object with descriptive message
32
+ */
33
+ export function missingTargetError(provider: string, hint?: string): Error {
34
+ return new Error(`Delivering to ${provider} requires target${hint ? ` ${hint}` : ""}`);
35
+ }
36
+
37
+ /**
38
+ * Generate a unique message ID for Twitch messages.
39
+ *
40
+ * Twurple's say() doesn't return the message ID, so we generate one
41
+ * for tracking purposes.
42
+ *
43
+ * @returns A unique message ID
44
+ */
45
+ export function generateMessageId(): string {
46
+ return `${Date.now()}-${randomUUID()}`;
47
+ }
48
+
49
+ /**
50
+ * Normalize OAuth token by removing the "oauth:" prefix if present.
51
+ *
52
+ * Twurple doesn't require the "oauth:" prefix, so we strip it for consistency.
53
+ *
54
+ * @param token - The OAuth token to normalize
55
+ * @returns Normalized token without "oauth:" prefix
56
+ *
57
+ * @example
58
+ * normalizeToken("oauth:abc123") // "abc123"
59
+ * normalizeToken("abc123") // "abc123"
60
+ */
61
+ export function normalizeToken(token: string): string {
62
+ return token.startsWith("oauth:") ? token.slice(6) : token;
63
+ }
64
+
65
+ /**
66
+ * Check if an account is properly configured with required credentials.
67
+ *
68
+ * @param account - The Twitch account config to check
69
+ * @returns true if the account has required credentials
70
+ */
71
+ export function isAccountConfigured(
72
+ account: {
73
+ username?: string;
74
+ accessToken?: string;
75
+ clientId?: string;
76
+ },
77
+ resolvedToken?: string | null,
78
+ ): boolean {
79
+ const token = resolvedToken ?? account?.accessToken;
80
+ return Boolean(account?.username && token && account?.clientId);
81
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "extends": "../tsconfig.package-boundary.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "."
5
+ },
6
+ "include": ["./*.ts", "./src/**/*.ts"],
7
+ "exclude": [
8
+ "./**/*.test.ts",
9
+ "./dist/**",
10
+ "./node_modules/**",
11
+ "./src/test-support/**",
12
+ "./src/**/*test-helpers.ts",
13
+ "./src/**/*test-harness.ts",
14
+ "./src/**/*test-support.ts"
15
+ ]
16
+ }