@sanely/flags
v0.1.0
Published
Type-safe feature flag library for TypeScript applications
Maintainers
Readme
@sanely/flags adds a type-safe evaluation layer to your feature flag service. You declare each flag once, write or install a provider, and read flags through a reader. The package manages identity resolution, validation, fallback values, hooks, caching, request coalescing, batches, timeouts, and server snapshots.
The package works in browsers and servers. React support is an optional entry point.
Features
- Flags as code. Each flag is a value with its key and default, so your editor can find every read.
- Type-safe flags. TypeScript infers boolean values, string variants, and payload types.
- Any provider. Write a provider with
defineProvider, and switch providers without a change to read sites. - Safe fallback. A failed evaluation returns the default, and its details carry a tagged error.
- Efficient evaluation. Store decisions, coalesce requests, or read several flags in one request.
- Server rendering. Render with a server snapshot and hydrate with no mismatch.
- React support. Use flags with React 19 and Suspense.
- Test helpers. Set flag values in tests with a fake provider.
- No external dependencies. The only runtime dependency is
@sanely/duration, which reads time expressions such as"500ms". Install React only when your application needs it.
Install
npm install @sanely/flagsThe package needs Node.js 22 or later on the server. The React entry point needs React 19.2 or later.
Get started
Declare your flags in a module that imports only
@sanely/flags:// flags/definitions.ts import { flag } from "@sanely/flags" export const betaAccess = flag.boolean("beta-access", false) export const theme = flag.variant("theme", ["light", "dark", "system"], "light")Write a provider for your feature flag service:
import { decision, defineProvider, type Identity } from "@sanely/flags" export const provider = defineProvider({ name: "my-service", decideOne: async (flag, identity: Identity) => { const result = await service.evaluate(flag.key, identity.distinctId) return flag.kind === "boolean" ? decision.boolean(result.enabled) : decision.variant(result.variant) }, })Create a reader, and read your flags:
import { createFlags } from "@sanely/flags" const flags = createFlags({ provider, identify: () => ({ distinctId: getCurrentUserId() }) }) if (await flags.get(betaAccess)) { showBetaFeatures() } const [hasBeta, selectedTheme] = await flags.getMany([betaAccess, theme])
flags.get(betaAccess) returns false when identity resolution or the provider fails, so your application continues with a known value.
Documentation
How-to guides:
- How to share flag definitions between server and client
- How to write a provider with
defineProvider - How to supply an identity
- How to use variant flags and evaluation details
- How to read several flags at once
- How to set timeouts and cancel evaluations
- How to observe evaluations with hooks
- How to cache provider decisions
- How to use flags in React
- How to render flags on the server
- How to test code that uses a flag
Reference:
Explanation:
Contributing
Read the contributor guide before changing the package. To report a vulnerability, read SECURITY.md.
License
MIT. See LICENSE.
