/
zMEGA-Dev
/
vibes-nodejs-sdk
Обзор
Документация
Войти
/
zMEGA-Dev
/
vibes-nodejs-sdk
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
example.js
202 строки
11 KB
zMEGA-Dev
Add files via upload
02 авг 2026, 20:22
Не верифицирован
02 авг 2026, 20:22
2fce33c
Код
Авторство
О чём код?
/** * example.js — Full SDK demonstration + production test * Run: node example.js */ import { VibesAPI, VibesAPIError } from './vibesClient.js'; // ─── Replace with your real API key ────────────────────────────────────────── const API_KEY = 'YOUR_API_KEY_HERE'; // ─── Helpers ───────────────────────────────────────────────────────────────── const pass = (msg) => console.log(` ✅ ${msg}`); const fail = (msg) => console.log(` ❌ ${msg}`); const section = (title) => console.log(`\n${'═'.repeat(50)}\n ${title}\n${'═'.repeat(50)}`); async function run(label, fn) { try { const result = await fn(); pass(label); return result; } catch (err) { if (err instanceof VibesAPIError) { fail(`${label} — [${err.status}] ${err.message}`); } else { fail(`${label} — ${err.message}`); } return null; } } // ─── 1. Sync validation (no network needed) ─────────────────────────────────── function testSyncValidation() { section('1. Sync key validation'); try { new VibesAPI(''); fail('Empty key should throw'); } catch (e) { pass(`Empty key → ${e.constructor.name}: ${e.message.slice(0, 60)}`); } try { new VibesAPI('мой-ключ'); fail('Cyrillic key should throw'); } catch (e) { pass(`Cyrillic key → ${e.constructor.name}`); } try { new VibesAPI('valid-ascii-key-123'); pass('Valid ASCII key accepted'); } catch (e) { fail(`Valid key rejected: ${e.message}`); } } // ─── 2. Live API tests ──────────────────────────────────────────────────────── async function testLive() { const api = new VibesAPI(API_KEY); // ── User ───────────────────────────────────────────────────────────────── section('2. User'); const user = await run('getUser()', () => api.getUser()); if (user) console.log(` → ${user.name} | plan: ${user.plan_id} | API: ${user.plan_settings?.api_is_enabled}`); // ── Links ───────────────────────────────────────────────────────────────── section('3. Links'); const newLink = await run('createLink()', () => api.createLink({ location_url: 'https://example.com/sdk-test-' + Date.now(), })); if (newLink) console.log(` → created: ${newLink.url} (id: ${newLink.id})`); const links = await run('getLinks()', () => api.getLinks({ results_per_page: 3 })); if (links) console.log(` → ${links.length} links returned`); if (newLink) { await run('getLink(id)', () => api.getLink(newLink.id)); await run('updateLink(id)', () => api.updateLink(newLink.id, { location_url: 'https://example.com/sdk-updated' })); await run('deleteLink(id)', () => api.deleteLink(newLink.id)); } // ── Projects ────────────────────────────────────────────────────────────── section('4. Projects'); const proj = await run('createProject()', () => api.createProject({ name: 'SDK Test Project', color: '#ff5500' })); if (proj) console.log(` → id: ${proj.id}`); await run('getProjects()', () => api.getProjects()); if (proj) { await run('getProject(id)', () => api.getProject(proj.id)); await run('updateProject(id)', () => api.updateProject(proj.id, { name: 'SDK Test Project (updated)' })); await run('deleteProject(id)', () => api.deleteProject(proj.id)); } // ── Pixels ──────────────────────────────────────────────────────────────── section('5. Pixels'); const px = await run('createPixel()', () => api.createPixel({ type: 'facebook', name: 'SDK FB Pixel', pixel: 'TEST123' })); if (px) { await run('getPixels()', () => api.getPixels()); await run('getPixel(id)', () => api.getPixel(px.id)); await run('updatePixel(id)', () => api.updatePixel(px.id, { name: 'SDK FB Pixel (updated)' })); await run('deletePixel(id)', () => api.deletePixel(px.id)); } // ── Domains ─────────────────────────────────────────────────────────────── section('6. Domains'); await run('getDomains()', () => api.getDomains()); await run('getAvailableDomains()', () => api.getAvailableDomains()); // ── QR Codes ────────────────────────────────────────────────────────────── section('7. QR Codes'); const qr = await run('createQRCode()', () => api.createQRCode({ type: 'url', name: 'SDK Test QR', url: 'https://example.com', url_dynamic: 1, })); if (qr) { console.log(` → id: ${qr.id} | svg: ${qr.qr_code}`); await run('getQRCodes()', () => api.getQRCodes()); await run('getQRCode(id)', () => api.getQRCode(qr.id)); await run('updateQRCode(id)', () => api.updateQRCode(qr.id, { name: 'SDK Test QR (updated)' })); await run('deleteQRCode(id)', () => api.deleteQRCode(qr.id)); } // ── Statistics ──────────────────────────────────────────────────────────── section('8. Statistics'); const allLinks = await api.getLinks({ results_per_page: 1 }).catch(() => null); if (allLinks?.length) { const statLink = allLinks[0]; await run(`getLinkStatistics(${statLink.id}, overview)`, () => api.getLinkStatistics(statLink.id, 'overview')); await run(`getLinkStatistics(${statLink.id}, country_code)`, () => api.getLinkStatistics(statLink.id, 'country_code')); } await run('getAllStatistics(overview)', () => api.getAllStatistics('overview')); // ── Notification Handlers ───────────────────────────────────────────────── section('9. Notification Handlers'); await run('getNotificationHandlers()', () => api.getNotificationHandlers()); // ── Teams ───────────────────────────────────────────────────────────────── section('10. Teams'); const team = await run('createTeam()', () => api.createTeam({ name: 'SDK Test Team' })); if (team) { await run('getTeams()', () => api.getTeams()); await run('getTeam(id)', () => api.getTeam(team.id)); await run('updateTeam(id)', () => api.updateTeam(team.id, { name: 'SDK Test Team (updated)' })); // Team members const member = await run('createTeamMember()', () => api.createTeamMember({ team_id: team.id, user_email: 'sdktest+member@example.com', access: ['read.all'], })); if (member) { await run('getTeamMembers(teamId)', () => api.getTeamMembers(team.id)); await run('updateTeamMember(id)', () => api.updateTeamMember(member.id, { access: ['read.all'] })); await run('deleteTeamMember(id)', () => api.deleteTeamMember(member.id)); } await run('deleteTeam(id)', () => api.deleteTeam(team.id)); } // ── Team Memberships (self) ─────────────────────────────────────────────── section('11. Team Memberships (self)'); await run('getTeamMemberships()', () => api.getTeamMemberships()); // ── Payments ────────────────────────────────────────────────────────────── section('12. Payments'); const payments = await run('getPayments()', () => api.getPayments({ results_per_page: 3 })); if (payments?.length) { await run(`getPayment(${payments[0].id})`, () => api.getPayment(payments[0].id)); } // ── Data ────────────────────────────────────────────────────────────────── section('13. Data'); await run('getData()', () => api.getData({ results_per_page: 3 })); // ── Logs ────────────────────────────────────────────────────────────────── section('14. Logs'); await run('getLogs()', () => api.getLogs({ results_per_page: 3 })); // ── Signatures ──────────────────────────────────────────────────────────── section('15. Email Signatures'); await run('getSignatures()', () => api.getSignatures()); // ── Splash Pages ────────────────────────────────────────────────────────── section('16. Splash Pages'); const sp = await run('createSplashPage()', () => api.createSplashPage({ name: 'SDK Test Splash', title: 'Wait a moment…', link_unlock_seconds: 5, })); if (sp) { await run('getSplashPages()', () => api.getSplashPages()); await run('getSplashPage(id)', () => api.getSplashPage(sp.id)); await run('updateSplashPage(id)', () => api.updateSplashPage(sp.id, { title: 'Updated title' })); await run('deleteSplashPage(id)', () => api.deleteSplashPage(sp.id)); } } // ─── Entry point ────────────────────────────────────────────────────────────── (async () => { console.log('\n🚀 vibes-sdk v2 — Production Test Suite'); testSyncValidation(); if (API_KEY === 'YOUR_API_KEY_HERE') { console.log('\n⚠ Set API_KEY in example.js to run live tests.'); process.exit(0); } await testLive(); console.log('\n✅ All tests finished.\n'); })().catch((err) => { console.error('\n💥 Fatal:', err.message); process.exit(1); });