@featureflare/sdk-js 0.0.26 → 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,6 +27,9 @@ const featureflare = new FeatureFlareClient({
27
27
 
28
28
  const enabled = await featureflare.bool('new-nav', { id: 'user-123' }, false);
29
29
  console.log(enabled);
30
+
31
+ const detailed = await featureflare.evaluate('new-nav', { id: 'user-123' }, false);
32
+ console.log(detailed.value, detailed.metadata.reason, detailed.metadata.source);
30
33
  ```
31
34
 
32
35
  ### Environment Variables
@@ -42,6 +45,43 @@ The SDK automatically reads from environment variables if `sdkKey` is not provid
42
45
  const featureflare = new FeatureFlareClient();
43
46
  ```
44
47
 
48
+ ### Resilience options (cache/retry/timeout/bootstrap)
49
+
50
+ ```typescript
51
+ const featureflare = new FeatureFlareClient({
52
+ sdkKey: 'featureflare_cli_...',
53
+ timeoutMs: 3000,
54
+ maxRetries: 2,
55
+ backoffMs: 200,
56
+ jitter: 0.25,
57
+ cacheTtlMs: 60_000,
58
+ staleTtlMs: 10_000,
59
+ bootstrap: {
60
+ flags: { 'new-nav': true },
61
+ killSwitches: ['dangerous-flow']
62
+ },
63
+ realtime: {
64
+ // optional: enabled defaults to true (SSE primary, polling fallback)
65
+ pollingIntervalMs: 15_000,
66
+ ssePath: '/api/v1/sdk/stream'
67
+ }
68
+ });
69
+ ```
70
+
71
+ - Cache-first evaluation order: fresh cache → stale cache (+ async revalidate) → bootstrap/persistent cache → default.
72
+ - `bool(...)` and `evaluate(...)` are outage-safe and return deterministic defaults for expected network failures.
73
+ - Hard kill switches have highest precedence and return `false` even when the API is unreachable.
74
+ - Realtime updates are enabled by default: SSE is primary transport; polling is used automatically as fallback when SSE is unavailable.
75
+
76
+ To explicitly disable realtime updates:
77
+
78
+ ```typescript
79
+ const featureflare = new FeatureFlareClient({
80
+ sdkKey: 'featureflare_cli_...',
81
+ realtime: { enabled: false }
82
+ });
83
+ ```
84
+
45
85
  ### API Base URL
46
86
 
47
87
  The SDK automatically determines the API base URL:
@@ -109,6 +149,15 @@ Evaluates a boolean feature flag for a user.
109
149
 
110
150
  Returns `Promise<boolean>` - The flag value for the user.
111
151
 
152
+ ##### `evaluate(flagKey: string, user: FeatureFlareUserPayload, defaultValue?: boolean): Promise<{ value: boolean; metadata: ... }>`
153
+
154
+ Evaluates a flag with diagnostics metadata:
155
+
156
+ - `reason`: `fresh_cache | stale_cache | bootstrap | default | network | kill_switch`
157
+ - `isStale`
158
+ - `latencyMs`
159
+ - `source`, `updatedAt`, `staleAt`, `expiresAt`
160
+
112
161
  **Example:**
113
162
 
114
163
  ```typescript
@@ -132,15 +181,10 @@ const allFlags = await featureflare.flags({ id: 'user-123' }, false);
132
181
 
133
182
  ## Error Handling
134
183
 
135
- If the API request fails (network error, non-2xx status), the SDK returns the `defaultValue`. Network errors (DNS, timeout, connection refused) will throw; wrap calls in `try/catch` if you want to handle them.
184
+ `bool(...)` and `evaluate(...)` are non-throwing for expected outage/network states. If the API request fails, the SDK falls back to stale/bootstrap/persistent/default values deterministically.
136
185
 
137
186
  ```typescript
138
- try {
139
- const enabled = await featureflare.bool('flag', user, false);
140
- } catch (error) {
141
- // Handle network errors
142
- console.error('Failed to evaluate flag:', error);
143
- }
187
+ const enabled = await featureflare.bool('flag', user, false);
144
188
  ```
145
189
 
146
190
  ## SDK Keys