@cervio/plugin-sdk 1.0.0 → 2.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present Cervio S.r.l.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -4,8 +4,8 @@ Typed client + runtime helpers for **Cervio route plugins**: `window.CervioUI`,
4
4
  the plugin boot context (`__CERVIO_PLUGIN_CONTEXT__`), the `/api/v1` HTTP
5
5
  client, host theme sync, and a tiny i18n resolver.
6
6
 
7
- Scaffold a new plugin with `create-cervio-plugin` (see
8
- `packages/create-cervio-plugin`) — it ships with this package pre-wired.
7
+ Scaffold a new plugin with `npm create @cervio/plugin` (package
8
+ [`@cervio/create-plugin`](../create-plugin)): it ships with this package pre-wired.
9
9
 
10
10
  ```bash
11
11
  pnpm add @cervio/plugin-sdk
@@ -13,10 +13,10 @@ pnpm add @cervio/plugin-sdk
13
13
 
14
14
  ## Why not `location.origin`?
15
15
 
16
- Route plugins run in a sandboxed `srcDoc` iframe (`sandbox="allow-scripts"`,
16
+ Route plugins run in a sandboxed iframe (`sandbox="allow-scripts"`,
17
17
  no `allow-same-origin`). `window.location.origin` frequently serializes as the
18
18
  literal string `"null"` there. Always resolve the API base URL from
19
- `__CERVIO_PLUGIN_CONTEXT__.parentOrigin` — every helper in this package does
19
+ `__CERVIO_PLUGIN_CONTEXT__.parentOrigin` - every helper in this package does
20
20
  this for you.
21
21
 
22
22
  ## CervioUI
@@ -30,9 +30,10 @@ if (isCervioUIAvailable()) {
30
30
  ```
31
31
 
32
32
  Never use `window.alert` / `window.confirm`, and never emit the deprecated
33
- `__cervioPluginToast` / `__cervioPluginModal` bridges — `CervioUI` is the only
34
- supported UI surface. Full capability docs: Admin → Plugins → UI components
35
- and `docs/plugins/ui/` in the host repo.
33
+ `__cervioPluginToast` / `__cervioPluginModal` bridges - `CervioUI` is the only
34
+ supported UI surface. Full capability docs:
35
+ [CervioUI docs](https://docs.cervio.app/docs/plugins/ui). Admin → Plugins →
36
+ Components is catalog + try-it only.
36
37
 
37
38
  ## Plugin context
38
39
 
@@ -46,11 +47,47 @@ console.log(ctx?.routeSlug, ctx?.parentOrigin, ctx?.runtimeSettings)
46
47
  ## HTTP client
47
48
 
48
49
  ```ts
49
- import { createCervioSessionClient } from '@cervio/plugin-sdk'
50
+ import { createCervioSessionClient, getPluginContext } from '@cervio/plugin-sdk'
50
51
 
51
52
  // baseUrl defaults to __CERVIO_PLUGIN_CONTEXT__.parentOrigin
52
53
  const client = createCervioSessionClient()
53
- const { entities } = await client.listEntities()
54
+
55
+ // Records through a view assigned to the user: its filters and permissions apply.
56
+ // Take the view id from the plugin settings, never hard-code it.
57
+ const viewId = String(getPluginContext()?.runtimeSettings?.view_id ?? '')
58
+ const { records, page } = await client.listViewRecords(viewId, { limit: 50 })
59
+ const next = page.next_cursor
60
+ ? await client.listViewRecords(viewId, { limit: 50, cursor: page.next_cursor })
61
+ : null
62
+ const { count } = await client.countViewRecords(viewId) // one call, counted by the server
63
+
64
+ // Entities by technical name: one round trip, no listEntities() first.
65
+ const { capabilities } = await client.getCapabilities('clienti')
66
+ ```
67
+
68
+ `getEntity`, `getCapabilities` and `listRecords` take the entity id **or** its
69
+ technical name. Never hard-code org-specific UUIDs.
70
+
71
+ `listRecords` (records of an entity without a view) works for organization
72
+ administrators only: for other users the API answers `view_required`. A plugin used
73
+ by everyone reads records with `listViewRecords` and counts them with
74
+ `countViewRecords`.
75
+
76
+ ### Fast start-up
77
+
78
+ - `me()` and `listEntities()` are answered from the boot payload when the manifest
79
+ declares `GET /api/v1/me` / `GET /api/v1/entities` in `cervio_apis`: no round trip.
80
+ - Declare the calls your plugin makes at start in the manifest `boot_requests`
81
+ (same path + query as the code, e.g. `/api/v1/views/<view id>/records?limit=50`):
82
+ Cervio starts them while the plugin loads and your call gets the response at once.
83
+ - Call `markReady()` once the first data is on screen:
84
+
85
+ ```ts
86
+ import { markReady } from '@cervio/plugin-sdk'
87
+
88
+ const { records } = await client.listViewRecords(viewId, { limit: 50 })
89
+ setRows(records)
90
+ markReady() // Cervio times the plugin open up to here; no-op outside Cervio
54
91
  ```
55
92
 
56
93
  `createCervioClient({ token })` is the Bearer-token equivalent (PAT / OAuth2
@@ -64,7 +101,7 @@ import { onThemeChange } from '@cervio/plugin-sdk'
64
101
 
65
102
  const unsubscribe = onThemeChange(({ tokens, colorScheme }) => {
66
103
  // react to a host theme change (host CSS variables are already applied
67
- // to `document` automatically — use this only if you need JS-side reactions)
104
+ // to `document` automatically - use this only if you need JS-side reactions)
68
105
  })
69
106
  ```
70
107
 
@@ -80,12 +117,31 @@ i18n.t('toolbar.newEntry')
80
117
  ```
81
118
 
82
119
  Resolve order: host locale (if enabled for this plugin) → `runtimeSettings.fallbackLocale`
83
- → plugin default. See `docs/plugins/AUTHORING.md` → "Plugin i18n / locale" in
120
+ → plugin default. See [Authoring](https://docs.cervio.app/docs/authoring) → "Plugin i18n / locale" in
84
121
  the host repo for the full recipe.
85
122
 
123
+ ## Write-gate plugins (`plugin_type: policy`)
124
+
125
+ A write-gate plugin runs in a server-side isolate before a record is created or
126
+ updated and answers allow or deny. It is not a record visibility policy (those are
127
+ configured in Admin, without code). Its helpers live in a separate entry with no
128
+ browser, React or PDF code:
129
+
130
+ ```js
131
+ import { definePolicy, allow, deny } from '@cervio/plugin-sdk/policy'
132
+
133
+ export default definePolicy(async (input, host) => {
134
+ if (input.record.data.blocked) return deny('Blocked by policy', 'BLOCKED')
135
+ return allow()
136
+ })
137
+ ```
138
+
139
+ The isolate loads a single self-contained module: bundle your entry (the scaffolder's
140
+ policy template does it with esbuild) instead of shipping bare imports.
141
+
86
142
  ## Versioning
87
143
 
88
- This package version tracks `CERVIO_UI_API_VERSION` (currently `1.0.0`), the
89
- `window.CervioUI.apiVersion` contract — not semver-independent feature
90
- releases. `@cervio/plugin-core` (manifest/ZIP validation) and `@cervio/cli`
91
- version independently.
144
+ This package follows semver on its own. `CERVIO_UI_API_VERSION` (currently `1.0.0`)
145
+ is the version of the `window.CervioUI` contract offered by the host, and changes
146
+ independently of this package's version. `@cervio/plugin-core` (manifest and ZIP
147
+ validation) and `@cervio/cli` also version independently.