@ogygia/flags
Declaring flags
Declare a flag with flag(name, shape, options). The shape makes it a kill switch, a sticky percentage rollout, a targeting rule or a set of weighted variants, and the options add a layer or a vendor payload.
On this page
You declare a flag once, in an ordinary module, and import it wherever you read it. flag(name, shape?, options?) takes the flag’s name, its shape and a few rare options. The shape decides what kind of flag it is and what calling it returns.
import { flag, type CtxLike } from '@ogygia/flags';
export const checkout = flag('new-checkout'); // kill switch
export const promo = flag('summer-promo', 10); // rollout to 10%
export const beta = flag(
'beta',
(c: CtxLike) => c.visitor?.beta === true
); // targeting
export const hero = flag('hero', { control: 80, bold: 20 }); // weighted variantsShapes
The second argument is the shape:
| Shape | Kind | Calling it returns | Decides |
|---|---|---|---|
| omitted | kill switch | boolean | Off for every visitor until a source, an override or carried claims say on. |
| a number | rollout | boolean | On for that sticky percentage of identified visitors (Rollouts). |
| a function | targeting | boolean | Per request: true is on, false is off, undefined falls through to off (Targeting). |
| a record | weighted variants | the variant name | One of the keys, sticky per visitor, by the weights (Experiments). |
The first three make a BoolFlag, whose variants are off and on. A record makes a VariantFlag typed with its own keys, so hero(event) returns 'control' | 'bold'. A targeting function gets the same request context a read gets, so it can read a cookie, a header, c.visitor or a value you keep in a database row, which gives you runtime toggles without a redeploy. It runs once per request that reads the flag, so keep it cheap. Under strict TypeScript, annotate its parameter as CtxLike, since the overloads don’t type it yet (#2310).
import { flag, type CtxLike } from '@ogygia/flags';
export const internal = flag(
'internal-tools',
(c: CtxLike) => c.request.headers.get('x-employee') === '1'
);
export const layout = flag('layout', {
control: 2,
grid: 1,
list: 1
});Variants
A record’s keys are its variants, in the order you write them (with one exception, below). The first key is the control: what an unidentified visitor gets, what a layer’s other members answer, and what pick falls back to. The values are weights, and weights are ratios, not percentages: { control: 3, bold: 1 } is 75/25, and { control: 80, bold: 20 } is 80/20. A weight of 0 or less is never chosen. When every weight is 0 or less, everyone gets the control.
Don’t name a variant with a plain number, such as '2' or '10'. JavaScript orders integer-like keys before all others, so { control: 1, '10': 1 } has the variants ['10', 'control'], and '10' becomes the control (#2310). Write v2 or ten instead.
A boolean flag’s control is off, so a flag nobody decided on stays off.
import { flag } from '@ogygia/flags';
// a third of visitors each; 'control' is what anonymous visitors see
export const pricing = flag('pricing-page', {
control: 1,
annual: 1,
monthly: 1
});
// a variant held at 0 while it's built: declared, never chosen by the weights
export const search = flag('search', {
control: 1,
semantic: 0
});Names
A flag’s name is its identity everywhere: the hash that sticks a visitor to a bucket, the key a source answers on, the ?og-exp override, the decision a federation mount carries, and the manifest. Some rules follow from that:
- Keep names unique. Two
flag()calls with one name share every decision from a source, an override or a shell, and the source is asked about only the later one’s variants. - Renaming reshuffles. A new name hashes every visitor afresh, so renaming a running experiment moves people between buckets.
- Avoid
:and,. The override parsesname:variantpairs split by commas, so a name with either can’t be overridden. - Write the name as a string literal. The manifest records only calls whose first argument is a literal.
Options
The third argument takes three rare options:
| Option | What it does |
|---|---|
layer | A group name. Flags that share it never put one visitor in two of their treatments (Layers). |
value | A Standard Schema for a payload the vendor sends with the decision, read with .value(c). A payload that fails it is ignored (Vendor values). |
fallback | The payload when the source sends none or an invalid one. Give it with value, so .value(c) always returns something of the schema’s type. |
import * as v from 'valibot';
import { flag } from '@ogygia/flags';
export const banner = flag('promo-banner', 0, {
value: v.object({
text: v.string(),
tone: v.picklist(['info', 'warn'])
}),
fallback: { text: '', tone: 'info' as const }
});Registration
A flag registers itself when its module first runs. The registry is the list a decision source is asked about and the set a layer splits its lanes over, so a flag whose module hasn’t loaded yet is unknown to both.
An app on the server router imports every route module at startup, so its list is complete before the first request. On plain SvelteKit, import your flags module in src/hooks.server.ts, so every flag is registered before the first request asks the source. Keeping all flags in one module, such as src/lib/flags.ts, makes that one import.
On the dev server, each flag belongs to the module that declares it. Delete a flag and save, and it leaves the registry when its module reloads; another module’s reload keeps it.
import * as ogygia from 'ogygia/server';
import '$lib/flags'; // registers every flag before the first request
export const handle = ogygia.handle();