@base44-preview/sdk 0.8.46-pr.272.f64ff62 → 0.8.46-pr.273.64a80af

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.
@@ -2,11 +2,12 @@
2
2
  * Extend this interface to add typed `subscribe` callbacks and `send` payloads
3
3
  * for your deployed Actors.
4
4
  *
5
- * This is separate from {@link ActorNameRegistry} (which is auto-generated
5
+ * This is separate from `ActorNameRegistry` (which is auto-generated
6
6
  * by `base44 types generate`), so there are no conflicts.
7
7
  *
8
8
  * @example
9
9
  * ```typescript
10
+ * // Declare message types for a deployed actor
10
11
  * declare module "@base44/sdk" {
11
12
  * interface ActorRegistry {
12
13
  * ChatRoom: {
@@ -21,7 +22,7 @@ export interface ActorRegistry {
21
22
  }
22
23
  /**
23
24
  * Auto-populated by `base44 types generate` with the names of your deployed actors.
24
- * Do not edit this interface manually. Use {@link ActorRegistry} for message types.
25
+ * Do not edit this interface manually. Use `ActorRegistry` for message types.
25
26
  */
26
27
  export interface ActorNameRegistry {
27
28
  }
@@ -32,75 +33,202 @@ type ToClientFor<N extends string> = N extends keyof ActorRegistry ? ActorRegist
32
33
  type ToServerFor<N extends string> = N extends keyof ActorRegistry ? ActorRegistry[N] extends {
33
34
  toServer: infer O;
34
35
  } ? O : unknown : unknown;
35
- /** Options for {@link ActorRef.connect}. */
36
+ /** Options for [connect()](#connect). */
36
37
  export interface ActorConnectOptions {
37
38
  /**
38
- * The connection id, used as the actor's `conn.id`. Supply a stable value
39
- * (e.g. persisted per tab) so a reconnect reuses the same server-side
40
- * identity; omit for an auto-generated per-connection id.
39
+ * The connection id, used as the actor's `conn.id`. Supply a stable value,
40
+ * such as one persisted per tab, so a reconnect reuses the same server-side
41
+ * identity. Omit it for an auto-generated per-connection id.
41
42
  */
42
43
  id?: string;
43
44
  }
44
- /** Handle for one listener registered via {@link Connection.subscribe}. */
45
+ /** Handle for one listener registered via `subscribe()`. */
45
46
  export interface ActorSubscription {
46
- /** Remove this listener; other listeners and the socket stay live. */
47
+ /**
48
+ * Removes this listener. Other listeners on the same connection, and the
49
+ * connection itself, stay live. To close the connection as well, call
50
+ * [close()](#close).
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * // Stop listening without closing the connection
55
+ * const sub = conn.subscribe((msg) => console.log(msg));
56
+ * sub.unsubscribe();
57
+ * ```
58
+ */
47
59
  unsubscribe(): void;
48
60
  }
49
61
  /**
50
- * A live connection to an actor instance, returned by {@link ActorRef.connect}.
62
+ * A live connection to an actor instance, returned by [connect()](#connect).
51
63
  * `subscribe`/`send` are always valid. You only get a `Connection` once the
52
64
  * socket has been opened, so there's no pre-connect state to guard against.
53
65
  */
