tiktok-live-api 1.2.11 → 1.2.12

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.
Files changed (2) hide show
  1. package/bin/demo.mjs +42 -54
  2. package/package.json +1 -1
package/bin/demo.mjs CHANGED
@@ -15,9 +15,9 @@ const REACT_TEMPLATE = [
15
15
  "",
16
16
  "export default function TikTokLiveComponent() {",
17
17
  " const [events, setEvents] = useState([]);",
18
- " const [username, setUsername] = useState('aljazeeraenglish');",
18
+ " const [username, setUsername] = useState('gbnews');",
19
19
  " const [apiKey, setApiKey] = useState('demo_tiktokliveapi_public_2026');",
20
- " const [inputUsername, setInputUsername] = useState('aljazeeraenglish');",
20
+ " const [inputUsername, setInputUsername] = useState('gbnews');",
21
21
  " const [connected, setConnected] = useState(false);",
22
22
  " const clientRef = useRef(null);",
23
23
  "",
@@ -59,7 +59,7 @@ const REACT_TEMPLATE = [
59
59
  " <form onSubmit={handleConnect} style={{ display: 'flex', flexDirection: 'column', gap: '16px', marginBottom: '24px' }}>",
60
60
  " <div>",
61
61
  " <label style={{ display: 'block', fontSize: '0.875rem', fontWeight: 500, marginBottom: '6px' }}>TikTok Username</label>",
62
- " <input type=\"text\" value={inputUsername} onChange={(e) => setInputUsername(e.target.value)} style={{ width: '100%', padding: '8px 12px', borderRadius: '6px', border: '1px solid #d1d5db', boxSizing: 'border-box' }} placeholder=\"e.g. aljazeeraenglish\" />",
62
+ " <input type=\"text\" value={inputUsername} onChange={(e) => setInputUsername(e.target.value)} style={{ width: '100%', padding: '8px 12px', borderRadius: '6px', border: '1px solid #d1d5db', boxSizing: 'border-box' }} placeholder=\"e.g. gbnews\" />",
63
63
  " </div>",
64
64
  " <div>",
65
65
  " <label style={{ display: 'block', fontSize: '0.875rem', fontWeight: 500, marginBottom: '6px' }}>API Key</label>",
@@ -95,7 +95,7 @@ const VUE_TEMPLATE = [
95
95
  ' <form @submit.prevent="handleConnect" style="display: flex; flex-direction: column; gap: 16px; margin-bottom: 24px">',
96
96
  " <div>",
97
97
  ' <label style="display: block; font-size: 0.875rem; font-weight: 500; margin-bottom: 6px">TikTok Username</label>',
98
- ' <input type="text" v-model="inputUsername" style="width: 100%; padding: 8px 12px; border-radius: 6px; border: 1px solid #d1d5db; box-sizing: border-box" placeholder="e.g. aljazeeraenglish" />',
98
+ ' <input type="text" v-model="inputUsername" style="width: 100%; padding: 8px 12px; border-radius: 6px; border: 1px solid #d1d5db; box-sizing: border-box" placeholder="e.g. gbnews" />',
99
99
  " </div>",
100
100
  " <div>",
101
101
  ' <label style="display: block; font-size: 0.875rem; font-weight: 500; margin-bottom: 6px">API Key</label>',
@@ -119,53 +119,52 @@ const VUE_TEMPLATE = [
119
119
  "</template>",
120
120
  "",
121
121
  "<script setup>",
122
- "import { ref, watch, onMounted } from 'vue'",
122
+ "import { ref, onMounted, onUnmounted } from 'vue'",
123
123
  "",
124
- "const inputUsername = ref('aljazeeraenglish')",
125
- "const activeUsername = ref('aljazeeraenglish')",
124
+ "const inputUsername = ref('gbnews')",
125
+ "const activeUsername = ref('gbnews')",
126
126
  "const apiKey = ref('demo_tiktokliveapi_public_2026')",
127
+ "const connected = ref(false)",
127
128
  "const events = ref([])",
129
+ "let ws = null",
128
130
  "",
129
- "let hook = null;",
130
- "try { if (typeof useTikTokLive !== 'undefined') hook = useTikTokLive; } catch (e) { }",
131
- "const proxyUsername = { replace: (r, s) => (activeUsername.value || '').replace(r, s) }",
132
- "const tiktok = hook ? hook(proxyUsername, { apiKey, autoConnect: false }) : { connected: ref(false), allEvents: ref([]) }",
133
- "const connected = tiktok.connected",
134
- "",
135
- "watch(() => tiktok.allEvents.value, (all) => {",
136
- " if (all && all.length) {",
137
- " const last = all[all.length - 1];",
138
- " if (last.type === 'chat') addEvent(`💬 ${last.data.user.uniqueId}: ${last.data.comment}`)",
139
- " if (last.type === 'gift') addEvent(`🎁 ${last.data.user.uniqueId} sent ${last.data.giftName}`)",
140
- " if (last.type === 'like') addEvent(`❤️ ${last.data.user.uniqueId} liked`)",
141
- " if (last.type === 'member') addEvent(`👋 ${last.data.user.uniqueId} joined`)",
142
- " if (last.type === 'connected') addEvent('✅ Connected successfully!')",
143
- " if (last.type === 'disconnected') addEvent('❌ Disconnected from stream.')",
144
- " }",
145
- "}, { deep: true })",
146
- "",
147
- "watch(() => tiktok.error ? tiktok.error.value : null, (e) => {",
148
- " if(e) addEvent('⚠️ Error: ' + e)",
149
- "})",
150
- "",
151
- "const addEvent = (str) => {",
131
+ "function addEvent(str) {",
152
132
  " events.value.push(str)",
153
- " if(events.value.length > 50) events.value.shift()",
133
+ " if (events.value.length > 50) events.value.shift()",
154
134
  "}",
155
135
  "",
156
- "const handleConnect = () => {",
157
- " if (tiktok.disconnect) tiktok.disconnect()",
136
+ "function connectStream() {",
137
+ " if (ws) { ws.close(); ws = null }",
158
138
  " events.value = []",
139
+ " connected.value = false",
159
140
  " activeUsername.value = inputUsername.value",
160
- " if (!activeUsername.value) return",
161
- " if (tiktok.connect) {",
162
- " tiktok.connect().catch(e => addEvent('⚠️ Failed: ' + e.message))",
141
+ " const user = (activeUsername.value || '').replace(/^@/, '')",
142
+ " if (!user) return",
143
+ " const key = apiKey.value || ''",
144
+ " try {",
145
+ " ws = new WebSocket('wss://api.tik.tools?uniqueId=' + user + '&apiKey=' + key)",
146
+ " } catch (e) { addEvent('⚠️ Connection failed: ' + e.message); return }",
147
+ " ws.onopen = () => { connected.value = true; addEvent('✅ Connected to @' + user) }",
148
+ " ws.onclose = () => { connected.value = false; ws = null; addEvent('❌ Disconnected') }",
149
+ " ws.onerror = () => { addEvent('⚠️ WebSocket error') }",
150
+ " ws.onmessage = (evt) => {",
151
+ " try {",
152
+ " const msg = JSON.parse(evt.data)",
153
+ " const t = msg.event || ''",
154
+ " const d = msg.data || {}",
155
+ " const u = d.user ? d.user.uniqueId : ''",
156
+ " if (t === 'chat') addEvent('💬 ' + u + ': ' + d.comment)",
157
+ " else if (t === 'gift') addEvent('🎁 ' + u + ' sent ' + (d.giftName || 'a gift'))",
158
+ " else if (t === 'like') addEvent('❤️ ' + u + ' liked')",
159
+ " else if (t === 'member') addEvent('👋 ' + u + ' joined')",
160
+ " } catch (e) {}",
163
161
  " }",
164
162
  "}",
165
163
  "",
166
- "onMounted(() => {",
167
- " handleConnect()",
168
- "})",
164
+ "const handleConnect = () => { connectStream() }",
165
+ "",
166
+ "onMounted(() => { connectStream() })",
167
+ "onUnmounted(() => { if (ws) ws.close() })",
169
168
  "</script>"
170
169
  ].join('\n') + "\n";
171
170
 
@@ -261,10 +260,6 @@ function runScaffold(choice, targetDir, pm) {
261
260
  }
262
261
 
263
262
  if (isNuxt) {
264
- console.log("\\n " + B + "📦 Installing " + C.cyan + "tiktok-live-nuxt" + B + " Module..." + R + "\\n");
265
- const nuxtInstallCmd = pm === 'npm' ? 'npm install tiktok-live-nuxt' : pm === 'yarn' ? 'yarn add tiktok-live-nuxt' : pm === 'pnpm' ? 'pnpm add tiktok-live-nuxt' : 'bun add tiktok-live-nuxt';
266
- execSync(nuxtInstallCmd, { cwd: fullPath, stdio: 'inherit' });
267
-
268
263
  const isNuxt4 = fs.existsSync(path.join(fullPath, 'app', 'app.vue')) || !fs.existsSync(path.join(fullPath, 'app.vue'));
269
264
  const baseApp = isNuxt4 ? path.join(fullPath, 'app') : fullPath;
270
265
 
@@ -276,19 +271,12 @@ function runScaffold(choice, targetDir, pm) {
276
271
  fs.writeFileSync(appVue, [
277
272
  '<template>',
278
273
  ' <main style="min-height: 100vh; background: #f9fafb; padding-top: 40px;">',
279
- ' <TikTokLive />',
274
+ ' <ClientOnly>',
275
+ ' <TikTokLive />',
276
+ ' </ClientOnly>',
280
277
  ' </main>',
281
278
  '</template>'
282
279
  ].join('\n') + '\n');
283
-
284
- const nuxtConfig = path.join(fullPath, 'nuxt.config.ts');
285
- if (fs.existsSync(nuxtConfig)) {
286
- let config = fs.readFileSync(nuxtConfig, 'utf8');
287
- if (config.includes('defineNuxtConfig({')) {
288
- config = config.replace('defineNuxtConfig({', "defineNuxtConfig({\n modules: ['tiktok-live-nuxt'],\n");
289
- fs.writeFileSync(nuxtConfig, config);
290
- }
291
- }
292
280
  } else {
293
281
  console.log("\\n " + B + "📦 Installing " + C.cyan + "tiktok-live-api" + B + " SDK..." + R + "\\n");
294
282
  execSync(installCmd, { cwd: fullPath, stdio: 'inherit' });
@@ -354,7 +342,7 @@ async function runDemo() {
354
342
  const WS_BASE = 'wss://api.tik.tools';
355
343
  const DEMO_KEY = 'demo_tiktokliveapi_public_2026';
356
344
  const CHANNELS = [
357
- 'aljazeeraenglish', 'cgtnofficial', 'france24_en',
345
+ 'gbnews', 'cgtnofficial', 'france24_en',
358
346
  'weathernewslive', 'gbnews', 'bbcnews',
359
347
  'skynews', 'tv_asahi_news', 'abc7chicago', 'thairath_news',
360
348
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiktok-live-api",
3
- "version": "1.2.11",
3
+ "version": "1.2.12",
4
4
  "description": "Unofficial TikTok LIVE API Client — Real-time chat, gifts, viewers, battles, and AI live captions from any TikTok livestream. Managed WebSocket API with 99.9% uptime.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",