@veriforms/webapp-react 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.
- package/LICENSE +8 -0
- package/README.md +56 -2
- package/dist/index.d.ts +74 -0
- package/dist/index.js +124 -0
- package/package.json +54 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright (c) 2026 In Data Veritas GmbH. All rights reserved.
|
|
2
|
+
|
|
3
|
+
@veriforms/webapp-react is proprietary software, published on npm as
|
|
4
|
+
a preview ("license": "UNLICENSED", like the VeriForms CLI). Until In Data Veritas GmbH
|
|
5
|
+
publishes it under a licence, no licence is granted to copy, modify or distribute it.
|
|
6
|
+
|
|
7
|
+
Using it to build web apps that run on VeriForms is governed by the VeriForms terms of
|
|
8
|
+
service (https://www.veriforms.net).
|
package/README.md
CHANGED
|
@@ -1,3 +1,57 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @veriforms/webapp-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React hooks and components for VeriForms web apps built in code (pilot until general availability),
|
|
4
|
+
over `@veriforms/webapp-sdk`.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
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
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
During the pilot the packages are on npm as **restricted**: only accounts the `@veriforms` scope
|
|
14
|
+
grants access can install them (run `npm login` with such an account, or put a read token into
|
|
15
|
+
`.npmrc`). Without access, use the pilot kit (`tools/webapp-starter/make-pilot-kit.mjs`). Public at
|
|
16
|
+
general availability. The three packages are released together at one version
|
|
17
|
+
(`webapp-react` names the SDK exactly). Install React at the version the platform serves (18.3.1) —
|
|
18
|
+
the upload compares your `package-lock.json`.
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import { useTable, useSend, useUser, PageLink, VfForm } from '@veriforms/webapp-react';
|
|
22
|
+
|
|
23
|
+
function Rooms() {
|
|
24
|
+
const rooms = useTable('rooms'); // reloads when the platform says the table changed here
|
|
25
|
+
if (rooms.loading) return <p>Loading…</p>;
|
|
26
|
+
if (rooms.error) return <p role="alert">{rooms.error.message}</p>; // the runtime's error, never empty data
|
|
27
|
+
return <ul>{rooms.data!.rows.map((r) => <li key={r._id}>{r.name}</li>)}</ul>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function Book() {
|
|
31
|
+
const book = useSend('book');
|
|
32
|
+
return (
|
|
33
|
+
<>
|
|
34
|
+
<button disabled={book.sending} onClick={() => book.submit({ room: 'A' })}>Book</button>
|
|
35
|
+
{book.error && <p role="alert">{book.error.message}</p>}
|
|
36
|
+
</>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
- `useSource`, `useTable`, `useSubmissions`, `useUser` — `{ data, error, loading, reload }`.
|
|
42
|
+
- A row has exactly the columns the source returns: with `"select"` in `config.json` those and no
|
|
43
|
+
others. `r._id` (a React key, the id `db.update` takes) is there when the source has no `"select"`
|
|
44
|
+
or its `"select"` lists `"_id"` — otherwise it is a compile error, because the row does not carry it.
|
|
45
|
+
Every column of your own can be `null` in the row type: the server's description of a table does
|
|
46
|
+
not say which columns are required, so none is assumed filled.
|
|
47
|
+
- `useSend(source)` — `{ send, submit, sending, result, error }`. Both set `error` when the write is
|
|
48
|
+
refused. `submit(body)` never rejects — it resolves `{ ok: true, result }` or `{ ok: false, error }`;
|
|
49
|
+
use it from an event handler that only shows the outcome (as above). `send(body)` rejects with the
|
|
50
|
+
same error; use it where the code goes on after the write, inside `try`/`catch`
|
|
51
|
+
(`try { await book.send(body); close(); } catch { /* book.error is set */ }`). Calling `send`
|
|
52
|
+
without a `catch` turns a refusal into an unhandled rejection.
|
|
53
|
+
- `useAccess()`, `useTheme()`.
|
|
54
|
+
- `<PageLink to="admin">` — the way across pages (each page is its own access area).
|
|
55
|
+
- `<VfForm source="booking" />` — the platform's form with its server rules mirrored.
|
|
56
|
+
|
|
57
|
+
Concept: `docs/webapp-builder/CWA-CODE-APPS-CONCEPT.md` (§4.2, §7.8).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type AnchorHTMLAttributes, type ReactNode } from 'react';
|
|
2
|
+
import type { FormKey, PageName, ParamsOf, ReadKey, ResultOf, RowOf, SendKey, SendResultOf, BodyOf, SubmissionsReadKey, FieldsOf, TableKey, TablePage, SubmissionsPage, UserInfo, WriteOptions, RoleName, SourceKey } from '@veriforms/webapp-sdk';
|
|
3
|
+
/** What a read hook holds: the data once loaded, the error as raised, and a way to read again. */
|
|
4
|
+
export interface Loaded<T> {
|
|
5
|
+
data: T | undefined;
|
|
6
|
+
/** The runtime's error (a VfRequestError with status and code), never turned into empty data. */
|
|
7
|
+
error: Error | undefined;
|
|
8
|
+
loading: boolean;
|
|
9
|
+
reload: () => void;
|
|
10
|
+
}
|
|
11
|
+
/** Reads a source (an Automation, an http upstream, demo data). */
|
|
12
|
+
export declare function useSource<K extends ReadKey>(source: K, params?: Partial<ParamsOf<K>>): Loaded<ResultOf<K>>;
|
|
13
|
+
/** One page of a table (Custom Databases) through a customdb source; reloads when its rows change here. */
|
|
14
|
+
export declare function useTable<K extends TableKey>(source: K, params?: Partial<ParamsOf<K>> & {
|
|
15
|
+
limit?: number;
|
|
16
|
+
offset?: number;
|
|
17
|
+
}): Loaded<TablePage<RowOf<K>>>;
|
|
18
|
+
/** This web app's own submissions, read back through a veriforms-data source. */
|
|
19
|
+
export declare function useSubmissions<K extends SubmissionsReadKey>(source: K, params?: Partial<ParamsOf<K>>): Loaded<SubmissionsPage<FieldsOf<K>>>;
|
|
20
|
+
/** Who is signed in. */
|
|
21
|
+
export declare function useUser(): Loaded<UserInfo>;
|
|
22
|
+
/** What the visitor may do, as the server told the page (the server checks every call again). */
|
|
23
|
+
export declare function useAccess(): {
|
|
24
|
+
has: (role: RoleName) => boolean;
|
|
25
|
+
can: (source: SourceKey) => boolean;
|
|
26
|
+
level: string | null;
|
|
27
|
+
signedIn: boolean;
|
|
28
|
+
};
|
|
29
|
+
/** "light" | "dark": the theme the page is shown in. */
|
|
30
|
+
export declare function useTheme(): 'light' | 'dark';
|
|
31
|
+
/** How a submit() ended: the answer, or the runtime's error — never a rejection. */
|
|
32
|
+
export type Submitted<R> = {
|
|
33
|
+
ok: true;
|
|
34
|
+
result: R;
|
|
35
|
+
} | {
|
|
36
|
+
ok: false;
|
|
37
|
+
error: Error;
|
|
38
|
+
};
|
|
39
|
+
/** What a write hook holds: send() and submit(), the state of the last one, and its result or error. */
|
|
40
|
+
export interface Sending<B, R = unknown> {
|
|
41
|
+
/**
|
|
42
|
+
* Posts, and REJECTS with the runtime's error when the write is refused (`error` is set too).
|
|
43
|
+
* For code that goes on after the write: `try { await send(body); next(); } catch { … }`. Called
|
|
44
|
+
* without a catch — `onClick={() => send(body)}` — a refusal is also an unhandled rejection.
|
|
45
|
+
*/
|
|
46
|
+
send: (body: B, opts?: WriteOptions) => Promise<R>;
|
|
47
|
+
/**
|
|
48
|
+
* The same write for an event handler that only shows the outcome: never rejects. A refusal sets
|
|
49
|
+
* `error` (render it) and resolves `{ ok: false, error }`; a success resolves `{ ok: true, result }`.
|
|
50
|
+
*/
|
|
51
|
+
submit: (body: B, opts?: WriteOptions) => Promise<Submitted<R>>;
|
|
52
|
+
sending: boolean;
|
|
53
|
+
/** The last send's answer: for an Automation source, its declared result (concept §7.12). */
|
|
54
|
+
result: R | undefined;
|
|
55
|
+
/** The runtime's error. An `outcome: 'unknown'` one may have gone through: see sdk.isOutcomeUnknown. */
|
|
56
|
+
error: Error | undefined;
|
|
57
|
+
}
|
|
58
|
+
/** Posts to a source (a submission, a table write, a workflow, an Automation, a webhook). */
|
|
59
|
+
export declare function useSend<K extends SendKey>(source: K): Sending<BodyOf<K>, SendResultOf<K>>;
|
|
60
|
+
/**
|
|
61
|
+
* A link to another page of THIS web app. Each page is its own access area the gateway checks
|
|
62
|
+
* before serving it, so this is the way across pages — never an in-page router route.
|
|
63
|
+
*/
|
|
64
|
+
export declare function PageLink({ to, suffix, children, ...rest }: {
|
|
65
|
+
to: PageName;
|
|
66
|
+
suffix?: `?${string}` | `#${string}`;
|
|
67
|
+
children?: ReactNode;
|
|
68
|
+
} & Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'>): import("react/jsx-runtime").JSX.Element;
|
|
69
|
+
/** The platform's form for a submission or table-insert source, with its server rules mirrored. */
|
|
70
|
+
export declare function VfForm<K extends FormKey>({ source, options, className }: {
|
|
71
|
+
source: K;
|
|
72
|
+
options?: Record<string, unknown>;
|
|
73
|
+
className?: string;
|
|
74
|
+
}): import("react/jsx-runtime").JSX.Element;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
// @veriforms/webapp-react — React hooks and components for VeriForms web apps built in code
|
|
3
|
+
// (docs/webapp-builder/CWA-CODE-APPS-CONCEPT.md §4.2 and §7.8).
|
|
4
|
+
//
|
|
5
|
+
// Thin on purpose: every call goes through @veriforms/webapp-sdk, which calls the page's injected
|
|
6
|
+
// runtime (window.VF) — one runtime, one behaviour. These hooks add React state around it: loading,
|
|
7
|
+
// the error AS the runtime raised it (never swallowed into empty data), and a reload when the
|
|
8
|
+
// platform says a source's rows changed on this page (vf:data-changed).
|
|
9
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
10
|
+
import * as sdk from '@veriforms/webapp-sdk';
|
|
11
|
+
/** One read, re-run when its key changes or reload() is called; a stale answer never overwrites a newer one. */
|
|
12
|
+
function useRead(read, key, sourceKey) {
|
|
13
|
+
const [state, setState] = useState({ loading: true });
|
|
14
|
+
const [tick, setTick] = useState(0);
|
|
15
|
+
const run = useRef(0);
|
|
16
|
+
const readRef = useRef(read);
|
|
17
|
+
readRef.current = read;
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const mine = ++run.current;
|
|
20
|
+
setState((s) => ({ data: s.data, error: undefined, loading: true }));
|
|
21
|
+
let promise;
|
|
22
|
+
try {
|
|
23
|
+
promise = readRef.current();
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
// A missing runtime (sdk_runtime_mismatch) or an unknown page: said, not swallowed.
|
|
27
|
+
setState({ error: e, loading: false });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
promise.then((data) => { if (run.current === mine)
|
|
31
|
+
setState({ data, loading: false }); }, (error) => { if (run.current === mine)
|
|
32
|
+
setState({ error, loading: false }); });
|
|
33
|
+
}, [key, tick]);
|
|
34
|
+
// The platform tells the page when a source's rows changed here (a confirmed proposal, an action).
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!sourceKey)
|
|
37
|
+
return;
|
|
38
|
+
return sdk.onDataChanged((change) => { if (change.sourceKey === sourceKey)
|
|
39
|
+
setTick((t) => t + 1); });
|
|
40
|
+
}, [sourceKey]);
|
|
41
|
+
const reload = useCallback(() => setTick((t) => t + 1), []);
|
|
42
|
+
return { data: state.data, error: state.error, loading: state.loading, reload };
|
|
43
|
+
}
|
|
44
|
+
const keyOf = (...parts) => JSON.stringify(parts);
|
|
45
|
+
/** Reads a source (an Automation, an http upstream, demo data). */
|
|
46
|
+
export function useSource(source, params) {
|
|
47
|
+
return useRead(() => sdk.fetchSource(source, params), keyOf('source', source, params), source);
|
|
48
|
+
}
|
|
49
|
+
/** One page of a table (Custom Databases) through a customdb source; reloads when its rows change here. */
|
|
50
|
+
export function useTable(source, params) {
|
|
51
|
+
return useRead(() => sdk.db.list(source, params), keyOf('table', source, params), source);
|
|
52
|
+
}
|
|
53
|
+
/** This web app's own submissions, read back through a veriforms-data source. */
|
|
54
|
+
export function useSubmissions(source, params) {
|
|
55
|
+
return useRead(() => sdk.data.list(source, params), keyOf('submissions', source, params), source);
|
|
56
|
+
}
|
|
57
|
+
/** Who is signed in. */
|
|
58
|
+
export function useUser() {
|
|
59
|
+
return useRead(() => sdk.user(), 'user', undefined);
|
|
60
|
+
}
|
|
61
|
+
/** What the visitor may do, as the server told the page (the server checks every call again). */
|
|
62
|
+
export function useAccess() {
|
|
63
|
+
return useMemo(() => ({
|
|
64
|
+
has: (role) => sdk.access.has(role),
|
|
65
|
+
can: (source) => sdk.access.can(source),
|
|
66
|
+
level: sdk.access.level(),
|
|
67
|
+
signedIn: sdk.access.signedIn(),
|
|
68
|
+
}), []);
|
|
69
|
+
}
|
|
70
|
+
/** "light" | "dark": the theme the page is shown in. */
|
|
71
|
+
export function useTheme() {
|
|
72
|
+
return useMemo(() => sdk.theme(), []);
|
|
73
|
+
}
|
|
74
|
+
/** Posts to a source (a submission, a table write, a workflow, an Automation, a webhook). */
|
|
75
|
+
export function useSend(source) {
|
|
76
|
+
const [state, setState] = useState({ sending: false });
|
|
77
|
+
const send = useCallback(async (body, opts) => {
|
|
78
|
+
setState({ sending: true });
|
|
79
|
+
try {
|
|
80
|
+
const result = await sdk.send(source, body, opts);
|
|
81
|
+
setState({ sending: false, result });
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
setState({ sending: false, error: e });
|
|
86
|
+
throw e;
|
|
87
|
+
}
|
|
88
|
+
}, [source]);
|
|
89
|
+
// R4-14: `send` rejects AND sets `error`, so the documented shape — call it from onClick, render
|
|
90
|
+
// `error` — showed the message and raised an unhandled rejection. `send` keeps rejecting (code that
|
|
91
|
+
// awaits it must not carry on after a refusal); `submit` is the one for a handler: the same write,
|
|
92
|
+
// the outcome as a value.
|
|
93
|
+
const submit = useCallback(async (body, opts) => {
|
|
94
|
+
try {
|
|
95
|
+
return { ok: true, result: await send(body, opts) };
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
return { ok: false, error: e };
|
|
99
|
+
}
|
|
100
|
+
}, [send]);
|
|
101
|
+
return { send, submit, sending: state.sending, result: state.result, error: state.error };
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* A link to another page of THIS web app. Each page is its own access area the gateway checks
|
|
105
|
+
* before serving it, so this is the way across pages — never an in-page router route.
|
|
106
|
+
*/
|
|
107
|
+
export function PageLink({ to, suffix, children, ...rest }) {
|
|
108
|
+
return _jsx("a", { ...rest, href: sdk.pageLink(to, suffix), children: children });
|
|
109
|
+
}
|
|
110
|
+
/** The platform's form for a submission or table-insert source, with its server rules mirrored. */
|
|
111
|
+
export function VfForm({ source, options, className }) {
|
|
112
|
+
const ref = useRef(null);
|
|
113
|
+
const optionsKey = keyOf(options);
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
const host = ref.current;
|
|
116
|
+
if (!host)
|
|
117
|
+
return;
|
|
118
|
+
host.replaceChildren();
|
|
119
|
+
sdk.form.render(host, source, options);
|
|
120
|
+
return () => host.replaceChildren();
|
|
121
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
122
|
+
}, [source, optionsKey]);
|
|
123
|
+
return _jsx("div", { ref: ref, className: className });
|
|
124
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veriforms/webapp-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "React hooks and components for VeriForms web apps built in code: data sources, tables, submissions, the visitor, access, page links and the platform's form — over @veriforms/webapp-sdk.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"veriforms",
|
|
7
|
+
"web app",
|
|
8
|
+
"react",
|
|
9
|
+
"hooks"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://www.veriforms.net",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/In-Data-Veritas-GmbH/VeriForms.git",
|
|
15
|
+
"directory": "tools/webapp-react"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"license": "UNLICENSED",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc -p tsconfig.build.json",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@veriforms/webapp-sdk": "0.1.1",
|
|
40
|
+
"react": "^18.3.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@testing-library/react": "16.1.0",
|
|
44
|
+
"@types/node": "22.10.2",
|
|
45
|
+
"@types/react": "18.3.12",
|
|
46
|
+
"@veriforms/webapp-sdk": "file:../webapp-sdk",
|
|
47
|
+
"happy-dom": "15.11.7",
|
|
48
|
+
"react": "18.3.1",
|
|
49
|
+
"react-dom": "18.3.1",
|
|
50
|
+
"typescript": "5.6.3",
|
|
51
|
+
"vitest": "2.1.8"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
}
|
|
56
|
+
}
|