railyard/apps/console/lib/flags/flagsmith-backend.ts
caelum0x 332446652a feat(flags): Railyard Flags behind the add-on, the Flags screen and the OpenFeature SDK (PY2.1)
RYP-E2 and SP-E6, dispatch 4.2 PY2.1 (Railyard side):
- lib/flags: flag environments per app environment (a Scribase environment
  or branch), each with a public client key that reads that one environment.
  Two backends, one surface: the unmodified Flagsmith server (one Flagsmith
  organisation per Railyard org, a project per app, an environment per env;
  FLAGS_API_URL + FLAGS_ADMIN_TOKEN) and, as the fallback the dispatch names
  when the box 2 gate keeps Flagsmith down, the console's own tables
  (migration 0005). An environment keeps the backend it was made on.
- A branch environment starts with its parent's flags (copy_from), then
  changes alone.
- GET /api/v1/flags/client?key=: CORS-open, cached 15 s when keyed by URL;
  the first read by an app is the slot's proof (first_flag_read).
- Bridge route flags (contract: id_format, three cases, flags_item_keys);
  BRIDGE_SLOTS.flags landed.
- The Flags screen (nav item) flips with one click and adds a flag by key;
  POST /api/flags writes only the signed-in org's environments.
- packages/openfeature-railyard: a zero-dependency polling client (30 s,
  offline start from storage, back-off) and OpenFeature client and server
  providers, with Expo and Next.js examples.

Tests: flags core 11/11 (both backends, a fake of the Flagsmith API, tenant
isolation, branch copies, a 502 that leaves nothing half made), bridge 19/19,
contract 3/3, migration 6/6 (0005 twice on a scratch cluster), console 85/85
with the bundle build, tsc clean, SDK 7/7 (a flip reaches the app within 60 s
in the worst cache timing on a fake clock; copy lint on docs and the screen).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 23:58:12 +03:00

112 lines
5.7 KiB
TypeScript

// The Flagsmith backend (Wave P PY2.1): the unmodified Flagsmith server on
// box 2 (BSD-3-Clause) holds the flag values. Railyard maps
// Railyard org -> one Flagsmith organisation (tenant isolation)
// app -> one Flagsmith project
// environment -> one Flagsmith environment (its API key stays server side)
// flag -> a feature, and its state in that environment
// and talks to the admin REST API with the instance admin's token
// (FLAGS_ADMIN_TOKEN, `Authorization: Token ...`). Nothing here reaches a
// browser: apps read flags through Railyard's client endpoint with their
// public client key. The engine name never appears in customer copy.
import type { BackendRefs, Flag, FlagEnv, FlagInput, FlagsBackend } from "./types.ts";
type Fetch = typeof fetch;
export class FlagsBackendError extends Error {}
interface FsFeature {
id: number;
name: string;
description?: string | null;
}
interface FsState {
id: number;
enabled: boolean;
feature: number | { id: number; name: string };
feature_state_value: string | number | boolean | null;
updated_at?: string | null;
}
interface FsFlag {
enabled: boolean;
feature_state_value: string | number | boolean | null;
feature: { id: number; name: string; description?: string | null };
}
const text = (v: FsState["feature_state_value"]): string | null => (v === null || v === undefined || v === "" ? null : String(v));
export class FlagsmithBackend implements FlagsBackend {
readonly name = "flagsmith" as const;
readonly base: string;
private readonly opts: { apiUrl: string; adminToken: string; fetchImpl?: Fetch };
constructor(opts: { apiUrl: string; adminToken: string; fetchImpl?: Fetch }) {
this.opts = opts;
this.base = `${opts.apiUrl.replace(/\/$/, "")}/api/v1`;
}
private async call<T>(method: string, path: string, body?: unknown, envKey?: string): Promise<T> {
const headers: Record<string, string> = { accept: "application/json", "content-type": "application/json" };
if (envKey) headers["x-environment-key"] = envKey;
else headers.authorization = `Token ${this.opts.adminToken}`;
const res = await (this.opts.fetchImpl ?? fetch)(`${this.base}${path}`, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) });
if (!res.ok) throw new FlagsBackendError(`flags service ${method} ${path.split("?")[0].replace(/[A-Za-z0-9]{20,}/g, "…")} answered ${res.status}`);
const raw = await res.text();
return (raw ? JSON.parse(raw) : null) as T;
}
async prepare(input: { org_id: string; app: string; env: string }, orgName: string, siblings: FlagEnv[]): Promise<BackendRefs> {
const sameApp = siblings.find((e) => e.app === input.app && e.backend === "flagsmith" && e.backend_project && e.backend_org);
let org = sameApp?.backend_org ?? siblings.find((e) => e.backend === "flagsmith" && e.backend_org)?.backend_org ?? null;
if (!org) org = String((await this.call<{ id: number }>("POST", "/organisations/", { name: `${orgName} (${input.org_id.slice(0, 8)})` })).id);
let project = sameApp?.backend_project ?? null;
if (!project) project = String((await this.call<{ id: number }>("POST", "/projects/", { name: input.app, organisation: Number(org) })).id);
const env = await this.call<{ id: number; api_key: string }>("POST", "/environments/", { name: input.env, project: Number(project) });
if (!env?.api_key) throw new FlagsBackendError("flags service did not return an environment key");
return { backend_ref: env.api_key, backend_project: project, backend_org: org };
}
private refs(env: FlagEnv): { key: string; project: string } {
if (!env.backend_ref || !env.backend_project) throw new FlagsBackendError("this flag environment has no flags service reference");
return { key: env.backend_ref, project: env.backend_project };
}
async list(env: FlagEnv): Promise<Flag[]> {
const { key } = this.refs(env);
const flags = await this.call<FsFlag[]>("GET", "/flags/", undefined, key);
return (flags ?? [])
.map((f) => ({ key: f.feature.name, enabled: f.enabled, value: text(f.feature_state_value), description: f.feature.description ?? null, updated_at: null }))
.sort((a, b) => a.key.localeCompare(b.key));
}
private async feature(project: string, input: FlagInput): Promise<FsFeature> {
const found = await this.call<{ results: FsFeature[] }>("GET", `/projects/${project}/features/?search=${encodeURIComponent(input.key)}&page_size=100`);
const hit = (found?.results ?? []).find((f) => f.name === input.key);
if (hit) return hit;
return this.call<FsFeature>("POST", `/projects/${project}/features/`, {
name: input.key,
description: input.description ?? "",
default_enabled: false,
initial_value: null,
});
}
async set(env: FlagEnv, input: FlagInput, _actor: string, now: Date): Promise<Flag> {
const { key, project } = this.refs(env);
const feature = await this.feature(project, input);
const states = await this.call<{ results: FsState[] }>("GET", `/environments/${key}/featurestates/?feature=${feature.id}`);
const state = (states?.results ?? [])[0];
if (!state) throw new FlagsBackendError("flags service has no state for this flag in the environment");
const patch: Record<string, unknown> = { enabled: input.enabled };
if (input.value !== undefined) patch.feature_state_value = input.value;
const saved = await this.call<FsState>("PATCH", `/environments/${key}/featurestates/${state.id}/`, patch);
return {
key: input.key,
enabled: saved?.enabled ?? input.enabled,
value: text(saved?.feature_state_value ?? (input.value === undefined ? state.feature_state_value : input.value)),
description: input.description ?? feature.description ?? null,
updated_at: now.toISOString(),
};
}
}