logic-sdk 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,15 +32,15 @@ Write a module:
32
32
  // src/bookings/mutations.ts
33
33
  export const confirm = mutation({
34
34
  description: "Confirm a pending booking",
35
- inputSchema: z.object({ bookingId: z.uuid() }),
36
- outputSchema: Booking,
35
+ input: z.object({ bookingId: z.uuid() }),
36
+ output: Booking,
37
37
  // Who may call it, on every call
38
38
  authorize: staff,
39
39
  // HTTP, oRPC and AI tools may publish it
40
40
  expose: true,
41
41
 
42
- async execute({ bookingId }, ctx) {
43
- const booking = await findBooking(ctx, bookingId);
42
+ async handler(ctx, input) {
43
+ const booking = await findBooking(ctx, input.bookingId);
44
44
  if (booking.status !== "pending") {
45
45
  throw ctx.errors.reject("not_pending");
46
46
  }
@@ -49,7 +49,7 @@ export const confirm = mutation({
49
49
  await ctx.run(availability.hold, rangeOf(booking));
50
50
 
51
51
  // Delivered to listeners only after commit
52
- ctx.emit(confirmed, { bookingId });
52
+ ctx.emit(confirmed, { bookingId: input.bookingId });
53
53
 
54
54
  return setStatus(ctx, booking.id, "confirmed");
55
55
  },
@@ -109,9 +109,8 @@ Read-only, `--json` everywhere, exit codes for CI. The `logic-sdk` command ships
109
109
 
110
110
  ## Examples
111
111
 
112
- - [`examples/node`](examples/node): a bookings app. Six modules, Hono, Postgres, and a race test on real Postgres.
113
- - [`examples/workers`](examples/workers): a link shortener on Cloudflare Workers, Hyperdrive and `waitUntil`.
114
- - [`examples/nextjs`](examples/nextjs): a Server Action and an AI agent calling the same `bookings.confirm`.
112
+ - [`examples/shop`](examples/shop): start here. A shop written once and run on Node, Cloudflare Workers and Next.js, with an AI assistant.
113
+ - [`examples/bookings`](examples/bookings): a bigger app. Six modules, Hono, Postgres, and a race test on real Postgres.
115
114
 
116
115
  ## Docs
117
116
 
@@ -124,7 +123,7 @@ bun install
124
123
  bun run test # build, typecheck, lint, package checks, all tests
125
124
  ```
126
125
 
127
- `packages/logic-sdk` is the library, tested with `bun test`. `examples/node` runs on PGlite by default and on real Postgres with `DATABASE_URL` (`bun run test:pg`), which CI always sets. `examples/workers` runs inside workerd. Decisions and their reasons are in [DESIGN.md](DESIGN.md); what's next is in [ROADMAP.md](ROADMAP.md).
126
+ `packages/logic-sdk` is the library, tested with `bun test`. `examples/bookings` runs on PGlite by default and on real Postgres with `DATABASE_URL` (`bun run test:pg`), which CI always sets. `examples/shop` also runs its Worker inside workerd. Decisions and their reasons are in [DESIGN.md](DESIGN.md); what's next is in [ROADMAP.md](ROADMAP.md).
128
127
 
129
128
  ## License
130
129
 
package/dist/ai.d.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import { type Tool } from "ai";
2
- import { type Caller, type Fn, type FnIn, type FnOut, type Registry, type RegisteredFn } from "./index.ts";
3
- type ExposedOf<T> = {
4
- [N in keyof T as T[N] extends Fn<any, any, any, true> ? N : never]: T[N];
5
- };
2
+ import { type Caller, type ExposedFn, type ExposedOf, type FnIn, type FnOut, type Registry, type RegisteredFn } from "./index.ts";
6
3
  type UnionToIntersection<U> = (U extends unknown ? (u: U) => void : never) extends (i: infer I) => void ? I : never;
7
4
  /** `{ bookings_confirm, pricing_priceStay, … }`: one AI SDK tool per exposed function, typed. */
8
5
  export type AIToolsOf<Mods> = UnionToIntersection<{
@@ -33,7 +30,7 @@ export interface AIToolsOptions extends AIToolOptions {
33
30
  * Domain errors reach the model (it sees the code and can react). Anything unexpected is reported
34
31
  * to `onError` and the model gets "internal_error", so internals never land in its context.
35
32
  */
36
- export declare function toAITool<F extends Fn<any, any, any, true>>(caller: Caller<any>, fn: F, options?: AIToolOptions): Tool<FnIn<F>, FnOut<F>>;
33
+ export declare function toAITool<F extends ExposedFn>(caller: Caller<object>, fn: F, options?: AIToolOptions): Tool<FnIn<F>, FnOut<F>>;
37
34
  /**
38
35
  * The exposed functions your `filter` picks, as AI SDK tools named `<module>_<function>`.
39
36
  *
package/dist/ai.js CHANGED
@@ -1,5 +1,7 @@
1
- import { tool } from "ai";
1
+ import { jsonSchema, tool } from "ai";
2
2
  import { isDomainError } from "./index.js";
3
+ /** What a function without input looks like to the model: an object with nothing in it. It sends `{}`, which run ignores. */
4
+ const noInput = jsonSchema({ type: "object", properties: {}, additionalProperties: false });
3
5
  /**
4
6
  * One exposed function as an AI SDK tool, run as `caller`: the agent goes through the same policy,
5
7
  * validation, transaction and events as your HTTP API. You name it, like any AI SDK tool.
@@ -18,8 +20,8 @@ export function toAITool(caller, fn, options = {}) {
18
20
  throw new Error("[logic-sdk] toAITool needs a function with expose: true.");
19
21
  return tool({
20
22
  description: fn.description,
21
- inputSchema: fn.inputSchema,
22
- outputSchema: fn.outputSchema,
23
+ inputSchema: fn.input ?? noInput,
24
+ outputSchema: fn.output,
23
25
  async execute(input, { abortSignal }) {
24
26
  try {
25
27
  return await caller.run(fn, input, { signal: abortSignal });
package/dist/fetch.d.ts CHANGED
@@ -19,4 +19,4 @@ export interface FetchOptions<S extends object> {
19
19
  * Publishes only functions with `expose: true`. POST `{base}{id}` with a JSON body (`content-type: application/json` required);
20
20
  * queries also accept GET `?input=<json>`.
21
21
  */
22
- export declare function toFetchHandler<S extends object>(registry: Registry<S, any>, options: FetchOptions<S>): (request: Request, ...rest: any[]) => Promise<Response>;
22
+ export declare function toFetchHandler<S extends object>(registry: Registry<S>, options: FetchOptions<S>): (request: Request, ...rest: any[]) => Promise<Response>;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { StandardSchemaV1 } from "@standard-schema/spec";
2
2
  type Schema<I = any, O = any> = StandardSchemaV1<I, O>;
3
- type In<S> = S extends Schema<infer I, any> ? I : never;
4
- type Out<S> = S extends Schema<any, infer O> ? O : never;
3
+ type In<S> = S extends StandardSchemaV1<infer I, unknown> ? I : never;
4
+ type Out<S> = S extends StandardSchemaV1<unknown, infer O> ? O : never;
5
5
  /** What you declare once in `init<...>()`. */
6
6
  export interface Config {
7
7
  /** Who and where: actor, account, locale... Must be JSON-serializable (it travels with events). */
@@ -50,19 +50,21 @@ export type Kind = "query" | "mutation" | "action";
50
50
  declare const types: unique symbol;
51
51
  declare const BRAND: unique symbol;
52
52
  /** A query, mutation or action. Call it with `caller.run(fn, input)` or `ctx.run(fn, input)`. */
53
- export interface Fn<K extends Kind = Kind, I = any, O = any, E extends boolean = boolean> {
53
+ export interface Fn<K extends Kind = Kind, I = unknown, O = unknown, E extends boolean = boolean> {
54
54
  readonly [BRAND]: K;
55
55
  readonly kind: K;
56
56
  readonly description: string;
57
- readonly inputSchema: Schema;
58
- readonly outputSchema?: Schema;
57
+ /** What callers pass, validated on every call. Left out: the function takes nothing (`run(fn)`). */
58
+ readonly input?: Schema;
59
+ /** What callers get back, checked before it leaves. */
60
+ readonly output?: Schema;
59
61
  /** Your own data about the function. Checked against `meta` in `init` where you set it, typed in `wrap`; loose here, for adapters. */
60
- readonly meta?: any;
62
+ readonly meta?: Readonly<Record<string, unknown>>;
61
63
  /** Who may call it (from `authorize`). Runs on every call, before validation. */
62
- readonly authorize?: Policy<any>;
63
- /** `true`: adapters may publish it (HTTP, oRPC, AI tools). Requires `authorize` and `outputSchema`. */
64
+ readonly authorize?: Policy<never>;
65
+ /** `true`: adapters may publish it (HTTP, oRPC, AI tools). Requires `authorize` and `output`. */
64
66
  readonly expose?: true;
65
- readonly execute: (input: any, ctx: any) => Promise<any>;
67
+ readonly handler: (ctx: never, input: never) => Promise<unknown>;
66
68
  /** Phantom types, never set at runtime. */
67
69
  readonly [types]?: {
68
70
  input: I;
@@ -70,17 +72,22 @@ export interface Fn<K extends Kind = Kind, I = any, O = any, E extends boolean =
70
72
  exposed: E;
71
73
  };
72
74
  }
75
+ /** The exposed functions of one module: what adapters publish. */
76
+ export type ExposedOf<T> = {
77
+ [N in keyof T as T[N] extends Fn<Kind, unknown, unknown, true> ? N : never]: T[N];
78
+ };
73
79
  /** A function with `expose: true`: adapters may publish it. */
74
- export type ExposedFn<K extends Kind = Kind, I = any, O = any> = Fn<K, I, O, true>;
80
+ export type ExposedFn<K extends Kind = Kind, I = unknown, O = unknown> = Fn<K, I, O, true>;
75
81
  /** What callers pass to a function. */
76
- export type FnIn<F> = F extends Fn<any, infer I, any> ? I : never;
82
+ export type FnIn<F> = F extends Fn<Kind, infer I, unknown> ? I : never;
77
83
  /** What a function returns. */
78
- export type FnOut<F> = F extends Fn<any, any, infer O> ? O : never;
79
- /** `I`: what `emit` takes. `O`: what listeners receive (after the schema's transforms and defaults). */
80
- export interface EventDef<I = any, O = I> {
84
+ export type FnOut<F> = F extends Fn<Kind, unknown, infer O> ? O : never;
85
+ /** `I`: what `emit` takes. `O`: what listeners receive (after the payload schema's transforms and defaults). */
86
+ export interface EventDef<I = unknown, O = I> {
81
87
  readonly [BRAND]: "event";
82
88
  readonly kind: "event";
83
- readonly schema: Schema;
89
+ /** The payload's schema, checked when emitted and again at delivery. */
90
+ readonly payload: Schema;
84
91
  readonly description?: string;
85
92
  readonly [types]?: {
86
93
  input: I;
@@ -89,7 +96,7 @@ export interface EventDef<I = any, O = I> {
89
96
  }
90
97
  /**
91
98
  * What a dispatcher stores and delivers. Fully JSON: `payload` is what was emitted (validated),
92
- * and listeners get it parsed again by the event's schema at delivery.
99
+ * and listeners get it parsed again by the event's payload schema at delivery.
93
100
  */
94
101
  export interface Envelope<P = unknown, S = object> {
95
102
  id: string;
@@ -98,13 +105,13 @@ export interface Envelope<P = unknown, S = object> {
98
105
  scope: S;
99
106
  occurredAt: string;
100
107
  }
101
- export interface Listener<O = any> {
108
+ export interface Listener<O = unknown> {
102
109
  readonly [BRAND]: "listener";
103
110
  readonly kind: "listener";
104
111
  /** Lazy, so listeners can import other modules' barrels without import cycles. */
105
- readonly event: () => EventDef<any, O>;
112
+ readonly event: () => EventDef<unknown, O>;
106
113
  readonly description?: string;
107
- readonly execute: (payload: O, ctx: any) => Promise<unknown>;
114
+ readonly handler: (ctx: never, payload: O) => Promise<unknown>;
108
115
  }
109
116
  /** What a single call may change: who it runs as (part of the scope) and when to give up. Nothing else. */
110
117
  export interface RunOptions<S = object> {
@@ -119,7 +126,9 @@ export interface RunOptions<S = object> {
119
126
  */
120
127
  signal?: AbortSignal;
121
128
  }
122
- type Run<Allowed extends Kind, S> = <F extends Fn<Allowed>>(fn: F, input: FnIn<F>, options?: RunOptions<S>) => Promise<FnOut<F>>;
129
+ /** `run(fn, input, options?)`, or `run(fn)` for a function that takes no input. */
130
+ export type RunArgs<F, S> = undefined extends FnIn<F> ? [input?: FnIn<F>, options?: RunOptions<S>] : [input: FnIn<F>, options?: RunOptions<S>];
131
+ type Run<Allowed extends Kind, S> = <F extends Fn<Allowed>>(fn: F, ...args: RunArgs<F, S>) => Promise<FnOut<F>>;
123
132
  /**
124
133
  * Expected failures, namespaced to the module you're in: inside `bookings`, `ctx.errors.reject("not_pending")` has
125
134
  * code `"bookings.not_pending"`. Throw what they return. Codes are plain strings on purpose (see the errors docs).
@@ -143,7 +152,7 @@ interface BaseCtx<C extends Config, S> {
143
152
  /** Current time. Fixed in tests. */
144
153
  now: () => Date;
145
154
  }
146
- /** `S` is the scope, narrowed by the function's policy. Alias it in init.ts: `type QueryCtx = Core.QueryCtx<Config>`. */
155
+ /** `S` is the scope, narrowed by the function's policy. Alias it next to `init`: `type QueryCtx = QueryCtx<Config>`. */
147
156
  export interface QueryCtx<C extends Config, S = ScopeOf<C>> extends BaseCtx<C, S> {
148
157
  db: ReaderOf<C>;
149
158
  /** Call another query, with its policy, validation and output check: `await ctx.run(pricing.quote, { nights })`. */
@@ -157,7 +166,7 @@ export interface MutationCtx<C extends Config, S = ScopeOf<C>> extends BaseCtx<C
157
166
  */
158
167
  run: Run<"query" | "mutation", ScopeOf<C>>;
159
168
  /** Queue an event. Delivered to listeners only after the transaction commits. */
160
- emit<I>(event: EventDef<I, any>, payload: I): void;
169
+ emit<I>(event: EventDef<I, unknown>, payload: I): void;
161
170
  }
162
171
  export interface ActionCtx<C extends Config, S = ScopeOf<C>> extends BaseCtx<C, S> {
163
172
  services: ServicesOf<C>;
@@ -173,17 +182,20 @@ export interface ListenerCtx<C extends Config, O = unknown> extends ActionCtx<C>
173
182
  }
174
183
  /**
175
184
  * Who may call a function at all: signed in, admin, staff. Sync, reads only the scope, returns exactly `true` to allow.
176
- * Write it as a type guard (`(s): s is Admin => ...`) to narrow `ctx.scope` in `execute`.
177
- * Rules about one record ("only the owner may cancel") go in `execute`, after loading it.
185
+ * Write it as a type guard (`(s): s is Admin => ...`) to narrow `ctx.scope` in `handler`.
186
+ * Rules about one record ("only the owner may cancel") go in `handler`, after loading it.
178
187
  */
179
188
  export type Policy<S> = (scope: S) => boolean;
180
189
  type Guarded<F, S> = F extends (scope: any) => scope is infer G ? G : S;
181
190
  type CtxOf<C extends Config, K extends Kind, S> = K extends "query" ? QueryCtx<C, S> : K extends "mutation" ? MutationCtx<C, S> : ActionCtx<C, S>;
191
+ /** What a function without `input` gets: nothing. */
192
+ type NoInput = StandardSchemaV1<undefined, undefined>;
182
193
  type Def<IS extends Schema, Ctx, R> = {
183
194
  /** One sentence, shown to humans, adapters and AI tools. */
184
195
  description: string;
185
- inputSchema: IS;
186
- execute: (input: Out<IS>, ctx: Ctx) => Promise<R>;
196
+ /** What callers pass, validated on every call. Leave it out when the function takes nothing. */
197
+ input?: IS;
198
+ handler: (ctx: Ctx, input: Out<IS>) => Promise<R>;
187
199
  };
188
200
  type NotExposed = {
189
201
  expose?: undefined;
@@ -195,25 +207,25 @@ type WithMeta<C extends Config> = {
195
207
  meta?: MetaOf<C>;
196
208
  };
197
209
  type WithOutput<OS extends Schema> = {
198
- outputSchema: OS;
210
+ output: OS;
199
211
  };
200
212
  /**
201
- * `expose: true` needs `authorize` and `outputSchema`: you can't publish something without saying who may call it,
213
+ * `expose: true` needs `authorize` and `output`: you can't publish something without saying who may call it,
202
214
  * and what leaves your process is a contract. Internal functions may skip both; `authorize` still runs if set.
203
215
  */
204
216
  interface Constructor<C extends Config, K extends Kind> {
205
- <IS extends Schema, OS extends Schema, P extends Policy<ScopeOf<C>>>(def: Def<IS, CtxOf<C, K, Guarded<P, ScopeOf<C>>>, In<OS>> & WithOutput<OS> & WithMeta<C> & {
217
+ <IS extends Schema = NoInput, OS extends Schema = Schema, P extends Policy<ScopeOf<C>> = Policy<ScopeOf<C>>>(def: Def<IS, CtxOf<C, K, Guarded<P, ScopeOf<C>>>, In<OS>> & WithOutput<OS> & WithMeta<C> & {
206
218
  authorize: P;
207
219
  expose: true;
208
220
  }): Fn<K, In<IS>, Out<OS>, true>;
209
- <IS extends Schema, OS extends Schema, P extends Policy<ScopeOf<C>>>(def: Def<IS, CtxOf<C, K, Guarded<P, ScopeOf<C>>>, In<OS>> & WithOutput<OS> & WithMeta<C> & {
221
+ <IS extends Schema = NoInput, OS extends Schema = Schema, P extends Policy<ScopeOf<C>> = Policy<ScopeOf<C>>>(def: Def<IS, CtxOf<C, K, Guarded<P, ScopeOf<C>>>, In<OS>> & WithOutput<OS> & WithMeta<C> & {
210
222
  authorize: P;
211
223
  } & NotExposed): Fn<K, In<IS>, Out<OS>, false>;
212
- <IS extends Schema, R, P extends Policy<ScopeOf<C>>>(def: Def<IS, CtxOf<C, K, Guarded<P, ScopeOf<C>>>, R> & WithMeta<C> & {
224
+ <IS extends Schema = NoInput, R = unknown, P extends Policy<ScopeOf<C>> = Policy<ScopeOf<C>>>(def: Def<IS, CtxOf<C, K, Guarded<P, ScopeOf<C>>>, R> & WithMeta<C> & {
213
225
  authorize: P;
214
226
  } & NotExposed): Fn<K, In<IS>, R, false>;
215
- <IS extends Schema, OS extends Schema>(def: Def<IS, CtxOf<C, K, ScopeOf<C>>, In<OS>> & WithOutput<OS> & WithMeta<C> & NoPolicy & NotExposed): Fn<K, In<IS>, Out<OS>, false>;
216
- <IS extends Schema, R>(def: Def<IS, CtxOf<C, K, ScopeOf<C>>, R> & WithMeta<C> & NoPolicy & NotExposed): Fn<K, In<IS>, R, false>;
227
+ <IS extends Schema = NoInput, OS extends Schema = Schema>(def: Def<IS, CtxOf<C, K, ScopeOf<C>>, In<OS>> & WithOutput<OS> & WithMeta<C> & NoPolicy & NotExposed): Fn<K, In<IS>, Out<OS>, false>;
228
+ <IS extends Schema = NoInput, R = unknown>(def: Def<IS, CtxOf<C, K, ScopeOf<C>>, R> & WithMeta<C> & NoPolicy & NotExposed): Fn<K, In<IS>, R, false>;
217
229
  }
218
230
  export type ErrorKind = "invalid" | "unauthenticated" | "forbidden" | "not_found" | "conflict" | "rejected";
219
231
  /**
@@ -260,7 +272,7 @@ export interface Call<M = unknown> {
260
272
  export interface RegistryOptions<Mods extends Record<string, object>> {
261
273
  /** One entry per module: `{ bookings, availability }` with `import * as bookings from "./bookings"`. */
262
274
  modules: Mods;
263
- database?: DatabaseAdapter<any, any>;
275
+ database?: DatabaseAdapter<unknown, unknown>;
264
276
  services?: object;
265
277
  /** Settings every function reads as `ctx.config`. Plain data: it's copied and frozen. */
266
278
  config?: object;
@@ -294,7 +306,7 @@ export interface RegistryOptions<Mods extends Record<string, object>> {
294
306
  export interface Caller<S extends object = object> {
295
307
  readonly scope: S;
296
308
  /** Run any function as this caller: `await caller.run(bookings.cancel, input, { signal: request.signal })`. */
297
- run<F extends Fn>(fn: F, input: FnIn<F>, options?: RunOptions<S>): Promise<FnOut<F>>;
309
+ run<F extends Fn>(fn: F, ...args: RunArgs<F, S>): Promise<FnOut<F>>;
298
310
  }
299
311
  export interface Registered {
300
312
  id: string;
@@ -321,7 +333,7 @@ type TypedOptions<C extends Config, Mods extends Record<string, object>> = Omit<
321
333
  }) & ([DbOf<C>] extends [never] ? {
322
334
  database?: undefined;
323
335
  } : {
324
- database: DatabaseAdapter<any, DbOf<C>>;
336
+ database: DatabaseAdapter<unknown, DbOf<C>>;
325
337
  });
326
338
  /** Keys `Config` doesn't know are errors, so a typo like `servises` can't be silently ignored. */
327
339
  type OnlyKnownKeys<C> = {
@@ -336,31 +348,7 @@ type OnlyKnownKeys<C> = {
336
348
  */
337
349
  export declare function init<C extends Config & OnlyKnownKeys<C>>(): {
338
350
  /** Create the registry once at startup: `createRegistry({ modules, database, services })`. */
339
- createRegistry: <const Mods extends Record<string, object>>(options: TypedOptions<C, Mods>) => {
340
- /** The modules you registered (typed), for adapters that build typed routers. */
341
- modules: Mods;
342
- /** Bind who is calling, once per request, job or test: `const caller = registry.as(scope)`. */
343
- as: (scope: ScopeOf<C>) => Caller<ScopeOf<C>>;
344
- /** Everything the registry can do. Adapters read this. */
345
- functions: readonly RegisteredFn[];
346
- events: readonly (Registered & {
347
- event: EventDef;
348
- })[];
349
- listeners: readonly (Registered & {
350
- listener: Listener;
351
- event: EventDef;
352
- })[];
353
- idOf: (x: object) => string;
354
- /** Deliver one stored envelope to its listeners (durable dispatchers). Throws if any listener failed, so you can retry. */
355
- dispatch: (envelope: Envelope) => Promise<void>;
356
- /** @internal used by `logic-sdk/testing` */
357
- [INTERNAL]: {
358
- options: RegistryOptions<Mods>;
359
- overrides: Overrides;
360
- config: Frozen<ConfigOf<C>>;
361
- derive: (more: Overrides) => /*elided*/ any;
362
- };
363
- };
351
+ createRegistry: <const Mods extends Record<string, object>>(options: TypedOptions<C, Mods>) => Registry<ScopeOf<C>, Mods, ConfigOf<C>>;
364
352
  /** Reads data. Can run other queries. No writes, no events, no services. */
365
353
  query: Constructor<C, "query">;
366
354
  /** Changes data inside one transaction. Can run queries and mutations (same transaction) and emit events. */
@@ -369,52 +357,55 @@ export declare function init<C extends Config & OnlyKnownKeys<C>>(): {
369
357
  action: Constructor<C, "action">;
370
358
  /** Something that happened. Emitted by mutations, delivered after commit. */
371
359
  event: <S extends Schema>(def: {
372
- schema: S;
360
+ payload: S;
373
361
  description?: string;
374
362
  }) => EventDef<In<S>, Out<S>>;
375
- /** React to an event after commit, like an action. Must be idempotent. `on(() => bookings.confirmed, ...)` */
376
- on: <O>(event: () => EventDef<any, O>, execute: (payload: O, ctx: ListenerCtx<C, O>) => Promise<unknown>, opts?: {
363
+ /** React to an event after commit, like an action. Must be idempotent. `on(() => bookings.confirmed, async (ctx, payload) => …)` */
364
+ on: <O>(event: () => EventDef<unknown, O>, handler: (ctx: ListenerCtx<C, O>, payload: O) => Promise<unknown>, opts?: {
377
365
  description?: string;
378
366
  }) => Listener<O>;
379
367
  };
380
368
  declare const INTERNAL: unique symbol;
381
369
  export interface Overrides {
382
- database?: DatabaseAdapter<any, any>;
370
+ database?: DatabaseAdapter<unknown, unknown>;
383
371
  services?: object;
384
372
  /** Merged over the registry's config, section by section. */
385
373
  config?: object;
386
374
  events?: Dispatcher;
387
375
  now?: () => Date;
388
- stubs?: Map<Fn, (input: any, ctx: any) => Promise<any>>;
376
+ stubs?: Map<Fn, (ctx: never, input: never) => Promise<unknown>>;
389
377
  record?: (envelope: Envelope) => void;
390
378
  }
391
- export type Registry<S extends object = object, Mods extends Record<string, object> = Record<string, object>, Cf = unknown> = ReturnType<typeof build<S, Mods, Cf>>;
392
- /** Create the registry once at startup. Prefer the `createRegistry` returned by `init()`: it's typed with your config. */
393
- export declare function createRegistry<S extends object = object, const Mods extends Record<string, object> = Record<string, object>>(options: RegistryOptions<Mods>): {
379
+ export interface RegisteredEvent extends Registered {
380
+ event: EventDef;
381
+ }
382
+ export interface RegisteredListener extends Registered {
383
+ listener: Listener;
384
+ event: EventDef;
385
+ }
386
+ /** What `createRegistry` returns. `Cf` is your config's type, so tests can override parts of it. */
387
+ export interface Registry<S extends object = object, Mods extends Record<string, object> = Record<string, object>, Cf = unknown> {
394
388
  /** The modules you registered (typed), for adapters that build typed routers. */
395
- modules: Mods;
389
+ readonly modules: Mods;
396
390
  /** Bind who is calling, once per request, job or test: `const caller = registry.as(scope)`. */
397
- as: (scope: S) => Caller<S>;
391
+ as(scope: S): Caller<S>;
398
392
  /** Everything the registry can do. Adapters read this. */
399
- functions: readonly RegisteredFn[];
400
- events: readonly (Registered & {
401
- event: EventDef;
402
- })[];
403
- listeners: readonly (Registered & {
404
- listener: Listener;
405
- event: EventDef;
406
- })[];
407
- idOf: (x: object) => string;
393
+ readonly functions: readonly RegisteredFn[];
394
+ readonly events: readonly RegisteredEvent[];
395
+ readonly listeners: readonly RegisteredListener[];
396
+ /** `"bookings.confirm"` for a registered function or event. Throws for anything unregistered. */
397
+ idOf(fnOrEvent: object): string;
408
398
  /** Deliver one stored envelope to its listeners (durable dispatchers). Throws if any listener failed, so you can retry. */
409
- dispatch: (envelope: Envelope) => Promise<void>;
399
+ dispatch(envelope: Envelope): Promise<void>;
410
400
  /** @internal used by `logic-sdk/testing` */
411
- [INTERNAL]: {
401
+ readonly [INTERNAL]: {
412
402
  options: RegistryOptions<Mods>;
413
- overrides: Overrides;
414
- config: {};
415
- derive: (more: Overrides) => /*elided*/ any;
403
+ config: Frozen<Cf>;
404
+ derive(more: Overrides): Registry<S, Mods, Cf>;
416
405
  };
417
- };
406
+ }
407
+ /** Create the registry once at startup. Prefer the `createRegistry` returned by `init()`: it's typed with your config. */
408
+ export declare function createRegistry<S extends object = object, const Mods extends Record<string, object> = Record<string, object>>(options: RegistryOptions<Mods>): Registry<S, Mods, unknown>;
418
409
  /**
419
410
  * What `createRegistry` finds in your modules, without a database or services: every function, event and
420
411
  * listener with its id. For tools (the CLI, docs generators) that inspect an app without running it.
@@ -422,63 +413,13 @@ export declare function createRegistry<S extends object = object, const Mods ext
422
413
  export declare function describeModules(modules: Record<string, object>): {
423
414
  ids: Map<object, string>;
424
415
  functions: RegisteredFn[];
425
- events: (Registered & {
426
- event: EventDef;
427
- })[];
428
- listeners: (Registered & {
429
- listener: Listener;
430
- event: EventDef;
431
- })[];
432
- };
433
- declare function build<S extends object, Mods extends Record<string, object>, Cf = unknown>(options: RegistryOptions<Mods>, o: Overrides): {
434
- /** The modules you registered (typed), for adapters that build typed routers. */
435
- modules: Mods;
436
- /** Bind who is calling, once per request, job or test: `const caller = registry.as(scope)`. */
437
- as: (scope: S) => Caller<S>;
438
- /** Everything the registry can do. Adapters read this. */
439
- functions: readonly RegisteredFn[];
440
- events: readonly (Registered & {
441
- event: EventDef;
442
- })[];
443
- listeners: readonly (Registered & {
444
- listener: Listener;
445
- event: EventDef;
446
- })[];
447
- idOf: (x: object) => string;
448
- /** Deliver one stored envelope to its listeners (durable dispatchers). Throws if any listener failed, so you can retry. */
449
- dispatch: (envelope: Envelope) => Promise<void>;
450
- /** @internal used by `logic-sdk/testing` */
451
- [INTERNAL]: {
452
- options: RegistryOptions<Mods>;
453
- overrides: Overrides;
454
- config: Frozen<Cf>;
455
- derive: (more: Overrides) => /*elided*/ any;
456
- };
416
+ events: RegisteredEvent[];
417
+ listeners: RegisteredListener[];
457
418
  };
458
419
  /** @internal */
459
- export declare const internal: (registry: Registry<any, any, any>) => {
460
- options: RegistryOptions<any>;
461
- overrides: Overrides;
462
- config: any;
463
- derive: (more: Overrides) => {
464
- /** The modules you registered (typed), for adapters that build typed routers. */
465
- modules: any;
466
- /** Bind who is calling, once per request, job or test: `const caller = registry.as(scope)`. */
467
- as: (scope: any) => Caller<any>;
468
- /** Everything the registry can do. Adapters read this. */
469
- functions: readonly RegisteredFn[];
470
- events: readonly (Registered & {
471
- event: EventDef;
472
- })[];
473
- listeners: readonly (Registered & {
474
- listener: Listener;
475
- event: EventDef;
476
- })[];
477
- idOf: (x: object) => string;
478
- /** Deliver one stored envelope to its listeners (durable dispatchers). Throws if any listener failed, so you can retry. */
479
- dispatch: (envelope: Envelope) => Promise<void>;
480
- /** @internal used by `logic-sdk/testing` */
481
- [INTERNAL]: /*elided*/ any;
482
- };
420
+ export declare const internal: <S extends object, Mods extends Record<string, object>, Cf>(registry: Registry<S, Mods, Cf>) => {
421
+ options: RegistryOptions<Mods>;
422
+ config: Frozen<Cf>;
423
+ derive(more: Overrides): Registry<S, Mods, Cf>;
483
424
  };
484
425
  export {};