54
66
  export interface Connection<N extends string = string> {
55
- /** The connection id (the value the actor sees as `conn.id`). */
67
+ /** The connection id, which is the value the actor sees as `conn.id`. */
56
68
  readonly id: string;
57
- /** Register a message listener. Multiple are allowed; returns a per-listener unsubscribe. */
69
+ /**
70
+ * Registers a listener for messages sent by the actor. Any number of
71
+ * listeners can be registered on one connection, and each receives every
72
+ * message.
73
+ *
74
+ * The callback payload is typed when the actor is declared in
75
+ * [ActorRegistry](#actorregistry), and is `unknown` otherwise.
76
+ *
77
+ * @param callback - Called with each message the actor sends to this client.
78
+ * @returns An `ActorSubscription` that removes this one listener.
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * // Listen for messages from the actor
83
+ * const sub = conn.subscribe((msg) => {
84
+ * if (msg.type === "message") console.log(msg.text);
85
+ * });
86
+ * ```
87
+ */
58
88
  subscribe(callback: (data: ToClientFor<N>) => void): ActorSubscription;
59
- /** Send a message. Buffered by the socket until it's open; dropped after
60
- * {@link close}. */
89
+ /**
90
+ * Sends a message to the actor.
91
+ *
92
+ * Messages sent before the socket finishes opening are buffered and flushed
93
+ * on open. Messages sent after {@link close} are dropped silently.
94
+ *
95
+ * The payload is typed when the actor is declared in
96
+ * [ActorRegistry](#actorregistry), and is `unknown` otherwise.
97
+ *
98
+ * @param data - The message to send to the actor.
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * // Send a message to the actor
103
+ * conn.send({ type: "message", text: "hi" });
104
+ * ```
105
+ */
61
106
  send(data: ToServerFor<N>): void;
62
107
  /**
63
- * Tear down the socket, heartbeat, and all listeners. Safe to call more
64
- * than once. A connection also closes itself when it fails permanently.
65
- * See {@link ActorRef.connect}.
108
+ * Closes the connection, tearing down the socket, the heartbeat, and every
109
+ * listener registered on it. Safe to call more than once.
110
+ *
111
+ * A connection also closes itself when it fails permanently. See
112
+ * [connect()](#connect) for how to recover from that.
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * // Close when the view that opened the connection goes away.
117
+ * useEffect(() => {
118
+ * const conn = base44.actors.ChatRoom(roomId).connect();
119
+ * conn.subscribe(setMessage);
120
+ * return () => conn.close();
121
+ * }, [roomId]);
122
+ * ```
66
123
  */
67
124
  close(): void;
68
125
  }
69
126
  /**
70
127
  * A handle to one actor instance, obtained from `base44.actors.MyActor(id)`. Call
71
- * {@link connect} to open the socket and get a {@link Connection}.
128
+ * {@link connect} to open the socket and get a [Connection](#connection).
72
129
  */
73
130
  export interface ActorRef<N extends string = string> {
74
131
  /**
75
- * Open the WebSocket and return the {@link Connection}. Idempotent while the
76
- * connection is open.
132
+ * Opens the WebSocket to this actor instance and returns the
133
+ * [Connection](#connection). Idempotent while the connection is open, so calling it
134
+ * again returns the same connection rather than opening a second socket.
135
+ *
136
+ * The returned connection is usable straight away. Messages passed to
137
+ * [send()](#send) before the socket finishes opening are buffered and
138
+ * flushed on open.
139
+ *
140
+ * A connection that fails permanently, for example because the actor doesn't
141
+ * exist or the caller isn't allowed to connect, closes itself and reports the
142
+ * error to the client's `onError` handler. Call `connect()` again once the
143
+ * cause is fixed to get a fresh [Connection](#connection), then re-subscribe, as
144
+ * listeners do not carry over.
145
+ *
146
+ * @param options - Connection options. See [ActorConnectOptions](#actorconnectoptions).
147
+ * @returns A live [Connection](#connection) to this actor instance.
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * // Connect to an actor instance
152
+ * const conn = base44.actors.ChatRoom("room-1").connect();
153
+ * ```
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * // Reuse a stable connection id so a reconnect keeps the same
158
+ * // server-side identity.
159
+ * let id = sessionStorage.getItem("chat-conn-id") ?? crypto.randomUUID();
160
+ * sessionStorage.setItem("chat-conn-id", id);
77
161
  *
78
- * A connection that fails permanently (for example, the actor doesn't exist
79
- * or the caller isn't allowed to connect) closes itself and reports the
80
- * error to the client's `onError` handler. Call `connect()` again after
81
- * fixing the cause to get a fresh {@link Connection}, and re-subscribe.
162
+ * const conn = base44.actors.ChatRoom("room-1").connect({ id });
163
+ * ```
82
164
  */
83
165
  connect(options?: ActorConnectOptions): Connection<N>;
84
166
  }
