@veriforms/webapp-sdk 0.0.0-stage → 0.1.1

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,419 @@
1
+ /**
2
+ * @veriforms/webapp-sdk — typed access to the VeriForms web app runtime.
3
+ *
4
+ * The gateway injects `/vf-ui.js` into every page it serves, and that script
5
+ * defines `window.VF`. This package does NOT re-implement any of it: every
6
+ * function here calls the injected runtime, so a web app built in code behaves
7
+ * exactly like one built with Vera (one runtime, one behaviour). What it adds:
8
+ *
9
+ * - types generated from the app's own config.json (`vf app types` writes a
10
+ * `vf-app.d.ts` that fills in {@link Register}), so a misspelt source key, a
11
+ * field the source does not declare or a page the app does not have is a
12
+ * compile error instead of a refusal at runtime;
13
+ * - a loud failure when the runtime is missing or lacks a function
14
+ * ({@link SdkRuntimeMismatchError}), never a silent no-op.
15
+ *
16
+ * Concept: docs/webapp-builder/CWA-CODE-APPS-CONCEPT.md (§4.2).
17
+ */
18
+ export declare const SDK_VERSION = "0.1.1";
19
+ /**
20
+ * Augmented by the `vf-app.d.ts` that `vf app types` generates:
21
+ *
22
+ * ```ts
23
+ * declare module '@veriforms/webapp-sdk' {
24
+ * interface Register { app: { sources: {…}; pages: 'main' | 'admin'; roles: 'treasurer' } }
25
+ * }
26
+ * ```
27
+ *
28
+ * Without it every key is a plain string and every row is untyped.
29
+ */
30
+ export interface Register {
31
+ }
32
+ /** A value a visitor may pass as a declared client param. */
33
+ export type ParamValue = string | number | boolean;
34
+ /** A sensitive column (an IBAN, a tax number) as every read returns it. */
35
+ export interface MaskedValue {
36
+ masked: true;
37
+ display: string;
38
+ last4: string;
39
+ }
40
+ /** One data source as the generated types describe it. */
41
+ export type SourceShape = {
42
+ type: 'customdb';
43
+ row: object;
44
+ params: object;
45
+ } | {
46
+ type: 'customdb-write';
47
+ mode: 'insert' | 'upsert' | 'update';
48
+ fields: object;
49
+ } | {
50
+ type: 'veriforms-submission';
51
+ fields: object;
52
+ } | {
53
+ type: 'veriforms-data';
54
+ fields: object;
55
+ params: object;
56
+ } | {
57
+ type: 'veriflow' | 'http' | 'demo';
58
+ params: object;
59
+ result: unknown;
60
+ } | {
61
+ type: 'webhook' | 'workflow';
62
+ body: object;
63
+ } | {
64
+ type: 'veriforms-payment';
65
+ } | {
66
+ type: 'veriforms-roles';
67
+ };
68
+ export interface AppShape {
69
+ sources: Record<string, SourceShape>;
70
+ pages: string;
71
+ roles: string;
72
+ }
73
+ /** What an app without generated types looks like: anything goes, nothing is checked. */
74
+ interface UntypedSource {
75
+ type: any;
76
+ row: Record<string, unknown>;
77
+ params: Record<string, ParamValue>;
78
+ fields: Record<string, unknown>;
79
+ body: Record<string, unknown>;
80
+ result: unknown;
81
+ mode: any;
82
+ }
83
+ interface UntypedApp {
84
+ sources: Record<string, UntypedSource>;
85
+ pages: string;
86
+ roles: string;
87
+ }
88
+ /** The app this code is typed against: the generated one, or {@link UntypedApp}. */
89
+ export type App = Register extends {
90
+ app: infer A;
91
+ } ? A : UntypedApp;
92
+ type Sources = App extends {
93
+ sources: infer S;
94
+ } ? S : never;
95
+ type SourceOf<K> = K extends keyof Sources ? Sources[K] : never;
96
+ /** The keys of the sources of one kind. */
97
+ export type SourceKey<T extends string = string> = {
98
+ [K in keyof Sources]: Sources[K] extends {
99
+ type: infer U;
100
+ } ? ([U] extends [T] ? K : never) : never;
101
+ }[keyof Sources] & string;
102
+ /** A source whose rows a page lists with {@link db.list}. */
103
+ export type TableKey = SourceKey<'customdb'>;
104
+ /** A source a page writes table rows to. */
105
+ export type TableWriteKey = SourceKey<'customdb-write'>;
106
+ /** This web app's own submissions as a sink. */
107
+ export type SubmissionKey = SourceKey<'veriforms-submission'>;
108
+ /** This web app's own submissions read back. */
109
+ export type SubmissionsReadKey = SourceKey<'veriforms-data'>;
110
+ /** A source read with {@link fetchSource}: an Automation, an http upstream or demo data. */
111
+ export type ReadKey = SourceKey<'veriflow' | 'http' | 'demo'>;
112
+ /** A source a page posts to with {@link send}. */
113
+ export type SendKey = SourceKey<'veriforms-submission' | 'webhook' | 'workflow' | 'veriflow' | 'customdb-write'>;
114
+ /** A source {@link runAction} starts: a workflow or an Automation. */
115
+ export type ActionKey = SourceKey<'workflow' | 'veriflow' | 'webhook'>;
116
+ /** A payment source. */
117
+ export type PaymentKey = SourceKey<'veriforms-payment'>;
118
+ /** A source {@link form.render} can draw a form for. */
119
+ export type FormKey = SourceKey<'veriforms-submission' | 'customdb-write'>;
120
+ /** A page of this web app ('main' or '' is the main page). */
121
+ export type PageName = (App extends {
122
+ pages: infer P;
123
+ } ? P : string) | 'main' | '';
124
+ /** A role the app's config declares. */
125
+ export type RoleName = App extends {
126
+ roles: infer R;
127
+ } ? R : string;
128
+ export type RowOf<K> = SourceOf<K> extends {
129
+ row: infer R;
130
+ } ? R : Record<string, unknown>;
131
+ export type FieldsOf<K> = SourceOf<K> extends {
132
+ fields: infer F;
133
+ } ? F : Record<string, unknown>;
134
+ export type ParamsOf<K> = SourceOf<K> extends {
135
+ params: infer P;
136
+ } ? P : Record<string, ParamValue>;
137
+ export type BodyOf<K> = SourceOf<K> extends {
138
+ body: infer B;
139
+ } ? B : SourceOf<K> extends {
140
+ fields: infer F;
141
+ } ? F : Record<string, unknown>;
142
+ export type ResultOf<K> = SourceOf<K> extends {
143
+ result: infer R;
144
+ } ? R : unknown;
145
+ /**
146
+ * What {@link send} resolves with: for an Automation (veriflow) source, its declared result — the
147
+ * Webhook Response body, typed by `vf app types` and held to by the server (concept §7.12); for every
148
+ * other source, whatever the runtime answers.
149
+ */
150
+ export type SendResultOf<K> = SourceOf<K> extends {
151
+ type: 'veriflow';
152
+ result: infer R;
153
+ } ? R : unknown;
154
+ /**
155
+ * Intersected with a params type so an object literal is checked for keys the
156
+ * source does not allow even when it allows none (`{}` alone accepts anything).
157
+ */
158
+ export interface NoUndeclaredParams {
159
+ readonly __vfNoUndeclaredParams?: never;
160
+ }
161
+ /** A page of table rows (customdb). */
162
+ export interface TablePage<R> {
163
+ rows: R[];
164
+ rowCount: number;
165
+ limit: number;
166
+ offset: number;
167
+ hasMore: boolean;
168
+ nextOffset: number | null;
169
+ /** The test version reads a schema that is not there yet. */
170
+ testSchemaUnavailable?: true;
171
+ }
172
+ /** This web app's own submissions read back (veriforms-data). */
173
+ export interface SubmissionsPage<F> {
174
+ Rows: Array<{
175
+ SubmissionId: string;
176
+ SubmittedAt: string;
177
+ Checkpoint: string | null;
178
+ Data: Partial<F>;
179
+ }>;
180
+ Count: number;
181
+ Truncated: boolean;
182
+ }
183
+ /** What a write answers: the rows written, or that the same clientToken was already answered. */
184
+ export interface WriteResult {
185
+ written?: number;
186
+ deduped?: true;
187
+ [key: string]: unknown;
188
+ }
189
+ /** Repeat-safe write options: the token of an earlier attempt whose outcome was unknown. */
190
+ export interface WriteOptions {
191
+ clientToken?: string;
192
+ }
193
+ /**
194
+ * The error every runtime call rejects with. `outcome === 'unknown'` means the
195
+ * request may have gone through: say "it may already have been received" and
196
+ * resend with the same `clientToken` — never "failed, try again".
197
+ */
198
+ export interface VfRequestError extends Error {
199
+ status?: number;
200
+ data?: unknown;
201
+ code?: string;
202
+ reason?: string;
203
+ outcome?: 'unknown' | 'pending' | 'failed';
204
+ clientToken?: string;
205
+ /** role_required: the roles that would do. */
206
+ anyOf?: string[];
207
+ /** The Automation answered with its own error status; settled, never asked again. */
208
+ flowAnswer?: true;
209
+ }
210
+ export declare function isVfRequestError(e: unknown): e is VfRequestError;
211
+ /** True when a write's outcome is not known and must be resent with the same clientToken. */
212
+ export declare function isOutcomeUnknown(e: unknown): e is VfRequestError & {
213
+ outcome: 'unknown';
214
+ clientToken: string;
215
+ };
216
+ export interface UserInfo {
217
+ signedIn: boolean;
218
+ email: string | null;
219
+ name: string | null;
220
+ roles: RoleName[];
221
+ level: string | null;
222
+ via: string | null;
223
+ signInOffered: boolean;
224
+ preview?: boolean;
225
+ }
226
+ export interface ActionResult {
227
+ ok: number;
228
+ failed: Array<{
229
+ row: Record<string, unknown>;
230
+ error: string;
231
+ status?: number;
232
+ outcome?: string;
233
+ clientToken?: string;
234
+ }>;
235
+ results: unknown[];
236
+ cancelled: boolean;
237
+ preview?: true;
238
+ error?: string;
239
+ }
240
+ export interface ActionOptions {
241
+ rows?: Record<string, unknown> | Array<Record<string, unknown>>;
242
+ params?: Record<string, ParamValue>;
243
+ /** Fields asked in one dialog before it runs (the source's allowClientParams). */
244
+ ask?: unknown[];
245
+ confirm?: string | boolean;
246
+ label?: string;
247
+ /** No toasts: the page shows the outcome itself. */
248
+ silent?: boolean;
249
+ }
250
+ export interface PaymentStatus {
251
+ CwaPaymentId: string;
252
+ Status: 'pending' | 'paid' | 'failed' | 'refunded' | 'expired';
253
+ Amount: number;
254
+ Currency: string;
255
+ ReceiptUrl: string | null;
256
+ CompletedAt: string | null;
257
+ }
258
+ /** The part of `window.VF` this SDK relies on. Untyped on purpose: the SDK's own functions carry the types. */
259
+ export interface VfRuntime {
260
+ fetch(sourceKey: string, opts?: {
261
+ params?: Record<string, ParamValue>;
262
+ body?: unknown;
263
+ method?: string;
264
+ }): Promise<unknown>;
265
+ send(sourceKey: string, body: unknown, opts?: WriteOptions): Promise<unknown>;
266
+ data: {
267
+ list(sourceKey: string, opts?: {
268
+ params?: Record<string, ParamValue>;
269
+ }): Promise<unknown>;
270
+ };
271
+ db: {
272
+ list(sourceKey: string, params?: Record<string, ParamValue>): Promise<unknown>;
273
+ insert(sourceKey: string, row: unknown, opts?: WriteOptions): Promise<unknown>;
274
+ upsert(sourceKey: string, rows: unknown, opts?: WriteOptions): Promise<unknown>;
275
+ update(sourceKey: string, id: unknown, patch: unknown, version?: unknown, opts?: WriteOptions): Promise<unknown>;
276
+ };
277
+ form: {
278
+ render(target: unknown, sourceKey: string, opts?: unknown): unknown;
279
+ submitPartial(sourceKey: string, checkpoint: string, data: unknown): Promise<unknown>;
280
+ };
281
+ pay: {
282
+ start(sourceKey: string, opts?: unknown): Promise<unknown>;
283
+ status(sourceKey: string, id: string): Promise<unknown>;
284
+ pendingId(): string | null;
285
+ receiptLink(sourceKey: string, id: string): string;
286
+ };
287
+ runAction(sourceKey: string, opts?: unknown): Promise<unknown>;
288
+ user(): Promise<unknown>;
289
+ signIn(path?: string): void;
290
+ signOut(path?: string): void;
291
+ appLink(target: string, path?: string): string;
292
+ pageLink(name?: string, suffix?: string): string;
293
+ page: string;
294
+ pages: string[];
295
+ file(name: string): string;
296
+ access: {
297
+ level: string | null;
298
+ roles: string[];
299
+ has(role: string): boolean;
300
+ can(sourceKey: string): boolean;
301
+ signedIn: boolean;
302
+ signInOffered: boolean;
303
+ };
304
+ theme: {
305
+ current(): string;
306
+ };
307
+ t(key: string, vars?: Record<string, unknown>): string;
308
+ newClientToken(): string;
309
+ }
310
+ /**
311
+ * Thrown when the page has no VeriForms runtime, or the runtime lacks a
312
+ * function this SDK calls. A web app built in code runs only on a page the
313
+ * VeriForms gateway serves (or `vf app dev`, which serves the draft through it).
314
+ */
315
+ export declare class SdkRuntimeMismatchError extends Error {
316
+ readonly missing: string;
317
+ readonly code = "sdk_runtime_mismatch";
318
+ constructor(missing: string);
319
+ }
320
+ /** The injected runtime, checked. Throws {@link SdkRuntimeMismatchError} when it is not there. */
321
+ export declare function runtime(): VfRuntime;
322
+ /** Tables (Custom Databases): read through a customdb source, write through a customdb-write source. */
323
+ export declare const db: {
324
+ /** One page of rows. `params` are the source's allowClientParams plus limit/offset. */
325
+ list<K extends TableKey>(source: K, params?: Partial<ParamsOf<K>> & {
326
+ limit?: number;
327
+ offset?: number;
328
+ }): Promise<TablePage<RowOf<K>>>;
329
+ /** Inserts one row or several (insert-mode source). Repeat-safe: see {@link isOutcomeUnknown}. */
330
+ insert<K extends TableWriteKey>(source: K, row: FieldsOf<K> | Array<FieldsOf<K>>, opts?: WriteOptions): Promise<WriteResult>;
331
+ /** Inserts or updates rows matched on the source's keyColumns (upsert-mode source). */
332
+ upsert<K extends TableWriteKey>(source: K, rows: FieldsOf<K> | Array<FieldsOf<K>>, opts?: WriteOptions): Promise<WriteResult>;
333
+ /**
334
+ * Changes one row by its `_id` (update-mode source). Pass the row's
335
+ * `_updated_at` as it was read (or the row itself) as `version`: the server
336
+ * then refuses with 409 row_changed when someone changed the row since.
337
+ */
338
+ update<K extends TableWriteKey>(source: K, id: number | string, patch: Partial<FieldsOf<K>>, version?: string | {
339
+ _updated_at: string;
340
+ } | null, opts?: WriteOptions): Promise<WriteResult>;
341
+ };
342
+ /** This web app's own submissions, read back through a veriforms-data source. */
343
+ export declare const data: {
344
+ list<K extends SubmissionsReadKey>(source: K, params?: Partial<ParamsOf<K>> & NoUndeclaredParams): Promise<SubmissionsPage<FieldsOf<K>>>;
345
+ };
346
+ /** Reads a source: an Automation (veriflow), an http upstream or demo data. */
347
+ export declare function fetchSource<K extends ReadKey>(source: K, params?: Partial<ParamsOf<K>> & NoUndeclaredParams): Promise<ResultOf<K>>;
348
+ /**
349
+ * Posts to a source with one clientToken for the visitor's one action, and asks
350
+ * again with the same token when the answer did not settle anything. A rejection
351
+ * with `outcome === 'unknown'` may have gone through: see {@link isOutcomeUnknown}.
352
+ */
353
+ export declare function send<K extends SendKey>(source: K, body: BodyOf<K>, opts?: WriteOptions): Promise<SendResultOf<K>>;
354
+ /** Starts a workflow or Automation for one row or several (at most 50), with the platform's dialog and toasts. */
355
+ export declare function runAction<K extends ActionKey>(source: K, opts?: ActionOptions): Promise<ActionResult>;
356
+ /** The platform's form for a submission or table-insert source, with its server rules mirrored. */
357
+ export declare const form: {
358
+ render<K extends FormKey>(target: Element | string, source: K, opts?: Record<string, unknown>): HTMLFormElement;
359
+ /** A named checkpoint: the given fields as a partial row (required fields may be missing). */
360
+ submitPartial<K extends SubmissionKey>(source: K, checkpoint: string, values: Partial<FieldsOf<K>>): Promise<unknown>;
361
+ };
362
+ /** Payments (Stripe Checkout, owner's own account). */
363
+ export declare const pay: {
364
+ /** Sends the visitor to checkout. A source with bindRow takes `rowId` and nothing about the amount. */
365
+ start<K extends PaymentKey>(source: K, opts?: {
366
+ amount?: number;
367
+ rowId?: number | string;
368
+ email?: string;
369
+ redirect?: boolean;
370
+ }): Promise<unknown>;
371
+ status<K extends PaymentKey>(source: K, paymentId: string): Promise<PaymentStatus>;
372
+ /** The payment id the checkout handed back in the address, or null. */
373
+ pendingId(): string | null;
374
+ /** The app's own address that forwards to the receipt (never Stripe's address directly). */
375
+ receiptLink<K extends PaymentKey>(source: K, paymentId: string): string;
376
+ };
377
+ /** Who is signed in. */
378
+ export declare function user(): Promise<UserInfo>;
379
+ /** Sends the visitor to the platform's sign-in, and back to `path` (default: here). */
380
+ export declare function signIn(path?: string): void;
381
+ export declare function signOut(path?: string): void;
382
+ /** What the visitor may do, as the server told the page. The server checks every call again. */
383
+ export declare const access: {
384
+ has(role: RoleName): boolean;
385
+ can(source: SourceKey): boolean;
386
+ level(): string | null;
387
+ signedIn(): boolean;
388
+ };
389
+ /**
390
+ * The address of a page of THIS web app in this environment. The way across
391
+ * pages: each page is its own access area, checked by the gateway before it is
392
+ * served, so in-page routing must never stand in for it. Throws for a page the
393
+ * app does not have.
394
+ */
395
+ export declare function pageLink(name: PageName, suffix?: `?${string}` | `#${string}`): string;
396
+ /** The page this code runs on ('' for the main page). */
397
+ export declare function currentPage(): string;
398
+ /** Another web app of the same space, by its id or slug. */
399
+ export declare function appLink(target: string, path?: string): string;
400
+ /** The address of one of the app's own files (served to exactly who may open the app). */
401
+ export declare function file(name: string): string;
402
+ /** A string of the page's bilingual texts. */
403
+ export declare function t(key: string, vars?: Record<string, unknown>): string;
404
+ /** "light" | "dark": the theme the page is shown in. */
405
+ export declare function theme(): 'light' | 'dark';
406
+ /** A fresh clientToken, for a write the page sends itself. */
407
+ export declare function newClientToken(): string;
408
+ /**
409
+ * Calls `listener` when rows of a source changed on this page (a confirmed
410
+ * assistant proposal, an action). Returns the function that stops listening.
411
+ */
412
+ export declare function onDataChanged(listener: (change: {
413
+ sourceKey: string;
414
+ count: number;
415
+ action?: boolean;
416
+ }) => void): () => void;
417
+ export * from './serverClient.js';
418
+ export { kebabOf, serverFunctionRoute, SERVER_FUNCTION_NAME } from './serverRoute.js';
419
+ export type { InputSchema, FromSchema, ServerContext, ServerSource, ServerVisitor, ServerFunction, ServerFunctionAccess, ServerFunctionDefinition, ServerFunctionInput, ServerFunctionOutput, ServerFunctionRefusal, } from './server.js';
package/dist/index.js ADDED
@@ -0,0 +1,203 @@
1
+ /**
2
+ * @veriforms/webapp-sdk — typed access to the VeriForms web app runtime.
3
+ *
4
+ * The gateway injects `/vf-ui.js` into every page it serves, and that script
5
+ * defines `window.VF`. This package does NOT re-implement any of it: every
6
+ * function here calls the injected runtime, so a web app built in code behaves
7
+ * exactly like one built with Vera (one runtime, one behaviour). What it adds:
8
+ *
9
+ * - types generated from the app's own config.json (`vf app types` writes a
10
+ * `vf-app.d.ts` that fills in {@link Register}), so a misspelt source key, a
11
+ * field the source does not declare or a page the app does not have is a
12
+ * compile error instead of a refusal at runtime;
13
+ * - a loud failure when the runtime is missing or lacks a function
14
+ * ({@link SdkRuntimeMismatchError}), never a silent no-op.
15
+ *
16
+ * Concept: docs/webapp-builder/CWA-CODE-APPS-CONCEPT.md (§4.2).
17
+ */
18
+ export const SDK_VERSION = '0.1.1';
19
+ export function isVfRequestError(e) {
20
+ return e instanceof Error;
21
+ }
22
+ /** True when a write's outcome is not known and must be resent with the same clientToken. */
23
+ export function isOutcomeUnknown(e) {
24
+ return e instanceof Error && e.outcome === 'unknown' && typeof e.clientToken === 'string';
25
+ }
26
+ /**
27
+ * Thrown when the page has no VeriForms runtime, or the runtime lacks a
28
+ * function this SDK calls. A web app built in code runs only on a page the
29
+ * VeriForms gateway serves (or `vf app dev`, which serves the draft through it).
30
+ */
31
+ export class SdkRuntimeMismatchError extends Error {
32
+ constructor(missing) {
33
+ super(missing === 'window.VF'
34
+ ? `@veriforms/webapp-sdk ${SDK_VERSION}: this page has no VeriForms runtime (window.VF). `
35
+ + 'A web app built in code runs only on a page the VeriForms gateway serves, or under `vf app dev`.'
36
+ : `@veriforms/webapp-sdk ${SDK_VERSION}: the VeriForms runtime on this page has no VF.${missing}. `
37
+ + 'The runtime is older or newer than this SDK expects; update @veriforms/webapp-sdk or reload the page.');
38
+ this.missing = missing;
39
+ this.code = 'sdk_runtime_mismatch';
40
+ this.name = 'SdkRuntimeMismatchError';
41
+ }
42
+ }
43
+ /** The injected runtime, checked. Throws {@link SdkRuntimeMismatchError} when it is not there. */
44
+ export function runtime() {
45
+ const vf = globalThis.window?.VF ?? globalThis.VF;
46
+ if (!vf || typeof vf !== 'object')
47
+ throw new SdkRuntimeMismatchError('window.VF');
48
+ return vf;
49
+ }
50
+ /** The function at a dotted path of the runtime, or a loud failure naming it. */
51
+ function fn(path) {
52
+ let at = runtime();
53
+ let owner = undefined;
54
+ for (const part of path.split('.')) {
55
+ owner = at;
56
+ at = at && typeof at === 'object' ? at[part] : undefined;
57
+ }
58
+ if (typeof at !== 'function')
59
+ throw new SdkRuntimeMismatchError(path);
60
+ return at.bind(owner);
61
+ }
62
+ /** A property of the runtime that must be there (not a function). */
63
+ function prop(path) {
64
+ let at = runtime();
65
+ for (const part of path.split('.'))
66
+ at = at && typeof at === 'object' ? at[part] : undefined;
67
+ if (at === undefined)
68
+ throw new SdkRuntimeMismatchError(path);
69
+ return at;
70
+ }
71
+ // ─────────────────────────────────────────────────────────────────────────────
72
+ // Data sources
73
+ // ─────────────────────────────────────────────────────────────────────────────
74
+ /** Tables (Custom Databases): read through a customdb source, write through a customdb-write source. */
75
+ export const db = {
76
+ /** One page of rows. `params` are the source's allowClientParams plus limit/offset. */
77
+ list(source, params) {
78
+ return fn('db.list')(source, params);
79
+ },
80
+ /** Inserts one row or several (insert-mode source). Repeat-safe: see {@link isOutcomeUnknown}. */
81
+ insert(source, row, opts) {
82
+ return fn('db.insert')(source, row, opts);
83
+ },
84
+ /** Inserts or updates rows matched on the source's keyColumns (upsert-mode source). */
85
+ upsert(source, rows, opts) {
86
+ return fn('db.upsert')(source, rows, opts);
87
+ },
88
+ /**
89
+ * Changes one row by its `_id` (update-mode source). Pass the row's
90
+ * `_updated_at` as it was read (or the row itself) as `version`: the server
91
+ * then refuses with 409 row_changed when someone changed the row since.
92
+ */
93
+ update(source, id, patch, version, opts) {
94
+ return fn('db.update')(source, id, patch, version ?? null, opts);
95
+ },
96
+ };
97
+ /** This web app's own submissions, read back through a veriforms-data source. */
98
+ export const data = {
99
+ list(source, params) {
100
+ return fn('data.list')(source, params ? { params: params } : undefined);
101
+ },
102
+ };
103
+ /** Reads a source: an Automation (veriflow), an http upstream or demo data. */
104
+ export function fetchSource(source, params) {
105
+ return fn('fetch')(source, params ? { params: params } : {});
106
+ }
107
+ /**
108
+ * Posts to a source with one clientToken for the visitor's one action, and asks
109
+ * again with the same token when the answer did not settle anything. A rejection
110
+ * with `outcome === 'unknown'` may have gone through: see {@link isOutcomeUnknown}.
111
+ */
112
+ export function send(source, body, opts) {
113
+ return fn('send')(source, body, opts);
114
+ }
115
+ /** Starts a workflow or Automation for one row or several (at most 50), with the platform's dialog and toasts. */
116
+ export function runAction(source, opts) {
117
+ return fn('runAction')(source, opts);
118
+ }
119
+ /** The platform's form for a submission or table-insert source, with its server rules mirrored. */
120
+ export const form = {
121
+ render(target, source, opts) {
122
+ return fn('form.render')(target, source, opts);
123
+ },
124
+ /** A named checkpoint: the given fields as a partial row (required fields may be missing). */
125
+ submitPartial(source, checkpoint, values) {
126
+ return fn('form.submitPartial')(source, checkpoint, values);
127
+ },
128
+ };
129
+ /** Payments (Stripe Checkout, owner's own account). */
130
+ export const pay = {
131
+ /** Sends the visitor to checkout. A source with bindRow takes `rowId` and nothing about the amount. */
132
+ start(source, opts) {
133
+ return fn('pay.start')(source, opts);
134
+ },
135
+ status(source, paymentId) {
136
+ return fn('pay.status')(source, paymentId);
137
+ },
138
+ /** The payment id the checkout handed back in the address, or null. */
139
+ pendingId() {
140
+ return fn('pay.pendingId')();
141
+ },
142
+ /** The app's own address that forwards to the receipt (never Stripe's address directly). */
143
+ receiptLink(source, paymentId) {
144
+ return fn('pay.receiptLink')(source, paymentId);
145
+ },
146
+ };
147
+ // ─────────────────────────────────────────────────────────────────────────────
148
+ // Visitor, access, pages
149
+ // ─────────────────────────────────────────────────────────────────────────────
150
+ /** Who is signed in. */
151
+ export function user() {
152
+ return fn('user')();
153
+ }
154
+ /** Sends the visitor to the platform's sign-in, and back to `path` (default: here). */
155
+ export function signIn(path) { fn('signIn')(path); }
156
+ export function signOut(path) { fn('signOut')(path); }
157
+ /** What the visitor may do, as the server told the page. The server checks every call again. */
158
+ export const access = {
159
+ has(role) { return fn('access.has')(role); },
160
+ can(source) { return fn('access.can')(source); },
161
+ level() { return prop('access').level ?? null; },
162
+ signedIn() { return prop('access').signedIn === true; },
163
+ };
164
+ /**
165
+ * The address of a page of THIS web app in this environment. The way across
166
+ * pages: each page is its own access area, checked by the gateway before it is
167
+ * served, so in-page routing must never stand in for it. Throws for a page the
168
+ * app does not have.
169
+ */
170
+ export function pageLink(name, suffix) {
171
+ return fn('pageLink')(name, suffix);
172
+ }
173
+ /** The page this code runs on ('' for the main page). */
174
+ export function currentPage() { return prop('page'); }
175
+ /** Another web app of the same space, by its id or slug. */
176
+ export function appLink(target, path) {
177
+ return fn('appLink')(target, path);
178
+ }
179
+ /** The address of one of the app's own files (served to exactly who may open the app). */
180
+ export function file(name) { return fn('file')(name); }
181
+ /** A string of the page's bilingual texts. */
182
+ export function t(key, vars) { return fn('t')(key, vars); }
183
+ /** "light" | "dark": the theme the page is shown in. */
184
+ export function theme() {
185
+ return fn('theme.current')() === 'dark' ? 'dark' : 'light';
186
+ }
187
+ /** A fresh clientToken, for a write the page sends itself. */
188
+ export function newClientToken() { return fn('newClientToken')(); }
189
+ /**
190
+ * Calls `listener` when rows of a source changed on this page (a confirmed
191
+ * assistant proposal, an action). Returns the function that stops listening.
192
+ */
193
+ export function onDataChanged(listener) {
194
+ const target = globalThis.window ?? globalThis;
195
+ const handler = (e) => listener(e.detail);
196
+ target.addEventListener('vf:data-changed', handler);
197
+ return () => target.removeEventListener('vf:data-changed', handler);
198
+ }
199
+ // ─────────────────────────────────────────────────────────────────────────────
200
+ // Server functions (R7 batch 3b) — PILOT, PLATFORM ADMINISTRATORS ONLY
201
+ // ─────────────────────────────────────────────────────────────────────────────
202
+ export * from './serverClient.js';
203
+ export { kebabOf, serverFunctionRoute, SERVER_FUNCTION_NAME } from './serverRoute.js';