@veriforms/webapp-sdk 0.1.3 → 0.1.4
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/README.md +25 -3
- package/dist/index.d.ts +45 -3
- package/dist/index.js +82 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,9 +5,27 @@ Typed access to the VeriForms web app runtime, for web apps built in code
|
|
|
5
5
|
|
|
6
6
|
## Install
|
|
7
7
|
|
|
8
|
+
The VeriForms CLI writes the project (batch 236c): `vf app init <appId>` for a web app created with
|
|
9
|
+
"Build in code" — or `vf app convert <appId> --to-code`, which switches an existing one first — writes
|
|
10
|
+
`package.json` (this package, `@veriforms/webapp-react` and `@veriforms/webapp-vite` at one version,
|
|
11
|
+
React 18.3.1, date-fns, clsx, TypeScript and Vite 6), `vite.config.ts`, `tsconfig.json`,
|
|
12
|
+
`src/pages/main.tsx`, the app's `config.json` and `veriforms.webapp.json` into `./<appId>`. Then:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
cd <appId>
|
|
16
|
+
npm install
|
|
17
|
+
vf app types # vf-app.d.ts from config.json
|
|
18
|
+
vf app dev # your code against the draft, preview data only
|
|
19
|
+
vf app build # tsc --noEmit, then vite build into vf-build/
|
|
20
|
+
vf app push # the build becomes the DRAFT
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
By hand, in a project of your own: the three packages at the same version, React and React DOM at
|
|
24
|
+
exactly 18.3.1, and a Vite the plugin accepts (6.x):
|
|
25
|
+
|
|
8
26
|
```sh
|
|
9
27
|
npm i @veriforms/webapp-sdk @veriforms/webapp-react react@18.3.1 react-dom@18.3.1
|
|
10
|
-
npm i -D @veriforms/webapp-vite vite
|
|
28
|
+
npm i -D @veriforms/webapp-vite vite@6
|
|
11
29
|
```
|
|
12
30
|
|
|
13
31
|
The packages are public on npm (owner decision 2026-12-03). Web apps built in code are open to
|
|
@@ -63,7 +81,8 @@ location.href = pageLink('admin'); // every area is its own page, gated by the
|
|
|
63
81
|
| `db.update(source, id, patch, version?, {clientToken}?)` | `customdb-write` | `VF.db.update` (version → 409 row_changed when someone changed the row) |
|
|
64
82
|
| `data.list(source, params?)` | `veriforms-data` | `VF.data.list` → `{Rows, Count, Truncated}` |
|
|
65
83
|
| `fetchSource(source, params?)` | `veriflow`, `http`, `demo` | `VF.fetch` |
|
|
66
|
-
| `knowledge.search(source, question)` (0.1.3) | `knowledge` | `VF.fetch` with `?q=` → `{status, message, passages, sourceLine}` |
|
|
84
|
+
| `knowledge.search(source, question)` (0.1.3) | `knowledge` | `VF.fetch` with `?q=` → `{status, message, passages, sourceLine}` (`sourceLine` always text with `ok`, 0.1.4) |
|
|
85
|
+
| `values.get(keys?)` (0.1.4) | `"values"` in config.json | `GET {base}/values?keys=…` → `{key: text}`; refusals are a `ValuesError` (`value_not_declared`, `value_not_found`) |
|
|
67
86
|
| `send(source, body, {clientToken}?)` | submission, webhook, workflow, veriflow, customdb-write | `VF.send` (repeat-safe) |
|
|
68
87
|
| `runAction(source, opts?)` | `workflow`, `veriflow`, `webhook` | `VF.runAction` |
|
|
69
88
|
| `form.render(target, source, opts?)`, `form.submitPartial` | submission, customdb-write | `VF.form.*` |
|
|
@@ -98,12 +117,15 @@ there is no key for "anyone who is signed in". So the pattern is three things:
|
|
|
98
117
|
"dataSources": {
|
|
99
118
|
"myBookings": { "type": "customdb", "table": "bookings", "select": ["_id", "item", "day"],
|
|
100
119
|
"rowRules": { "member": { "column": "booked_by", "equals": "$visitor.email" } } },
|
|
101
|
-
"book": { "type": "customdb-write", "table": "bookings",
|
|
120
|
+
"book": { "type": "customdb-write", "table": "bookings", "mode": "insert",
|
|
102
121
|
"fields": { "item": { "type": "string", "required": true }, "booked_by": { "type": "server-user" } },
|
|
103
122
|
"rowRules": { "member": { "column": "booked_by", "equals": "$visitor.email" } } }
|
|
104
123
|
}
|
|
105
124
|
```
|
|
106
125
|
|
|
126
|
+
A `customdb-write` source always names its `"mode"` — `"insert"`, `"upsert"` or `"update"`: there is
|
|
127
|
+
no default, the server refuses a config without it, and so does `vf app types` (it never assumes one).
|
|
128
|
+
|
|
107
129
|
1. the role is declared under `"roles"` (a `rowRules` key that is not a
|
|
108
130
|
declared role is refused when the config is saved);
|
|
109
131
|
2. the rule compares a column with `"$visitor.email"`, the signed-in address —
|
package/dist/index.d.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*
|
|
16
16
|
* Concept: docs/webapp-builder/CWA-CODE-APPS-CONCEPT.md (§4.2).
|
|
17
17
|
*/
|
|
18
|
-
export declare const SDK_VERSION = "0.1.
|
|
18
|
+
export declare const SDK_VERSION = "0.1.4";
|
|
19
19
|
/**
|
|
20
20
|
* Augmented by the `vf-app.d.ts` that `vf app types` generates:
|
|
21
21
|
*
|
|
@@ -71,6 +71,8 @@ export interface AppShape {
|
|
|
71
71
|
sources: Record<string, SourceShape>;
|
|
72
72
|
pages: string;
|
|
73
73
|
roles: string;
|
|
74
|
+
/** The Definitions values config.json declares in "values" (since 0.1.4). */
|
|
75
|
+
values: string;
|
|
74
76
|
}
|
|
75
77
|
/** What an app without generated types looks like: anything goes, nothing is checked. */
|
|
76
78
|
interface UntypedSource {
|
|
@@ -86,6 +88,7 @@ interface UntypedApp {
|
|
|
86
88
|
sources: Record<string, UntypedSource>;
|
|
87
89
|
pages: string;
|
|
88
90
|
roles: string;
|
|
91
|
+
values: string;
|
|
89
92
|
}
|
|
90
93
|
/** The app this code is typed against: the generated one, or {@link UntypedApp}. */
|
|
91
94
|
export type App = Register extends {
|
|
@@ -129,6 +132,10 @@ export type PageName = (App extends {
|
|
|
129
132
|
export type RoleName = App extends {
|
|
130
133
|
roles: infer R;
|
|
131
134
|
} ? R : string;
|
|
135
|
+
/** A value the app's config.json declares in "values" (a Definitions key such as company.iban), since 0.1.4. */
|
|
136
|
+
export type ValueKey = App extends {
|
|
137
|
+
values: infer V;
|
|
138
|
+
} ? V : string;
|
|
132
139
|
export type RowOf<K> = SourceOf<K> extends {
|
|
133
140
|
row: infer R;
|
|
134
141
|
} ? R : Record<string, unknown>;
|
|
@@ -321,6 +328,11 @@ export interface VfRuntime {
|
|
|
321
328
|
};
|
|
322
329
|
t(key: string, vars?: Record<string, unknown>): string;
|
|
323
330
|
newClientToken(): string;
|
|
331
|
+
/** The page's addresses: base (the app's path on this plane) and fileQuery (the draft preview's token). */
|
|
332
|
+
bridge?: {
|
|
333
|
+
base?: string;
|
|
334
|
+
fileQuery?: string;
|
|
335
|
+
};
|
|
324
336
|
}
|
|
325
337
|
/**
|
|
326
338
|
* Thrown when the page has no VeriForms runtime, or the runtime lacks a
|
|
@@ -386,13 +398,14 @@ export interface KnowledgePassage {
|
|
|
386
398
|
export declare const KNOWLEDGE_PASSAGE_FIELDS: readonly ["document", "url", "table", "rowId", "position", "score", "text"];
|
|
387
399
|
/**
|
|
388
400
|
* What {@link knowledge.search} resolves with. `sourceLine` is the server-built citation ("Quelle: …" /
|
|
389
|
-
* "Source: …"): show it under any answer built from the
|
|
401
|
+
* "Source: …"), always there with `ok` (since 0.1.4 typed so): show it under any answer built from the
|
|
402
|
+
* passages — never write a citation yourself.
|
|
390
403
|
*/
|
|
391
404
|
export type KnowledgeAnswer = {
|
|
392
405
|
status: 'ok';
|
|
393
406
|
message: string | null;
|
|
394
407
|
passages: KnowledgePassage[];
|
|
395
|
-
sourceLine: string
|
|
408
|
+
sourceLine: string;
|
|
396
409
|
} | {
|
|
397
410
|
status: 'no_match' | 'empty';
|
|
398
411
|
message: string | null;
|
|
@@ -444,6 +457,35 @@ export declare const knowledge: {
|
|
|
444
457
|
*/
|
|
445
458
|
search<K extends KnowledgeKey>(source: K, question: string): Promise<KnowledgeAnswer>;
|
|
446
459
|
};
|
|
460
|
+
/**
|
|
461
|
+
* The codes a values read is refused with (the gateway's GET {base}/values, ServeController.Values):
|
|
462
|
+
* a key config.json does not declare in "values" (`value_not_declared`, 403), a declared value the
|
|
463
|
+
* space does not serve — missing, not marked "may be shown in web apps", another application's
|
|
464
|
+
* (`value_not_found` and the resolver's codes, 502). Pinned against the gateway by test/pins.test.ts.
|
|
465
|
+
*/
|
|
466
|
+
export declare const VALUE_REFUSAL_CODES: readonly ["value_not_declared", "value_not_found", "values_unavailable"];
|
|
467
|
+
/** A values read that did not answer every key: refused, unreadable or unreachable. Never an empty text. */
|
|
468
|
+
export declare class ValuesError extends Error {
|
|
469
|
+
readonly code: string;
|
|
470
|
+
readonly keys: string[];
|
|
471
|
+
readonly status?: number | undefined;
|
|
472
|
+
constructor(code: string, keys: string[], message: string, status?: number | undefined, options?: {
|
|
473
|
+
cause?: unknown;
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
/** Definitions values: settings, fees, an IBAN — the same for every visitor, edited in VeriForms, never in the page. */
|
|
477
|
+
export declare const values: {
|
|
478
|
+
/**
|
|
479
|
+
* The values of `keys` (or every value config.json declares), each as TEXT exactly as
|
|
480
|
+
* `{{value:<key>}}` puts it in the config — formatted; parse a number yourself. Resolves with every
|
|
481
|
+
* key asked for, or rejects with a {@link ValuesError} naming the code and the keys: a key that is
|
|
482
|
+
* not declared, a value that cannot be served, or an answer without one of the keys. Works on the
|
|
483
|
+
* served planes and on the draft (the builder's preview, `vf app dev`).
|
|
484
|
+
*/
|
|
485
|
+
get<K extends ValueKey>(keys?: readonly K[]): Promise<Record<K, string>>;
|
|
486
|
+
};
|
|
487
|
+
/** True for a values read the server refused (an undeclared key, a value it does not serve). */
|
|
488
|
+
export declare function isValuesError(e: unknown): e is ValuesError;
|
|
447
489
|
/**
|
|
448
490
|
* Posts to a source with one clientToken for the visitor's one action, and asks
|
|
449
491
|
* again with the same token when the answer did not settle anything. A rejection
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*
|
|
16
16
|
* Concept: docs/webapp-builder/CWA-CODE-APPS-CONCEPT.md (§4.2).
|
|
17
17
|
*/
|
|
18
|
-
export const SDK_VERSION = '0.1.
|
|
18
|
+
export const SDK_VERSION = '0.1.4';
|
|
19
19
|
export function isVfRequestError(e) {
|
|
20
20
|
return e instanceof Error;
|
|
21
21
|
}
|
|
@@ -160,9 +160,11 @@ function readKnowledgeAnswer(source, raw) {
|
|
|
160
160
|
throw unreadable();
|
|
161
161
|
const message = typeof a.message === 'string' ? a.message : null;
|
|
162
162
|
if (a.status === 'ok') {
|
|
163
|
-
|
|
163
|
+
// An ok answer always carries the server-built line (KnowledgeCitations.SourceLineOf over its passages):
|
|
164
|
+
// without it the page would show an answer with no source, so it is unreadable, never null (batch 236c).
|
|
165
|
+
if (a.passages.length === 0 || typeof a.sourceLine !== 'string' || !a.sourceLine)
|
|
164
166
|
throw unreadable();
|
|
165
|
-
return { status: 'ok', message, passages: a.passages, sourceLine:
|
|
167
|
+
return { status: 'ok', message, passages: a.passages, sourceLine: a.sourceLine };
|
|
166
168
|
}
|
|
167
169
|
if (a.passages.length > 0)
|
|
168
170
|
throw unreadable();
|
|
@@ -195,6 +197,83 @@ export const knowledge = {
|
|
|
195
197
|
return readKnowledgeAnswer(source, raw);
|
|
196
198
|
},
|
|
197
199
|
};
|
|
200
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
201
|
+
// Values (Definitions), since 0.1.4
|
|
202
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
203
|
+
/**
|
|
204
|
+
* The codes a values read is refused with (the gateway's GET {base}/values, ServeController.Values):
|
|
205
|
+
* a key config.json does not declare in "values" (`value_not_declared`, 403), a declared value the
|
|
206
|
+
* space does not serve — missing, not marked "may be shown in web apps", another application's
|
|
207
|
+
* (`value_not_found` and the resolver's codes, 502). Pinned against the gateway by test/pins.test.ts.
|
|
208
|
+
*/
|
|
209
|
+
export const VALUE_REFUSAL_CODES = ['value_not_declared', 'value_not_found', 'values_unavailable'];
|
|
210
|
+
/** A values read that did not answer every key: refused, unreadable or unreachable. Never an empty text. */
|
|
211
|
+
export class ValuesError extends Error {
|
|
212
|
+
constructor(code, keys, message, status, options) {
|
|
213
|
+
super(message, options);
|
|
214
|
+
this.code = code;
|
|
215
|
+
this.keys = keys;
|
|
216
|
+
this.status = status;
|
|
217
|
+
this.name = 'ValuesError';
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/** Definitions values: settings, fees, an IBAN — the same for every visitor, edited in VeriForms, never in the page. */
|
|
221
|
+
export const values = {
|
|
222
|
+
/**
|
|
223
|
+
* The values of `keys` (or every value config.json declares), each as TEXT exactly as
|
|
224
|
+
* `{{value:<key>}}` puts it in the config — formatted; parse a number yourself. Resolves with every
|
|
225
|
+
* key asked for, or rejects with a {@link ValuesError} naming the code and the keys: a key that is
|
|
226
|
+
* not declared, a value that cannot be served, or an answer without one of the keys. Works on the
|
|
227
|
+
* served planes and on the draft (the builder's preview, `vf app dev`).
|
|
228
|
+
*/
|
|
229
|
+
async get(keys) {
|
|
230
|
+
const bridge = runtime().bridge;
|
|
231
|
+
if (!bridge || typeof bridge.base !== 'string')
|
|
232
|
+
throw new SdkRuntimeMismatchError('bridge.base');
|
|
233
|
+
const asked = [...new Set((keys ?? []).map(k => String(k).trim()).filter(Boolean))];
|
|
234
|
+
const query = bridge.fileQuery ?? '';
|
|
235
|
+
const url = `${bridge.base}/values${query}${asked.length ? `${query.includes('?') ? '&' : '?'}keys=${asked.map(encodeURIComponent).join(',')}` : ''}`;
|
|
236
|
+
let res;
|
|
237
|
+
try {
|
|
238
|
+
res = await fetch(url, { credentials: 'same-origin', headers: { Accept: 'application/json' } });
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
throw new ValuesError('values_unavailable', asked, `@veriforms/webapp-sdk ${SDK_VERSION}: the values could not be read (${e.message}).`, undefined, { cause: e });
|
|
242
|
+
}
|
|
243
|
+
let body;
|
|
244
|
+
try {
|
|
245
|
+
body = await res.json();
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
body = undefined;
|
|
249
|
+
}
|
|
250
|
+
const b = (body && typeof body === 'object' ? body : {});
|
|
251
|
+
if (!res.ok) {
|
|
252
|
+
const code = typeof b.code === 'string' ? b.code : 'values_unavailable';
|
|
253
|
+
const named = Array.isArray(b.keys) ? b.keys.filter((k) => typeof k === 'string') : asked;
|
|
254
|
+
throw new ValuesError(code, named, typeof b.message === 'string' ? b.message : `The values could not be read (${res.status}).`, res.status);
|
|
255
|
+
}
|
|
256
|
+
const got = b.values;
|
|
257
|
+
if (!got || typeof got !== 'object' || Array.isArray(got)) {
|
|
258
|
+
throw new ValuesError('values_unreadable', asked, `@veriforms/webapp-sdk ${SDK_VERSION}: the values answer could not be read.`, res.status);
|
|
259
|
+
}
|
|
260
|
+
const map = got;
|
|
261
|
+
// Every key asked for is in the answer as text — a missing one is a fault, never "".
|
|
262
|
+
const missing = asked.filter(k => typeof map[k] !== 'string');
|
|
263
|
+
if (missing.length) {
|
|
264
|
+
throw new ValuesError('value_not_found', missing, `The values answer has no text for ${missing.join(', ')}.`, res.status);
|
|
265
|
+
}
|
|
266
|
+
const out = {};
|
|
267
|
+
for (const [k, v] of Object.entries(map))
|
|
268
|
+
if (typeof v === 'string')
|
|
269
|
+
out[k] = v;
|
|
270
|
+
return out;
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
/** True for a values read the server refused (an undeclared key, a value it does not serve). */
|
|
274
|
+
export function isValuesError(e) {
|
|
275
|
+
return e instanceof ValuesError;
|
|
276
|
+
}
|
|
198
277
|
/**
|
|
199
278
|
* Posts to a source with one clientToken for the visitor's one action, and asks
|
|
200
279
|
* again with the same token when the answer did not settle anything. A rejection
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veriforms/webapp-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Typed access to the VeriForms web app runtime (window.VF) for web apps built in code: data sources, tables, submissions, actions, payments, sign-in and pages.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"veriforms",
|