Skip to content

@smitejs/core ​

Semantic registrar: descriptors (IR nodes), relationships (IR edges) and the global registry.

bash
yarn add @smitejs/core

19 documented symbols · version 2.0.0-SNAPSHOT.0 · source

Descriptors ​

functionchildrenOf ​

ts
function childrenOf(from: Descriptor<string, unknown>, relation?: string): readonly Descriptor<string, unknown>[];

Walks the runtime child index of a descriptor, optionally filtered by relation.

ts
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");
ts
"relationship"
route

packages/core/src/descriptor.ts:122

functiondefineDescriptor ​

ts
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.

ts
const route = defineDescriptor("http.route", "GET /users/:id", {
  path: "/users/:id",
  method: "GET",
});
const found = lookup("GET /users/:id");
ts
route

packages/core/src/descriptor.ts:44

interfaceDescriptor ​

ts
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 ​

ts
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.

ts
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
  path: "/users/:id",
  method: "GET",
});
relate(app, "http.route", route);
finalizeDescriptor(app);
ts
true
true

packages/core/src/descriptor.ts:157

functionrefine ​

ts
function refine<Data>(descriptor: {
    data: Data;
}, patch: Record<string, unknown>): void;

Replaces the node's data with a frozen shallow-merged snapshot.

ts
const route = defineDescriptor("http.route", "GET /users/:id", {
  path: "/users/:id",
  method: "GET",
});
refine(route, { summary: "list users" });
ts
"list users"

packages/core/src/descriptor.ts:141

functionrelate ​

ts
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.

ts
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");
ts
"relationship"
route

packages/core/src/descriptor.ts:62

interfaceRelationshipDescriptor ​

ts
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 ​

ts
interface AppDescriptor extends Descriptor<"app", {
    name?: string;
}> {
}

Descriptor node that roots a Smite application graph.

packages/core/src/app.ts:4

functioncreateApp ​

ts
function createApp(name?: string): AppDescriptor;

Roots a new descriptor graph.

ts
const app = createApp("store");
ts
"app"
"store"

packages/core/src/app.ts:11

Reference ​

constchildren ​

ts
const children: unique symbol;

Symbol key for the runtime child index attached to parent descriptors.

packages/core/src/descriptor.ts:37

Registry ​

functionclear ​

ts
function clear(): void;

Empties the global registry.

ts
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();
ts
1
route
1
0

packages/core/src/registry.ts:65

functionlookup ​

ts
function lookup(key: string): Descriptor<string, unknown> | undefined;

Looks up a single descriptor by key.

ts
const route = defineDescriptor("http.route", "GET /users/:id", {
  path: "/users/:id",
  method: "GET",
});
const found = lookup("GET /users/:id");
ts
route

packages/core/src/registry.ts:29

functionlookupAll ​

ts
function lookupAll(kind?: string): readonly Descriptor<string, unknown>[];

Lists every descriptor of a given kind.

ts
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();
ts
1
route
1
0

packages/core/src/registry.ts:39

functionregister ​

ts
function register(descriptor: Descriptor<string, unknown>): void;

Inserts a descriptor into the global registry. Collect mode only.

ts
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();
ts
1
route
1
0

packages/core/src/registry.ts:13

functionrelationships ​

ts
function relationships(): readonly RelationshipDescriptor[];

Lists every relationship edge in the registry.

ts
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();
ts
1
route
1
0

packages/core/src/registry.ts:53

Scope ​

functioncurrentScope ​

ts
function currentScope(): ScopeContext | undefined;

Returns the context of the innermost enclosing execution unit, or undefined when no scope is active.

packages/core/src/scope.ts:28

functionregisterLogger ​

ts
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.

ts
const named = runWithScope({ id: "req-1" }, () =>
  registerLogger((context) => `logger-${context?.id}`),
);
ts
"logger-req-1"

packages/core/src/scope.ts:38

functionrunWithScope ​

ts
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.

ts
const context: ScopeContext = { method: "GET", path: "/health" };
const observed = await runWithScope(context, async () => {
  await Promise.resolve();
  return currentScope();
});
ts
context

packages/core/src/scope.ts:16

typeScopeContext ​

ts
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.

packages/core/src/scope.ts:3

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•api/smitejs-core