apps 0.0.1-beta.46 → 0.0.1-beta.47

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.
@@ -0,0 +1,59 @@
1
+ /** Event declarations and the per-invocation emitter. Emitted events leave in the reply. */
2
+ import { Effect, Schema as EffectSchema } from "effect";
3
+ import { EmittedEvent, type AppEvent, type EmitOptions } from "../contracts/events.js";
4
+ import { type Fields, type Infer, type ObjectValue, type Schema } from "./schema.js";
5
+ declare const Native: unique symbol;
6
+ /** An event an app emits. Declare it in `defineApp`'s requirements under `events`. */
7
+ export interface EventDeclaration<F extends Fields, P extends Schema<unknown, boolean>> {
8
+ readonly [Native]: AppEvent;
9
+ readonly description: string;
10
+ readonly filters: F;
11
+ readonly payload: P;
12
+ }
13
+ /** Any declared event, for requirement constraints. */
14
+ export type AnyEventDeclaration = EventDeclaration<Fields, Schema<unknown, boolean>>;
15
+ /**
16
+ * Declare an event. `filters` are scalar fields subscribers can narrow on, such as a repository;
17
+ * every emitted occurrence supplies their values. `payload` describes each occurrence's data.
18
+ */
19
+ export declare const event: <const F extends Fields = {}, P extends Schema<unknown, boolean> = Schema<unknown>>(options: {
20
+ readonly description: string;
21
+ readonly filters?: F;
22
+ readonly payload: P;
23
+ }) => EventDeclaration<F, P>;
24
+ /** The native declaration retained by an author event. */
25
+ export declare const nativeEvent: (declaration: unknown) => AppEvent | undefined;
26
+ /** JSON Schema for each declared event, as the host reads it from requirements. */
27
+ export declare const declaredEvents: (events: Readonly<Record<string, AppEvent>>) => Effect.Effect<{
28
+ readonly [x: string]: {
29
+ readonly description: string;
30
+ readonly filters: {
31
+ readonly [x: string]: EffectSchema.Json;
32
+ };
33
+ readonly payload: {
34
+ readonly [x: string]: EffectSchema.Json;
35
+ };
36
+ };
37
+ }, EffectSchema.SchemaError, never>;
38
+ /** Emit options typed by the event's filters: required exactly when it declares some. */
39
+ type OptionsFor<F extends Fields> = keyof F extends never ? [options?: Omit<EmitOptions<never>, "filters">] : [options: EmitOptions<ObjectValue<F>> & {
40
+ readonly filters: ObjectValue<F>;
41
+ }];
42
+ /** `ctx.events` in mutations and webhook handlers. */
43
+ export interface EventEmitter<Events extends Readonly<Record<string, AnyEventDeclaration>>> {
44
+ /**
45
+ * Record an occurrence. It is delivered only if this invocation succeeds. Invalid data, filters
46
+ * or options throw here, before anything is recorded.
47
+ */
48
+ readonly emit: <Name extends keyof Events & string>(name: Name, data: Infer<Events[Name]["payload"]>, ...options: OptionsFor<Events[Name]["filters"]>) => void;
49
+ }
50
+ /** One invocation's buffer. The handler reads `emitted` only after the invocation succeeds. */
51
+ export declare const makeEmitter: (input: {
52
+ readonly events: Readonly<Record<string, AppEvent>>;
53
+ /** Every account ID bound to this invocation. */
54
+ readonly accounts: readonly string[];
55
+ /** The webhook's source account, which an emit names by default. */
56
+ readonly sourceAccount?: string;
57
+ readonly emitted: EmittedEvent[];
58
+ }) => EventEmitter<Readonly<Record<string, AnyEventDeclaration>>>;
59
+ export {};
@@ -22,6 +22,11 @@ export interface StepReplay {
22
22
  export declare const authorSql: (storage: AppSqlStorage, options: {
23
23
  readonly signal: AbortSignal;
24
24
  readonly step?: StepReplay;
25
+ /**
26
+ * The invocation's emitted events. A transaction that rolls back also discards the events its
27
+ * callback emitted, so subscribers never hear of writes that did not commit.
28
+ */
29
+ readonly emitted?: unknown[];
25
30
  }) => {
26
31
  reader: SqlReader;
27
32
  writer: Sql;
@@ -59,6 +59,8 @@ export declare function oauth2<Response extends Schema<unknown, boolean>>(option
59
59
  export type { AccountSlots } from "./contracts/app.js";
60
60
  export type { AppRequirements, AppContext, QueryContext, MutationContext, WebhookContext, } from "./contracts/context.js";
61
61
  export { type App, type AppDefinition, defineApp } from "./implementation/app.js";
62
+ export { event, type EventDeclaration, type EventEmitter } from "./implementation/events.js";
63
+ export type { EmitOptions } from "./contracts/events.js";
62
64
  export type { Approval, ApprovalContext, ApprovalDecision } from "./approval.js";
63
65
  type AuthorWebhook<Member, Config, State> = Member extends object ? Omit<PromiseMethods<Member>, "config" | "state"> & {
64
66
  readonly config: Config;