@protobase/cli 0.22.12 → 0.23.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/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 +285 -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,38 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/user-menu.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# User menu
|
|
4
|
+
|
|
5
|
+
The user menu at the bottom of the sidebar, with the app's own account pages and links.
|
|
6
|
+
|
|
7
|
+
The signed-in user sits at the bottom of the sidebar. Clicking them opens a menu with their name and email, the app's own pages, the theme switch, and "Sign out". Account pages (organization, billing, users) and help links belong here rather than among the working resources.
|
|
8
|
+
|
|
9
|
+
Export one [`userMenu(...)`](https://docs.protobase.net/api/schema/functions/usermenu/)) from the config module:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// examples/erp/config/user-menu.ts, re-exported from config/index.ts
|
|
13
|
+
import { userMenu } from '@protobase/schema'
|
|
14
|
+
|
|
15
|
+
export const accountMenu = userMenu((m) => [
|
|
16
|
+
m.resource('organizations', { label: 'Organization', icon: 'building' }),
|
|
17
|
+
m.page('accounts', { label: 'Users', icon: 'users' }),
|
|
18
|
+
m.link('Documentation', 'https://docs.protobase.net', { icon: 'book-open' }),
|
|
19
|
+
m.link('Support', 'mailto:support@example.com', { icon: 'life-buoy' }),
|
|
20
|
+
])
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
| Item | Does |
|
|
24
|
+
| --- | --- |
|
|
25
|
+
| `m.resource(name, { label?, icon? })` | Opens the resource's list. The resource leaves the sidebar. The label defaults to the view's plural name. |
|
|
26
|
+
| `m.page(name, { label?, icon? })` | Opens a [composed page](layouts.md)). The page leaves the sidebar. The label defaults to its title. |
|
|
27
|
+
| `m.link(label, href, { icon? })` | Opens `href` in a new tab. Only `https://`, `http://` and `mailto:` are accepted. |
|
|
28
|
+
|
|
29
|
+
`accounts` is the built-in [Users page](auth.md#users-page)), which the config exports as `usersPage` from `@protobase/layout`; without the export, `createAdmin` refuses the item. Items keep their order. After them come the app's own account pages: "Sign-in & security" for everyone (their [passkeys](auth.md#passkeys)) and [two-factor authentication](auth.md#two-factor-authentication))) and "Sign-in policy" for admins (the [sign-in policy](auth.md#sign-in-policy)), and the log of [staff sign-ins](auth.md#staff-sign-in))); then the [theme switch](theme.md#light-dark-or-the-system)), and "Sign out" always comes last. `icon` is one of `bell`, `book-open`, `bookmark`, `building`, `credit-card`, `external-link`, `file-text`, `heart`, `key`, `layout-dashboard`, `life-buoy`, `mail`, `receipt`, `settings`, `shield`, `users`. A resource item without one uses the resource's sidebar icon, and a link item uses `external-link`.
|
|
30
|
+
|
|
31
|
+
Every caller gets the menu through `/meta`, without the resources and pages they cannot see, so a user who cannot read `organizations` has no "Organization" item. Links are shown to everyone. `createAdmin` refuses to start when an item names a resource or page that does not exist, and `configExports` refuses a config module that exports two menus.
|
|
32
|
+
|
|
33
|
+
`protobase dev`, `protobase build` and `protobase serve` pick the menu up from the config module. With `createAdmin` directly, pass it as `userMenu`:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const { resources, views, userMenu } = configExports(config)
|
|
37
|
+
const admin = createAdmin({ resources, views, userMenu, db, authenticate })
|
|
38
|
+
```
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/versioning.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Versioning
|
|
4
|
+
|
|
5
|
+
How Protobase is versioned and released, the commit convention behind it, and which bundles a serve runtime runs.
|
|
6
|
+
|
|
7
|
+
Protobase follows [semantic versioning](https://semver.org). Every push to `main` that holds a fix, a feature or a breaking change is released: the pushed commit is tagged `v<version>`, and a [GitHub release](https://github.com/PrototeamHQ/protobase/releases) of that tag holds the release's changes. Every `@protobase` package is then published to npm at that version. A release adds no commit to `main`: a release's version is its tag, not a `package.json` field, so the version in the repository's `package.json` files is not bumped. `CHANGELOG.md` lists only the releases from before releases stopped committing.
|
|
8
|
+
|
|
9
|
+
## Versions below 1.0
|
|
10
|
+
|
|
11
|
+
Until 1.0, a minor version may break, so it plays the part a major plays from 1.0 on:
|
|
12
|
+
|
|
13
|
+
| Commits since the last release | Below 1.0 | From 1.0 |
|
|
14
|
+
| --- | --- | --- |
|
|
15
|
+
| a breaking change (`feat!:`, a `BREAKING CHANGE:` footer) | minor: 0.1.4 → 0.2.0 | major: 1.4.2 → 2.0.0 |
|
|
16
|
+
| a feature (`feat:`) | patch: 0.1.4 → 0.1.5 | minor: 1.4.2 → 1.5.0 |
|
|
17
|
+
| a fix (`fix:`) | patch: 0.1.4 → 0.1.5 | patch: 1.4.2 → 1.4.3 |
|
|
18
|
+
| anything else (`docs:`, `test:`, `chore:`, ...) | no release | no release |
|
|
19
|
+
|
|
20
|
+
The first release is 0.1.0.
|
|
21
|
+
|
|
22
|
+
The version goes up from the newest `v<version>` tag reachable from the released commit, never from a newer tag elsewhere in the repository. When the repository already has a tag above that one, the commit is on a maintenance branch whose next minor and major versions are taken, so a fix or a feature there is released as a patch: with `v0.3.0` released from `main`, a branch from `v0.2.4` releases 0.2.5, then 0.2.6. A breaking change cannot be released from a maintenance branch: the release fails, and there is no override, so the breaking change has to be removed from the branch's history.
|
|
23
|
+
|
|
24
|
+
## Commit messages
|
|
25
|
+
|
|
26
|
+
Every commit message follows [Conventional Commits](https://www.conventionalcommits.org), checked by [commitlint](https://commitlint.js.org) with its conventional config (`commitlint.config.mjs`):
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
<type>(<optional scope>)<optional !>: <subject>
|
|
30
|
+
|
|
31
|
+
<optional body>
|
|
32
|
+
|
|
33
|
+
<optional footers, e.g. BREAKING CHANGE: what breaks and what to do instead>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- **Types:** `feat` and `fix` are released; `docs`, `test`, `refactor`, `perf`, `build`, `ci`, `style`, `chore` and `revert` are not on their own.
|
|
37
|
+
- **Scopes** name the part of the repository: `cli`, `query`, `schema`, `server`, `ui`, `erp`, `docs`, ...
|
|
38
|
+
- **Subjects** start in lowercase and end without a period; the header and every body line are at most 100 characters.
|
|
39
|
+
- **Breaking changes** take a `!` after the type or scope, or a `BREAKING CHANGE:` footer that says what to do instead.
|
|
40
|
+
|
|
41
|
+
`bun install` points git at `.githooks/` (the `prepare` script), whose `commit-msg` hook runs commitlint on every commit. The release workflow checks every pushed commit again before it releases.
|
|
42
|
+
|
|
43
|
+
## Releases
|
|
44
|
+
|
|
45
|
+
`.github/workflows/release.yml` runs on every push to `main`:
|
|
46
|
+
|
|
47
|
+
1. The checks of `ci.yml` (`bun run check`, the smoke test on Node, the docs build, the Storybook play tests and the docs check), of the tests and steps the changes since the last `v<version>` tag affect (see What a push checks in `CONTRIBUTING.md`), and commitlint over the pushed commits. When the push cuts a release, the Postgres suite (`bun run test:postgres`, on Postgres and PgBouncer) runs in full as well, whatever changed.
|
|
48
|
+
2. `bun run release` (`scripts/release.mjs`, with [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version)) computes the next version and its notes from the commits since the last `v<version>` tag, as above, and tags the pushed commit `v<version>`. It commits nothing and changes no file. Without a fix, feature or breaking change it tags nothing.
|
|
49
|
+
3. The tag is pushed, and the GitHub release of the tag gets the notes.
|
|
50
|
+
4. `scripts/stamp-version.mjs` writes the tag's version into every `package.json` and `bun.lock` in the runner, never committed. `scripts/publish.mjs` then builds every package at the tag to `dist` and packs it with Bun (the private `@protobase/presets` is not published), and npm, under Node, publishes it with [trusted publishing](https://docs.npmjs.com/trusted-publishers) and provenance; there is no npm token. A run that cut no release publishes nothing.
|
|
51
|
+
5. Once npm serves the packages, the tag's version is written into the `package.json` files the same way, and `bun run images:context <image>` writes each image's build context at the tag from them, and the images are built natively for `linux/amd64` and `linux/arm64` and pushed to GitHub's registry as `ghcr.io/prototeamhq/protobase:<version>` and `ghcr.io/prototeamhq/protobase-dev:<version>` (see [Deploy](../guides/deploy.md#images))). A release whose publish failed has no images.
|
|
52
|
+
|
|
53
|
+
When `main` moved on while the checks ran, the run of the newer push releases both. `bun run release --dry-run` prints the next version and its notes without tagging anything.
|
|
54
|
+
|
|
55
|
+
Running the workflow by hand with a tag (`gh workflow run release.yml -f tag=v0.1.0`) only publishes: the packages at that tag and its version, skipping every version npm already has, and the images, which repeats a missed or half-done publish. `node scripts/publish.mjs --dry-run` packs the packages and shows what `npm publish` would send, and `node scripts/publish.mjs --pack`, which CI runs, packs every package without asking npm or publishing. Each package on npm trusts `release.yml` of `PrototeamHQ/protobase` as its publisher; a new package has to be published once by hand before that can be set up. The workflow pushes the images with its own token; a new image on GitHub's registry starts out private and is made public once in its package settings.
|
|
56
|
+
|
|
57
|
+
## Bundles and runtimes
|
|
58
|
+
|
|
59
|
+
`protobase build` writes the Protobase version that built the bundle into [the manifest](cli.md#the-manifest)) as `protobase`. The serve runtime knows its own version, inlined into `protobase-serve.js` by `protobase build-serve` and inlined into the CLI's `dist` when the package is built, and read from `@protobase/cli`'s `package.json` when `protobase serve` runs from source in this repository. Before it loads a bundle, it compares the two:
|
|
60
|
+
|
|
61
|
+
- **From 1.0:** the major versions are the same and the runtime's minor is the bundle's or newer. A 1.4.x runtime serves bundles built by 1.0.0 up to 1.4.x.
|
|
62
|
+
- **Below 1.0:** the minor versions are the same. A 0.2.x runtime serves only bundles built by 0.2.x, not those of 0.1.x or 0.3.x.
|
|
63
|
+
- **Patches** never matter: a 0.1.2 runtime serves a bundle built by 0.1.7.
|
|
64
|
+
|
|
65
|
+
Otherwise, and for a bundle whose manifest has no `protobase` version or one that is not `x.y.z`, startup stops with exit code 1 and an error naming both versions:
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
error: dist/protobase.bundle.json: the bundle was built by Protobase 0.2.0 and this runtime is Protobase 0.1.3, which serves bundles built by 0.1.x; rebuild the bundle with Protobase 0.1.x or serve it with a runtime of 0.2.x
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Rebuild the bundle with the runtime's version of Protobase, or upgrade the runtime to one that serves the bundle. The rule is `bundleVersionProblem` in `packages/cli/src/version/compatibility.ts`.
|
|
72
|
+
|
|
73
|
+
## Plugins
|
|
74
|
+
|
|
75
|
+
[Plugins](plugins.md)) are versioned by a plugin API of their own, not by the bundle's rule above: `@protobase/ui`'s `pluginApi`, 1 so far. A plugin's `plugin.json` names the plugin API it is built for and a range of Protobase versions, from the `@protobase/ui` it is built with on. An app loads a plugin of its own plugin API whose range holds its version, and turns off any other, telling its admins why.
|
|
76
|
+
|
|
77
|
+
A project's own `protobase.ui.tsx` is a plugin of its bundle, so the same rule holds for it. The admin app it runs in, the shell, is prebuilt once per release; a host that serves a newer release's shell for a bundle whose plugins all load in it gives the app that release's fixes without a rebuild (see [the shell](plugins.md#the-shell))). The server config still follows the bundle's rule above.
|
|
78
|
+
|
|
79
|
+
Releases keep the plugin API, below 1.0 too, where a minor version may change the config: they add exports to `@protobase/ui`, but remove or change one, add or remove a module the app shares with plugins, or move to a new major version of React only with a new plugin API. A test pins every export of `@protobase/ui` (`packages/ui/src/plugins/plugin-api.exports.json`) and fails when one goes away while `pluginApi` stays the same.
|
|
80
|
+
|
|
81
|
+
## Runtime updates
|
|
82
|
+
|
|
83
|
+
An app whose host can update its runtime, such as a hosting platform, can show admins an **Update** button in the top bar. `PROTOBASE_RUNTIME_URL` (or `createAdmin`'s `options.runtime.url`) names the host's endpoint, and `/api/meta` passes it on as `runtime: { url }` to callers with the `admin` role only. Without it, or for anyone else, the app shows nothing and asks nothing.
|
|
84
|
+
|
|
85
|
+
The app sends the signed-in user's API token as `Authorization: Bearer <token>` (verified like an [assistant backend](assistant.md#another-backend)) does) and expects:
|
|
86
|
+
|
|
87
|
+
- `GET {url}`: `200` with `{ version, latest, updateAvailable, indicator, policy, nextUpdateAt, updating }`. `version` is the runtime the app is pinned to, `policy` one of `immediate`, `weekly`, `scheduled` or `manual`, and `nextUpdateAt` when that policy applies `latest` (null when it never will or nothing is pending).
|
|
88
|
+
It may also send `updateStage` (`job` or `build`) and `updateStartedAt` while an update runs, and `lastUpdate`, `{ toVersion, outcome, errorCode, message, finishedAt }` with `outcome` one of `succeeded`, `unchanged` or `failed` and `message` plain text for the app's owner. A host that leaves them out gets the dialog without them.
|
|
89
|
+
- `POST {url}/update`: applies `latest` now, `202` with the same body; `409` when the app is on it already or an update is running. A refusal's message, as `{ error: { code, message } }` or a problem's `detail`, is shown as it is.
|
|
90
|
+
|
|
91
|
+
The app reads the status on load and every half hour, every 5 seconds while an update runs. It shows the button only while `updateAvailable` and `indicator` are both true; the button opens the two versions, when the policy updates or how far the running update is, how the last update ended, and **Update now**. When and whether to update is the host's setting.
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/webhooks.mdx: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Webhooks
|
|
4
|
+
|
|
5
|
+
Subscriptions that deliver resource events to URLs, API functions and code, signed with Standard Webhooks, retried and shaped for who receives them.
|
|
6
|
+
|
|
7
|
+
A **subscription** delivers the [events](events.md)) of resources as they happen: to a URL, to one of the app's [API functions](../guides/api-functions.md)), or to code of its own. Subscriptions are config: export them from the config module like resources, so the server has them and the admin UI never does. A URL or secret usually comes from the environment.
|
|
8
|
+
|
|
9
|
+
```ts title="docs-examples/events/subscriptions.ts"
|
|
10
|
+
import { subscription } from '@protobase/server'
|
|
11
|
+
|
|
12
|
+
// Where the events of docs-examples/events/orders.ts go: exported from the config module, like the resource.
|
|
13
|
+
|
|
14
|
+
/** A Slack message per order, to an incoming webhook whose URL is in the environment; off without it. */
|
|
15
|
+
export const newOrdersToSlack = subscription('newOrdersToSlack', {
|
|
16
|
+
events: ['orders.created'],
|
|
17
|
+
url: process.env.SLACK_ORDERS_WEBHOOK,
|
|
18
|
+
enabled: Boolean(process.env.SLACK_ORDERS_WEBHOOK),
|
|
19
|
+
body: ({ data }) => ({ text: `New order ${data.after?.number}: €${data.after?.total}` }),
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Every change of an order to the app's own `sync-order` function, one at a time per order and in order, with only
|
|
24
|
+
* what an accountant may read, signed so the function checks it as it would a call from outside.
|
|
25
|
+
*/
|
|
26
|
+
export const ordersToAccounting = subscription('ordersToAccounting', {
|
|
27
|
+
events: ['orders.*'],
|
|
28
|
+
function: 'sync-order',
|
|
29
|
+
secret: process.env.ACCOUNTING_WEBHOOK_SECRET,
|
|
30
|
+
as: { roles: ['accountant'] },
|
|
31
|
+
ordered: true,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
/** Code of the app's own, for each rush order; it throws to have the event tried again. */
|
|
35
|
+
export const rushOrders = subscription('rushOrders', {
|
|
36
|
+
events: ['orders.rush'],
|
|
37
|
+
run: async (event, { db }) => {
|
|
38
|
+
await db.insertInto('sales.pick_lists').values({ order_id: Number(event.data.key), priority: 'rush' }).execute()
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## subscription()
|
|
44
|
+
|
|
45
|
+
`subscription(name, options)`: the name is unique across the config and the configs it extends, letters, digits, `_` and `-`. Each has one target:
|
|
46
|
+
|
|
47
|
+
| Target | |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| `url` | A `POST` of the event as JSON. 2xx is a success; anything else, a redirect too, is a failure. The receiver has 15 seconds. |
|
|
50
|
+
| `function` | One of the app's API functions, called in this process with the same request a URL gets. |
|
|
51
|
+
| `run` | `(event, { db, attempt, signal }) => ...`, run for each event; it throws to fail the try. |
|
|
52
|
+
|
|
53
|
+
| Option | |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| `events` | Event names (`orders.created`), every event of a resource (`orders.*`), or all of them (`*`). Each must match a declared event. |
|
|
56
|
+
| `when` | A filter on the record after the change, or before it for a delete, in the language of access rules. |
|
|
57
|
+
| `as: { roles }` | Delivers as these roles: only records they may read, and only fields they may read. Without it, every record and every field but the sensitive ones. |
|
|
58
|
+
| `tenant` | Only events of this tenant's records. |
|
|
59
|
+
| `payload` | `thin` leaves `after` and `before` out, for a receiver that reads the record through the API. Default `full`. |
|
|
60
|
+
| `ordered` | One delivery at a time per record, in commit order. Default false. |
|
|
61
|
+
| `enabled` | Default true; `false` keeps the subscription in the config without delivering. |
|
|
62
|
+
| `secret` | `whsec_` and base64: signs the requests of a URL or function target. Without it they are unsigned. `newWebhookSecret()` makes one. |
|
|
63
|
+
| `headers` | Extra request headers, such as an API key the receiver wants. |
|
|
64
|
+
| `body` | `(event) => ...`: what is sent instead of the event, such as Slack's `{ text }`. |
|
|
65
|
+
|
|
66
|
+
The server does not start while a subscription names an event no resource declares, has a `when` that does not check against a resource it covers, calls a function the app lacks, or is enabled with an empty URL.
|
|
67
|
+
|
|
68
|
+
## What a receiver gets
|
|
69
|
+
|
|
70
|
+
```http
|
|
71
|
+
POST /hooks/protobase
|
|
72
|
+
content-type: application/json
|
|
73
|
+
webhook-id: evt_48211_large
|
|
74
|
+
webhook-timestamp: 1791666245
|
|
75
|
+
webhook-signature: v1,K5oZfzN95Z9UVu1EsfQmfVNQhnkZ2pj9o9NDN/H/pI4=
|
|
76
|
+
|
|
77
|
+
{ "id": "evt_48211_large", "type": "orders.large", "timestamp": "2026-10-10T21:04:05.123Z",
|
|
78
|
+
"data": { "resource": "orders", "operation": "update", "key": "7", "sequence": "48211",
|
|
79
|
+
"after": { ... }, "before": { ... }, "changed": ["total"], "tenant": 1,
|
|
80
|
+
"actor": { "id": "u_12", "roles": ["sales"] }, "source": "api", "transaction": "753" } }
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
- **`webhook-id`** is the same on every retry: a receiver that keeps the ids it handled ignores a second copy.
|
|
84
|
+
- **The signature** follows [Standard Webhooks](https://www.standardwebhooks.com): an HMAC-SHA256 of `id.timestamp.body` with the secret. A Protobase function checks it with [`verifyStandardWebhook`](../guides/api-functions.md)), and the Standard Webhooks libraries do the same elsewhere.
|
|
85
|
+
- **Records** use field names and values as `GET /orders/{key}` returns them, never sensitive fields. `changed` lists sensitive fields that changed, by name only.
|
|
86
|
+
- **`sequence`** grows with every change of a record, so a receiver can drop an event older than one it already has.
|
|
87
|
+
- **`actor`** is the caller of a write through the API, `null` for any other write (`source: "database"`).
|
|
88
|
+
|
|
89
|
+
A function target is called as the subscription's `as`, so `context.caller()` and its records are those roles; without `as`, as a caller without roles.
|
|
90
|
+
|
|
91
|
+
## Delivery and retries
|
|
92
|
+
|
|
93
|
+
- **The first try** goes out from the dispatch job, right after the write: within milliseconds for a write through the API, within two seconds for one from elsewhere.
|
|
94
|
+
- **A failed try** becomes a `webhooks.deliver` job, retried on the Standard Webhooks schedule with up to 10% jitter: 5 s, 5 m, 30 m, 2 h, 5 h, 10 h, 14 h, 20 h and 24 h, 10 tries over about three days. A `Retry-After` header makes the wait longer, up to a day.
|
|
95
|
+
- **`410 Gone`** fails the delivery at once.
|
|
96
|
+
- **After the last try** the job is kept as failed, on the [failed jobs page](jobs.md#failed-jobs-page)), where it can be retried.
|
|
97
|
+
- **Ordered subscriptions** get no first try from the dispatch job: a `webhooks.deliver-ordered` job delivers a record's events of one dispatch in order, retried with doubling waits from 5 s up to a day, 10 tries. Meanwhile the record's later events to that subscription wait. A retry sends again the events of that job that went through before the one that failed.
|
|
98
|
+
- **At least once:** a process that stops after delivering but before recording it delivers again when the dispatch job is retried, under the same `webhook-id`.
|
|
99
|
+
- **Event code that throws**, such as a `match` that reads a field it did not expect to be empty, a subscription's `when` or an access rule of its `as` roles, keeps that change as a failed `webhooks.dispatch-change` job on the failed jobs page, naming the event or subscription and the error, and dispatch moves on to later changes. Retry it once the code is fixed to deliver the change's events. A database error instead fails the whole dispatch job, which is retried.
|
|
100
|
+
|
|
101
|
+
Every outcome is a line in the server log, such as `webhook newOrdersToSlack orders.created evt_48211_created delivered (200)`.
|
|
102
|
+
|
|
103
|
+
## Subscriptions in the admin app
|
|
104
|
+
|
|
105
|
+
A subscription in the config suits a developer. For people who make their own at runtime, such as a tenant admin wiring their organization to a tool they use, extend the config with `webhooks()`:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { defineConfig, webhooks } from '@protobase/server'
|
|
109
|
+
|
|
110
|
+
export default defineConfig({ extends: [webhooks()], config, auth, authenticate })
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
It adds two pages, over a `webhooks` schema that [`protobase migration`](cli.md#migration)) writes (`NNN_webhooks.sql`):
|
|
114
|
+
|
|
115
|
+
- **Webhook subscriptions** (`webhook_subscriptions`, the table `webhooks.subscriptions`): a name, the events (`orders.created orders.large`, or `orders.*`), the URL, a filter, extra headers, full or thin payload, and whether it is enabled. The server sets the tenant and the owner, the person who made it, and makes the secret, which only a [reveal](api.md#sensitive-fields-and-audit)) shows. Saving checks the events and the filter against the resources, and refuses a URL whose host is a private, loopback or link-local address.
|
|
116
|
+
- **Webhook deliveries** (`webhook_deliveries`, a view over the delivery jobs): the deliveries that wait for their next try or failed for good, with their last error. A delivery leaves the list once it succeeds.
|
|
117
|
+
|
|
118
|
+
Both are scoped to the tenant like any resource with `.tenant()`, and their access decides who may do what: `webhook_subscriptions.create`, `webhook_deliveries.update` and so on, as for the app's own resources.
|
|
119
|
+
|
|
120
|
+
A subscription made here **delivers as its owner**, with the role they act with now in its tenant (the [active role](auth.md#active-role)) they last chose, else their first): only records and fields it may read. Letting people make subscriptions therefore grants them nothing they could not read already. The roles are read from the project's Better Auth; a project that signs people in otherwise passes `webhooks({ ownerRoles: async ({ id, tenant }) => [...] })`.
|
|
121
|
+
|
|
122
|
+
| Action | |
|
|
123
|
+
| --- | --- |
|
|
124
|
+
| Send test event | Sends the first event the subscription takes, made up from the newest record it may read, with `"test": true`. |
|
|
125
|
+
| Resend failed | Tries each delivery that failed for good once more. Webhook deliveries has the same for one delivery. |
|
|
126
|
+
| Rotate secret | Makes a new secret. For 24 hours requests carry a signature with each, so the receiver can switch over. |
|
|
127
|
+
| Disable | Stops its deliveries; waiting ones fail when their turn comes. Enabling it again starts afresh. |
|
|
128
|
+
|
|
129
|
+
A subscription is **disabled by itself** when its receiver answers `410 Gone`, or when no delivery succeeded for three days; the page says why.
|
|
130
|
+
|
|
131
|
+
| `webhooks(options)` | |
|
|
132
|
+
| --- | --- |
|
|
133
|
+
| `schema` | The schema of its tables, for a database that has a `webhooks` schema already. Default `webhooks`. |
|
|
134
|
+
| `deliveryLog` | Also keeps every try, successes too, for 30 days, listed as Webhook delivery log: status, try, status code, the first 2 KB of the answer and how long it took. Default false. |
|
|
135
|
+
| `ownerRoles` | The roles an owner holds now in a tenant, for a project without Better Auth. |
|
|
136
|
+
| `allowPrivateAddresses` | Lets URLs reach private addresses, for development and tests. Default false. |
|