Feature flag service with percentage rollout
Views
3.1K
Copies
876
Likes
595
Comments
0
Copy rate
28.0%
Prompt
Act as a senior backend engineer. Implement a minimal, dependency-free feature-flag service in TypeScript that supports percentage rollout, user targeting, and kill-switches.
Flag store type: [flag_store]
Target environment: [target_env]
Requirements:
- Deterministic bucketing: the same user always lands in the same bucket for the same flag
- Use FNV-1a or a similar fast hash — no external hashing deps
- Flags support: boolean on/off, percentage (0–100), allow-list of userIds, block-list of userIds
- Order of evaluation: kill-switch → block-list → allow-list → percentage → default
- Async `getFlag(key)` with in-memory caching and a configurable TTL
- `isEnabled(key, user)` returns a boolean synchronously once flags are loaded
- `useFeature(key)` React hook wrapper (SSR-safe default)
- 100% unit-testable — no global singletons
Output format:
- Single `feature-flags.ts` file, no markdown fences
- Export: `createFeatureFlagClient`, `type FlagConfig`, `type User`, `useFeature`
- At the bottom, a `// Example` comment block showing a typical flag JSONCustomise this prompt
Fill in 2 variables to personalise this prompt
Preview
Act as a senior backend engineer. Implement a minimal, dependency-free feature-flag service in TypeScript that supports percentage rollout, user targeting, and kill-switches.
Flag store type: [flag_store]
Target environment: [target_env]
Requirements:
- Deterministic bucketing: the same user always lands in the same bucket for the same flag
- Use FNV-1a or a similar fast hash — no external hashing deps
- Flags support: boolean on/off, percentage (0–100), allow-list of userIds, block-list of userIds
- Order of evaluation: kill-switch → block-list → allow-list → percentage → default
- Async `getFlag(key)` with in-memory caching and a configurable TTL
- `isEnabled(key, user)` returns a boolean synchronously once flags are loaded
- `useFeature(key)` React hook wrapper (SSR-safe default)
- 100% unit-testable — no global singletons
Output format:
- Single `feature-flags.ts` file, no markdown fences
- Export: `createFeatureFlagClient`, `type FlagConfig`, `type User`, `useFeature`
- At the bottom, a `// Example` comment block showing a typical flag JSON