85
167
  /**
86
168
  * Client for a single named Actor. Call it with an instance id to get an
87
- * {@link ActorRef}. Typed automatically when the actor is registered in
88
- * {@link ActorRegistry}.
169
+ * [ActorRef](#actorref). Typed automatically when the actor is registered in
170
+ * [ActorRegistry](#actorregistry).
89
171
  */
90
172
  export interface ActorClient<N extends string = string> {
91
173
  (instanceId: string): ActorRef<N>;
92
174
  }
93
175
  /**
94
- * The actors module provides access to Cloudflare Durable Object-backed
176
+ * Actors module for real-time messaging with Cloudflare Durable Object-backed
95
177
  * Actors deployed by the Base44 platform.
96
178
  *
179
+ * An Actor is a named server-side object with persistent state. Each instance
180
+ * is addressed by an id, so `base44.actors.ChatRoom("room-1")` and
181
+ * `base44.actors.ChatRoom("room-2")` are separate instances with separate
182
+ * state. Clients open a WebSocket to an instance and exchange messages with it.
183
+ *
184
+ * This module provides:
185
+ * - Per-instance WebSocket connections, opened with [connect()](#connect)
186
+ * - Message listeners, registered with [subscribe()](#subscribe)
187
+ * - Message sending, with [send()](#send)
188
+ * - Automatic reconnection with backoff, including recovery from half-open
189
+ * sockets that stop delivering messages without emitting a close event
190
+ * - End-to-end typing of message payloads through [ActorRegistry](#actorregistry)
191
+ *
192
+ * This module is available to use with a client in anonymous and user
193
+ * authentication modes. It is not available on `base44.asServiceRole`.
194
+ *
195
+ * Connections stay open until you call [close()](#close), so close them
196
+ * when the view that opened them goes away.
197
+ *
198
+ * @example
97
199
  * ```typescript
98
- * const conn = base44.actors.MyActor("room-1").connect();
99
- * const sub = conn.subscribe((msg) => console.log(msg)); // typed via ActorRegistry
200
+ * // Open a connection to one instance of the ChatRoom actor.
201
+ * const conn = base44.actors.ChatRoom("room-1").connect();
202
+ *
203
+ * const sub = conn.subscribe((msg) => {
204
+ * console.log(msg);
205
+ * });
206
+ *
100
207
  * conn.send({ type: "message", text: "hi" });
208
+ *
209
+ * // Later, when the view goes away.
101
210
  * sub.unsubscribe();
102
211
  * conn.close();
103
212
  * ```
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * // Register the actor to type both directions of the conversation.
217
+ * declare module "@base44/sdk" {
218
+ * interface ActorRegistry {
219
+ * ChatRoom: {
220
+ * toClient: { type: "joined" | "message"; from?: string; text?: string };
221
+ * toServer: { type: "message"; text: string };
222
+ * };
223
+ * }
224
+ * }
225
+ *
226
+ * const conn = base44.actors.ChatRoom("room-1").connect();
227
+ * conn.subscribe((msg) => {
228
+ * // msg is typed as the toClient union.
229
+ * if (msg.type === "message") console.log(msg.text);
230
+ * });
231
+ * ```
104
232
  */
105
233
  export type ActorsModule = {
106
234
  [K in AllActorNames]: K extends keyof ActorRegistry ? ActorClient<string & K> : ActorClient;
@@ -4,7 +4,7 @@
4
4
  */
5
5
  export type AppPublicSettings = "private_with_login" | "public_with_login" | "public_without_login" | "workspace_with_login" | string;
6
6
  /**
7
- * The app's public configuration, as returned by {@link AppModule.getPublicSettings}.
7
+ * The app's public configuration, as returned by `getPublicSettings()`.
8
8
  */
9
9
  export interface AppPublicSettingsResponse {
10
10
  /** The app's ID. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.46-pr.272.f64ff62",
3
+ "version": "0.8.46-pr.273.64a80af",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",