@ featureflip/ 节点
节点.js SDK 用于 特性flip - 评估当地地标,高度接近零; 建于顶上 @featureflip/js 带有 Node.js 特定默认值.
使用 打开形状看到了吗
@featureflip/openfeature-node, (中文(简体) ), openFeature Node.js SDK 的特性flip提供商.
安装
npm install @featureflip/node
快速启动
import { FeatureflipClient } from '@featureflip/node';
// Blocks until flags are loaded
const client = await FeatureflipClient.create({
sdkKey: 'your-sdk-key',
});
const enabled = client.boolVariation('my-feature', { user_id: '123' }, false);
if (enabled) {
console.log('Feature is enabled!');
}
await client.close();
或者,同步获得一个手柄并手动等待:
const client = FeatureflipClient.get({ sdkKey: 'your-sdk-key' });
await client.waitForInitialization();
单通由建筑.
FeatureflipClient.get()是获得客户的唯一方法——在v2.0中公共建筑师被移除. 通话get()不止一次使用相同的 SDK 密钥返回手柄指向一个共享的下方客户端. 工厂被重新计算,因此关闭一个手柄只有在最后一个手柄被关闭时才会关闭共享的核心. 这使得SDK能够安全地从每个请求处理器和DI容器中调用而不会泄露SSE连接.
配置
const client = await FeatureflipClient.create({
sdkKey: 'your-sdk-key',
baseUrl: 'https://eval.featureflip.io', // Evaluation API URL (default)
streaming: true, // Use SSE for real-time updates (default)
pollInterval: 30000, // Polling interval in ms if streaming=false
flushInterval: 30000, // Event flush interval in ms
flushBatchSize: 100, // Events per batch
initTimeout: 10000, // Max ms to wait for initialization
maxStreamRetries: 5, // SSE retries before falling back to polling
});
评价
const context = { user_id: '123', email: '[email protected]' };
// Boolean flag
const enabled = client.boolVariation('feature-key', context, false);
// String flag
const tier = client.stringVariation('pricing-tier', context, 'free');
// Number flag
const limit = client.numberVariation('rate-limit', context, 100);
// JSON flag
const config = client.jsonVariation('ui-config', context, { theme: 'light' });
详细评价
const detail = client.variationDetail('feature-key', { user_id: '123' }, false);
console.log(detail.value); // The evaluated value
console.log(detail.reason); // "RuleMatch", "Fallthrough", "FlagDisabled", etc.
console.log(detail.ruleId); // Rule ID if reason is "RuleMatch"
事件跟踪
// Track custom events
client.track('checkout-completed', { user_id: '123' }, { total: 99.99 });
// Record an identify event for analytics (does not affect flag evaluation)
client.identify({ user_id: '123', email: '[email protected]', plan: 'pro' });
// Force flush pending events
await client.flush();
特性flip计算每个事件名称,每个环境. 您通过的元数据会随事件一同传输,但不会被存储,事件计数也不会被浮出应用或API.
测试
使用 forTesting() 创建具有预定旗帜值的客户端 -- -- 没有网络呼叫.
const client = FeatureflipClient.forTesting({
'my-feature': true,
'pricing-tier': 'pro',
});
client.boolVariation('my-feature', {}, false); // true
client.stringVariation('pricing-tier', {}, 'free'); // 'pro'
特征
- 地方评价 - 初始化后近零纬度
- 实时更新 - SSE 自动倒计时
- 事件跟踪 - 自动批量和背景冲洗
- 测试支持 - 说吧
forTesting()用于确定性单位测试的工厂 - 类型脚本 - 包括全部类型定义
- 节点.js 优化 - 本地用途
httpsSE 和 HTTP 模块
所需资源
- 节点.js 18+
许可证
阿帕奇 - 2.0