@teable/app 0.0.0-stage → 5.0.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/LICENSE +21 -0
- package/README.md +227 -2
- package/dist/build/cli.d.ts +2 -0
- package/dist/build/cli.js +19 -0
- package/dist/build/index.d.ts +31 -0
- package/dist/build/index.js +8 -0
- package/dist/chunks/323GVFSV.js +117 -0
- package/dist/chunks/5QIBCQL5.js +52 -0
- package/dist/chunks/EVNB2DU2.js +1126 -0
- package/dist/chunks/JQWQD45Z.js +116 -0
- package/dist/chunks/LV3ZHKKL.js +157 -0
- package/dist/client.d.ts +100 -0
- package/dist/client.js +9 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +14 -0
- package/dist/protocol.d.ts +401 -0
- package/dist/protocol.js +38 -0
- package/dist/schema.d.ts +45 -0
- package/dist/schema.js +13 -0
- package/dist/server/app.d.ts +138 -0
- package/dist/server/changefeed.d.ts +52 -0
- package/dist/server/cron.d.ts +26 -0
- package/dist/server/errors.d.ts +21 -0
- package/dist/server/migrations.d.ts +36 -0
- package/dist/server/schedules.d.ts +16 -0
- package/dist/server/tasks.d.ts +51 -0
- package/dist/testing.d.ts +38 -0
- package/dist/testing.js +69 -0
- package/package.json +55 -4
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The contract between an app and everything outside it.
|
|
3
|
+
*
|
|
4
|
+
* This module is the single source of truth for the shapes that cross the
|
|
5
|
+
* app boundary: the host Durable Object talks to an app through these types,
|
|
6
|
+
* the generated API and MCP tools are derived from `AppManifest`, and the
|
|
7
|
+
* table component renders columns from `ColumnInfo`. It is isomorphic: no
|
|
8
|
+
* runtime-specific imports, so workerd, the browser and build tools can all
|
|
9
|
+
* depend on it.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* The version of the host ↔ app contract (the region below). A host deploys
|
|
13
|
+
* and starts only apps built for its own version, so a change to the contract
|
|
14
|
+
* comes with a new version, and every app is deployed again.
|
|
15
|
+
*/
|
|
16
|
+
export declare const PROTOCOL_VERSION = 5;
|
|
17
|
+
/**
|
|
18
|
+
* The workerd compatibility date every app runs with: the host loads app code
|
|
19
|
+
* with it, and the SDK's and templates' tests run with it. Bump deliberately,
|
|
20
|
+
* with a test run.
|
|
21
|
+
*/
|
|
22
|
+
export declare const APP_COMPATIBILITY_DATE = "2026-12-01";
|
|
23
|
+
/** Space roles, strongest first. Mirrors the old platform's space roles. */
|
|
24
|
+
export declare const SPACE_ROLES: readonly ["owner", "creator", "editor", "commenter", "viewer"];
|
|
25
|
+
export type SpaceRole = (typeof SPACE_ROLES)[number];
|
|
26
|
+
/** Who is calling a method. Identity is verified by the platform, never by the app. */
|
|
27
|
+
export type Caller = {
|
|
28
|
+
kind: "user";
|
|
29
|
+
userId: string;
|
|
30
|
+
role: SpaceRole | null;
|
|
31
|
+
via: ExternalChannel;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "anonymous";
|
|
34
|
+
via: ExternalChannel;
|
|
35
|
+
} | {
|
|
36
|
+
kind: "system";
|
|
37
|
+
via: "task";
|
|
38
|
+
task: string;
|
|
39
|
+
};
|
|
40
|
+
export type ExternalChannel = "web" | "api" | "mcp";
|
|
41
|
+
/**
|
|
42
|
+
* Who may call a method from outside the app.
|
|
43
|
+
* - a role: members of the app's space with at least that role
|
|
44
|
+
* - "public": anyone, including anonymous visitors
|
|
45
|
+
* - "internal": only the app itself (tasks); never exposed
|
|
46
|
+
*/
|
|
47
|
+
export type Access = SpaceRole | "public" | "internal";
|
|
48
|
+
export type MethodKind = "read" | "write" | "task";
|
|
49
|
+
export interface MethodInfo {
|
|
50
|
+
name: string;
|
|
51
|
+
kind: MethodKind;
|
|
52
|
+
access: Access;
|
|
53
|
+
description?: string;
|
|
54
|
+
/** JSON Schema (draft 2020-12) of the method input, generated from its zod schema. */
|
|
55
|
+
input: JsonSchema;
|
|
56
|
+
/**
|
|
57
|
+
* For a read or write that declares one: JSON Schema (draft 2020-12) of
|
|
58
|
+
* what it returns as callers receive it, JSON (Dates are ISO strings),
|
|
59
|
+
* generated from the zod schema every result is checked against. Values
|
|
60
|
+
* that hold a column's values say so in `KindKeywords`.
|
|
61
|
+
*/
|
|
62
|
+
output?: JsonSchema;
|
|
63
|
+
/** For scheduled tasks: when they run. */
|
|
64
|
+
schedule?: {
|
|
65
|
+
cron: string;
|
|
66
|
+
timezone: string;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* What a method output's JSON Schema says about a value that holds a
|
|
71
|
+
* column's values, where JSON Schema has no word for it: the column's kind,
|
|
72
|
+
* and what the kind needs. `"x-teable-kind": "money"` with
|
|
73
|
+
* `"x-teable-currency": "USD"` is an integer count of cents. Choices are
|
|
74
|
+
* also `enum`, and dates `format: "date-time"`, as JSON Schema says them.
|
|
75
|
+
*/
|
|
76
|
+
export interface KindKeywords {
|
|
77
|
+
"x-teable-kind": ColumnKind;
|
|
78
|
+
"x-teable-currency"?: ColumnInfo["currency"];
|
|
79
|
+
"x-teable-references"?: ColumnInfo["references"];
|
|
80
|
+
}
|
|
81
|
+
/** A JSON value. Everything that crosses the app boundary is JSON, plus the few extras below. */
|
|
82
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
83
|
+
[key: string]: JsonValue;
|
|
84
|
+
};
|
|
85
|
+
export type JsonSchema = {
|
|
86
|
+
[key: string]: JsonValue;
|
|
87
|
+
};
|
|
88
|
+
/** What a method may return: JSON plus Dates (timestamp columns), which JSON encodes as ISO strings. */
|
|
89
|
+
export type MethodValue = JsonValue | Date | undefined | MethodValue[] | {
|
|
90
|
+
[key: string]: MethodValue;
|
|
91
|
+
};
|
|
92
|
+
/** How a column is presented and edited. Derived from the Drizzle column definition. */
|
|
93
|
+
export type ColumnKind = "text" | "number" | "money" | "boolean" | "date" | "select" | "reference" | "user" | "attachments" | "json";
|
|
94
|
+
export interface ColumnInfo {
|
|
95
|
+
/** Property name in the Drizzle table definition; rows returned by methods use these keys. */
|
|
96
|
+
key: string;
|
|
97
|
+
/** Column name in SQLite. */
|
|
98
|
+
name: string;
|
|
99
|
+
kind: ColumnKind;
|
|
100
|
+
notNull: boolean;
|
|
101
|
+
/** Part of the primary key; every column of a composite key is. */
|
|
102
|
+
primaryKey: boolean;
|
|
103
|
+
hasDefault: boolean;
|
|
104
|
+
/** Generated columns are computed by SQLite and never written by methods. */
|
|
105
|
+
generated: boolean;
|
|
106
|
+
enumValues?: readonly string[];
|
|
107
|
+
references?: {
|
|
108
|
+
table: string;
|
|
109
|
+
column: string;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* For `money` columns: the ISO 4217 code. Amounts are integers counting
|
|
113
|
+
* units of 10^-currencyDigits(currency) of it: cents for USD, yen for JPY,
|
|
114
|
+
* fils for KWD.
|
|
115
|
+
*/
|
|
116
|
+
currency?: string;
|
|
117
|
+
}
|
|
118
|
+
export interface TableInfo {
|
|
119
|
+
/** Export name in the schema module. */
|
|
120
|
+
key: string;
|
|
121
|
+
/** Table name in SQLite. */
|
|
122
|
+
name: string;
|
|
123
|
+
columns: ColumnInfo[];
|
|
124
|
+
}
|
|
125
|
+
export interface AppManifest {
|
|
126
|
+
/** The PROTOCOL_VERSION the app was built for; a host runs only its own. */
|
|
127
|
+
protocol: number;
|
|
128
|
+
methods: MethodInfo[];
|
|
129
|
+
tables: TableInfo[];
|
|
130
|
+
/** Platform capabilities the app declared; the host wires only these. */
|
|
131
|
+
capabilities: Capability[];
|
|
132
|
+
}
|
|
133
|
+
/** A stored file, as apps keep it in their rows. */
|
|
134
|
+
export interface Attachment {
|
|
135
|
+
key: string;
|
|
136
|
+
name: string;
|
|
137
|
+
size: number;
|
|
138
|
+
type: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The platform's capabilities (ADR 0010), the single list. A capability's name
|
|
142
|
+
* is what apps declare, the name of its binding in the app's env, and its
|
|
143
|
+
* property on the task context; the runtime must provide every one.
|
|
144
|
+
*/
|
|
145
|
+
export interface Capabilities {
|
|
146
|
+
/** Store, read and share files. Declare `capabilities: ["files"]` in createApp. */
|
|
147
|
+
files: FilesCapability;
|
|
148
|
+
/**
|
|
149
|
+
* Call other services over HTTP. Declare `capabilities: ["http"]` in createApp:
|
|
150
|
+
* the app's own `fetch()` then reaches the internet (npm SDKs work unchanged),
|
|
151
|
+
* through the platform, which meters every request and refuses its own hosts.
|
|
152
|
+
* `ctx.http.fetch` is the same.
|
|
153
|
+
*/
|
|
154
|
+
http: HttpCapability;
|
|
155
|
+
}
|
|
156
|
+
export type Capability = keyof Capabilities;
|
|
157
|
+
/** Every capability, by name. */
|
|
158
|
+
export declare const CAPABILITIES: readonly Capability[];
|
|
159
|
+
/**
|
|
160
|
+
* Files, for tasks: store a file, read one back, or make a link anyone can
|
|
161
|
+
* open until it expires. Scoped to the calling app by the host.
|
|
162
|
+
*/
|
|
163
|
+
export interface FilesCapability {
|
|
164
|
+
put(file: {
|
|
165
|
+
name: string;
|
|
166
|
+
type: string;
|
|
167
|
+
data: ArrayBuffer | string;
|
|
168
|
+
}): Promise<Attachment>;
|
|
169
|
+
get(key: string): Promise<{
|
|
170
|
+
name: string;
|
|
171
|
+
type: string;
|
|
172
|
+
data: ArrayBuffer;
|
|
173
|
+
} | null>;
|
|
174
|
+
share(key: string, options?: {
|
|
175
|
+
expiresInSeconds?: number;
|
|
176
|
+
}): Promise<PublicLink>;
|
|
177
|
+
}
|
|
178
|
+
/** Outbound HTTP, for tasks: `fetch`, as the platform forwards it for the calling app. */
|
|
179
|
+
export interface HttpCapability {
|
|
180
|
+
fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>;
|
|
181
|
+
}
|
|
182
|
+
/** A link anyone can open to read one file, until `expiresAt` (epoch seconds). */
|
|
183
|
+
export interface PublicLink {
|
|
184
|
+
url: string;
|
|
185
|
+
expiresAt: number;
|
|
186
|
+
}
|
|
187
|
+
export interface InvokeRequest {
|
|
188
|
+
method: string;
|
|
189
|
+
input: JsonValue | undefined;
|
|
190
|
+
caller: Caller;
|
|
191
|
+
}
|
|
192
|
+
/** Side effects of a call the host must act on. */
|
|
193
|
+
export interface Effects {
|
|
194
|
+
/** Sequence number of the latest change after the call. Never decreases. */
|
|
195
|
+
cursor: number;
|
|
196
|
+
/** Tables changed by the call, for realtime fan-out. */
|
|
197
|
+
changedTables: string[];
|
|
198
|
+
/** Earliest time (epoch ms) the app needs to be woken for pending tasks, or null. */
|
|
199
|
+
wakeAt: number | null;
|
|
200
|
+
}
|
|
201
|
+
/** A call's outcome, with the side effects the host acts on either way. */
|
|
202
|
+
export type InvokeResult = Result<MethodValue> & {
|
|
203
|
+
effects: Effects;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* The RPC surface every app exposes to its host Durable Object.
|
|
207
|
+
* Apps implement it through `createApp(...).define(...)`; nothing else does.
|
|
208
|
+
*/
|
|
209
|
+
export interface AppFacet {
|
|
210
|
+
manifest(): Promise<AppManifest>;
|
|
211
|
+
invoke(request: InvokeRequest): Promise<InvokeResult>;
|
|
212
|
+
/** Run tasks due at `now` (epoch ms). */
|
|
213
|
+
wake(now: number): Promise<WakeResult>;
|
|
214
|
+
/**
|
|
215
|
+
* Changes after the cursor `since`, oldest first. Null when some of them are
|
|
216
|
+
* no longer kept (history is kept for 30 days): read the data again and
|
|
217
|
+
* continue from the cursor that read reports.
|
|
218
|
+
*/
|
|
219
|
+
changes(since: number, limit?: number): Promise<Change[] | null>;
|
|
220
|
+
}
|
|
221
|
+
export interface WakeResult {
|
|
222
|
+
/**
|
|
223
|
+
* Task attempts started since the last report: this wake's, and those of
|
|
224
|
+
* wakes that crashed before they could report (a task over its CPU limit).
|
|
225
|
+
* The host meters them. The app forgets them as it reports them, so each
|
|
226
|
+
* attempt is reported once.
|
|
227
|
+
*/
|
|
228
|
+
attempts: number;
|
|
229
|
+
/** The task runs that failed; each is retried later unless it is `dead`. */
|
|
230
|
+
failed: TaskFailure[];
|
|
231
|
+
effects: Effects;
|
|
232
|
+
}
|
|
233
|
+
export interface TaskFailure {
|
|
234
|
+
method: string;
|
|
235
|
+
/** 1 on the first run. */
|
|
236
|
+
attempt: number;
|
|
237
|
+
error: AppErrorShape;
|
|
238
|
+
/** The task used its last attempt and will not run again. */
|
|
239
|
+
dead: boolean;
|
|
240
|
+
}
|
|
241
|
+
export interface Change {
|
|
242
|
+
seq: number;
|
|
243
|
+
table: string;
|
|
244
|
+
op: "insert" | "update" | "delete";
|
|
245
|
+
/** Primary key of the changed row; a JSON array string for composite keys. */
|
|
246
|
+
pk: string | number | null;
|
|
247
|
+
/** Row before the change (update, delete) and after it (insert, update). */
|
|
248
|
+
old: {
|
|
249
|
+
[column: string]: JsonValue;
|
|
250
|
+
} | null;
|
|
251
|
+
new: {
|
|
252
|
+
[column: string]: JsonValue;
|
|
253
|
+
} | null;
|
|
254
|
+
/**
|
|
255
|
+
* The write method call that made the change. Migrations run with the
|
|
256
|
+
* changefeed off, so their changes are not in the feed: a deploy that ran
|
|
257
|
+
* migrations is a point where subscribers read the data again.
|
|
258
|
+
*/
|
|
259
|
+
invocation: number;
|
|
260
|
+
}
|
|
261
|
+
export declare const ERROR_CODES: readonly ["BAD_INPUT", "UNAUTHENTICATED", "FORBIDDEN", "NOT_FOUND", "UNKNOWN_METHOD", "CONFLICT", "CONSTRAINT", "RATE_LIMITED", "INTERNAL"];
|
|
262
|
+
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
263
|
+
export interface AppErrorShape {
|
|
264
|
+
code: ErrorCode;
|
|
265
|
+
message: string;
|
|
266
|
+
/** Field-level problems, e.g. from input validation. */
|
|
267
|
+
issues?: {
|
|
268
|
+
path: (string | number)[];
|
|
269
|
+
message: string;
|
|
270
|
+
}[];
|
|
271
|
+
}
|
|
272
|
+
/** What anything that can be refused answers: its value, or the error to report. */
|
|
273
|
+
export type Result<T> = {
|
|
274
|
+
ok: true;
|
|
275
|
+
value: T;
|
|
276
|
+
} | {
|
|
277
|
+
ok: false;
|
|
278
|
+
error: AppErrorShape;
|
|
279
|
+
};
|
|
280
|
+
/** The currency of a `money` column that names none. */
|
|
281
|
+
export declare const DEFAULT_CURRENCY = "USD";
|
|
282
|
+
/**
|
|
283
|
+
* How many digits a currency's amounts have after the decimal point: its
|
|
284
|
+
* ISO 4217 minor unit (2 for USD, 0 for JPY, 3 for KWD). A `money` column
|
|
285
|
+
* stores an integer count of 10^-digits units of its currency, so this is
|
|
286
|
+
* what turns a stored amount into the amount people read, and back.
|
|
287
|
+
*
|
|
288
|
+
* A table, not `Intl`: engines disagree (V8 reports CLDR's 0 digits for RSD
|
|
289
|
+
* and IQD, JavaScriptCore ISO's 2 and 3), and a stored amount must read the
|
|
290
|
+
* same in every browser.
|
|
291
|
+
*/
|
|
292
|
+
export declare function currencyDigits(currency: string): number;
|
|
293
|
+
/** HTTP status for each error code, shared by every HTTP surface. */
|
|
294
|
+
export declare const ERROR_STATUS: Record<ErrorCode, number>;
|
|
295
|
+
/** What a value becomes on its way to a caller: JSON turns Dates into strings. */
|
|
296
|
+
export type Jsonify<T> = T extends Date ? string : T extends readonly (infer U)[] ? Jsonify<U>[] : T extends object ? {
|
|
297
|
+
[K in keyof T]: Jsonify<T[K]>;
|
|
298
|
+
} : T;
|
|
299
|
+
/** The body of every error response. */
|
|
300
|
+
export interface ErrorBody {
|
|
301
|
+
error: AppErrorShape;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* `GET {APPS_PATH}/:appId/manifest`: the live version's manifest as far as the
|
|
305
|
+
* caller may see it, which version that is, and who the caller is to the app.
|
|
306
|
+
*/
|
|
307
|
+
export interface ManifestResponse extends AppManifest {
|
|
308
|
+
appId: string;
|
|
309
|
+
version: number;
|
|
310
|
+
caller: Caller;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Sent on `GET {APPS_PATH}/:appId/realtime`: `changes` after every call that
|
|
314
|
+
* changed data, and `deployed` after a new version went live, which can change
|
|
315
|
+
* both the data (its migrations are not in the changefeed) and the manifest,
|
|
316
|
+
* so pages read everything again.
|
|
317
|
+
*/
|
|
318
|
+
export type RealtimeMessage = {
|
|
319
|
+
type: "changes";
|
|
320
|
+
cursor: number;
|
|
321
|
+
tables: string[];
|
|
322
|
+
} | {
|
|
323
|
+
type: "deployed";
|
|
324
|
+
version: number;
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* Where every app's API lives on the platform's hosts: `${APPS_PATH}/:appId/...`
|
|
328
|
+
* (methods, manifest, OpenAPI, MCP, realtime, files, versions and rollbacks).
|
|
329
|
+
* It sits under the old platform's `/api`, as its next version (ADR 0006 §4).
|
|
330
|
+
*/
|
|
331
|
+
export declare const APPS_PATH = "/api/v4/apps";
|
|
332
|
+
/** The header that names an uploaded file (URI-encoded); the body is its bytes. */
|
|
333
|
+
export declare const FILE_NAME_HEADER = "X-File-Name";
|
|
334
|
+
/** A file's key (`<uuid>/<name>`) as it goes in a URL path: the name encoded. */
|
|
335
|
+
export declare function fileKeyPath(key: string): string;
|
|
336
|
+
/**
|
|
337
|
+
* What a deploy (`POST {APPS_PATH}/:appId/versions`, the server bundle as its
|
|
338
|
+
* body) may say about the bundle: the git commit it was built from, and that
|
|
339
|
+
* commit's subject, URI-encoded. The version keeps both (`VersionInfo`).
|
|
340
|
+
*/
|
|
341
|
+
export declare const DEPLOY_HEADERS: {
|
|
342
|
+
readonly commit: "X-Teable-Commit";
|
|
343
|
+
readonly message: "X-Teable-Message";
|
|
344
|
+
};
|
|
345
|
+
/** A deploy's answer (201): the version the bundle went live as, and its manifest. */
|
|
346
|
+
export interface DeployResponse {
|
|
347
|
+
version: number;
|
|
348
|
+
manifest: AppManifest;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* `POST {APPS_PATH}/:appId/rollback` with `{ "to": <version> }` deploys that
|
|
352
|
+
* version's bundle again, as a new version. Its answer (201) is a deploy's,
|
|
353
|
+
* and the version that was live before.
|
|
354
|
+
*/
|
|
355
|
+
export interface RollbackResponse extends DeployResponse {
|
|
356
|
+
from: number;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* A version of an app, as `GET {APPS_PATH}/:appId/versions` lists them, newest
|
|
360
|
+
* first. What was not recorded when it was deployed is null.
|
|
361
|
+
*/
|
|
362
|
+
export interface VersionInfo {
|
|
363
|
+
version: number;
|
|
364
|
+
/** When it was deployed, in epoch milliseconds. */
|
|
365
|
+
createdAt: number;
|
|
366
|
+
/** The user who deployed it, or "platform" when the old platform did. */
|
|
367
|
+
deployedBy: string | null;
|
|
368
|
+
/** The git commit its bundle was built from. */
|
|
369
|
+
commit: string | null;
|
|
370
|
+
/** That commit's subject; for a rollback, the version it brought back. */
|
|
371
|
+
message: string | null;
|
|
372
|
+
/** The app protocol it was built for: a runtime starts only versions built for its own. */
|
|
373
|
+
protocol: number;
|
|
374
|
+
/** Whether it is the version that serves the app now. */
|
|
375
|
+
live: boolean;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* The one access rule: may this caller invoke a method with this access level?
|
|
379
|
+
* Returns the error code to report, or null when the call is allowed.
|
|
380
|
+
*/
|
|
381
|
+
export declare function checkAccess(caller: Caller, access: Access): "UNAUTHENTICATED" | "FORBIDDEN" | null;
|
|
382
|
+
/**
|
|
383
|
+
* Whether a call may reach a method at all: the method must be a read or write
|
|
384
|
+
* the app defines (anything else is UNKNOWN_METHOD), and the caller must pass
|
|
385
|
+
* `checkAccess`. The host decides this from the stored manifest before it
|
|
386
|
+
* starts the app or counts the call; the app decides it again itself.
|
|
387
|
+
*/
|
|
388
|
+
export declare function admit(manifest: AppManifest, name: string, caller: Caller): Result<MethodInfo>;
|
|
389
|
+
/**
|
|
390
|
+
* Whether the caller belongs to the app's space, with any role (the app's own
|
|
391
|
+
* tasks count too). Members see the whole app: its tables, every exposed
|
|
392
|
+
* method, and the app's own error messages.
|
|
393
|
+
*/
|
|
394
|
+
export declare function isMember(caller: Caller): boolean;
|
|
395
|
+
/**
|
|
396
|
+
* Whether a method exists outside the app (API, MCP, pages, the builder's
|
|
397
|
+
* console): reads and writes that are not internal. Tasks never are.
|
|
398
|
+
*/
|
|
399
|
+
export declare function isExposed(method: MethodInfo): boolean;
|
|
400
|
+
/** The exposed methods this caller may call. */
|
|
401
|
+
export declare function callableMethods(manifest: AppManifest, caller: Caller): MethodInfo[];
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
APPS_PATH,
|
|
3
|
+
APP_COMPATIBILITY_DATE,
|
|
4
|
+
CAPABILITIES,
|
|
5
|
+
DEFAULT_CURRENCY,
|
|
6
|
+
DEPLOY_HEADERS,
|
|
7
|
+
ERROR_CODES,
|
|
8
|
+
ERROR_STATUS,
|
|
9
|
+
FILE_NAME_HEADER,
|
|
10
|
+
PROTOCOL_VERSION,
|
|
11
|
+
SPACE_ROLES,
|
|
12
|
+
admit,
|
|
13
|
+
callableMethods,
|
|
14
|
+
checkAccess,
|
|
15
|
+
currencyDigits,
|
|
16
|
+
fileKeyPath,
|
|
17
|
+
isExposed,
|
|
18
|
+
isMember
|
|
19
|
+
} from "./chunks/JQWQD45Z.js";
|
|
20
|
+
export {
|
|
21
|
+
APPS_PATH,
|
|
22
|
+
APP_COMPATIBILITY_DATE,
|
|
23
|
+
CAPABILITIES,
|
|
24
|
+
DEFAULT_CURRENCY,
|
|
25
|
+
DEPLOY_HEADERS,
|
|
26
|
+
ERROR_CODES,
|
|
27
|
+
ERROR_STATUS,
|
|
28
|
+
FILE_NAME_HEADER,
|
|
29
|
+
PROTOCOL_VERSION,
|
|
30
|
+
SPACE_ROLES,
|
|
31
|
+
admit,
|
|
32
|
+
callableMethods,
|
|
33
|
+
checkAccess,
|
|
34
|
+
currencyDigits,
|
|
35
|
+
fileKeyPath,
|
|
36
|
+
isExposed,
|
|
37
|
+
isMember
|
|
38
|
+
};
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type GetColumnData } from "drizzle-orm";
|
|
2
|
+
import { type SQLiteColumn, SQLiteTable } from "drizzle-orm/sqlite-core";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { type Attachment, type TableInfo } from "./protocol.js";
|
|
5
|
+
/**
|
|
6
|
+
* Semantic column helpers. Use plain Drizzle columns for everything else.
|
|
7
|
+
* Like Drizzle's own builders, the SQL column name is the property key.
|
|
8
|
+
*/
|
|
9
|
+
export declare const col: {
|
|
10
|
+
/** Money as an INTEGER in its currency's minor unit (cents for USD, yen for JPY). */
|
|
11
|
+
money: (options?: {
|
|
12
|
+
currency?: string;
|
|
13
|
+
}) => import("drizzle-orm/sqlite-core").SQLiteIntegerBuilderInitial<"">;
|
|
14
|
+
/** A Teable user id, stored as TEXT. */
|
|
15
|
+
user: () => import("drizzle-orm/sqlite-core").SQLiteTextBuilderInitial<"", [string, ...string[]], undefined>;
|
|
16
|
+
/** A list of uploaded files, stored as JSON TEXT. Validate inputs with `z.array(attachment)`. */
|
|
17
|
+
attachments: () => import("drizzle-orm").$Type<import("drizzle-orm/sqlite-core").SQLiteTextJsonBuilderInitial<"">, Attachment[]>;
|
|
18
|
+
};
|
|
19
|
+
/** One file in a `col.attachments()` column, for method inputs that take files. */
|
|
20
|
+
export declare const attachment: z.ZodObject<{
|
|
21
|
+
key: z.ZodString;
|
|
22
|
+
name: z.ZodString;
|
|
23
|
+
size: z.ZodNumber;
|
|
24
|
+
type: z.ZodString;
|
|
25
|
+
}, z.core.$strip>;
|
|
26
|
+
/** What a column holds, as Drizzle reads it: its data, and null unless it is NOT NULL. */
|
|
27
|
+
type ValueOf<C extends SQLiteColumn> = GetColumnData<C>;
|
|
28
|
+
/** A table's rows as zod sees them: one schema per column. */
|
|
29
|
+
type RowShape<T extends SQLiteTable> = {
|
|
30
|
+
[K in keyof T["$inferSelect"]]: z.ZodType<T["$inferSelect"][K], T["$inferSelect"][K]>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The zod schema of a column's values, for method outputs: what Drizzle reads
|
|
34
|
+
* from the column (null too, unless it is NOT NULL). It carries the column's
|
|
35
|
+
* kind into the JSON Schema callers see (`KindKeywords`), so they tell money
|
|
36
|
+
* from a plain number, and know its currency. Given a table, its rows.
|
|
37
|
+
*
|
|
38
|
+
* output: z.array(schemaOf(skus)),
|
|
39
|
+
* output: z.object({ orders: z.number().int(), revenue: schemaOf(orders.amount) }),
|
|
40
|
+
*/
|
|
41
|
+
export declare function schemaOf<T extends SQLiteTable>(table: T): z.ZodObject<RowShape<T>>;
|
|
42
|
+
export declare function schemaOf<C extends SQLiteColumn>(column: C): z.ZodType<ValueOf<C>, ValueOf<C>>;
|
|
43
|
+
/** Describe every table exported by a schema module. */
|
|
44
|
+
export declare function describeTables(schema: Record<string, unknown>): TableInfo[];
|
|
45
|
+
export {};
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { DurableObject } from "cloudflare:workers";
|
|
2
|
+
import { type DrizzleSqliteDODatabase } from "drizzle-orm/durable-sqlite";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { type Access, type AppFacet, type AppManifest, type Caller, type Capabilities, type Capability } from "../protocol.js";
|
|
5
|
+
import { type Migrations } from "./migrations.js";
|
|
6
|
+
type Schema = Record<string, unknown>;
|
|
7
|
+
/** The Drizzle database handed to read and write handlers. */
|
|
8
|
+
export type Database<S extends Schema> = DrizzleSqliteDODatabase<S>;
|
|
9
|
+
export type InputSchema = z.ZodType<unknown, Record<string, unknown>>;
|
|
10
|
+
type OutputSchema = z.ZodType;
|
|
11
|
+
interface Common<In extends InputSchema> {
|
|
12
|
+
input?: In;
|
|
13
|
+
description?: string;
|
|
14
|
+
}
|
|
15
|
+
/** A read's or write's options beyond its input: who may call it, what it returns, its handler. */
|
|
16
|
+
interface MethodOptions<Ctx, R, A extends Access, Out extends OutputSchema | undefined> {
|
|
17
|
+
access?: A;
|
|
18
|
+
/**
|
|
19
|
+
* What the method returns. Every value the handler returns is checked
|
|
20
|
+
* against it, and callers get what it outputs (keys it does not declare
|
|
21
|
+
* are dropped); the API's and MCP's response schemas come from it.
|
|
22
|
+
*/
|
|
23
|
+
output?: Out;
|
|
24
|
+
handler: (ctx: Ctx) => Returned<Out, R>;
|
|
25
|
+
}
|
|
26
|
+
/** What a handler returns: what the output schema accepts, or anything without one. */
|
|
27
|
+
type Returned<Out, R> = Out extends OutputSchema ? z.input<Out> : R;
|
|
28
|
+
/** What callers get: what the output schema outputs, or the handler's value without one. */
|
|
29
|
+
type Given<Out, R> = Out extends OutputSchema ? z.output<Out> : R;
|
|
30
|
+
export interface ReadContext<S extends Schema, I> {
|
|
31
|
+
db: Database<S>;
|
|
32
|
+
input: I;
|
|
33
|
+
caller: Caller;
|
|
34
|
+
}
|
|
35
|
+
export interface WriteContext<S extends Schema, I> extends ReadContext<S, I> {
|
|
36
|
+
/** Run a task after this write commits. The task row commits with the data. */
|
|
37
|
+
enqueue<T extends TaskMethod<InputSchema>>(task: T, input: z.input<T["input"]>, options?: {
|
|
38
|
+
delayMs?: number;
|
|
39
|
+
}): void;
|
|
40
|
+
}
|
|
41
|
+
/** What a task gets: its input, and every platform capability (declared ones work). */
|
|
42
|
+
export interface TaskContext<I> extends Capabilities {
|
|
43
|
+
input: I;
|
|
44
|
+
/** 1 on the first run; tasks are retried with backoff until they succeed. */
|
|
45
|
+
attempt: number;
|
|
46
|
+
/** Call a read or write method of this app. Tasks touch data only through methods. */
|
|
47
|
+
call<M extends ReadMethod<InputSchema, unknown> | WriteMethod<InputSchema, unknown>>(method: M, input: z.input<M["input"]>): Promise<MethodResult<M>>;
|
|
48
|
+
}
|
|
49
|
+
type MethodResult<M> = M extends {
|
|
50
|
+
handler(ctx: never): infer R;
|
|
51
|
+
} ? R : never;
|
|
52
|
+
export interface ReadMethod<In extends InputSchema, R, A extends Access = Access> {
|
|
53
|
+
kind: "read";
|
|
54
|
+
input: In;
|
|
55
|
+
output: OutputSchema | undefined;
|
|
56
|
+
access: A;
|
|
57
|
+
description: string | undefined;
|
|
58
|
+
handler(ctx: ReadContext<Schema, z.output<In>>): R;
|
|
59
|
+
}
|
|
60
|
+
export interface WriteMethod<In extends InputSchema, R, A extends Access = Access> {
|
|
61
|
+
kind: "write";
|
|
62
|
+
input: In;
|
|
63
|
+
output: OutputSchema | undefined;
|
|
64
|
+
access: A;
|
|
65
|
+
description: string | undefined;
|
|
66
|
+
handler(ctx: WriteContext<Schema, z.output<In>>): R;
|
|
67
|
+
}
|
|
68
|
+
export interface TaskMethod<In extends InputSchema> {
|
|
69
|
+
kind: "task";
|
|
70
|
+
input: In;
|
|
71
|
+
access: "internal";
|
|
72
|
+
description: string | undefined;
|
|
73
|
+
maxAttempts: number;
|
|
74
|
+
/** Run on a cron in a time zone, in addition to being enqueued by writes. */
|
|
75
|
+
schedule: {
|
|
76
|
+
cron: string;
|
|
77
|
+
timezone: string;
|
|
78
|
+
} | undefined;
|
|
79
|
+
handler(ctx: TaskContext<z.output<In>>): Promise<void> | void;
|
|
80
|
+
}
|
|
81
|
+
export type AnyMethod = ReadMethod<InputSchema, unknown> | WriteMethod<InputSchema, unknown> | TaskMethod<InputSchema>;
|
|
82
|
+
/** Rejects handlers that return a Promise, with a message that says why. */
|
|
83
|
+
type MustBeSync<R> = [R] extends [never] ? unknown : [R] extends [PromiseLike<unknown>] ? {
|
|
84
|
+
handler: "read and write handlers must be synchronous; move async work into a task";
|
|
85
|
+
} : unknown;
|
|
86
|
+
declare const emptyInput: z.ZodObject<{}, z.core.$strip>;
|
|
87
|
+
/**
|
|
88
|
+
* Define an app: its Drizzle schema, its migrations, and its methods.
|
|
89
|
+
*
|
|
90
|
+
* const app = createApp({ schema, migrations });
|
|
91
|
+
* const placeOrder = app.write({ input, handler });
|
|
92
|
+
* export const App = app.define({ placeOrder });
|
|
93
|
+
*
|
|
94
|
+
* `define` returns the Durable Object class the platform runs. Every way into
|
|
95
|
+
* the app's data — pages, the table component, tasks, the generated API and
|
|
96
|
+
* MCP tools — goes through these methods.
|
|
97
|
+
*/
|
|
98
|
+
export declare function createApp<S extends Schema>(options: {
|
|
99
|
+
schema: S;
|
|
100
|
+
migrations: Migrations;
|
|
101
|
+
/** Platform capabilities tasks may use; the host wires only these. */
|
|
102
|
+
capabilities?: readonly Capability[];
|
|
103
|
+
}): {
|
|
104
|
+
/** Read data. Synchronous and read-only: a read that writes is rolled back and fails. */
|
|
105
|
+
read<R, In extends InputSchema = typeof emptyInput, A extends Access = "viewer", Out extends OutputSchema | undefined = undefined>(def: Common<In> & MethodOptions<ReadContext<S, z.output<In>>, R, A, Out> & MustBeSync<R>): ReadMethod<In, Given<Out, R>, A>;
|
|
106
|
+
/** Change data. Synchronous and atomic: if it throws, nothing it wrote is kept. */
|
|
107
|
+
write<R, In extends InputSchema = typeof emptyInput, A extends Access = "editor", Out extends OutputSchema | undefined = undefined>(def: Common<In> & MethodOptions<WriteContext<S, z.output<In>>, R, A, Out> & MustBeSync<R>): WriteMethod<In, Given<Out, R>, A>;
|
|
108
|
+
/** Async work outside transactions: external services, multi-step flows. Retried on failure. */
|
|
109
|
+
task<In extends InputSchema = typeof emptyInput>(def: Common<In> & {
|
|
110
|
+
maxAttempts?: number;
|
|
111
|
+
/** e.g. `{ cron: "0 9 * * *", timezone: "Asia/Shanghai" }`; the time zone defaults to UTC. */
|
|
112
|
+
schedule?: {
|
|
113
|
+
cron: string;
|
|
114
|
+
timezone?: string;
|
|
115
|
+
};
|
|
116
|
+
handler: (ctx: TaskContext<z.output<In>>) => Promise<void> | void;
|
|
117
|
+
}): TaskMethod<In>;
|
|
118
|
+
/**
|
|
119
|
+
* Register the methods under their names and return the app's Durable
|
|
120
|
+
* Object class. It keeps the method map's type, which pages read.
|
|
121
|
+
*/
|
|
122
|
+
define<M extends Record<string, AnyMethod>>(methods: M): AppClass<M>;
|
|
123
|
+
};
|
|
124
|
+
/** The name a method was registered under in `define()`. */
|
|
125
|
+
export declare function methodName(method: AnyMethod): string;
|
|
126
|
+
/**
|
|
127
|
+
* The class `define()` returns: the Durable Object the platform runs, with
|
|
128
|
+
* the app's manifest and its methods. Named, so that declarations can say
|
|
129
|
+
* what `define()` returns; the class itself has private members, which a
|
|
130
|
+
* declaration cannot spell out.
|
|
131
|
+
*/
|
|
132
|
+
interface AppClass<M> {
|
|
133
|
+
new (ctx: DurableObjectState, env: Record<string, unknown>): DurableObject<Record<string, unknown>> & AppFacet;
|
|
134
|
+
readonly manifest: AppManifest;
|
|
135
|
+
/** The methods by the names `define()` gave them; pages take their types from here. */
|
|
136
|
+
readonly methods: M;
|
|
137
|
+
}
|
|
138
|
+
export {};
|