tiktok-live-api 1.2.6 → 1.2.8

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 +85 -81
  2. package/package.json +1 -1
package/bin/demo.mjs CHANGED
@@ -119,62 +119,51 @@ const VUE_TEMPLATE = [
119
119
  "</template>",
120
120
  "",
121
121
  "<script setup>",
122
- "import { ref, onMounted, onUnmounted } from 'vue'",
123
- "import { TikTokLive } from 'tiktok-live-api'",
122
+ "import { ref, watch, onMounted } from 'vue'",
124
123
  "",
125
124
  "const inputUsername = ref('aljazeeraenglish')",
126
125
  "const activeUsername = ref('aljazeeraenglish')",
127
126
  "const apiKey = ref('demo_tiktokliveapi_public_2026')",
128
- "const connected = ref(false)",
129
127
  "const events = ref([])",
130
- "let client = null",
131
128
  "",
132
- "const connectClient = () => {",
133
- " if (client) client.disconnect()",
134
- " events.value = []",
135
- " connected.value = false",
136
- " activeUsername.value = inputUsername.value",
137
- " ",
138
- " if (!activeUsername.value) return",
139
- " ",
140
- " client = new TikTokLive(activeUsername.value, { apiKey: apiKey.value })",
141
- " ",
142
- " client.on('chat', (e) => {",
143
- ' events.value.push("💬 " + e.user.uniqueId + ": " + e.comment)',
144
- " if (events.value.length > 40) events.value.shift()",
145
- " })",
146
- " ",
147
- " client.on('gift', (e) => {",
148
- ' events.value.push("🎁 " + e.user.uniqueId + " sent " + e.giftName)',
149
- " if (events.value.length > 40) events.value.shift()",
150
- " })",
129
+ "let hook = null;",
130
+ "try { if (typeof useTikTokLive !== 'undefined') hook = useTikTokLive; } catch (e) { }",
131
+ "const tiktok = hook ? hook(activeUsername, { apiKey, autoConnect: false }) : { connected: ref(false), allEvents: ref([]) }",
132
+ "const connected = tiktok.connected",
151
133
  "",
152
- " client.on('member', (e) => {",
153
- ' events.value.push("👋 " + e.user.uniqueId + " joined")',
154
- " if (events.value.length > 40) events.value.shift()",
155
- " })",
156
- " ",
157
- " client.on('like', (e) => {",
158
- ' events.value.push("❤️ " + e.user.uniqueId + " liked")',
159
- " if (events.value.length > 40) events.value.shift()",
160
- " })",
134
+ "watch(() => tiktok.allEvents.value, (all) => {",
135
+ " if (all && all.length) {",
136
+ " const last = all[all.length - 1];",
137
+ " if (last.type === 'chat') addEvent(`💬 ${last.data.user.uniqueId}: ${last.data.comment}`)",
138
+ " if (last.type === 'gift') addEvent(`🎁 ${last.data.user.uniqueId} sent ${last.data.giftName}`)",
139
+ " if (last.type === 'like') addEvent(`❤️ ${last.data.user.uniqueId} liked`)",
140
+ " if (last.type === 'member') addEvent(`👋 ${last.data.user.uniqueId} joined`)",
141
+ " if (last.type === 'connected') addEvent('✅ Connected successfully!')",
142
+ " if (last.type === 'disconnected') addEvent('❌ Disconnected from stream.')",
143
+ " }",
144
+ "}, { deep: true })",
145
+ "",
146
+ "watch(() => tiktok.error ? tiktok.error.value : null, (e) => {",
147
+ " if(e) addEvent('⚠️ Error: ' + e)",
148
+ "})",
161
149
  "",
162
- " client.on('connected', () => connected.value = true)",
163
- " client.on('disconnected', () => connected.value = false)",
164
- " ",
165
- " client.connect().catch(() => connected.value = false)",
150
+ "const addEvent = (str) => {",
151
+ " events.value.push(str)",
152
+ " if(events.value.length > 50) events.value.shift()",
166
153
  "}",
167
154
  "",
168
155
  "const handleConnect = () => {",
169
- " connectClient()",
156
+ " if (tiktok.disconnect) tiktok.disconnect()",
157
+ " events.value = []",
158
+ " activeUsername.value = inputUsername.value",
159
+ " if (!activeUsername.value) return",
160
+ " if (tiktok.connect) {",
161
+ " tiktok.connect().catch(e => addEvent('⚠️ Failed: ' + e.message))",
162
+ " }",
170
163
  "}",
171
164
  "",
172
165
  "onMounted(() => {",
173
- " connectClient()",
174
- "})",
175
- "",
176
- "onUnmounted(() => {",
177
- " if (client) client.disconnect()",
166
+ " handleConnect()",
178
167
  "})",
179
168
  "</script>"
180
169
  ].join('\n') + "\n";
@@ -270,10 +259,11 @@ function runScaffold(choice, targetDir, pm) {
270
259
  execSync(createCmd + ' "' + targetDir + '" -- --template react', { stdio: 'inherit' });
271
260
  }
272
261
 
