@zaptek/application-livel 0.0.0-stage → 0.15.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/CHANGELOG.md +150 -0
- package/README.md +49 -2
- package/dist/client/livel-application-client.d.ts +32 -0
- package/dist/client/livel-application-client.js +71 -0
- package/dist/components/files/file-download-button.component.d.ts +7 -0
- package/dist/components/files/file-download-button.component.js +36 -0
- package/dist/components/files/file-upload-button.component.d.ts +11 -0
- package/dist/components/files/file-upload-button.component.js +41 -0
- package/dist/components/files/index.d.ts +4 -0
- package/dist/components/files/index.js +2 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +3 -0
- package/dist/components/livel-application.provider.d.ts +20 -0
- package/dist/components/livel-application.provider.js +75 -0
- package/dist/components/livel-data-connection-status.component.d.ts +4 -0
- package/dist/components/livel-data-connection-status.component.js +122 -0
- package/dist/dev/dev-errors.contract.d.ts +23 -0
- package/dist/dev/dev-errors.contract.js +2 -0
- package/dist/hooks/files.hooks.d.ts +51 -0
- package/dist/hooks/files.hooks.js +296 -0
- package/dist/hooks/index.d.ts +6 -0
- package/dist/hooks/index.js +3 -0
- package/dist/hooks/rpc.hooks.d.ts +8 -0
- package/dist/hooks/rpc.hooks.js +26 -0
- package/dist/host/embedded-host.d.ts +8 -0
- package/dist/host/embedded-host.js +47 -0
- package/dist/host/livel-host.contract.d.ts +17 -0
- package/dist/host/livel-host.contract.js +1 -0
- package/dist/host/livel-host.d.ts +14 -0
- package/dist/host/livel-host.js +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +4 -0
- package/dist/rpc/rpc-caller.d.ts +9 -0
- package/dist/rpc/rpc-caller.js +10 -0
- package/dist/vite/index.d.ts +1 -0
- package/dist/vite/index.js +1 -0
- package/dist/vite/livel-dev-errors.plugin.d.ts +13 -0
- package/dist/vite/livel-dev-errors.plugin.js +109 -0
- package/docs/README.md +24 -0
- package/docs/authentication.md +66 -0
- package/docs/data.md +134 -0
- package/docs/files.md +130 -0
- package/docs/getting-started.md +83 -0
- package/docs/rpc.md +49 -0
- package/docs/troubleshooting.md +15 -0
- package/package.json +66 -3
- package/template/.env.example +8 -0
- package/template/AGENTS.md +104 -0
- package/template/README.md +83 -0
- package/template/_gitignore +11 -0
- package/template/components.json +25 -0
- package/template/livel.json +14 -0
- package/template/package.json +48 -0
- package/template/pnpm-workspace.yaml +28 -0
- package/template/src/components/route-error.tsx +33 -0
- package/template/src/components/ui/button.tsx +57 -0
- package/template/src/components/ui/toast.tsx +221 -0
- package/template/src/lib/utils.ts +1 -0
- package/template/src/routeTree.gen.ts +68 -0
- package/template/src/router.tsx +19 -0
- package/template/src/routes/__root.tsx +53 -0
- package/template/src/routes/index.tsx +85 -0
- package/template/src/styles.css +148 -0
- package/template/tsconfig.json +21 -0
- package/template/vite.config.ts +28 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { createLogger } from "vite";
|
|
2
|
+
import { livelDevErrorsPath, } from "../dev/dev-errors.contract.js";
|
|
3
|
+
/** Only bundler errors locate a module; others (port in use…) are not the app's. */
|
|
4
|
+
function isModuleError(error) {
|
|
5
|
+
if (!(error instanceof Error))
|
|
6
|
+
return false;
|
|
7
|
+
const located = error;
|
|
8
|
+
return Boolean(located.id || located.loc || located.plugin);
|
|
9
|
+
}
|
|
10
|
+
function toDevError(error) {
|
|
11
|
+
const file = error.loc
|
|
12
|
+
? `${error.loc.file ?? error.id}:${error.loc.line}:${error.loc.column}`
|
|
13
|
+
: error.id;
|
|
14
|
+
return {
|
|
15
|
+
message: error.message,
|
|
16
|
+
stack: error.stack,
|
|
17
|
+
file,
|
|
18
|
+
frame: error.frame,
|
|
19
|
+
plugin: error.plugin,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Streams the dev server's build errors to Livel, which shows them over the
|
|
24
|
+
* preview and offers to fix them. It works even when the application cannot
|
|
25
|
+
* load, since only the dev server sees these errors. They come from Vite's
|
|
26
|
+
* logger (`customLogger`, wrapping any logger already configured), where Vite
|
|
27
|
+
* reports every module that fails to transform, for the browser or the server.
|
|
28
|
+
*
|
|
29
|
+
* The error clears once its module builds again; an error tied to no module
|
|
30
|
+
* clears on the next file change, and comes back if it persists.
|
|
31
|
+
* Dev server only.
|
|
32
|
+
*/
|
|
33
|
+
export function livelDevErrors() {
|
|
34
|
+
let current = null;
|
|
35
|
+
let currentModule;
|
|
36
|
+
const clients = new Set();
|
|
37
|
+
const broadcast = (event) => {
|
|
38
|
+
const message = `data: ${JSON.stringify(event)}\n\n`;
|
|
39
|
+
for (const client of clients)
|
|
40
|
+
client.write(message);
|
|
41
|
+
};
|
|
42
|
+
const report = (error, moduleId) => {
|
|
43
|
+
current = error;
|
|
44
|
+
currentModule = moduleId;
|
|
45
|
+
broadcast({ type: "error", error });
|
|
46
|
+
};
|
|
47
|
+
const clear = () => {
|
|
48
|
+
if (!current)
|
|
49
|
+
return;
|
|
50
|
+
current = null;
|
|
51
|
+
currentModule = undefined;
|
|
52
|
+
broadcast({ type: "cleared" });
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
name: "livel:dev-errors",
|
|
56
|
+
apply: "serve",
|
|
57
|
+
config(config) {
|
|
58
|
+
const logger = config.customLogger ??
|
|
59
|
+
createLogger(config.logLevel, { allowClearScreen: config.clearScreen });
|
|
60
|
+
return {
|
|
61
|
+
customLogger: {
|
|
62
|
+
get hasWarned() {
|
|
63
|
+
return logger.hasWarned;
|
|
64
|
+
},
|
|
65
|
+
info: (message, options) => logger.info(message, options),
|
|
66
|
+
warn: (message, options) => logger.warn(message, options),
|
|
67
|
+
warnOnce: (message, options) => logger.warnOnce(message, options),
|
|
68
|
+
clearScreen: type => logger.clearScreen(type),
|
|
69
|
+
hasErrorLogged: error => logger.hasErrorLogged(error),
|
|
70
|
+
error(message, options) {
|
|
71
|
+
const error = options?.error;
|
|
72
|
+
if (isModuleError(error))
|
|
73
|
+
report(toDevError(error), error.id);
|
|
74
|
+
logger.error(message, options);
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
configureServer(server) {
|
|
80
|
+
server.middlewares.use(livelDevErrorsPath, (req, res) => {
|
|
81
|
+
// Livel reads the stream from its own origin; it carries no secret
|
|
82
|
+
// beyond what the public preview already shows.
|
|
83
|
+
res.writeHead(200, {
|
|
84
|
+
"Access-Control-Allow-Origin": "*",
|
|
85
|
+
"Cache-Control": "no-cache",
|
|
86
|
+
Connection: "keep-alive",
|
|
87
|
+
"Content-Type": "text/event-stream",
|
|
88
|
+
});
|
|
89
|
+
const initial = current
|
|
90
|
+
? { type: "error", error: current }
|
|
91
|
+
: { type: "cleared" };
|
|
92
|
+
res.write(`data: ${JSON.stringify(initial)}\n\n`);
|
|
93
|
+
clients.add(res);
|
|
94
|
+
req.on("close", () => clients.delete(res));
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
transform: {
|
|
98
|
+
order: "post",
|
|
99
|
+
handler(_code, id) {
|
|
100
|
+
if (current && id === currentModule)
|
|
101
|
+
clear();
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
hotUpdate() {
|
|
105
|
+
if (current && !currentModule)
|
|
106
|
+
clear();
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Livel Application SDK documentation
|
|
2
|
+
|
|
3
|
+
This documentation ships with `@zaptek/application-livel`. In an
|
|
4
|
+
application, read it from `node_modules/@zaptek/application-livel/docs/`:
|
|
5
|
+
it then matches the version installed in this repository.
|
|
6
|
+
|
|
7
|
+
- [Getting started](getting-started.md): configuration and first data screen.
|
|
8
|
+
- [Authentication](authentication.md): iframe, access token and provider states.
|
|
9
|
+
- [RPC](rpc.md): TanStack Query hook and transport for the platform's API.
|
|
10
|
+
- [Data](data.md): workspace objects, resources, reads, aggregates and writes.
|
|
11
|
+
- [Files](files.md): browse, upload and manage files, with
|
|
12
|
+
ready-to-use buttons.
|
|
13
|
+
- [Troubleshooting](troubleshooting.md): diagnose a connection or a request.
|
|
14
|
+
- [`CHANGELOG.md`](../CHANGELOG.md): published changes and any migrations.
|
|
15
|
+
|
|
16
|
+
The SDK provides an RPC read hook, a caller authenticated with the
|
|
17
|
+
application's client, a low-level RPC transport, the binding of resources
|
|
18
|
+
(`@zaptek/resource`) to the workspace's objects, and file hooks and
|
|
19
|
+
components. It provides neither an
|
|
20
|
+
independent connection to the platform nor server-side access to the
|
|
21
|
+
user's token. The Supabase client remains available under the RLS of the signed-in
|
|
22
|
+
member. The public exports are those of `@zaptek/application-livel`,
|
|
23
|
+
`@zaptek/application-livel/hooks` and `@zaptek/application-livel/components`;
|
|
24
|
+
internal files are not an API.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Authentication in the iframe
|
|
2
|
+
|
|
3
|
+
Livel remains responsible for the Supabase connection, the refresh token and the
|
|
4
|
+
session. On load, the application's client opens a
|
|
5
|
+
[Penpal](https://github.com/Aaronius/penpal) bridge with the parent window, restricted to
|
|
6
|
+
the origin `VITE_LIVEL_HOST_ORIGIN`. Before each Supabase or RPC request,
|
|
7
|
+
it asks Livel for the current access token (`getAccessToken`, described by the
|
|
8
|
+
`LivelHostMethods` type). Livel always returns a valid token and refreshes it
|
|
9
|
+
itself; the application stores no token, neither in memory nor in
|
|
10
|
+
cookies or local storage.
|
|
11
|
+
|
|
12
|
+
`LivelApplicationProvider` creates this client in the browser and mounts a
|
|
13
|
+
`QueryClientProvider`. `useLivelApplication()` exposes:
|
|
14
|
+
|
|
15
|
+
| Field | Meaning |
|
|
16
|
+
| --- | --- |
|
|
17
|
+
| `status: "connecting"` | The bridge with Livel is being established. |
|
|
18
|
+
| `status: "ready"` | The bridge is established; the hooks can read the data. |
|
|
19
|
+
| `status: "unavailable"` | The client could not start or Livel did not respond within ten seconds. |
|
|
20
|
+
| `client` | Browser Supabase and RPC clients, or `null` before they are created. |
|
|
21
|
+
|
|
22
|
+
For a read from the platform's API, use `useRpcQuery`: it waits for
|
|
23
|
+
`"ready"`, includes the workspace in its cache key, and passes the current
|
|
24
|
+
token on each call. For an imperative call,
|
|
25
|
+
`useLivelApplication()` exposes `client.rpc` when the client is available.
|
|
26
|
+
For a Supabase query that the hooks do not cover yet, use
|
|
27
|
+
`client.supabase` in an application-specific hook and handle its loading
|
|
28
|
+
and error states there.
|
|
29
|
+
|
|
30
|
+
This client uses the member's token and therefore their Supabase/RLS permissions. It
|
|
31
|
+
grants no additional rights. In a component, prefer the
|
|
32
|
+
SDK hooks for the resources they cover and keep any new
|
|
33
|
+
query in an application-specific hook.
|
|
34
|
+
|
|
35
|
+
The origin of the session is isolated behind `LivelHost`: in the iframe,
|
|
36
|
+
it is the Livel page (`createEmbeddedHost`). A standalone application will provide
|
|
37
|
+
its own connection behind the same contract, without changing the hooks.
|
|
38
|
+
|
|
39
|
+
The low-level export `createLivelApplicationClient()` is browser-only
|
|
40
|
+
and must be released with `client.dispose()` when its owner goes away.
|
|
41
|
+
`client.ready` resolves to `true` once the bridge is established, and to `false` if Livel
|
|
42
|
+
did not respond in time. A server loader cannot use this iframe
|
|
43
|
+
session.
|
|
44
|
+
|
|
45
|
+
## Errors reported to Livel
|
|
46
|
+
|
|
47
|
+
The same bridge forwards runtime errors to Livel (`reportError`, described
|
|
48
|
+
by `LivelHostMethods`). The client reports on its own uncaught
|
|
49
|
+
errors and promises rejected without a handler; Livel displays them
|
|
50
|
+
above the preview and the member can ask for them to be fixed. An error
|
|
51
|
+
that the application catches itself, for example in a route's error
|
|
52
|
+
component, is reported with `useReportError()`.
|
|
53
|
+
|
|
54
|
+
Compilation errors cannot go through this bridge: when a module
|
|
55
|
+
does not compile, no application code runs. The Vite plugin
|
|
56
|
+
`livelDevErrors()` (`@zaptek/application-livel/vite`) observes them on the development
|
|
57
|
+
server and broadcasts them on `/__livel/dev-errors`; Livel displays them
|
|
58
|
+
above the preview in the same way. The error disappears as soon as the
|
|
59
|
+
affected module compiles again.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
// vite.config.ts
|
|
63
|
+
import { livelDevErrors } from "@zaptek/application-livel/vite"
|
|
64
|
+
|
|
65
|
+
export default defineConfig({ plugins: [/* … */ livelDevErrors()] })
|
|
66
|
+
```
|
package/docs/data.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# Reading and changing Livel data
|
|
2
|
+
|
|
3
|
+
The workspace's data is its **objects** (policyholders, contracts, tasks…).
|
|
4
|
+
Each object is a real table; each **record** is one of its rows; each
|
|
5
|
+
field, a column. The application reads and writes them with the session and
|
|
6
|
+
rights of the signed-in member: it only sees the records that member can see.
|
|
7
|
+
|
|
8
|
+
## The generated tables
|
|
9
|
+
|
|
10
|
+
Livel writes `src/livel-objects.gen.ts` into the application: the tables of the
|
|
11
|
+
workspace's objects, declared with Drizzle. Do not modify this file: Livel
|
|
12
|
+
regenerates it when the objects change, and the build then reports the code to
|
|
13
|
+
adapt. The names in it are those of the database:
|
|
14
|
+
|
|
15
|
+
- an object created by the workspace and a field it adds to a template object
|
|
16
|
+
carry the `x_` prefix (`x_broker`);
|
|
17
|
+
- an amount is in cents, in `<field>_cents`;
|
|
18
|
+
- a relation contains the referenced id, in `<field>_id`;
|
|
19
|
+
- each record has `id`, `workspace_id`, `owner_workspace_member_id` (its
|
|
20
|
+
owner), `owner_workspace_group_id` (its group), `created_at` and
|
|
21
|
+
`updated_at`.
|
|
22
|
+
|
|
23
|
+
The file also exports, for each object, the labels the workspace gives it,
|
|
24
|
+
per column: display them rather than writing your own; a label
|
|
25
|
+
renamed in Livel is reflected on regeneration.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { health_contract_labels } from "@/livel-objects.gen"
|
|
29
|
+
|
|
30
|
+
health_contract_labels.label_plural // "Contrats santé"
|
|
31
|
+
health_contract_labels.fields.monthly_premium_cents // "Prime mensuelle"
|
|
32
|
+
health_contract_labels.choices.status[contract.status] // "En cours"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Declaring a resource
|
|
36
|
+
|
|
37
|
+
A resource is declared once per object, in
|
|
38
|
+
`src/lib/resources/<object>.resource.ts`, with `defineResource` from
|
|
39
|
+
`@zaptek/resource`, on the generated table:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { defineResource, eqFilter, searchFilter } from "@zaptek/resource"
|
|
43
|
+
import { health_contract } from "@/livel-objects.gen"
|
|
44
|
+
|
|
45
|
+
type Contract = typeof health_contract.$inferSelect & {
|
|
46
|
+
policyholder?: { last_name: string; first_name: string | null } | null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const Contracts = defineResource<
|
|
50
|
+
Contract,
|
|
51
|
+
{ status?: string; search?: string }
|
|
52
|
+
>()(health_contract, {
|
|
53
|
+
relations: { policyholder: health_contract.policyholder_id },
|
|
54
|
+
filters: {
|
|
55
|
+
status: eqFilter(health_contract.status),
|
|
56
|
+
search: searchFilter([health_contract.number]),
|
|
57
|
+
},
|
|
58
|
+
sort: { by: health_contract.created_at, order: "desc" },
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
- `relations`: the linked records a component can request, through their
|
|
63
|
+
`<field>_id` column (another object, a member or a group).
|
|
64
|
+
- `filters`: the filters the interface applies. `eqFilter`, `inFilter`,
|
|
65
|
+
`gteFilter`, `lteFilter`, `searchFilter`, `isNullFilter`, `containsFilter`,
|
|
66
|
+
`overlapsFilter`, `prefixFilter`. A filter without a value (`undefined`, `null`,
|
|
67
|
+
`""`, `[]`) does not apply.
|
|
68
|
+
- `sort`: the default order; a call can pass `sort_by` and `sort_order`.
|
|
69
|
+
|
|
70
|
+
## Reading
|
|
71
|
+
|
|
72
|
+
The hooks wait for the connection to Livel before reading.
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
const contracts = Contracts.useList({ status: "active", page: 1, limit: 20 }, [
|
|
76
|
+
"policyholder",
|
|
77
|
+
])
|
|
78
|
+
contracts.data.results // the page's records, with their policyholder
|
|
79
|
+
contracts.data.pagination.total // the total number of filtered records
|
|
80
|
+
|
|
81
|
+
const contract = Contracts.useDetail(id, ["policyholder"])
|
|
82
|
+
const total = Contracts.useCount({ status: "active" })
|
|
83
|
+
const pages = Contracts.useInfiniteList({ status: "active" })
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Aggregating
|
|
87
|
+
|
|
88
|
+
Totals are computed in the database, never by loading every record:
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
import { count, sum } from "@zaptek/resource"
|
|
92
|
+
|
|
93
|
+
const totals = Contracts.useAggregate(
|
|
94
|
+
{ status: "active" },
|
|
95
|
+
{
|
|
96
|
+
premiums_cents: sum(health_contract.monthly_premium_cents),
|
|
97
|
+
contracts: count(health_contract.id),
|
|
98
|
+
}
|
|
99
|
+
)
|
|
100
|
+
// totals.data: { premiums_cents: 26450, contracts: 3 }
|
|
101
|
+
|
|
102
|
+
const byStatus = Contracts.useAggregate(
|
|
103
|
+
{},
|
|
104
|
+
{ premiums_cents: sum(health_contract.monthly_premium_cents) },
|
|
105
|
+
{ by: { status: health_contract.status } }
|
|
106
|
+
)
|
|
107
|
+
// byStatus.data: [{ status: "active", premiums_cents: 26450 }, …]
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`sum`, `avg`, `min`, `max` and `count` are available.
|
|
111
|
+
|
|
112
|
+
## Writing
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
const create = Contracts.useCreate()
|
|
116
|
+
create.mutate({
|
|
117
|
+
number: "CS-0006",
|
|
118
|
+
policyholder_id: policyholderId,
|
|
119
|
+
monthly_premium_cents: 5200,
|
|
120
|
+
owner_workspace_group_id: groupId,
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
const update = Contracts.useUpdate()
|
|
124
|
+
update.mutate({ id, values: { status: "terminated" } })
|
|
125
|
+
|
|
126
|
+
const remove = Contracts.useDelete()
|
|
127
|
+
remove.mutate(id)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Each write refreshes the resource's reads. A new record
|
|
131
|
+
states its owning group; its owner is the signed-in member and
|
|
132
|
+
the workspace is set by the SDK. The result and the error of a write are read
|
|
133
|
+
on the mutation (`isSuccess`, `error`): the application displays its own
|
|
134
|
+
messages.
|
package/docs/files.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Using the workspace's files
|
|
2
|
+
|
|
3
|
+
The hooks are imported from `@zaptek/application-livel/hooks` and the buttons
|
|
4
|
+
from `@zaptek/application-livel/components`. They use the session of the
|
|
5
|
+
`LivelApplicationProvider` and the member's rights in the current workspace.
|
|
6
|
+
The application does not have to pass a token to Livel's procedures itself.
|
|
7
|
+
|
|
8
|
+
Keep the **file identifier** in a field of your entries,
|
|
9
|
+
for example in `data.attachment_file_id`. A private file URL is
|
|
10
|
+
temporary: do not store it as a durable reference.
|
|
11
|
+
|
|
12
|
+
| Hook | Use |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| `useFiles(filters?)` | Browse the accessible files. |
|
|
15
|
+
| `useFile(id)` | Read a file's information. |
|
|
16
|
+
| `useFileDownloadUrl(id)` | Get a URL to display or open a file. |
|
|
17
|
+
| `useDownloadFile()` | Request a fresh download URL on click. |
|
|
18
|
+
| `useUploadFiles()` | Upload files and record their metadata. |
|
|
19
|
+
| `useUpdateFile()` | Change a file's metadata. |
|
|
20
|
+
| `useDeleteFile()` / `useDeleteFiles()` | Delete one or more files. |
|
|
21
|
+
| `useChangeFileVisibility()` | Change the public or private visibility. |
|
|
22
|
+
|
|
23
|
+
Reads are TanStack Query queries; changes are TanStack Query
|
|
24
|
+
mutations. Network, authorization and validation errors
|
|
25
|
+
are exposed through `isError` and `error`.
|
|
26
|
+
|
|
27
|
+
`useFiles` accepts `page`, `limit`, `search` (on the name), `type` (exact MIME
|
|
28
|
+
type or a prefix such as `image/`), `is_public` and `enabled`. It returns
|
|
29
|
+
`{ results, pagination }`. `useFile(id)` returns the record or `null` if no
|
|
30
|
+
visible row matches this ID; a `null` ID disables its query.
|
|
31
|
+
|
|
32
|
+
## Uploading a file
|
|
33
|
+
|
|
34
|
+
`FileUploadButton` opens the native picker, disables the button during the upload
|
|
35
|
+
and passes the files to `useUploadFiles()`. Its `onUploaded` callback receives
|
|
36
|
+
the recorded files. `accept` and `multiple` configure the picker;
|
|
37
|
+
`uploadOptions` lets you choose, among others, `folder`, `is_public` or `metadata`.
|
|
38
|
+
The button also accepts the props of an HTML button, such as
|
|
39
|
+
`className` and `disabled`. `onUploadError` receives the error if the upload fails.
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { FileUploadButton } from "@zaptek/application-livel/components"
|
|
43
|
+
|
|
44
|
+
function AddAttachment({ onFileId }: { onFileId: (id: string) => void }) {
|
|
45
|
+
return (
|
|
46
|
+
<FileUploadButton
|
|
47
|
+
accept="image/*,.pdf"
|
|
48
|
+
uploadOptions={{ folder: "attachments" }}
|
|
49
|
+
onUploaded={files => {
|
|
50
|
+
const file = files[0]
|
|
51
|
+
if (file) onFileId(file.id)
|
|
52
|
+
}}
|
|
53
|
+
>
|
|
54
|
+
Joindre un fichier
|
|
55
|
+
</FileUploadButton>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
For a custom interface, `useUploadFiles().mutateAsync({ files })`
|
|
61
|
+
accepts an array of `File` and returns the recorded files. The upload follows
|
|
62
|
+
three steps: Livel prepares an authorized upload, the browser transfers the
|
|
63
|
+
bytes directly to Supabase Storage with the signed URL, then Livel confirms
|
|
64
|
+
the upload and creates the file record. The client never creates this
|
|
65
|
+
record directly in `files`.
|
|
66
|
+
|
|
67
|
+
For a batch, the files are uploaded one after the other. If one fails, the
|
|
68
|
+
previous ones stay recorded: the button passes these files to `onUploaded`
|
|
69
|
+
before calling `onUploadError`. With the hook alone, retrieve these successes with
|
|
70
|
+
`getCompletedUploads(error)` to keep their IDs.
|
|
71
|
+
|
|
72
|
+
## Opening or downloading
|
|
73
|
+
|
|
74
|
+
`useFileDownloadUrl(file_id)` provides a display URL for an accessible
|
|
75
|
+
file. It sends no request if `file_id` is `null` and returns a
|
|
76
|
+
TanStack Query query whose `data` is a URL string. For example, for an
|
|
77
|
+
image:
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
import { useFileDownloadUrl } from "@zaptek/application-livel/hooks"
|
|
81
|
+
|
|
82
|
+
function ImagePreview({ file_id }: { file_id: string | null }) {
|
|
83
|
+
const file_url = useFileDownloadUrl(file_id)
|
|
84
|
+
|
|
85
|
+
if (!file_id) return null
|
|
86
|
+
if (file_url.isPending) return <span>Chargement du fichier…</span>
|
|
87
|
+
if (file_url.isError) return <span>Fichier inaccessible.</span>
|
|
88
|
+
|
|
89
|
+
return <img src={file_url.data} alt="Pièce jointe" />
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
For an actual download, use `FileDownloadButton`. It requests a
|
|
94
|
+
new URL with an attachment disposition on each click, then triggers
|
|
95
|
+
the download. It accepts the props of an HTML button and
|
|
96
|
+
`onDownloadError` to react to a failure.
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { FileDownloadButton } from "@zaptek/application-livel/components"
|
|
100
|
+
|
|
101
|
+
<FileDownloadButton fileId={file_id}>Télécharger</FileDownloadButton>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
A private URL expires after one hour. For a display link that stays open
|
|
105
|
+
a long time, call `refetch()` on `useFileDownloadUrl()` to get a
|
|
106
|
+
new one; the download button makes this request on each click.
|
|
107
|
+
|
|
108
|
+
## Managing a file
|
|
109
|
+
|
|
110
|
+
`useFile(id)` and `useFiles(filters?)` let you read files without
|
|
111
|
+
rebuilding the Supabase queries in the template. The hooks for changes,
|
|
112
|
+
deletion and visibility changes use the same authenticated
|
|
113
|
+
client. Their success invalidates the affected file reads.
|
|
114
|
+
|
|
115
|
+
The SDK does not copy the full schema of the `files` table: it guarantees the
|
|
116
|
+
`id` field, and you can type the other fields your screen needs with
|
|
117
|
+
`useFiles<{ id: string; name: string; type: string }>()` or
|
|
118
|
+
`useFile<{ id: string; name: string }>(id)`.
|
|
119
|
+
|
|
120
|
+
| Mutation | Argument of `mutate` or `mutateAsync` |
|
|
121
|
+
| --- | --- |
|
|
122
|
+
| `useUpdateFile()` | `{ id, name?, metadata? }`; at least one field to change. |
|
|
123
|
+
| `useDeleteFile()` | The file identifier. |
|
|
124
|
+
| `useDeleteFiles()` | An array of identifiers. |
|
|
125
|
+
| `useChangeFileVisibility()` | `{ id, is_public }`. |
|
|
126
|
+
|
|
127
|
+
Changes are limited to the files authorized in the current
|
|
128
|
+
workspace. A refused mutation exposes an error; the application chooses how
|
|
129
|
+
to present it. An empty list or record can also simply mean
|
|
130
|
+
that no visible file matches the filters.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Getting started in a Livel application
|
|
2
|
+
|
|
3
|
+
The application must be displayed in Livel's iframe to receive the session
|
|
4
|
+
of the signed-in member. The application template already mounts
|
|
5
|
+
`LivelApplicationProvider` in `src/routes/__root.tsx`. If you start from
|
|
6
|
+
another React application, mount this provider once above the routes
|
|
7
|
+
that use the hooks:
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { LivelApplicationProvider } from "@zaptek/application-livel/components"
|
|
11
|
+
|
|
12
|
+
;<LivelApplicationProvider>{children}</LivelApplicationProvider>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The Vite server must receive five **public** values at startup, or at
|
|
16
|
+
build time for a deployed application:
|
|
17
|
+
|
|
18
|
+
```dotenv
|
|
19
|
+
VITE_LIVEL_PLATFORM_URL=https://livel.example.com
|
|
20
|
+
VITE_LIVEL_HOST_ORIGIN=https://livel.example.com
|
|
21
|
+
VITE_LIVEL_WORKSPACE_ID=00000000-0000-0000-0000-000000000000
|
|
22
|
+
VITE_SUPABASE_URL=https://example.supabase.co
|
|
23
|
+
VITE_SUPABASE_PUBLISHABLE_KEY=...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`VITE_LIVEL_PLATFORM_URL` is the public address of the platform, which
|
|
27
|
+
the application calls. `VITE_LIVEL_HOST_ORIGIN` is the origin of the Livel page
|
|
28
|
+
that displays the application in its iframe: only its messages are accepted.
|
|
29
|
+
Both are identical in production; in local development, the page is
|
|
30
|
+
opened on `localhost` while the API goes through a public address.
|
|
31
|
+
|
|
32
|
+
During development in the sandbox, Livel provides them to the server. The
|
|
33
|
+
`workspace_id` chooses the workspace to display when a member has
|
|
34
|
+
several; it does not represent the application. Never put a private key,
|
|
35
|
+
a refresh token or an access token in a `VITE_` variable: Vite embeds it in the
|
|
36
|
+
browser code.
|
|
37
|
+
|
|
38
|
+
The ready-to-display diagnostic component checks access to the workspace's
|
|
39
|
+
objects and the identity recognized by Livel's API:
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { LivelDataConnectionStatus } from "@zaptek/application-livel/components"
|
|
43
|
+
|
|
44
|
+
;<LivelDataConnectionStatus />
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
It shows "Données connectées" only after a successful read, including
|
|
48
|
+
when no object exists yet. It separately shows "API connectée" after
|
|
49
|
+
an authenticated call to `me`. It distinguishes waiting, a missing session and the
|
|
50
|
+
errors of each connection. It accepts a `className` property for its
|
|
51
|
+
container and requires no CSS import. A successful `me` does not guarantee
|
|
52
|
+
access to the objects or to other resources.
|
|
53
|
+
|
|
54
|
+
A first component reads an object's records with its resource (see
|
|
55
|
+
[Data](data.md) to declare it):
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
import { useLivelApplication } from "@zaptek/application-livel/hooks"
|
|
59
|
+
import { Contracts } from "@/lib/resources/contracts.resource"
|
|
60
|
+
|
|
61
|
+
export function ActiveContracts() {
|
|
62
|
+
const { status } = useLivelApplication()
|
|
63
|
+
const contracts = Contracts.useList({ status: "active" })
|
|
64
|
+
|
|
65
|
+
if (status === "connecting") return <p>Connexion en cours…</p>
|
|
66
|
+
if (status === "unavailable")
|
|
67
|
+
return <p>Ouvrez cette application depuis Livel.</p>
|
|
68
|
+
if (contracts.isPending) return <p>Chargement des contrats…</p>
|
|
69
|
+
if (contracts.isError) return <p>Impossible de charger les contrats.</p>
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<ul>
|
|
73
|
+
{contracts.data.results.map(contract => (
|
|
74
|
+
<li key={contract.id}>{contract.number}</li>
|
|
75
|
+
))}
|
|
76
|
+
</ul>
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The hooks are TanStack Query queries: `data`, `isPending`, `isError` and
|
|
82
|
+
`error` follow its contract. The SDK starts the queries only once
|
|
83
|
+
connected to Livel.
|
package/docs/rpc.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Calling the RPC API
|
|
2
|
+
|
|
3
|
+
In a React component, `useRpcQuery` handles the API call, its TanStack
|
|
4
|
+
Query state and its cache key. It waits for the iframe connection and uses the
|
|
5
|
+
current token and the workspace identifier:
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useRpcQuery } from "@zaptek/application-livel/hooks"
|
|
9
|
+
|
|
10
|
+
function useIdentity() {
|
|
11
|
+
return useRpcQuery({ path: ["core", "me"], retry: false })
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`path` designates the path in the router and `input` provides the procedure's
|
|
16
|
+
parameters. The input must be serializable: it is part of the cache key.
|
|
17
|
+
The result remains `unknown`; check its shape before using its fields.
|
|
18
|
+
The usual TanStack Query options, such as `retry`, `staleTime` and `select`, are
|
|
19
|
+
available. `LivelDataConnectionStatus` shows a validation of the `me`
|
|
20
|
+
response before displaying the identity recognized by the API.
|
|
21
|
+
|
|
22
|
+
For a call outside a read hook, the client returned by
|
|
23
|
+
`useLivelApplication()` exposes `client.rpc`. It passes the same authentication
|
|
24
|
+
on each request:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
const identity = await client.rpc.call(["core", "me"])
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`call` accepts the router path and the procedure's input. Its result is
|
|
31
|
+
also `unknown`.
|
|
32
|
+
|
|
33
|
+
For a transport independent of the iframe client, `createRpcCaller` receives
|
|
34
|
+
the router URL and an optional function that produces the headers of each
|
|
35
|
+
request:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createRpcCaller } from "@zaptek/application-livel"
|
|
39
|
+
|
|
40
|
+
const rpc = createRpcCaller({
|
|
41
|
+
url: new URL("/api/rpc", import.meta.env.VITE_LIVEL_PLATFORM_URL).toString(),
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const result = await rpc.call(["system", "health_check"])
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This low-level caller does not handle the iframe session. Its `headers` function
|
|
48
|
+
is called again on each call; protected procedures require the headers
|
|
49
|
+
expected by the platform. The application's `client.rpc` already provides them.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Troubleshooting
|
|
2
|
+
|
|
3
|
+
| Symptom | Check |
|
|
4
|
+
| --- | --- |
|
|
5
|
+
| `status` stays `connecting`, then becomes `unavailable` | Is the application open in the Livel iframe? Does the parent respond? Does `VITE_LIVEL_HOST_ORIGIN` match the origin of the Livel page open in the browser? |
|
|
6
|
+
| `Missing application environment variable: …` | Is the public variable named in the error provided to the Vite server? Restart it after an environment change. |
|
|
7
|
+
| The hooks stay disabled | Is the provider mounted? Is `status` `ready`? Does the hook have `enabled: false`, or a null id for `useEntry`? |
|
|
8
|
+
| A request fails | Read `error` during development. Check the Supabase connection, the member's permissions/RLS and the filters; do not show the raw SQL error to users. |
|
|
9
|
+
| The list is empty | Check the resource's filters, the requested page and the visibility granted by RLS (the member only sees the records their rights open to them). |
|
|
10
|
+
| The installed code does not match the documentation | Compare the installed version of `@zaptek/application-livel` with the expected one and read its `CHANGELOG.md` in `node_modules`. An already created application does not automatically receive the template's changes. |
|
|
11
|
+
|
|
12
|
+
The SDK does not provide a token in a standalone tab opened outside Livel.
|
|
13
|
+
An HTTP response from the development server only confirms that it serves
|
|
14
|
+
the interface; it validates neither the authentication exchange nor access to the
|
|
15
|
+
data.
|