Skip to content

@ogygia/flags

OpenFeature

Connect a flag vendor through OpenFeature. openfeature() bridges an OpenFeature server client you already have, and ofrep() speaks the OpenFeature Remote Evaluation Protocol directly, with no SDK.

On this page

OpenFeature is the open standard most flag vendors implement. @ogygia/flags/openfeature ships two decision sources built on it, and ogygia depends on no vendor SDK for either:

  • openfeature(client, options?) wraps an OpenFeature server client you create, with whatever provider your vendor publishes;
  • ofrep(options) talks to any service that speaks the OpenFeature Remote Evaluation Protocol over HTTP, with no SDK at all.
import { OpenFeature } from '@openfeature/server-sdk';
import { decide } from '@ogygia/flags';
import { openfeature } from '@ogygia/flags/openfeature';
import * as ogygia from 'ogygia/server';
import { provider } from '$lib/server/flag-provider'; // your vendor's OpenFeature provider
import '$lib/flags';

await OpenFeature.setProviderAndWait(provider);
decide({ source: openfeature(OpenFeature.getClient()) });

export const handle = ogygia.handle();

openfeature

openfeature(client, options?) takes any object with getBooleanValue and getStringValue (and optionally getObjectValue) in the shape of OpenFeature’s server Client, so any SDK version fits. For each request it evaluates every registered flag in parallel:

  • a boolean flag asks getBooleanValue(name, false, context), and the answer is on or off;
  • a weighted flag asks getStringValue(name, control, context), and the answer is the variant’s name. A name the flag doesn’t declare is ignored.

The defaults matter. A flag the vendor doesn’t know answers with the default, so with openfeature() every boolean flag the vendor doesn’t define is off and every weighted flag is its control. The native rule (a rollout, targeting, weights) and the layer check don’t run for those flags, because the source already answered. Define every flag at the vendor when you use openfeature(), or use ofrep(), which skips flags the service doesn’t know.

A flag whose evaluation throws falls back to its native rule, and the server says flags_source_failed; the other flags keep their answers. All the evaluations share the 1-second deadline every source has. An OpenFeature SDK’s own hooks and event handlers work as usual, since ogygia calls your client.

OptionDefaultWhat it does
contextc.visitor’s keys, with sub (else vid) as targetingKeyBuilds the evaluation context from the request (Evaluation context).
valuesfalseAlso asks getObjectValue(name, undefined, context) for each flag, one more call per flag, and hands the result to .value(c).
import { OpenFeature } from '@openfeature/server-sdk';
import { decide } from '@ogygia/flags';
import { openfeature } from '@ogygia/flags/openfeature';

decide({
  source: openfeature(OpenFeature.getClient(), {
    values: true // payloads for flags declared with a `value` schema
  })
});

ofrep

ofrep(options) sends one bulk POST per request to <url>/ofrep/v1/evaluate/flags/bulk, with { context } as its body, and reads the flags in the answer. Any OFREP service works: a self-hosted flag daemon, an OpenFeature operator’s sidecar, or a vendor’s OFREP endpoint.

OptionDefaultWhat it does
urlrequiredThe service’s base URL. A trailing / is dropped.
headersnoneHeaders sent with every evaluation, such as an auth token or a tenant.
contextc.visitor’s keys, with sub (else vid) as targetingKeyBuilds the evaluation context (Evaluation context).
fetchthe global fetchThe fetch to use.
timeout800Milliseconds before the evaluation is aborted. Above 1,000, the source deadline comes first.

For each flag in the answer:

  • a flag with an errorCode (such as FLAG_NOT_FOUND), a key you didn’t declare, or a variant you didn’t declare is skipped, so that flag keeps its native rule;
  • the answer’s variant is the decision; without one, a boolean value reads as on or off;
  • a value that isn’t a boolean becomes the flag’s .value(c) payload.

A non-2xx answer, a network error or the timeout leaves every flag on its native rule for that request, and the server says flags_source_failed. When the request ends before the service answers (the visitor went away), the call is stopped and nothing is reported.

import { env } from '$env/dynamic/private';
import { decide } from '@ogygia/flags';
import { ofrep } from '@ogygia/flags/openfeature';

decide({
  source: ofrep({
    url: 'http://flags.internal:8016',
    headers: { authorization: `Bearer ${env.FLAGS_TOKEN}` },
    timeout: 300
  })
});

Evaluation context

Both sources send the vendor an evaluation context built from the request. By default it’s the visitor the source can see, with every key of it and targetingKey set to its sub, else its vid. The source runs before the router and your loads, so that visitor comes from a federation hop’s claims or the og-vid cookie, never from a session your app reads later (Sources and exposures).

To target signed-in users, or to add attributes like a country, pass context. It gets the same request context the source does:

Keep targetingKey the same id your flags hash on natively, so a visitor gets one bucket whether the vendor or the native rule decides.

import { OpenFeature } from '@openfeature/server-sdk';
import { decide } from '@ogygia/flags';
import { openfeature } from '@ogygia/flags/openfeature';
import { sessions } from '$lib/server/sessions';

decide({
  source: openfeature(OpenFeature.getClient(), {
    context: (c) => {
      const s = sessions.read(c.cookies?.get('session'));
      return {
        targetingKey: s?.userId ?? c.visitor?.vid,
        plan: s?.plan,
        country: c.request.headers.get('x-country') ?? undefined
      };
    }
  })
});