@smitejs/core
Semantic registrar: descriptors (IR nodes), relationships (IR edges) and the global registry.
yarn add @smitejs/core19 documented symbols · version 2.0.0-SNAPSHOT.0 · source
Descriptors
functionchildrenOf
function childrenOf(from: Descriptor<string, unknown>, relation?: string): readonly Descriptor<string, unknown>[];Walks the runtime child index of a descriptor, optionally filtered by relation.
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
const edge = relate(app, "http.route", route);
const routes = childrenOf(app, "http.route");"relationship"
routepackages/core/src/descriptor.ts:122
functiondefineDescriptor
function defineDescriptor<Kind extends string, Data>(kind: Kind, key: string, data: Data): Descriptor<Kind, Data>;Creates a descriptor node and (in collect mode) registers it.
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
const found = lookup("GET /users/:id");routepackages/core/src/descriptor.ts:44
interfaceDescriptor
interface Descriptor<Kind extends string, Data> {
readonly __kind: Kind;
readonly __key: string;
readonly data: Data;
}A node in the IR: a typed __kind, a unique __key, and a frozen data snapshot.
packages/core/src/descriptor.ts:6
functionfinalizeDescriptor
function finalizeDescriptor<D extends Descriptor<string, unknown>>(root: D): D;Deep-freeze the IR reachable from root: the node, its data, and every child (via the child index), recursively. Guards against cycles.
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
relate(app, "http.route", route);
finalizeDescriptor(app);true
truepackages/core/src/descriptor.ts:157
functionrefine
function refine<Data>(descriptor: {
data: Data;
}, patch: Record<string, unknown>): void;Replaces the node's data with a frozen shallow-merged snapshot.
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
refine(route, { summary: "list users" });"list users"packages/core/src/descriptor.ts:141
functionrelate
function relate<Rel extends string, From extends Descriptor<string, unknown>, To extends Descriptor<string, unknown>, Data = undefined>(from: From, relation: Rel, to: To, data?: Data): RelationshipDescriptor<Rel, Data>;Creates an edge between two descriptors and attaches it to the parent's runtime child index.
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
const edge = relate(app, "http.route", route);
const routes = childrenOf(app, "http.route");"relationship"
routepackages/core/src/descriptor.ts:62
interfaceRelationshipDescriptor
interface RelationshipDescriptor<Rel extends string = string, Data = undefined> extends Descriptor<"relationship", Readonly<{
from: string;
to: string;
relation: Rel;
data?: Data;
}>> {
}An edge in the IR, created by relate, carrying from, to, relation, and optional data.
packages/core/src/descriptor.ts:18
Junction
interfaceAppDescriptor
interface AppDescriptor extends Descriptor<"app", {
name?: string;
}> {
}Descriptor node that roots a Smite application graph.
functioncreateApp
function createApp(name?: string): AppDescriptor;Roots a new descriptor graph.
const app = createApp("store");"app"
"store"Reference
constchildren
const children: unique symbol;Symbol key for the runtime child index attached to parent descriptors.
packages/core/src/descriptor.ts:37
Registry
functionclear
function clear(): void;Empties the global registry.
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
relate(app, "http.route", route);
const apps = lookupAll("app");
const routes = lookupAll("http.route");
const rels = relationships();
clear();1
route
1
0packages/core/src/registry.ts:65
functionlookup
function lookup(key: string): Descriptor<string, unknown> | undefined;Looks up a single descriptor by key.
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
const found = lookup("GET /users/:id");routepackages/core/src/registry.ts:29
functionlookupAll
function lookupAll(kind?: string): readonly Descriptor<string, unknown>[];Lists every descriptor of a given kind.
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
relate(app, "http.route", route);
const apps = lookupAll("app");
const routes = lookupAll("http.route");
const rels = relationships();
clear();1
route
1
0packages/core/src/registry.ts:39
functionregister
function register(descriptor: Descriptor<string, unknown>): void;Inserts a descriptor into the global registry. Collect mode only.
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
relate(app, "http.route", route);
const apps = lookupAll("app");
const routes = lookupAll("http.route");
const rels = relationships();
clear();1
route
1
0packages/core/src/registry.ts:13
functionrelationships
function relationships(): readonly RelationshipDescriptor[];Lists every relationship edge in the registry.
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
path: "/users/:id",
method: "GET",
});
relate(app, "http.route", route);
const apps = lookupAll("app");
const routes = lookupAll("http.route");
const rels = relationships();
clear();1
route
1
0packages/core/src/registry.ts:53
Scope
functioncurrentScope
function currentScope(): ScopeContext | undefined;Returns the context of the innermost enclosing execution unit, or undefined when no scope is active.
functionregisterLogger
function registerLogger<T>(build: (context: ScopeContext | undefined) => T): T;Builds a request-scoped dependency (a logger, tracer, ...) by reading the active currentScope context and passing it to build. Because the scope is an AsyncLocalStorage, build may be invoked synchronously and still observe the request it belongs to.
const named = runWithScope({ id: "req-1" }, () =>
registerLogger((context) => `logger-${context?.id}`),
);"logger-req-1"functionrunWithScope
function runWithScope<T>(context: ScopeContext, run: () => T): T;Marks the start of an execution unit, propagating context through currentScope for the duration of run (synchronously and across every awaited promise; it spawns). Returns whatever run returns.
const context: ScopeContext = { method: "GET", path: "/health" };
const observed = await runWithScope(context, async () => {
await Promise.resolve();
return currentScope();
});contexttypeScopeContext
type ScopeContext = Record<PropertyKey, unknown>;The request-scoped context attached to a single execution unit (an HTTP request handler, a domain job, ...). It is carried across async boundaries by an AsyncLocalStorage so that code deep in a call stack can read the intent that spawned it. Mutable: middleware may stash shared values (a logger, a tracer) on it for the rest of the call stack to read.