SDK TypeScript
Satu paket dengan enam titik masuk. Impor yang sesuai dengan runtime Anda, dan build dengan tree shaking meninggalkan sisanya di luar bundel Anda.
Tempat diterbitkan
@prodantix/sdk on npm.
Instal
bun add @prodantix/sdkTitik masuk
Setiap subpath menyertakan build ESM dan CommonJS sendiri beserta tipenya sendiri. Root bersifat netral terhadap runtime, dan sisanya menambahkan apa yang dibutuhkan satu runtime tertentu.
| Subpath | What it exports |
|---|---|
| @prodantix/sdk | ProdantixClient, createClient, evaluateFlag, FlagsClient, MessagesClient |
| @prodantix/sdk/web | createWebClient, installAutocapture, installSessionReplay, WebTransport |
| @prodantix/sdk/node | createNodeClient |
| @prodantix/sdk/csp | prodantixConnectSrc, prodantixScriptSrc |
| @prodantix/sdk/chat | mountChat, ChatClient, ChatPanel, resolveAppearance |
| @prodantix/sdk/messenger | fetchMessengerConfig, cachedMessengerConfig, MESSENGER_CACHE_KEY |
@prodantix/sdk
Klien, pengevaluasi flag, serta antarmuka penyimpanan dan transport. Tidak ada di sini yang menyentuh DOM, jadi Anda bisa memasok penyimpanan, transport, jam, atau pabrik id sendiri dan menjalankannya di mana saja.
import { createClient, MemoryStorage } from '@prodantix/sdk';
const prodantix = createClient({
apiKey: PRODANTIX_PUBLIC_KEY,
host: 'https://eu.api.prodantix.com',
storage: new MemoryStorage(),
});
prodantix.capture('order.completed', { properties: { amount: 4200, currency: 'XAF' } });
await prodantix.flush();@prodantix/sdk/web
Perkabelan peramban di atas klien yang sama: penyimpanan di local storage, pengiriman lewat beacon saat halaman disembunyikan, serta autocapture pageview dan klik. Session replay tetap mati sampai Anda memberinya host replay.
import { createWebClient } from '@prodantix/sdk/web';
const prodantix = createWebClient({
apiKey: PRODANTIX_PUBLIC_KEY,
host: 'https://eu.api.prodantix.com',
flagsHost: 'https://api.prodantix.com',
autocaptureOptions: { clicks: true, pageviews: true },
});
prodantix.identify('user-42', { set: { plan: 'pro' } });
if (await prodantix.isFeatureEnabled('new-checkout')) {
// ...
}
// Every per-flag read records one $feature_flag_called exposure.
const variant = await prodantix.getVariant('new-checkout');
if (variant?.key === 'treatment') {
// ...
}
// A flag targeting a cohort reads this user's memberships from the store;
// pass them yourself to skip the lookup.
if (await prodantix.isFeatureEnabledLocal('members', { cohorts: ['<cohort id>'] })) {
// ...
}@prodantix/sdk/node
Klien yang sama dengan bawaan sisi server. Event disangga lalu dikirim di latar belakang, jadi panggil shutdown sebelum proses berakhir atau batch terakhir tidak akan pernah terkirim.
import { createNodeClient } from '@prodantix/sdk/node';
// A node client streams flag changes over Socket.IO by default and refetches
// the snapshot on push; streamFlags: false leaves only the 30s poll.
const prodantix = createNodeClient({
apiKey: process.env.PRODANTIX_PUBLIC_KEY,
host: 'https://eu.api.prodantix.com',
streamFlags: true,
});
prodantix.capture('job.completed', { properties: { queue: 'emails' } });
await prodantix.shutdown();@prodantix/sdk/csp
Sumber content security policy yang dibutuhkan sebuah aplikasi untuk menjangkau Prodantix. Modul ini tidak mengimpor apa pun dari React maupun peramban, sehingga berkas konfigurasi build bisa membacanya langsung.
import { prodantixConnectSrc, prodantixScriptSrc } from '@prodantix/sdk/csp';
const hosts = {
ingest: 'https://eu.api.prodantix.com',
edge: 'https://eu.edge.prodantix.com',
replay: 'https://eu.replay.prodantix.com',
};
const policy = [
`connect-src ${prodantixConnectSrc(hosts)}`,
`script-src ${prodantixScriptSrc(hosts)}`,
].join('; ');@prodantix/sdk/chat
Widget dukungan. Pasang, dan Anda mendapat handle yang membuka, menutup, memusnahkan, serta mengidentifikasi pengguna. Ia dirender di dalam shadow root tertutup, sehingga gaya halaman induk tidak pernah menjangkaunya.
import { mountChat } from '@prodantix/sdk/chat';
const messenger = mountChat({
apiKey: PRODANTIX_PUBLIC_KEY,
host: 'https://eu.edge.prodantix.com',
distinctId: prodantix.distinctId,
});
document.querySelector('#help')?.addEventListener('click', () => messenger.open());@prodantix/sdk/messenger
Pembaca pengaturan di balik widget: ia mengambil konfigurasi messenger sebuah proyek, menyimpannya di cache, dan melaporkan apakah proyek itu mengaktifkan messenger. Cuplikan yang berjalan sendiri dibangun terpisah dan tidak dapat dijangkau lewat subpath ini.
import { fetchMessengerConfig } from '@prodantix/sdk/messenger';
const config = await fetchMessengerConfig({
apiKey: PRODANTIX_PUBLIC_KEY,
host: 'https://eu.edge.prodantix.com',
});
if (config?.enabled) {
// the project has the messenger switched on
}