@featureflip/js

中文文档
2.19.1发布于 2026/11/10Apache-2.0TypeScriptESM + CJS近 30 天下载 2973

JavaScript/TypeScript SDK for Featureflip

原始使用文档
文档目录10 节
  1. Installation
  2. Quick Start
  3. Configuration
  4. Evaluation
  5. Detailed Evaluation
  6. Event Tracking
  7. Testing
  8. Features
  9. Requirements
  10. License

@featureflip/js

Server-side JavaScript/TypeScript SDK for Featureflip - evaluate feature flags locally with near-zero latency.

Installation

npm install @featureflip/js

Quick Start

import { FeatureflipClient, createNodePlatform } from '@featureflip/js';

const client = FeatureflipClient.get(
  { sdkKey: 'your-sdk-key', baseUrl: 'https://eval.featureflip.io' },
  createNodePlatform(),
);

await client.waitForInitialization();

const enabled = client.boolVariation('my-feature', { user_id: '123' }, false);

if (enabled) {
  console.log('Feature is enabled!');
}

await client.close();

Singleton by construction. FeatureflipClient.get() is the only way to obtain a client. Calling get() more than once with the same SDK key returns handles pointing at one shared underlying client — the factory is refcounted, so closing a handle only shuts down the shared core when the last handle is closed. This makes the SDK safe to call from per-request handlers and DI containers without leaking SSE connections.

Configuration

const client = FeatureflipClient.get(
  {
    sdkKey: 'your-sdk-key',
    baseUrl: 'https://eval.featureflip.io', // Evaluation API URL
    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
  },
  createNodePlatform(),
);

Evaluation

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' });

Detailed Evaluation

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"

Event Tracking

// 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();

Featureflip counts each event name, per environment. The metadata you pass is transmitted with the event but is not stored, and event counts are not surfaced in the app or API.

Testing

Use forTesting() to create a client with predetermined flag values -- no network calls.

const client = FeatureflipClient.forTesting({
  'my-feature': true,
  'pricing-tier': 'pro',
});

client.boolVariation('my-feature', {}, false);     // true
client.stringVariation('pricing-tier', {}, 'free'); // 'pro'
client.boolVariation('unknown', {}, false);         // false (default)

Features

  • Local evaluation - Near-zero latency after initialization
  • Real-time updates - SSE streaming with automatic polling fallback
  • Event tracking - Automatic batching and background flushing
  • Test support - forTesting() factory for deterministic unit tests
  • TypeScript - Full type definitions included

Requirements

  • Node.js 18+

License

Apache-2.0

版本与发布记录

@featureflip/js 共发布 34 个版本,首次发布于 2026/04/02;过去 12 个月发布了 34 个版本。

版本发布日期距今
2.19.1latest2026/11/104 天前
2.19.02026/11/086 天前
2.18.02026/10/3015 天前
2.17.22026/10/2817 天前
2.17.12026/10/2717 天前
2.17.02026/10/2718 天前

关于 @featureflip/js 的常见问题

@featureflip/js 是什么?

JavaScript/TypeScript SDK for Featureflip

如何安装 @featureflip/js?

在项目目录运行 npm install @featureflip/js,或使用 pnpm add / yarn add / bun add。

@featureflip/js 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@featureflip/js 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@featureflip/js 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@featureflip/js 可以商用吗?

@featureflip/js 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@featureflip/js 的下载量有多少?

根据 npm 官方下载统计,@featureflip/js 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,973 次,日均约 99 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@featureflip/js 还在维护吗?

最新版本 2.19.1 发布于 2026/11/10(4 天前);至今共发布 34 个版本,其中过去 12 个月发布了 34 个;首次发布于 2026/04/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。