TypeScript SDK
一个包,六个入口。导入与你的运行时匹配的那个,摇树构建会把其余部分挡在你的产物之外。
发布位置
@prodantix/sdk on npm.
安装
Bash
bun add @prodantix/sdk入口
每个子路径都带有自己的 ESM 与 CommonJS 构建以及各自的类型定义。根入口与运行时无关,其余入口则补上某一种运行时所需的部分。
| 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
客户端、功能开关求值器,以及存储与传输接口。这里没有任何代码触碰 DOM,因此你可以传入自己的存储、传输、时钟或 id 生成器,在任何地方运行它。
TypeScript
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
在同一个客户端之上补齐浏览器接线:本地存储持久化、页面隐藏时用信标投递,以及页面浏览与点击的自动采集。在你给出回放主机之前,会话回放始终关闭。
TypeScript
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
同一个客户端,采用服务端默认值。事件先缓冲,再由后台批量发送,因此请在进程退出前调用 shutdown,否则最后一批永远发不出去。
TypeScript
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
一个应用访问 Prodantix 所需的内容安全策略来源。它不从 React 或浏览器导入任何东西,因此构建配置文件可以直接读取它。
TypeScript
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
客服挂件。挂载后你会得到一个句柄,可以打开、关闭、销毁并标识用户。它渲染在封闭的 shadow root 内,宿主页面的样式永远够不到它。
TypeScript
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
挂件背后的配置读取器:它拉取某个项目的消息助手配置、做缓存,并告诉你该项目是否已启用消息助手。自执行的代码片段是单独构建的,无法通过这个子路径取得。
TypeScript
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
}