@ogygia/federation
Widgets and catalog
federate({ widgets }) serves named components, such as a KPI panel or a price box, that other teams place in their own pages. An unsigned catalog lists them with their props, and npx ogygia fragments turns it into types and a CI check.
A route is a whole page of your app. A widget is one piece that other teams put on their own pages, such as a KPI panel or a price box. You declare widgets in federate({ widgets }), each under the name callers use, and a shell places one with peer.widget(name, props).
import { federate } from '@ogygia/federation';
import { region } from 'ogygia';
import { env } from '$env/dynamic/private';
import Kpis from '$lib/Kpis.svelte' with { region: 'raw' };
import Clock from '$lib/Clock.svelte' with { region: 'raw' };
federate({
name: 'dash',
key: env.DASH_SIGNING_KEY,
audience: 'dash.internal',
peers: {
shell: {
origin: 'https://www.example.com',
key: env.SHELL_PUBLIC_KEY
}
},
widgets: {
// a bare function: served, and listed in the catalog without props
clock: (props) =>
region(Clock, { zone: props.zone ?? 'UTC' }),
// { props, make }: the same, with its prop names in the catalog
kpis: {
props: ['org'],
make: (props, { user }) =>
region(Kpis, {
org: props.org ?? 'acme',
viewer: user ?? null
})
}
}
});Widget functions
A widget is a function that takes the props and { user, request, url }, and returns anything with an html field, or a promise of one. A held region from region(Component, props) is that already: your app awaits it, which renders the component. Import the component with region: 'raw', so its scoped CSS becomes a real stylesheet that travels with the HTML. Islands inside it keep their own wake settings and wake on the other team’s page (Foreign islands).
useris the visitor the caller signed into the call, orundefinedfor an anonymous call (Visitor identity).requestandurlare the call’s own: aGETto/og/fragment/<name>on your app.requestis the raw call, headers included, the unverifiedx-og-usertoo (#2311, item 13). Read identity only fromuser.
A widget that returns { html: null }, or nothing, answers 500. The answer’s HTML has its asset URLs made absolute to your origin, and server islands inside it work on the caller’s page.
Props
A caller’s dash.widget('kpis', { org: 'acme' }) becomes GET /og/fragment/kpis?org=acme. Every query parameter arrives in props as a string. Parse numbers and booleans yourself.
The props list in { props, make } filters nothing and checks nothing: a widget gets every parameter the caller sent, whatever the list says. It only names the props in the catalog. Treat each value as chosen by the caller, and decide what the visitor may see from user, which the signature proves, never from a prop.
Names
A widget’s name is the last segment of its URL, so it can’t hold a /. Two names are taken: page is the exposed table’s endpoint and __catalog is the catalog, so a widget under either name is never reached. A name your app doesn’t have gets a 404 once the call’s signature has passed; an unsigned call gets a 401 first.
Catalog
/og/fragment/__catalog lists your widgets and their props, sorted. It’s unsigned on purpose: it’s a list of names, not data, and a CI job can read it without a key.
{
"names": ["clock", "kpis"],
"widgets": {
"clock": { "props": [] },
"kpis": { "props": ["org"] }
}
}Typed stubs
npx ogygia fragments <origin> --out <file> reads a peer’s catalog and writes it as TypeScript for the app that uses the widgets: a WidgetName union, a WIDGET_NAMES list and a WidgetProps map. Widget stubs shows the file it writes and its errors. A small typed helper then checks every call’s name and props:
import type { RemoteDials } from '@ogygia/federation';
import { dash } from '$lib/server/peers';
import type {
WidgetName,
WidgetProps
} from '$lib/dash-widgets';
export const dashWidget = <N extends WidgetName>(
name: N,
props: WidgetProps[N],
dials?: RemoteDials
) => dash.widget(name, props, dials);Drift checks
In CI, npx ogygia fragments <origin> --check <file> fails the consuming team’s build when the catalog no longer matches the committed stub, so a renamed widget or a changed prop list fails a build, not a page. Drift checks covers how it compares, including writing the origin the same way each time.