273
- console.log("\n " + B + "📦 Installing " + C.cyan + "tiktok-live-api" + B + " SDK..." + R + "\n");
274
- execSync(installCmd, { cwd: fullPath, stdio: 'inherit' });
275
-
276
262
  if (isNuxt) {
263
+ console.log("\\n " + B + "📦 Installing " + C.cyan + "tiktok-live-nuxt" + B + " Module..." + R + "\\n");
264
+ 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';
265
+ execSync(nuxtInstallCmd, { cwd: fullPath, stdio: 'inherit' });
266
+
277
267
  const isNuxt4 = fs.existsSync(path.join(fullPath, 'app', 'app.vue')) || !fs.existsSync(path.join(fullPath, 'app.vue'));
278
268
  const baseApp = isNuxt4 ? path.join(fullPath, 'app') : fullPath;
279
269
 
@@ -289,43 +279,57 @@ function runScaffold(choice, targetDir, pm) {
289
279
  ' </main>',
290
280
  '</template>'
291
281
  ].join('\\n') + '\\n');
292
- } else if (isNext) {
293
- const compDir = path.join(fullPath, 'src', 'components');
294
- if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
295
- fs.writeFileSync(path.join(compDir, 'TikTokLive.jsx'), REACT_TEMPLATE);
296
-
297
- const pageJs = path.join(fullPath, 'src', 'app', 'page.js');
298
- if (fs.existsSync(pageJs)) {
299
- fs.writeFileSync(pageJs, [
300
- "import TikTokLive from '@/components/TikTokLive';",
301
- "",
302
- "export default function Home() {",
303
- " return (",
304
- ' <main style={{ minHeight: "100vh", background: "#f9fafb", paddingTop: "40px" }}>',
305
- " <TikTokLive />",
306
- " </main>",
307
- " );",
308
- "}"
309
- ].join('\n') + '\n');
282
+
283
+ const nuxtConfig = path.join(fullPath, 'nuxt.config.ts');
284
+ if (fs.existsSync(nuxtConfig)) {
285
+ let config = fs.readFileSync(nuxtConfig, 'utf8');
286
+ if (config.includes('defineNuxtConfig({')) {
287
+ config = config.replace('defineNuxtConfig({', "defineNuxtConfig({\\n modules: ['tiktok-live-nuxt'],\\n");
288
+ fs.writeFileSync(nuxtConfig, config);
289
+ }
310
290
  }
311
- } else if (isVite) {
312
- const compDir = path.join(fullPath, 'src', 'components');
313
- if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
314
- fs.writeFileSync(path.join(compDir, 'TikTokLive.jsx'), REACT_TEMPLATE);
315
-
316
- const appJsx = path.join(fullPath, 'src', 'App.jsx');
317
- if (fs.existsSync(appJsx)) {
318
- fs.writeFileSync(appJsx, [
319
- "import TikTokLive from './components/TikTokLive';",
320
- "",
321
- "export default function App() {",
322
- " return (",
323
- ' <main style={{ minHeight: "100vh", background: "#f9fafb", paddingTop: "40px" }}>',
324
- " <TikTokLive />",
325
- " </main>",
326
- " );",
327
- "}"
328
- ].join('\n') + '\n');
291
+ } else {
292
+ console.log("\\n " + B + "📦 Installing " + C.cyan + "tiktok-live-api" + B + " SDK..." + R + "\\n");
293
+ execSync(installCmd, { cwd: fullPath, stdio: 'inherit' });
294
+
295
+ if (isNext) {
296
+ const compDir = path.join(fullPath, 'src', 'components');
297
+ if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
298
+ fs.writeFileSync(path.join(compDir, 'TikTokLive.jsx'), REACT_TEMPLATE);
299
+
300
+ const pageJs = path.join(fullPath, 'src', 'app', 'page.js');
301
+ if (fs.existsSync(pageJs)) {
302
+ fs.writeFileSync(pageJs, [
303
+ "import TikTokLive from '@/components/TikTokLive';",
304
+ "",
305
+ "export default function Home() {",
306
+ " return (",
307
+ ' <main style={{ minHeight: "100vh", background: "#f9fafb", paddingTop: "40px" }}>',
308
+ " <TikTokLive />",
309
+ " </main>",
310
+ " );",
311
+ "}"
312
+ ].join('\n') + '\n');
313
+ }
314
+ } else if (isVite) {
315
+ const compDir = path.join(fullPath, 'src', 'components');
316
+ if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
317
+ fs.writeFileSync(path.join(compDir, 'TikTokLive.jsx'), REACT_TEMPLATE);
318
+
319
+ const appJsx = path.join(fullPath, 'src', 'App.jsx');
320
+ if (fs.existsSync(appJsx)) {
321
+ fs.writeFileSync(appJsx, [
322
+ "import TikTokLive from './components/TikTokLive';",
323
+ "",
324
+ "export default function App() {",
325
+ " return (",
326
+ ' <main style={{ minHeight: "100vh", background: "#f9fafb", paddingTop: "40px" }}>',
327
+ " <TikTokLive />",
328
+ " </main>",
329
+ " );",
330
+ "}"
331
+ ].join('\n') + '\n');
332
+ }
329
333
  }
330
334
  }
331
335
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiktok-live-api",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
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",