@protobase/cli 0.22.13 → 0.23.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/dist/main.js +2 -0
- package/dist/package.js +1 -1
- package/dist/skill/install.js +29 -0
- package/dist/skill/register.js +14 -0
- package/package.json +8 -7
- package/skills/protobase/SKILL.md +89 -0
- package/skills/protobase/references/getting-started.md +78 -0
- package/skills/protobase/references/guides/api-functions.md +240 -0
- package/skills/protobase/references/guides/billing-page.md +288 -0
- package/skills/protobase/references/guides/create-page.md +108 -0
- package/skills/protobase/references/guides/deploy.md +52 -0
- package/skills/protobase/references/guides/export-a-list.md +44 -0
- package/skills/protobase/references/guides/field-type.md +131 -0
- package/skills/protobase/references/guides/first-composed-page.md +230 -0
- package/skills/protobase/references/guides/first-plugin.md +166 -0
- package/skills/protobase/references/guides/import-records.md +43 -0
- package/skills/protobase/references/guides/plugin-build.md +64 -0
- package/skills/protobase/references/guides/record-card.md +70 -0
- package/skills/protobase/references/guides/remote-resources.md +185 -0
- package/skills/protobase/references/guides/table-to-admin.md +205 -0
- package/skills/protobase/references/guides/theme-an-app.md +87 -0
- package/skills/protobase/references/reference/access.md +115 -0
- package/skills/protobase/references/reference/ai.md +106 -0
- package/skills/protobase/references/reference/api.md +231 -0
- package/skills/protobase/references/reference/architecture.md +63 -0
- package/skills/protobase/references/reference/assistant.md +157 -0
- package/skills/protobase/references/reference/auth.md +462 -0
- package/skills/protobase/references/reference/blocks.md +341 -0
- package/skills/protobase/references/reference/bottom-bar.md +44 -0
- package/skills/protobase/references/reference/cli.md +493 -0
- package/skills/protobase/references/reference/client.md +38 -0
- package/skills/protobase/references/reference/comments.md +83 -0
- package/skills/protobase/references/reference/custom-components.md +287 -0
- package/skills/protobase/references/reference/data-config.md +266 -0
- package/skills/protobase/references/reference/email.md +166 -0
- package/skills/protobase/references/reference/events.md +121 -0
- package/skills/protobase/references/reference/files.md +291 -0
- package/skills/protobase/references/reference/forms.md +133 -0
- package/skills/protobase/references/reference/history.md +86 -0
- package/skills/protobase/references/reference/jobs.md +174 -0
- package/skills/protobase/references/reference/layouts.md +126 -0
- package/skills/protobase/references/reference/plugins.md +328 -0
- package/skills/protobase/references/reference/remote-resources.md +198 -0
- package/skills/protobase/references/reference/sidebar.md +51 -0
- package/skills/protobase/references/reference/theme.md +132 -0
- package/skills/protobase/references/reference/ui-config.md +880 -0
- package/skills/protobase/references/reference/user-menu.md +38 -0
- package/skills/protobase/references/reference/versioning.md +91 -0
- package/skills/protobase/references/reference/webhooks.md +136 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/plugins.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Plugins
|
|
4
|
+
|
|
5
|
+
React code the admin app loads when it starts instead of building it in, so it keeps working while Protobase moves on.
|
|
6
|
+
|
|
7
|
+
A plugin is a `protobase.ui.tsx` that the admin app loads at runtime instead of building it in. It provides what a project's own `protobase.ui.tsx` provides: [custom components](custom-components.md)) for list displays, record sections, page elements, record cards and the assistant's widget parts, [field types](#field-types), action handlers and shell slots. It is built once, on its own, and served from any URL; the app lists it, and every app on a version that fits loads it.
|
|
8
|
+
|
|
9
|
+
A project's own `protobase.ui.tsx` is a plugin too: `protobase build` builds it as the plugin `app` beside a prebuilt admin app, and builds each `--extend` folder's the same way. So everything below holds for the code an app's own developers write, for code shared across apps, and for code Protobase adds.
|
|
10
|
+
|
|
11
|
+
Plugins are written by an app's developers and by Protobase itself, and they run in the app with the signed-in user's rights, like the app's own code, so load only plugins you trust.
|
|
12
|
+
|
|
13
|
+
[Your first plugin](../guides/first-plugin.md)) builds one, step by step. [Adding a field type](../guides/field-type.md)), [A custom record card](../guides/record-card.md)) and [Theming an app](../guides/theme-an-app.md)) add to an app, and [Moving onto the plugin build](../guides/plugin-build.md)) moves an existing project's `protobase.ui.tsx` onto it.
|
|
14
|
+
|
|
15
|
+
## Who maintains what
|
|
16
|
+
|
|
17
|
+
Plugins split the work of keeping an app current. Protobase maintains the admin app, the API and the server; an app's developers maintain their plugins, and those seldom need work. A plugin is built against the plugin API, which releases add to but never break, so the same build keeps loading as the app moves to newer releases, and gets their fixes and new features on the way. Nobody ports it, rebuilds it or tests it again for each release; it needs a new build only when Protobase starts a new plugin API (see [Versions](#versions)).
|
|
18
|
+
|
|
19
|
+
What still follows Protobase's releases is the rest of the bundle. Below 1.0 a new minor version may change how `data.ts`, `ui.ts` and the server config are written, and a runtime serves only bundles built by its own minor, so moving to a new minor means running `protobase build` again with that version, after any change the release notes ask of the config (see [Bundles and runtimes](versioning.md#bundles-and-runtimes))). The plugins' code stays as it is. Within a minor, where features ship as patches below 1.0, a host that serves the newer release's [shell](#the-shell) gives the app its fixes with nothing rebuilt.
|
|
20
|
+
|
|
21
|
+
## How a plugin stays working
|
|
22
|
+
|
|
23
|
+
Code built together with the app is frozen at the app it was built with: a newer release of the app needs a new build of both. A plugin is built against a promise instead, and the app supplies the rest when it loads it.
|
|
24
|
+
|
|
25
|
+
1. **The app is built once per release.** The admin app, the shell, is the same for every project. Each release of `@protobase/ui` ships it prebuilt, and `protobase build` copies it into the bundle beside the project's plugins and theme. A host that has the shell of a newer release can serve that one in its place (see [the shell](#the-shell)), so an app gets the release's fixes without a rebuild of its own code.
|
|
26
|
+
2. **The app shares its modules.** The admin app hands four modules to plugins: `react`, `react/jsx-runtime`, `react-dom` and `@protobase/ui`. Its `index.html` maps those names to the app's own files with an [import map](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script/type/importmap), so a plugin's `import { useList } from '@protobase/ui'` runs on the app's copy: one React, one query cache, one signed-in session and the app's version of every hook and component. A plugin carries only its own code, and gets the fixes of every release the app moves to.
|
|
27
|
+
3. **The plugin API is a promise.** What a plugin can import, the four modules and every export of `@protobase/ui`, is the plugin API, numbered: `pluginApi` is 1. Releases add to it but never remove or change what is in it; only a new plugin API does, as for a new major version of React. So a plugin keeps working on every release of the plugin API it was built for, with no rebuild. Below 1.0 a minor release may change how a project's config is written, but not the plugin API.
|
|
28
|
+
4. **The app checks before it loads.** A plugin's `plugin.json` says which plugin API and which Protobase versions it is built for. The app loads a plugin only when it has that plugin API and its own version is in the plugin's range; it never runs the code of one that does not fit.
|
|
29
|
+
5. **A plugin that does not fit is turned off, not the app.** Each place it would fill draws a fallback, the app's admins see why it is off, and everything else works as before. A component that throws while drawing shows its fallback in its own place.
|
|
30
|
+
6. **The look is values, not rules.** A project themes the app with a [`protobase.theme.css`](theme.md)) that holds only values of the app's tokens, and a plugin's stylesheet holds only utilities that read those tokens. Neither reaches into the app's markup, so neither breaks when the app's markup changes.
|
|
31
|
+
|
|
32
|
+
## Extension points
|
|
33
|
+
|
|
34
|
+
Each place a plugin can fill is an extension point. The config names it, or a plugin adds to it, and each defines the props the app passes, what the app draws when the plugin is missing or off, and where its error boundary sits, so one failing part never takes more than its own place.
|
|
35
|
+
|
|
36
|
+
| Extension point | Named by | Registered as | Gets | Missing, off or throwing |
|
|
37
|
+
| --- | --- | --- | --- | --- |
|
|
38
|
+
| View: [list display](custom-components.md#list-displays)), [record section](custom-components.md#record-sections)), [page element](custom-components.md#components)) | `component('Name')` in `d.custom`, `l.custom` or a layout | A PascalCase export of `protobase.ui.tsx` | Its props, field references as names; `useListDisplay()`, `useLayoutRecord()` | A warning in its place: "No component is registered as *Name*" or "*Name* could not be drawn." |
|
|
39
|
+
| [Record card](custom-components.md#record-cards)) | `d.cards(fields, { card: component('Name') })` | A PascalCase export | `{ row }` | The built-in card: title, subtitle, badge, meta fields |
|
|
40
|
+
| [Field type](#field-types) | `r.field.widget('name', props)` in a view's `fields` | `fields: { name: { of, display, editor?, cell? } }` | `{ field, value, props }`, and `onChange`, `invalid` for the editor | The field as it shows without one |
|
|
41
|
+
| Widget part (the [assistant](assistant.md#widget-parts))) | A widget part's name | A PascalCase export | The part's props | The part's fallback card |
|
|
42
|
+
| [Action handler](custom-components.md#action-handlers)) | `a.action('name')` in a view | `actions: { name }` | `ActionContext` | The button is disabled |
|
|
43
|
+
| [Shell slot](custom-components.md#shell-slots)) | Nothing: drawn on every page | `shell: { actions, rightPanel, logo }` | Nothing; `logo` gets `compact` and `workspace` | Not drawn; Protobase's logo for `logo` |
|
|
44
|
+
| [Before sign-in](#before-sign-in) | `beforeSignIn: true` where the plugin is listed | `beforeSignIn` | `children`, `params`, `removeParams`, `signedIn` | The pages as they are without it |
|
|
45
|
+
|
|
46
|
+
Components and cards share one namespace of names, which `component('Name')` joins to the export; field types and action handlers each have their own. Props that name fields go through the same walker as everything else in `/api/meta`, so a user who may not read a field never gets it in a component's props; a field type's `props` are plain data, and a field the user may not read is left out of the view, field type and all.
|
|
47
|
+
|
|
48
|
+
New extension points are new optional keys of `definePlugin`, so adding one never changes what a plugin built before it does.
|
|
49
|
+
|
|
50
|
+
## Writing a plugin
|
|
51
|
+
|
|
52
|
+
A plugin is a folder with a `protobase.ui.tsx` and a `package.json` that depends on `@protobase/ui`, the version it is built for, and on `react`:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
sales-tools/
|
|
56
|
+
package.json
|
|
57
|
+
protobase.ui.tsx
|
|
58
|
+
components/draft-orders.tsx
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
// protobase.ui.tsx
|
|
63
|
+
import { definePlugin } from '@protobase/ui'
|
|
64
|
+
|
|
65
|
+
export { DraftOrders } from './components/draft-orders'
|
|
66
|
+
|
|
67
|
+
export default definePlugin({ name: 'sales-tools', actions: { remindCustomer } })
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
It is written as a project's `protobase.ui.tsx` is: each component exported under the PascalCase name the config declares, from a file of its own, and the rest in the default export, `definePlugin({ name, actions, fields, shell, beforeSignIn, extends })` (`defineUi` is the same function). Its components use the hooks and components of `@protobase/ui`, which read the API as the signed-in user, under the same access rules as everything else.
|
|
71
|
+
|
|
72
|
+
**Imports.** `react`, `react/jsx-runtime` (the JSX), `react-dom` and `@protobase/ui`, by those names, come from the app. Everything else a plugin imports, its own files, icons or a chart library, is built into it. Other modules of those packages, such as `react-dom/client` or a file inside `@protobase/ui`, fail the build: a copy of its own would not work with the app's. Read data with `@protobase/ui`'s hooks (`useList`, `useRecord`, `useClient`), which share the app's cache: a data library of the plugin's own keeps a cache of its own.
|
|
73
|
+
|
|
74
|
+
**Styling.** Use Tailwind classes, as the app's own code does. The plugin's stylesheet holds the utilities its files use and nothing else, no reset and no global style, and each reads the app's [tokens](theme.md)): `bg-primary` is the app's primary color and `rounded-md` its radius, whatever the app's version and whatever its theme, and a token the app does not set falls back to Tailwind's default.
|
|
75
|
+
|
|
76
|
+
**Top-level code.** The build loads `protobase.ui.tsx` once in Node to read its field types, so code at the top level of its modules runs there. Keep browser-only work (`window`, `document`, `localStorage`) inside components and handlers.
|
|
77
|
+
|
|
78
|
+
**Its own settings.** `usePlugin()` gives a plugin's components the plugin they belong to:
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
import { usePlugin } from '@protobase/ui'
|
|
82
|
+
|
|
83
|
+
export const Greeting = () => {
|
|
84
|
+
const { name, version, url, settings } = usePlugin()
|
|
85
|
+
return <p>{`${name} ${version}: ${String(settings.greeting ?? 'hello')}`}</p>
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| `usePlugin()` | |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| `name` | The plugin's name, from its `plugin.json`. |
|
|
92
|
+
| `version` | Its own version, from its `package.json`, when it has one. |
|
|
93
|
+
| `url` | Its `plugin.json`: `new URL('./data.json', url)` finds a file served beside it. |
|
|
94
|
+
| `settings` | What the app gives it in `options.plugins` (see [loading](#loading)), `{}` otherwise. |
|
|
95
|
+
|
|
96
|
+
It works in the components, field types, shell slots and `beforeSignIn` a plugin registers and everything they render, and throws anywhere else.
|
|
97
|
+
|
|
98
|
+
## Declaring its components in the config
|
|
99
|
+
|
|
100
|
+
A layout or view declares a plugin's component as it declares any custom component, with `component()` from `@protobase/layout`, naming the plugin that provides it:
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
export const DraftOrders = component('DraftOrders', { plugin: 'sales-tools' })
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The name is what joins the two, as for a project's component. `plugin` tells `protobase build` that the project's `protobase.ui.tsx` does not have it, so the build does not fail on it (see [Components](custom-components.md#components))). Without the plugin, the app draws the component's fallback in its place.
|
|
107
|
+
|
|
108
|
+
## Field types
|
|
109
|
+
|
|
110
|
+
A field type shows and edits one kind of value everywhere the app shows a field: a star rating for a score, steps for a status, a color swatch for a hex code. A plugin registers it by name, with the storage types it shows, and a view picks it for a field:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
// protobase.ui.tsx
|
|
114
|
+
import { definePlugin } from '@protobase/ui'
|
|
115
|
+
import { StepsDisplay, StepsEditor } from './components/steps'
|
|
116
|
+
|
|
117
|
+
export default definePlugin({ fields: { steps: { of: ['enum'], display: StepsDisplay, editor: StepsEditor } } })
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
// config/repairs/ui.ts
|
|
122
|
+
view<typeof repairs>('repairs').fields((r) => ({ status: r.status.widget('steps', { done: 'collected' }) }))
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
| `fields.<name>` | |
|
|
126
|
+
| --- | --- |
|
|
127
|
+
| `of` | The storage types it shows, from `f.*`: `'text'`, `'integer'`, `'decimal'`, `'enum'`, `'boolean'`, `'date'`, ... A field of another type shows as it would without the field type. |
|
|
128
|
+
| `display` | Shows a value: on the record page when the field may not change, in the record's summary, on cards and in page elements. |
|
|
129
|
+
| `editor` | Edits a value, on the record page and the create form, when the field may change. Without it, the field keeps its built-in input. Files and relations are uploaded and chosen as the app does it; their field types only show them. |
|
|
130
|
+
| `cell` | Shows a value in a table cell; `display` when left out. |
|
|
131
|
+
|
|
132
|
+
Each part gets:
|
|
133
|
+
|
|
134
|
+
| Props | |
|
|
135
|
+
| --- | --- |
|
|
136
|
+
| `field` | The field's model: `name`, `type`, `nullable`, `enumValues` and the rest of `FieldModel`. |
|
|
137
|
+
| `value` | The value as the API gives it, `null` when empty: a number for an integer, text for a decimal, a boolean for a boolean. |
|
|
138
|
+
| `props` | What the view gives in `.widget(name, props)`, `{}` without; plain data. |
|
|
139
|
+
| `onChange(value)` | The editor's only: sets the value, as the API takes it, `null` to empty it. The record is saved with the page's Save. |
|
|
140
|
+
| `invalid` | The editor's only: whether the value it holds is refused, as the page shows under the field. |
|
|
141
|
+
|
|
142
|
+
`FieldWidgetProps<Value, Props>` and `FieldWidgetEditorProps<Value, Props>` from `@protobase/ui` type them, and `FieldWidget` a whole field type.
|
|
143
|
+
|
|
144
|
+
**Checks.** The field type's storage types are checked against the views twice. `protobase build` loads each of the bundle's plugins once in Node and writes the storage types of each field type it registers into `plugin.json` and the bundle's manifest; the serve runtime refuses to start when a view shows a field with one of them that does not show its storage type, naming the view, the field and what the field type shows. The app checks again when it draws: a field type no loaded plugin registers, or one that does not show the field's storage type, leaves the field as it shows without one. A field type a remote plugin provides is checked only there, since the server never loads remote code.
|
|
145
|
+
|
|
146
|
+
**Fallbacks.** Each part draws inside an error boundary whose fallback is the field's built-in rendering, so a field type that throws costs only its own field, which still shows its value. A plugin that is off leaves every field it would draw as it shows without it.
|
|
147
|
+
|
|
148
|
+
[Adding a field type](../guides/field-type.md)) builds one step by step.
|
|
149
|
+
|
|
150
|
+
## Building
|
|
151
|
+
|
|
152
|
+
```sh
|
|
153
|
+
protobase build-plugin [dir] [--out <dir>] [--name <name>]
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Builds the plugin in `dir` (default: the current folder) into `--out` (default: `dist` in it):
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
dist/
|
|
160
|
+
plugin.json
|
|
161
|
+
assets/index-<hash>.js the module: protobase.ui.tsx and what it imports, but the shared modules
|
|
162
|
+
assets/styles-<hash>.css the utilities its files use
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The output folder is emptied first, so it must not hold the plugin's own files. The module may come with chunks of its own (`import()` in the plugin), beside it in `assets/`. See [build-plugin](cli.md#build-plugin)) for the details. `protobase build` builds the project's own `protobase.ui.tsx` and each extension's the same way, into the bundle's `public/plugins/<name>/`.
|
|
166
|
+
|
|
167
|
+
`plugin.json` tells the app what it loads:
|
|
168
|
+
|
|
169
|
+
```json
|
|
170
|
+
{
|
|
171
|
+
"name": "sales-tools",
|
|
172
|
+
"version": "1.0.0",
|
|
173
|
+
"api": 1,
|
|
174
|
+
"protobase": ">=0.12.0",
|
|
175
|
+
"entry": "assets/index-DDnoullN.js",
|
|
176
|
+
"css": "assets/styles-CToS5pKa.css",
|
|
177
|
+
"fields": { "steps": ["enum"] }
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
| | |
|
|
182
|
+
| --- | --- |
|
|
183
|
+
| `name` | The plugin's name: `--name`, else its `package.json`'s `name`, else its folder's. A project's own is `app`. |
|
|
184
|
+
| `version` | Its `package.json`'s `version`, when it has one. |
|
|
185
|
+
| `api` | The plugin API of the `@protobase/ui` it is built with. |
|
|
186
|
+
| `protobase` | The Protobase versions it runs on: from that `@protobase/ui`'s version on, unless its `package.json` says otherwise (see [versions](#versions)). |
|
|
187
|
+
| `entry`, `css` | The module and the stylesheet, relative to `plugin.json`. |
|
|
188
|
+
| `fields` | The field types it registers, each with the storage types it shows. Only when it registers some. |
|
|
189
|
+
|
|
190
|
+
## Loading
|
|
191
|
+
|
|
192
|
+
The app loads the plugins `/api/meta` names to the signed-in user, in this order:
|
|
193
|
+
|
|
194
|
+
1. the bundle's own, from its manifest: each `--extend` folder's, then the project's (`app`), for everyone;
|
|
195
|
+
2. those `createAdmin`'s `options.plugins` lists, in `protobase.config.ts`;
|
|
196
|
+
3. those `PROTOBASE_PLUGINS` lists: URLs separated by commas, which a hosting platform sets for the plugins it adds to its apps;
|
|
197
|
+
4. those `PROTOBASE_PLUGINS_BEFORE_SIGN_IN` lists, the same way, which also load [before sign-in](#before-sign-in).
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
// protobase.config.ts
|
|
201
|
+
export default {
|
|
202
|
+
config,
|
|
203
|
+
options: {
|
|
204
|
+
plugins: [
|
|
205
|
+
{ url: 'https://plugins.example.com/sales-tools/plugin.json' },
|
|
206
|
+
{ url: '/plugins/board/plugin.json', settings: { columns: 3 }, roles: ['admin', 'sales'] },
|
|
207
|
+
],
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
| `options.plugins[]` | |
|
|
213
|
+
| --- | --- |
|
|
214
|
+
| `url` | The plugin's `plugin.json`: an `https://` or `http://` URL, or a path on the app's own origin. |
|
|
215
|
+
| `settings` | Data the plugin reads with `usePlugin()`. |
|
|
216
|
+
| `roles` | When set, only callers with one of these roles load it; without, everyone does. This decides what the app loads, not what anyone may do: the API's access rules still decide that. |
|
|
217
|
+
| `beforeSignIn` | `true` loads it [before sign-in](#before-sign-in) too, for everyone; it cannot have `roles`. Off by default. |
|
|
218
|
+
|
|
219
|
+
`options.plugins: false` turns off all but the first; the bundle's own plugins are the app's own UI and always load. A URL that is not one stops the server at startup, naming where it came from, and so does a plugin with both `roles` and `beforeSignIn`. Each URL is loaded once, where it is first listed; one `PROTOBASE_PLUGINS_BEFORE_SIGN_IN` lists loads before sign-in wherever it is listed.
|
|
220
|
+
|
|
221
|
+
When the user signs in, or before for a plugin that loads [before sign-in](#before-sign-in), the app reads each `plugin.json`, then loads the module and the stylesheet of each plugin that fits, and draws once they have loaded, or after ten seconds at most: a slower plugin comes in when it arrives. So a host serves them like this:
|
|
222
|
+
|
|
223
|
+
- `plugin.json` without caching, or with a short one (`Cache-Control: no-cache`): a new build of the plugin reaches every app on its next load.
|
|
224
|
+
- The files in `assets/`, named by their content, forever (`Cache-Control: public, max-age=31536000, immutable`).
|
|
225
|
+
- From another origin than the app's, with `Access-Control-Allow-Origin` (`*` is fine for public files), since the browser loads modules and `plugin.json` with CORS.
|
|
226
|
+
|
|
227
|
+
The plugins are merged in the order listed. Two of them cannot define one name: a plugin that defines a component, action handler or field type an earlier one defines is turned off, and the earlier one keeps it. So the project's own names win over a platform's, and within a bundle the build already fails on such a name.
|
|
228
|
+
|
|
229
|
+
`protobase dev` loads plugins the same way, its page sharing the same four modules. The project's own `protobase.ui.tsx` is built into the dev page with hot reload instead, merged after the plugins, and keeps its names over theirs.
|
|
230
|
+
|
|
231
|
+
The components a [remote resource](remote-resources.md#custom-components))'s view names are drawn from the same plugins, by name, so whoever runs a source lists the plugin that provides them with it. Inside one, `useList` and `useRecord` read the source for its resources, and `useResourceClient(resource)` gives the client of the API that serves a resource.
|
|
232
|
+
|
|
233
|
+
## Before sign-in
|
|
234
|
+
|
|
235
|
+
Plugins load once someone has signed in, or for a visitor of a project whose anonymous role reads a resource, so no plugin code runs for someone signed out: they get the sign-in page. A plugin listed with `beforeSignIn: true`, or in `PROTOBASE_PLUGINS_BEFORE_SIGN_IN`, loads before that. The sign-in status (`GET /api/auth/status`) names it to everyone, as `plugins`, and the app loads it first and waits for it, as it waits for the others. Its `beforeSignIn` component is drawn around what someone signed out sees: the sign-in page, or the app a visitor browses. Once they are in, it is no longer drawn, and the plugin stays loaded: `/api/meta` names it to everyone, and the app merges its other parts as any plugin's, without loading it again.
|
|
236
|
+
|
|
237
|
+
So a plugin can sign someone in from a link, such as one a platform hands out with a one-time code:
|
|
238
|
+
|
|
239
|
+
```tsx
|
|
240
|
+
// protobase.ui.tsx
|
|
241
|
+
import { useEffect, useRef, useState } from 'react'
|
|
242
|
+
import { definePlugin, type BeforeSignInProps } from '@protobase/ui'
|
|
243
|
+
|
|
244
|
+
const GuestAccess = ({ children, params, removeParams, signedIn }: BeforeSignInProps) => {
|
|
245
|
+
const [code] = useState(() => params.get('access'))
|
|
246
|
+
const [failed, setFailed] = useState(false)
|
|
247
|
+
const started = useRef(false)
|
|
248
|
+
useEffect(() => {
|
|
249
|
+
if (!code || started.current) return
|
|
250
|
+
started.current = true
|
|
251
|
+
removeParams(['access'])
|
|
252
|
+
// An endpoint on the app's origin that trades the code for the session cookie.
|
|
253
|
+
void fetch('/guest/exchange', { method: 'POST', body: code })
|
|
254
|
+
.then(signedIn)
|
|
255
|
+
.then((entered) => setFailed(!entered))
|
|
256
|
+
}, [code, removeParams, signedIn])
|
|
257
|
+
if (code && !failed) return <p role="status">Signing you in…</p>
|
|
258
|
+
return children
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export default definePlugin({ name: 'guest', beforeSignIn: GuestAccess })
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
| `BeforeSignInProps` | |
|
|
265
|
+
| --- | --- |
|
|
266
|
+
| `children` | What the app shows someone signed out: the sign-in page, or the app a visitor browses. Draw it when the plugin has nothing to do. |
|
|
267
|
+
| `params` | The query parameters of the page's address. |
|
|
268
|
+
| `removeParams(names)` | Takes those query parameters out of the address, in place of it in the tab's history, as the app does with a used [sign-in link](auth.md#sign-in-links-and-connected-accounts)). |
|
|
269
|
+
| `signedIn()` | Enters the app with a session something other than the sign-in page started: it reads the session cookie again and shows the app signed in, as the sign-in page does, with no reload. Resolves `false`, and changes nothing, when there is no session. |
|
|
270
|
+
|
|
271
|
+
The plugin starts the session itself, with a request that sets the app's session cookie on the app's origin; `signedIn()` only tells the app. Several plugins' `beforeSignIn` are drawn one around the other, the first listed outermost, and one that throws leaves the pages as they are. Like every plugin, one loaded before sign-in runs with the rights of whoever opens the app, here someone not signed in, and its `plugin.json` and files are served to them, so list only plugins you trust there.
|
|
272
|
+
|
|
273
|
+
## The shell
|
|
274
|
+
|
|
275
|
+
The shell is the admin app: `index.html` and its `assets/`, the same for every project on a release. Each release ships it prebuilt in `@protobase/ui`'s `dist/shell/` and in the [protobase image](../guides/deploy.md#images)) at `/opt/protobase/shell/`, with a `protobase.shell.json` naming its Protobase version and plugin API. A bundle carries a copy, so any static host can serve it, and its manifest names that copy's version and paths (`shell` in [the manifest](cli.md#the-manifest))).
|
|
276
|
+
|
|
277
|
+
A host that keeps the shells of its releases can serve the shell of a newer one for those paths, when every plugin of the bundle loads in it; the plugins, the theme and the API stay the bundle's. `protobase serve` does this with its own shell (see [serve](cli.md#serve))). The app then has the newer release's fixes with nothing rebuilt; only a change of the server config's format, a new minor below 1.0, still needs a [rebuild](versioning.md#bundles-and-runtimes)).
|
|
278
|
+
|
|
279
|
+
## Versions
|
|
280
|
+
|
|
281
|
+
A plugin is built for one plugin API and a range of Protobase versions, both written by `build-plugin` into `plugin.json`:
|
|
282
|
+
|
|
283
|
+
- `api` is the plugin API of the `@protobase/ui` installed for the plugin.
|
|
284
|
+
- `protobase` is `>=` that `@protobase/ui`'s version: the plugin may use anything that version has. To run on older releases too, name the range in the plugin's `package.json`:
|
|
285
|
+
|
|
286
|
+
```json
|
|
287
|
+
{ "engines": { "protobase": ">=0.12.0" } }
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
A range is comparators separated by spaces, all of which must hold: `>=`, `>`, `<=`, `<` or `=` and an `x.y.z` version, such as `>=0.12.0 <2.0.0`. The build refuses one that leaves out the version it is built with. If the plugin uses something an older release in its range does not have, it fails to load there and is turned off, as described below.
|
|
291
|
+
|
|
292
|
+
The app loads a plugin when the plugin's `api` is its own and its version is in the plugin's `protobase` range. Protobase keeps the plugin API through its releases and starts a new one only when a plugin built for the old one could break:
|
|
293
|
+
|
|
294
|
+
- an export of `@protobase/ui` removed, or changed in a way its users would notice;
|
|
295
|
+
- an extension point's props changed in a way a plugin would notice;
|
|
296
|
+
- a module added to or removed from the four the app shares;
|
|
297
|
+
- a new major version of React, which the app and every plugin share.
|
|
298
|
+
|
|
299
|
+
A plugin built for an older plugin API needs a new build against a `@protobase/ui` of the new one. `pluginApi` and `protobaseVersion`, exported by `@protobase/ui`, give a plugin the app's own at runtime.
|
|
300
|
+
|
|
301
|
+
## When a plugin is off
|
|
302
|
+
|
|
303
|
+
Nothing a plugin does stops the app. Each of these turns off that plugin alone:
|
|
304
|
+
|
|
305
|
+
- its `plugin.json` cannot be read, or is not one;
|
|
306
|
+
- it is built for another plugin API, or for a range without the app's version;
|
|
307
|
+
- its module cannot be loaded, or does not link: it imports something the app's `@protobase/ui` does not have;
|
|
308
|
+
- it defines a name an earlier plugin defines.
|
|
309
|
+
|
|
310
|
+
Every place it would fill then draws its fallback (see [Extension points](#extension-points)). The app's admins (the `admin` role) see a notice above the app naming each plugin that is off and why, such as "The plugin sales-tools is off: it needs Protobase >=0.12.3, and this app runs 0.12.1." Everyone else sees only the fallbacks. An action handler that throws shows its error in a toast, as a project's does, and a part that throws while drawing shows its fallback in its own place.
|
|
311
|
+
|
|
312
|
+
## API
|
|
313
|
+
|
|
314
|
+
From `@protobase/ui`:
|
|
315
|
+
|
|
316
|
+
| | |
|
|
317
|
+
| --- | --- |
|
|
318
|
+
| `definePlugin(config)` | Typed identity for a plugin's default export: `name`, `actions`, `fields`, `shell`, `beforeSignIn`, `extends`, as `defineUi`. |
|
|
319
|
+
| `BeforeSignInProps` | The props of a plugin's [`beforeSignIn`](#before-sign-in) component. |
|
|
320
|
+
| `usePlugin()` | The plugin the calling component belongs to (see [above](#writing-a-plugin)). |
|
|
321
|
+
| `useResourceClient(resource)` | The client of the API that serves a resource: the app's own, or a source's for a [remote resource](remote-resources.md)). |
|
|
322
|
+
| `FieldWidget`, `FieldWidgetProps`, `FieldWidgetEditorProps` | The types of a [field type](#field-types) and of its parts' props. |
|
|
323
|
+
| `RecordCardProps` | The props of a [record card](custom-components.md#record-cards)), `{ row }`. |
|
|
324
|
+
| `AuthLayout`, `CodeForm`, `ResendCodeButton`, `Button`, `buttonClass`, `Input`, `Spinner`, `LogoMark` | The pieces of the app's sign-in pages, for a page of a plugin or a host's own that looks like them. Outside the app, `@protobase/ui/styles.css` is the stylesheet that draws them, for a Vite build with Tailwind. |
|
|
325
|
+
| `pluginApi` | The plugin API of the app's `@protobase/ui`. |
|
|
326
|
+
| `protobaseVersion` | The Protobase version of the app's `@protobase/ui`. |
|
|
327
|
+
|
|
328
|
+
From `@protobase/layout`: `component(name, { plugin })` (see [above](#declaring-its-components-in-the-config)). From `@protobase/schema`: `.widget(name, props)` on a view's fields and `d.cards(fields, { card })` (see [Views](ui-config.md))). From `@protobase/server`: `options.plugins` of `createAdmin`, `PROTOBASE_PLUGINS` and `PROTOBASE_PLUGINS_BEFORE_SIGN_IN` (see [loading](#loading)). The tokens a plugin's utilities read are in [Theme](theme.md)).
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/remote-resources.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Remote resources
|
|
4
|
+
|
|
5
|
+
Resources another Protobase API serves, drawn in an app with its own list pages, record pages, sidebar and links, at their own addresses.
|
|
6
|
+
|
|
7
|
+
An app can draw resources that another Protobase API serves, as if they were its own: the same list page, record page, create form, breadcrumbs, sidebar entry with its recent records, global search, links and actions, at the same `/<resource>/<key>` addresses. The other API is a **source**. Its config, its data, its access rules and its views stay where they are; the app lists which of its resources it draws, and the signed-in person works with them under the source's rules.
|
|
8
|
+
|
|
9
|
+
A shop's admin app can show the tickets of the company's service desk this way, next to its orders, and a hosting platform can show each app the tasks it keeps for that app. [Showing another app's records](../guides/remote-resources.md)) builds the first, step by step.
|
|
10
|
+
|
|
11
|
+
## How it works
|
|
12
|
+
|
|
13
|
+
Nothing new is invented for a remote resource. Every page of the admin app is drawn from two things: the resource's model and view from `/api/meta`, and its records from `/api/v1`. A source is another place to get both.
|
|
14
|
+
|
|
15
|
+
1. **The app names its sources.** Its `/api/meta` lists each source the caller may use: where its API is, what it is called, and the resources the app draws from it.
|
|
16
|
+
2. **The browser reads each source's `/meta` itself,** with the signed-in person's own token, and keeps only the resources the app draws. The source answers as it answers anyone: with the resources the person may see there, each as they may see it, and the view for their role.
|
|
17
|
+
3. **The app merges them into its own.** A remote resource's model, view and permissions sit next to the app's own resources, so every part of the app that reads a resource reads it the same way: the sidebar places it by its view's `nav`, the breadcrumbs name it by its view's names and title, the list page shows its columns, filters and displays.
|
|
18
|
+
4. **Every call about a remote resource goes to its source.** The app's client sends each call to the API that serves the resource, with the same token: lists, records, writes, aggregates, uploads. The app's server is not in between and stores nothing of it.
|
|
19
|
+
|
|
20
|
+
So the source decides what a person sees and may do, exactly as in its own admin app, and the app decides where it shows it. The app does not wait for its sources: its own resources show at once, and a source's resources join when its `/meta` has loaded.
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
browser, on the app's origin the app's API the source's API
|
|
24
|
+
───────────────────────────── ───────────── ────────────────
|
|
25
|
+
GET /api/meta ───────────▶ resources, views,
|
|
26
|
+
◀─────────── sources: [{ url, label, resources }]
|
|
27
|
+
GET <source>/api/meta (token) ─────────────────────────────────────▶ the person's resources
|
|
28
|
+
◀───────────────────────────────────── and views there
|
|
29
|
+
GET <source>/api/v1/tickets?... ─────────────────────────────────────▶ rows, by its rules
|
|
30
|
+
PATCH <source>/api/v1/tickets/12 ─────────────────────────────────────▶ the write, by its rules
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Declaring a source
|
|
34
|
+
|
|
35
|
+
A source is declared on the app's server, in `options.sources` of its config, or by the platform that runs the app in `PROTOBASE_SOURCES`, without the app's own code:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// protobase.config.ts
|
|
39
|
+
export default {
|
|
40
|
+
config,
|
|
41
|
+
auth,
|
|
42
|
+
authenticate,
|
|
43
|
+
options: {
|
|
44
|
+
sources: [{ url: 'https://desk.example.com/api/v1', label: 'Service desk', resources: ['tickets', 'ticket_notes'] }],
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
PROTOBASE_SOURCES='[{"url":"https://desk.example.com/api/v1","label":"Service desk","resources":["tickets","ticket_notes"]}]'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
| Source | |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| `url` | The source's resources API, the `/api/v1` of another Protobase app: a path, with nothing after it. Its `/meta` is beside it, at `/api/meta`. |
|
|
56
|
+
| `label` | The source's name as people read it, when it cannot be reached. |
|
|
57
|
+
| `resources` | The resources the app draws from it, by their names there. Its other resources stay out of the app. |
|
|
58
|
+
| `roles` | Optional: only callers holding one of these roles are told about the source. Without, everyone is. |
|
|
59
|
+
|
|
60
|
+
The sources of `options.sources` come first, then those of `PROTOBASE_SOURCES`; `options.sources: false` turns both off.
|
|
61
|
+
|
|
62
|
+
**Checks at startup.** The server refuses to start, naming the source, when a source cannot work: a `url` that is no http(s) URL or names no path, a missing `label` or `resources`, a key a source does not take (such as `resource` for `resources`), the same `url` listed twice, or one resource drawn from two sources. It refuses to start too when a source draws a resource under a name the app already has, for one of its own resources or a [composed page](layouts.md)): the address `/<name>` would mean two things. Rename one of them, or leave it out of the source's resources.
|
|
63
|
+
|
|
64
|
+
## Who sees what
|
|
65
|
+
|
|
66
|
+
Two places decide, each about what it knows:
|
|
67
|
+
|
|
68
|
+
- **The app** decides who is told about a source, with its `roles`. A caller without one of them gets no `sources` in `/api/meta`, and their browser never calls that source.
|
|
69
|
+
- **The source** decides everything else, as for its own users. Its `/meta` names only the resources the person may see there, each without the fields they may not read and with the view for their role; its API applies its [access rules](access.md)), row filters and record checks to every call. A resource the app draws but the person may not see there is not in their app.
|
|
70
|
+
|
|
71
|
+
A source's view can be written for the people of another app: views are picked [per role](ui-config.md#actions-and-views-per-role)) with `.forRoles([...])`, and the role the source gives the app's users (see below) can have a view of its own, with its own `nav` group, columns and layout.
|
|
72
|
+
|
|
73
|
+
## Signing in at the source
|
|
74
|
+
|
|
75
|
+
The browser sends the source the token the app issued to the signed-in person, as it sends it to the app's own API (`Authorization: Bearer`). The source accepts it or not: there is no second sign-in, and the app never sees a password or key of the source.
|
|
76
|
+
|
|
77
|
+
A Protobase API accepts the tokens of another Protobase app when it lists that app in `betterAuthAuthenticator`'s `apps`:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
// the source's auth.ts
|
|
81
|
+
export const authenticate = betterAuthAuthenticator({
|
|
82
|
+
auth,
|
|
83
|
+
apps: [{ issuer: 'https://shop.example.com', roles: { admin: 'shop' } }],
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
| Trusted app | |
|
|
88
|
+
| --- | --- |
|
|
89
|
+
| `issuer` | The other app's Better Auth `baseURL`, its `BETTER_AUTH_URL`, with or without a trailing slash: the issuer and audience of its tokens. Its keys are read from `<issuer>/api/auth/jwks`. |
|
|
90
|
+
| `roles` | Its global roles as they are here: `{ admin: 'shop' }` makes its admins `shop` here. A role it does not list is dropped, and a user left with none is refused with `403`. A role someone holds in one of its [organizations](auth.md#organizations)) is never mapped, since that organization's owners hand it out; with organizations there, only a global role counts. |
|
|
91
|
+
| `tenant` | The tenant its users work in here, for resources with a [tenant](data-config.md#resource)). Nothing in its tokens names a tenant here. |
|
|
92
|
+
| `keys` | Its key set, instead of reading it from its `/api/auth/jwks`: for tests, or keys at hand. |
|
|
93
|
+
|
|
94
|
+
- A token whose issuer is a trusted app's is verified against that app's keys, with that issuer as issuer and audience; every other token goes to the app's own check. The key set is kept for 10 minutes and read again for an unknown key, as jose's remote key sets do.
|
|
95
|
+
- The user's id here is `<sub>@<that app's host>`, such as `x81kd2@shop.example.com`, so it never equals an id of this app's own users: an [owner field](access.md)) or an audit entry tells them apart.
|
|
96
|
+
- Give the other app's users roles of their own (`shop`, not `admin`) and open to those roles exactly what you share. A role that is only in `apps` needs no place in `createAuth`'s `roles`, which are the roles of this app's own users; with [`defineRoles`](access.md)), define what it may do like any role.
|
|
97
|
+
- An app without `defineRoles` [allows](access.md#defaults)) every operation without a rule to everyone signed in, the other app's users included. Give the resources you do not share an access rule that leaves their role out, or define roles.
|
|
98
|
+
- A trusted app cannot be this app itself.
|
|
99
|
+
|
|
100
|
+
A source that is not a Protobase app accepts the app's tokens its own way: they are EdDSA JWTs whose issuer and audience are the app's `BETTER_AUTH_URL`, whose keys are at `<that URL>/api/auth/jwks`, which carry the user's id in `sub`, their active role in `role` and live for 15 minutes (see [Tokens and roles](auth.md#tokens-and-roles))).
|
|
101
|
+
|
|
102
|
+
**What it trusts.** A source sees the person's token, which the app's own API also accepts until it expires, within 15 minutes. List as a source only an API you trust as much as a [plugin](plugins.md)), whose code runs in the app with the same rights.
|
|
103
|
+
|
|
104
|
+
## CORS
|
|
105
|
+
|
|
106
|
+
The browser calls the source from the app's origin, so the source answers [CORS](api.md#cors)) for it: it lists the app's origin in `options.cors` or `PROTOBASE_CORS_ORIGINS`:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
// the source's protobase.config.ts
|
|
110
|
+
export default { config, auth, authenticate, options: { cors: ['https://shop.example.com'] } }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
A Protobase API then answers preflights for the bearer token and the `If-Match` and `If-None-Match` headers, and exposes `ETag`, `Location` and `X-Meta-Version`, which the app uses. Without it the browser refuses every answer of the source, and the app shows its resources as unavailable.
|
|
114
|
+
|
|
115
|
+
## What the app draws
|
|
116
|
+
|
|
117
|
+
Everything the app draws for a resource of its own, for a remote resource too:
|
|
118
|
+
|
|
119
|
+
| Part | For a remote resource |
|
|
120
|
+
| --- | --- |
|
|
121
|
+
| Sidebar | An entry where the source's view places it (`nav.group`, `nav.order`), with its [recent records](sidebar.md#recent-records)), read from the source. `nav.hidden` keeps it out. |
|
|
122
|
+
| List page | Its columns, sort, filters, search, displays and chart, at `/<resource>`, with every list state in the address. |
|
|
123
|
+
| Record page | Its sections, related records, sidebar, actions and custom sections, at `/<resource>/<key>`, saved back to the source with its ETag. |
|
|
124
|
+
| Create form | At `/<resource>/new`, when the person may create there. |
|
|
125
|
+
| Breadcrumbs | The sidebar group, the list and the record's title, each a link. |
|
|
126
|
+
| Global search | Its search fields, when it has them and a sidebar entry. |
|
|
127
|
+
| Links and actions | Links to its own records and lists; `a.update` and `a.remove` write to the source; handlers run with the source's client (see [Custom components](#custom-components)). |
|
|
128
|
+
| Files | Uploaded to the source, and downloaded from it. |
|
|
129
|
+
|
|
130
|
+
What leads out of the source is left out, since the app has no such page or has another resource by that name:
|
|
131
|
+
|
|
132
|
+
- a relation to a resource the app does not draw from the same source shows the key it holds;
|
|
133
|
+
- related records of such a resource, and related columns that go through such a relation;
|
|
134
|
+
- links to such a resource, and links to a page.
|
|
135
|
+
|
|
136
|
+
The source's composed pages, user menu, bottom bar, assistant and plugins are its own app's; the app draws only resources. The app's own config cannot name a remote resource either, in its user menu, bottom bar or pages, since it is not there when the server starts.
|
|
137
|
+
|
|
138
|
+
A batch write goes to the one API its writes are for. One that writes to the app's own resources and a source's, or to two sources, cannot be atomic and is refused before anything is sent.
|
|
139
|
+
|
|
140
|
+
## Custom components
|
|
141
|
+
|
|
142
|
+
A source's view can name [custom components](custom-components.md)) like any view: a record section with `l.custom(TicketTimeline)`, a list display, a record card or a field type. The app draws them by name from its [plugins](plugins.md)), as it draws its own: a component no loaded plugin provides draws its fallback in its place.
|
|
143
|
+
|
|
144
|
+
So whoever runs a source ships its components as a plugin, and the app lists the plugin with the source:
|
|
145
|
+
|
|
146
|
+
```sh
|
|
147
|
+
PROTOBASE_SOURCES='[{"url":"https://desk.example.com/api/v1","label":"Service desk","resources":["tickets"]}]'
|
|
148
|
+
PROTOBASE_PLUGINS='https://desk.example.com/plugins/desk/plugin.json'
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
```tsx
|
|
152
|
+
// the desk's config: the component its plugin provides
|
|
153
|
+
export const TicketTimeline = component('TicketTimeline', { plugin: 'desk' })
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Inside, the component reads its record with `useLayoutRecord()`, and its data with `useList` and `useRecord`, which go to the source for a remote resource like every other call. `useMeta()` gives the app's `/meta` with the sources' resources in it, and how far each source has loaded in `sources`. `useResourceClient(resource)` gives the client of the API that serves a resource, the source's for a remote one, whose `invoke` calls that API's [functions](../guides/api-functions.md)); `useClient()` sends resource calls where they belong, and `invoke` to the app's own functions. An action handler gets the client of the resource's API as `client`.
|
|
157
|
+
|
|
158
|
+
## When a source cannot be reached
|
|
159
|
+
|
|
160
|
+
The rest of the app keeps working. A source's resources are in the app while its `/meta` has loaded, and say why while it has not:
|
|
161
|
+
|
|
162
|
+
- **While it loads** they are not in the sidebar yet, and their addresses show "Loading from *label*". The app waits at most 10 seconds.
|
|
163
|
+
- **When it cannot be read**, because it does not answer, refuses the token or answers an error, each resource the app draws from it is in the sidebar under the source's `label`, marked "Cannot reach *label*", and its addresses show the same with the reason, such as "It did not accept your sign-in", and **Try again**.
|
|
164
|
+
- **When it fails later**, after it answered once, the app keeps what it read and a call that fails shows its error where it was made, as for the app's own API.
|
|
165
|
+
|
|
166
|
+
Each source's `/meta` is read again when the window regains focus, with `If-None-Match`, and when one of its answers carries a new `X-Meta-Version`, as the app's own is.
|
|
167
|
+
|
|
168
|
+
## API
|
|
169
|
+
|
|
170
|
+
**`/api/meta`** names the caller's sources in `sources`: `[{ url, label, resources }]`, without `roles`; none without sources.
|
|
171
|
+
|
|
172
|
+
**`@protobase/server`**
|
|
173
|
+
|
|
174
|
+
| | |
|
|
175
|
+
| --- | --- |
|
|
176
|
+
| `options.sources` | `SourceOption[]` (`{ url, label, resources, roles? }`) or `false`. |
|
|
177
|
+
| `PROTOBASE_SOURCES` | A JSON list of the same, after `options.sources`. |
|
|
178
|
+
| `betterAuthAuthenticator({ apps })` | `TrustedApp[]` (`{ issuer, roles, tenant?, keys? }`): other apps whose tokens the API admits. |
|
|
179
|
+
| `withTrustedApps(own, ownIssuer, apps)` | The same around any authenticator. |
|
|
180
|
+
| `sourceOptions`, `checkSourceNames`, `sourcesFor` | What `createAdmin` reads, checks and names, for hosts that read sources themselves. |
|
|
181
|
+
|
|
182
|
+
**`@protobase/client`**
|
|
183
|
+
|
|
184
|
+
| | |
|
|
185
|
+
| --- | --- |
|
|
186
|
+
| `sourceMeta(meta, source)` | The part of a source's `/meta` an app draws: `{ resources, views, permissions }` of the resources `source` lists, with what leads out of the source left out. |
|
|
187
|
+
| `routedClient(own, sourceOf)` | One `Client` over the app's API and its sources: `sourceOf(resource)` gives a source's client, or `undefined` for the app's own. `meta` and `invoke` go to the app's own. |
|
|
188
|
+
| `createClient({ baseUrl })` | With an absolute `baseUrl`, each file in a record it answers has an absolute `url`, on that API's origin. |
|
|
189
|
+
|
|
190
|
+
**`@protobase/ui`**
|
|
191
|
+
|
|
192
|
+
| | |
|
|
193
|
+
| --- | --- |
|
|
194
|
+
| `useResourceClient(resource)` | The client of the API that serves `resource`. |
|
|
195
|
+
| `useClient()` | Below the app's `MetaGate`, the client that sends each resource's calls to its API. |
|
|
196
|
+
| `useMeta()` | Below the app's `MetaGate`, `data` holds the resources of its sources too, and `sources`: each source with how far it has loaded (`loading`, `ready`, `error`). |
|
|
197
|
+
| `ActionContext.client` | The client of the action's resource's API. |
|
|
198
|
+
| `App` `sources` | Ready-made clients of the sources by their `url`, for stories and tests. |
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/sidebar.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Sidebar
|
|
4
|
+
|
|
5
|
+
How sidebar entries are grouped, and how an entry lists recent records with status dots.
|
|
6
|
+
|
|
7
|
+
The admin's sidebar has one entry per resource view, grouped by `nav.group` (default: the database schema) and sorted by `nav.order`. A view's [named lists](ui-config.md#more-than-one-list)) are entries of their own, placed by their own `nav`. `nav({ hidden: true })` leaves a resource out, for line and link tables; a resource in the [user menu](user-menu.md)) is left out too. [Composed pages](layouts.md#setup)) have entries of their own, and groups that hold pages come first. A [remote resource](remote-resources.md)) has its entry where its source's view places it; while its source cannot be reached, it sits under the source's label, marked "Cannot reach" with the source's name.
|
|
8
|
+
|
|
9
|
+
## Recent records
|
|
10
|
+
|
|
11
|
+
An entry can list a few of its records under it, each with a status dot, so the ones that need attention are one click away:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// examples/erp/config/orders/ui.ts
|
|
15
|
+
export const ordersView = view<typeof orders>('orders')
|
|
16
|
+
.nav({
|
|
17
|
+
recent: {
|
|
18
|
+
status: 'status',
|
|
19
|
+
tones: { draft: 'neutral', confirmed: 'info', picking: 'warning', shipped: 'success' },
|
|
20
|
+
pulse: ['picking'],
|
|
21
|
+
filter: 'status != "delivered" AND status != "cancelled"',
|
|
22
|
+
orderBy: 'created_at desc',
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
| Key | Meaning |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| `status` | The field whose value picks each record's dot. Typed against the resource's fields. |
|
|
30
|
+
| `tones` | Dot colour per status value: `neutral`, `info`, `success`, `warning` or `danger`. Values not listed are `neutral`. |
|
|
31
|
+
| `pulse` | Status values whose dot pulses, for states in progress (a deploy running, an order being picked). |
|
|
32
|
+
| `filter` | Which records qualify, as AIP-160 text, exactly as `?filter=` on the list API. Use it to leave out finished or inactive records, or to keep them while they are recent: `status != "stopped" OR updated_at > now() - 7d`. |
|
|
33
|
+
| `orderBy` | AIP-132 order, as `?order_by=`; the list's `sort` when omitted. |
|
|
34
|
+
| `limit` | How many records, 1 to 20; 3 when omitted. |
|
|
35
|
+
|
|
36
|
+
The entry then works like this:
|
|
37
|
+
|
|
38
|
+
- The label opens the full list, as before; the chevron beside it shows or hides the records. The choice is kept per entry in this browser (`localStorage`), and an entry starts open.
|
|
39
|
+
- Each record shows its title (the view's `title`, as in breadcrumbs) and opens its record page. The open record is highlighted.
|
|
40
|
+
- "View all" ends the list and opens the full list, like the label.
|
|
41
|
+
- Only text sidebars (`text-large`, `text-small`, and the phone drawer) show the records; icon sidebars keep a plain entry.
|
|
42
|
+
|
|
43
|
+
The records come from the list API with the caller's token, so they follow the same access rules as the list page: row filters such as `.own` apply, and when the list is refused the group says it could not load. Any write to the resource made in the admin refreshes them, and so does returning to the window, unless a record has unsaved changes, so statuses changed elsewhere show up. A record read anew, by its record page or by project code refetching `useRecord`, refreshes its group too when it shows another status or title than the group's row.
|
|
44
|
+
|
|
45
|
+
### Checks
|
|
46
|
+
|
|
47
|
+
`createAdmin` refuses to start when a group does not fit its resource: an unknown `status` field, a tone outside the palette, a `limit` outside 1 to 20, or a `filter` or `orderBy` the list API would reject (unknown, non-filterable or non-sortable fields, bad syntax). `view.toModel(resourceModel)` runs the same checks.
|
|
48
|
+
|
|
49
|
+
`/meta` drops the group, and keeps the rest of `nav`, for a caller who cannot read a field it uses (its status, or a field in its filter or order), so it never names a hidden field.
|
|
50
|
+
|
|
51
|
+
`EMAIL=... PASSWORD=... bun scripts/e2e-shell.mjs` checks the groups and the user menu in a browser against a running `bun run --cwd examples/erp dev` (`BASE` for another address), comparing them with what the API gives that user.
|