@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 275 lines 13 KB
Raw
1 /**
2 * Model a domain from commands, events and rejections; aggregates that decide;
3 * views that fold; and policies that react. Expose it to clients as an Api,
4 * and host it on whichever Platform Layer is provided.
5 *
6 * ```typescript
7 * import * as Domain from "@alchemy.run/domain";
8 *
9 * export class MoneyDeposited extends Domain.Event("MoneyDeposited", {
10 * amount: Cents,
11 * balanceAfter: Cents,
12 * }) {}
13 * ```
14 *
15 * Every building block is `Domain.<Noun>(...)`, and its types live on the
16 * same name: `Domain.Event.Any`, `Domain.Aggregate.StateOf<typeof Account>`.
17 * Stories (given / when / then specifications) are in `@alchemy.run/domain/Story`;
18 * the Cloudflare platform is in `@alchemy.run/cloudflare/Domain`.
19 */
20 import * as AggregateModule from "./Aggregate.ts";
21 import * as ApiModule from "./Api.ts";
22 import * as CommandModule from "./Command.ts";
23 import * as EventModule from "./Event.ts";
24 import * as ExternalSystemModule from "./ExternalSystem.ts";
25 import type * as KeyModule from "./Key.ts";
26 import * as ModelModule from "./Model.ts";
27 import * as PolicyModule from "./Policy.ts";
28 import * as ProjectionModule from "./Projection.ts";
29 import * as RejectionModule from "./Rejection.ts";
30 import * as RequestContextModule from "./RequestContext.ts";
31 import * as SagaModule from "./Saga.ts";
32 import * as StreamModule from "./Stream.ts";
33 import * as ViewModule from "./View.ts";
34
35 export declare namespace Event {
36 /** Any Event class. */
37 export type Any = EventModule.Any;
38 /** An event value: its `_tag` plus its data. */
39 export type Instance<Tag extends string, F extends EventModule.Fields> = EventModule.Instance<
40 Tag,
41 F
42 >;
43 }
44 /** A fact the domain recorded. See {@link EventModule.make}. */
45 export const Event = EventModule.make;
46
47 export declare namespace Command {
48 /** Any Command class. */
49 export type Any = CommandModule.Any;
50 export type Instance<Tag extends string, F extends EventModule.Fields> = CommandModule.Instance<
51 Tag,
52 F
53 >;
54 /** The services a command's middleware provides to `decide`. */
55 export type ContextOf<C extends CommandModule.Any> = CommandModule.ContextOf<C>;
56 }
57 /** A request to change one aggregate. See {@link CommandModule.make}. */
58 export const Command = CommandModule.make;
59
60 export declare namespace Rejection {
61 /** Any Rejection class. */
62 export type Any = RejectionModule.Any;
63 export type Instance<Tag extends string, F extends EventModule.Fields> = RejectionModule.Instance<
64 Tag,
65 F
66 >;
67 }
68 /** A typed reason a command or read was refused. See {@link RejectionModule.make}. */
69 export const Rejection = RejectionModule.make;
70
71 export declare namespace Aggregate {
72 /** Any Aggregate class. */
73 export type Any = AggregateModule.Any;
74 export type IdOf<A extends AggregateModule.Any> = AggregateModule.IdOf<A>;
75 export type StateOf<A extends AggregateModule.Any> = AggregateModule.StateOf<A>;
76 export type EventOf<A extends AggregateModule.Any> = AggregateModule.EventOf<A>;
77 export type CommandOf<A extends AggregateModule.Any> = AggregateModule.CommandOf<A>;
78 /** A reference to one instance of an aggregate. */
79 export type Ref<A extends AggregateModule.Any = AggregateModule.Any> = AggregateModule.Ref<A>;
80 /** What an accepted command returns: its stream, version, events and reply. */
81 export type Receipt<Ev = unknown, Reply = unknown> = AggregateModule.Receipt<Ev, Reply>;
82 export type ScheduleReceipt = AggregateModule.ScheduleReceipt;
83 /** Where an event sits: its instance, stream, sequence, time and command. */
84 export type Envelope<Id = string> = AggregateModule.Envelope<Id>;
85 export type SendOptions = AggregateModule.SendOptions;
86 export type ScheduleOptions = AggregateModule.ScheduleOptions;
87 /** The instance type of a class: `InstanceOf<typeof NotOwner>` is `NotOwner`. */
88 export type InstanceOf<C> = AggregateModule.InstanceOf<C>;
89 /** Options for `MyAggregate.predict`. */
90 export type PredictOptions<Id = string, R = never> = AggregateModule.PredictOptions<Id, R>;
91 /** What `MyAggregate.predict` gives: accepted with events and state, rejected, or unpredictable. */
92 export type Prediction<State, Ev, Rej, Id = string> = AggregateModule.Prediction<
93 State,
94 Ev,
95 Rej,
96 Id
97 >;
98 }
99 /** A consistency boundary that decides commands. See {@link AggregateModule.make}. */
100 export const Aggregate = AggregateModule.make;
101 /** A command with this id already ran, so it can't be scheduled or cancelled. */
102 export const AlreadyExecuted = AggregateModule.AlreadyExecuted;
103 export type AlreadyExecuted = AggregateModule.AlreadyExecuted;
104 /** The scheduled command was already cancelled. */
105 export const AlreadyCancelled = AggregateModule.AlreadyCancelled;
106 export type AlreadyCancelled = AggregateModule.AlreadyCancelled;
107 /** No command was scheduled with that id. */
108 export const NotScheduled = AggregateModule.NotScheduled;
109 export type NotScheduled = AggregateModule.NotScheduled;
110
111 /**
112 * What keys a view, policy, projection or stream: an aggregate (one instance
113 * per ref) or a string Schema such as an org id (one instance per value).
114 */
115 export declare namespace Key {
116 /** An aggregate, or a string Schema. */
117 export type Any = KeyModule.Any;
118 /** A string Schema that keys instances by value. */
119 export type Schema = KeyModule.Schema;
120 /** How one instance of a key is addressed: a ref, or a Schema key's value. */
121 export type RefOf<K> = KeyModule.RefOf<K>;
122 /** The id of one instance of a key: the aggregate id, or the Schema key's value. */
123 export type IdOf<K> = KeyModule.IdOf<K>;
124 }
125
126 export declare namespace View {
127 /** Any View class. */
128 export type Any = ViewModule.Any;
129 export type StateOf<V extends ViewModule.Any> = ViewModule.StateOf<V>;
130 export type EventOf<V extends ViewModule.Any> = ViewModule.EventOf<V>;
131 export type KeyOf<V extends ViewModule.Any> = ViewModule.KeyOf<V>;
132 /** An event a view emitted, with its state after it. */
133 export type Entry<S, E, Id = string> = ViewModule.Entry<S, E, Id>;
134 export type Page<E> = ViewModule.Page<E>;
135 /** What `MyView.evolve` applies. */
136 export type LocalInput<Def, Key extends KeyModule.Any> = ViewModule.LocalInput<Def, Key>;
137 /** How much of the history recorded before a view or projection has reached it. */
138 export type History = ViewModule.History;
139 }
140 /** A read model folded from events, per key. See {@link ViewModule.make}. */
141 export const View = ViewModule.make;
142 /** A view read waited longer than allowed. */
143 export const WaitTimeout = ViewModule.WaitTimeout;
144 export type WaitTimeout = ViewModule.WaitTimeout;
145 /** A view did not catch up to a write in time. */
146 export const ConsistencyTimeout = ViewModule.ConsistencyTimeout;
147 export type ConsistencyTimeout = ViewModule.ConsistencyTimeout;
148
149 export declare namespace Policy {
150 /** Any Policy class. */
151 export type Any = PolicyModule.Any;
152 /** What a policy reacts to: the event, its source and the source's state. */
153 export type Trigger<From extends PolicyModule.From, E> = PolicyModule.Trigger<From, E>;
154 }
155 /** A standing business rule: on an event, decide what to do next. See {@link PolicyModule.make}. */
156 export const Policy = PolicyModule.make;
157
158 export declare namespace Saga {
159 /** Any Saga class. */
160 export type Any = SagaModule.Any;
161 /** Where a saga run is. */
162 export type Status<A, E> = SagaModule.Status<A, E>;
163 /** A saga's client. */
164 export type Client<Key, Input, A, E> = SagaModule.Client<Key, Input, A, E>;
165 }
166 /** A process that spans aggregates and takes time, run as an Effect workflow. See {@link SagaModule.make}. */
167 export const Saga = SagaModule.make;
168
169 export declare namespace Projection {
170 /** Any Projection class. */
171 export type Any = ProjectionModule.Any;
172 /** One input a projection's handler receives. */
173 export type Input<K extends KeyModule.Any, S, E> = ProjectionModule.Input<K, S, E>;
174 /** How a projection's Layer tunes it. */
175 export type Options = ProjectionModule.Options;
176 }
177 /** Consumes events into a store you own, in order per key; replayable, never sends commands. See {@link ProjectionModule.make}. */
178 export const Projection = ProjectionModule.make;
179
180 export declare namespace Stream {
181 /** Any Stream class. */
182 export type Any = StreamModule.Any;
183 export type EventOf<S extends StreamModule.Any> = StreamModule.EventOf<S>;
184 export type KeyOf<S extends StreamModule.Any> = StreamModule.KeyOf<S>;
185 /** One entry of a stream key. */
186 export type Entry<E, Id = string> = StreamModule.Entry<E, Id>;
187 }
188 /** A keyed, append-only, replayable log a projection writes and clients read. See {@link StreamModule.make}. */
189 export const Stream = StreamModule.make;
190
191 export declare namespace ExternalSystem {
192 /** Any ExternalSystem class. */
193 export type Any = ExternalSystemModule.Any;
194 export type Op<Args = unknown, Success = unknown, Error = unknown> = ExternalSystemModule.Op<
195 Args,
196 Success,
197 Error
198 >;
199 }
200 /** An outside system the model calls, as an Effect service. See {@link ExternalSystemModule.make}. */
201 export const ExternalSystem = ExternalSystemModule.make;
202
203 export declare namespace Api {
204 /** What an Api may serve: commands, views, streams, and aggregates with a `read` contract. */
205 export type Servable = ApiModule.Servable;
206 /** What `Domain.Api` takes: the model, and the names of what of it the Api serves. */
207 export type Options<
208 M extends ModelModule.Any,
209 C extends ReadonlyArray<ApiModule.CommandName<M>>,
210 V extends ReadonlyArray<ApiModule.ViewName<M>>,
211 S extends ReadonlyArray<ApiModule.StreamName<M>>,
212 A extends ReadonlyArray<ApiModule.AggregateName<M>>,
213 > = ApiModule.Options<M, C, V, S, A>;
214 /** The names of the commands a model holds: their tags. */
215 export type CommandName<M extends ModelModule.Any> = ApiModule.CommandName<M>;
216 /** The names of the views a model holds. */
217 export type ViewName<M extends ModelModule.Any> = ApiModule.ViewName<M>;
218 /** The names of the streams a model holds. */
219 export type StreamName<M extends ModelModule.Any> = ApiModule.StreamName<M>;
220 /** The names of the aggregates a model holds. */
221 export type AggregateName<M extends ModelModule.Any> = ApiModule.AggregateName<M>;
222 /** The typed client of an Api. */
223 export type Client<E extends ApiModule.Servable> = ApiModule.ApiClient<E>;
224 /** Type-level identity of an Api's client service, e.g. `Domain.Api.ClientKey<"BankApi">`. */
225 export type ClientKey<Name extends string> = ApiModule.ClientKey<Name>;
226 export type CommandClient<C extends CommandModule.Any> = ApiModule.CommandClient<C>;
227 /** How a command is sent: its `commandId` and the call's headers. */
228 export type CommandOptions = ApiModule.CommandOptions;
229 export type ViewClient<V extends ViewModule.Any> = ApiModule.ViewClient<V>;
230 export type StreamClient<S extends StreamModule.Any> = ApiModule.StreamClient<S>;
231 export type AggregateClient<A extends AggregateModule.Any> = ApiModule.AggregateClient<A>;
232 /** Everything a read can fail with. */
233 export type ReadError<D> = ApiModule.ReadError<D>;
234 /** Any Api class, for code that works with every Api. */
235 export type Any = ApiModule.Any;
236 /** The command classes an Api exposes. */
237 export type CommandsOf<A extends ApiModule.Any> = ApiModule.CommandsOf<A>;
238 /** The rejection classes the commands of an Api declare. */
239 export type RejectionsOf<A extends ApiModule.Any> = ApiModule.RejectionsOf<A>;
240 /** The client an Api's `Client` service gives. */
241 export type ClientOf<A extends ApiModule.Any> = ApiModule.ClientOf<A>;
242 }
243 /** The public surface of a domain for one client. See {@link ApiModule.make}. */
244 export const Api = ApiModule.make;
245 /** An Api client protocol: everything over HTTP, live reads as streamed responses. */
246 export const protocolHttp = ApiModule.protocolHttp;
247 /** An Api client protocol: calls over HTTP, each live read a WebSocket to the object that owns it. */
248 export const protocolWebSocket = ApiModule.protocolWebSocket;
249 /** An Api client protocol: the Api served in the caller's process, through its middleware. */
250 export const protocolInProcess = ApiModule.protocolInProcess;
251
252 export declare namespace Model {
253 /** Any Model class. */
254 export type Any = ModelModule.Any;
255 /** How a model's `layer` hosts it, including each view's version. */
256 export type LayerOptions<Views extends string = string> = ModelModule.LayerOptions<Views>;
257 }
258 /** The aggregates, views, policies, projections and streams hosted together. See {@link ModelModule.make}. */
259 export const Model = ModelModule.make;
260
261 /** Every definition problem in a Model, without hosting it. See {@link ModelModule.verify}. */
262 export const verify = ModelModule.verify;
263 /** A problem in a Model's definitions, found by {@link verify} or when its Layer is built. */
264 export { DefinitionError } from "./Kernel.ts";
265
266 /** A service middleware provides to a request, carried into the domain. See {@link RequestContextModule.make}. */
267 export const RequestContext = RequestContextModule.make;
268
269 export type { Filter, OrderBy } from "./Filter.ts";
270 export { InMemory } from "./InMemory.ts";
271 export type * as Middleware from "./Middleware.ts";
272 export { Platform } from "./Platform.ts";
273 export * as Read from "./Read.ts";
274 /** Trace attributes, hosting options and the metrics a model records. */
275 export * as Telemetry from "./Telemetry.ts";