@cervio/create-plugin 0.0.0-stage → 1.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 +21 -0
- package/README.md +68 -2
- package/dist/chunk-NNLHAM7U.js +185 -0
- package/dist/cli.js +10 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +6 -0
- package/package.json +54 -3
- package/template/.cursor/rules/cervio-plugin.mdc +39 -0
- package/template/AGENTS.md +213 -0
- package/template/README.md +43 -0
- package/template/cervio.json +5 -0
- package/template/gitignore +4 -0
- package/template/index.html +12 -0
- package/template/manifest.json +13 -0
- package/template/package.json +28 -0
- package/template/src/App.tsx +92 -0
- package/template/src/SurfaceApp.tsx +40 -0
- package/template/src/i18n/en.ts +17 -0
- package/template/src/i18n/index.ts +15 -0
- package/template/src/i18n/it.ts +17 -0
- package/template/src/index.css +99 -0
- package/template/src/main.tsx +10 -0
- package/template/src/surface-main.tsx +10 -0
- package/template/src/vite-env.d.ts +5 -0
- package/template/surface.html +12 -0
- package/template/tsconfig.json +18 -0
- package/template/vite.config.ts +37 -0
- package/template/vite.surface.config.ts +25 -0
- package/template-policy/README.md +46 -0
- package/template-policy/cervio.json +4 -0
- package/template-policy/gitignore +4 -0
- package/template-policy/manifest.json +28 -0
- package/template-policy/package.json +19 -0
- package/template-policy/server/index.js +35 -0
- package/template-policy/src/index.js +50 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="it">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>{{PLUGIN_NAME}}</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/@cervio/plugin-core/manifest.schema.json",
|
|
3
|
+
"plugin_key": "{{PLUGIN_KEY}}",
|
|
4
|
+
"name": "{{PLUGIN_NAME}}",
|
|
5
|
+
"plugin_type": "route",
|
|
6
|
+
"description": "Describe what this plugin does for the org that installs it.",
|
|
7
|
+
"requiredApiVersion": "1.0.0",
|
|
8
|
+
"scopes": ["entity:read"],
|
|
9
|
+
"entry": "src/main.tsx",
|
|
10
|
+
"cervio_apis": [
|
|
11
|
+
{ "method": "GET", "path_prefix": "/api/v1/me" }
|
|
12
|
+
]
|
|
13
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{PLUGIN_KEY}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "vite build && vite build --config vite.surface.config.ts",
|
|
9
|
+
"preview": "vite preview",
|
|
10
|
+
"validate": "cervio validate",
|
|
11
|
+
"push": "cervio push",
|
|
12
|
+
"push:dev": "cervio push --dev"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@cervio/plugin-sdk": "^2.0.0",
|
|
16
|
+
"react": "^19.0.0",
|
|
17
|
+
"react-dom": "^19.0.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@cervio/cli": "^1.0.0",
|
|
21
|
+
"@cervio/plugin-core": "^1.0.0",
|
|
22
|
+
"@types/react": "^19.0.0",
|
|
23
|
+
"@types/react-dom": "^19.0.0",
|
|
24
|
+
"@vitejs/plugin-react": "^6.2.0",
|
|
25
|
+
"typescript": "~5.9.0",
|
|
26
|
+
"vite": "^8.3.5"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import {
|
|
3
|
+
CervioApiError,
|
|
4
|
+
createCervioSessionClient,
|
|
5
|
+
getCervioUI,
|
|
6
|
+
getPluginContext,
|
|
7
|
+
isCervioUIAvailable,
|
|
8
|
+
isInsideCervioSandbox,
|
|
9
|
+
markReady,
|
|
10
|
+
type Identity,
|
|
11
|
+
} from '@cervio/plugin-sdk'
|
|
12
|
+
import { createAppI18n } from './i18n'
|
|
13
|
+
|
|
14
|
+
type State =
|
|
15
|
+
| { status: 'loading' }
|
|
16
|
+
| { status: 'error'; message: string }
|
|
17
|
+
| { status: 'ready'; identity: Identity }
|
|
18
|
+
|
|
19
|
+
export function App() {
|
|
20
|
+
const i18n = createAppI18n()
|
|
21
|
+
const ctx = getPluginContext()
|
|
22
|
+
const insideSandbox = isInsideCervioSandbox()
|
|
23
|
+
const [state, setState] = useState<State>({ status: 'loading' })
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (!insideSandbox) return
|
|
27
|
+
let cancelled = false
|
|
28
|
+
const client = createCervioSessionClient()
|
|
29
|
+
client
|
|
30
|
+
.me()
|
|
31
|
+
.then((identity) => {
|
|
32
|
+
if (!cancelled) setState({ status: 'ready', identity })
|
|
33
|
+
// First useful content is on screen: lets Cervio time the plugin open.
|
|
34
|
+
markReady()
|
|
35
|
+
})
|
|
36
|
+
.catch((err: unknown) => {
|
|
37
|
+
if (cancelled) return
|
|
38
|
+
const message =
|
|
39
|
+
err instanceof CervioApiError ? `${err.message} (${err.code})` : String(err)
|
|
40
|
+
setState({ status: 'error', message })
|
|
41
|
+
markReady()
|
|
42
|
+
})
|
|
43
|
+
return () => {
|
|
44
|
+
cancelled = true
|
|
45
|
+
}
|
|
46
|
+
}, [insideSandbox])
|
|
47
|
+
|
|
48
|
+
async function openSurfaceDemo() {
|
|
49
|
+
if (!isCervioUIAvailable()) return
|
|
50
|
+
// `dialog.surface` renders dist/surface.html in a host-owned Dialog -
|
|
51
|
+
// this is the recommended pattern for custom modals. Never build modals
|
|
52
|
+
// by injecting HTML into the parent document.
|
|
53
|
+
await getCervioUI().dialog.surface.open({
|
|
54
|
+
title: i18n.t('surface.title'),
|
|
55
|
+
size: 'md',
|
|
56
|
+
params: { greeting: i18n.t('surface.greeting') },
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div className="shell">
|
|
62
|
+
<header className="shell-header">
|
|
63
|
+
<h1 className="shell-title">{i18n.t('app.title')}</h1>
|
|
64
|
+
{ctx?.routeSlug ? (
|
|
65
|
+
<span className="shell-meta">
|
|
66
|
+
{i18n.t('app.routeLabel')}: <strong>{ctx.routeSlug}</strong>
|
|
67
|
+
</span>
|
|
68
|
+
) : null}
|
|
69
|
+
</header>
|
|
70
|
+
|
|
71
|
+
{!insideSandbox ? (
|
|
72
|
+
<div className="card">{i18n.t('app.outsideSandbox')}</div>
|
|
73
|
+
) : (
|
|
74
|
+
<div className="card">
|
|
75
|
+
{state.status === 'loading' ? <p>{i18n.t('app.loading')}</p> : null}
|
|
76
|
+
{state.status === 'error' ? (
|
|
77
|
+
<p className="error-banner">{i18n.t('app.error', { message: state.message })}</p>
|
|
78
|
+
) : null}
|
|
79
|
+
{state.status === 'ready' ? (
|
|
80
|
+
<p className="identity">
|
|
81
|
+
{i18n.t('app.identityLabel')}: {state.identity.user_id}
|
|
82
|
+
</p>
|
|
83
|
+
) : null}
|
|
84
|
+
</div>
|
|
85
|
+
)}
|
|
86
|
+
|
|
87
|
+
<button type="button" className="button-primary" onClick={openSurfaceDemo}>
|
|
88
|
+
{i18n.t('app.openSurface')}
|
|
89
|
+
</button>
|
|
90
|
+
</div>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import { getCervioUI, isCervioUIAvailable, type CervioUiHostEvent } from '@cervio/plugin-sdk'
|
|
3
|
+
import { createAppI18n } from './i18n'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Body of `CervioUI.dialog.surface` (`dist/surface.html`), rendered by the
|
|
7
|
+
* host inside a shadcn Dialog. Receives `params` from `dialog.surface.open()`
|
|
8
|
+
* via the `dialog.surface.opened` host event - the host does not pass params
|
|
9
|
+
* as querystring or postMessage payload on load.
|
|
10
|
+
*/
|
|
11
|
+
export function SurfaceApp() {
|
|
12
|
+
const i18n = createAppI18n()
|
|
13
|
+
const [params, setParams] = useState<unknown>(null)
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
function onMessage(event: MessageEvent) {
|
|
17
|
+
const data = event.data as CervioUiHostEvent | undefined
|
|
18
|
+
if (!data || data.__cervioUI !== 1) return
|
|
19
|
+
if (data.event === 'dialog.surface.opened') setParams(data.params ?? null)
|
|
20
|
+
}
|
|
21
|
+
window.addEventListener('message', onMessage)
|
|
22
|
+
return () => window.removeEventListener('message', onMessage)
|
|
23
|
+
}, [])
|
|
24
|
+
|
|
25
|
+
function close() {
|
|
26
|
+
if (!isCervioUIAvailable()) return
|
|
27
|
+
void getCervioUI().dialog.surface.close()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<div className="surface">
|
|
32
|
+
<h2>{i18n.t('surface.title')}</h2>
|
|
33
|
+
<p>{i18n.t('surface.greeting')}</p>
|
|
34
|
+
{params != null ? <pre>{JSON.stringify(params, null, 2)}</pre> : null}
|
|
35
|
+
<button type="button" className="button-primary" onClick={close}>
|
|
36
|
+
{i18n.t('surface.close')}
|
|
37
|
+
</button>
|
|
38
|
+
</div>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const en = {
|
|
2
|
+
app: {
|
|
3
|
+
title: '{{PLUGIN_NAME}}',
|
|
4
|
+
routeLabel: 'Route',
|
|
5
|
+
loading: 'Loading…',
|
|
6
|
+
error: 'Error: {message}',
|
|
7
|
+
identityLabel: 'User',
|
|
8
|
+
openSurface: 'Open sample dialog',
|
|
9
|
+
outsideSandbox:
|
|
10
|
+
'Local preview (outside the Cervio sandbox): CervioUI and /api/v1 calls are unavailable.',
|
|
11
|
+
},
|
|
12
|
+
surface: {
|
|
13
|
+
title: 'Sample dialog',
|
|
14
|
+
greeting: 'Hello from the plugin!',
|
|
15
|
+
close: 'Close',
|
|
16
|
+
},
|
|
17
|
+
} as const
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route plugins run in a sandboxed `srcDoc` iframe: next-intl / `public/locales`
|
|
3
|
+
* are not available, so catalogs ship inside the ZIP. Keep identical key sets
|
|
4
|
+
* across every locale - see AGENTS.md and the host's
|
|
5
|
+
* https://docs.cervio.app/docs/authoring/performance-safety → "Plugin i18n / locale" for the resolve order.
|
|
6
|
+
*/
|
|
7
|
+
import { createI18n, type CervioI18nCatalog } from '@cervio/plugin-sdk'
|
|
8
|
+
import { it } from './it'
|
|
9
|
+
import { en } from './en'
|
|
10
|
+
|
|
11
|
+
export const messages: CervioI18nCatalog = { it, en }
|
|
12
|
+
|
|
13
|
+
export function createAppI18n() {
|
|
14
|
+
return createI18n({ defaultLocale: 'it', messages })
|
|
15
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const it = {
|
|
2
|
+
app: {
|
|
3
|
+
title: '{{PLUGIN_NAME}}',
|
|
4
|
+
routeLabel: 'Route',
|
|
5
|
+
loading: 'Caricamento…',
|
|
6
|
+
error: 'Errore: {message}',
|
|
7
|
+
identityLabel: 'Utente',
|
|
8
|
+
openSurface: 'Apri finestra di esempio',
|
|
9
|
+
outsideSandbox:
|
|
10
|
+
'Anteprima locale (fuori dalla sandbox Cervio): CervioUI e le API /api/v1 non sono disponibili.',
|
|
11
|
+
},
|
|
12
|
+
surface: {
|
|
13
|
+
title: 'Finestra di esempio',
|
|
14
|
+
greeting: 'Ciao dal plugin!',
|
|
15
|
+
close: 'Chiudi',
|
|
16
|
+
},
|
|
17
|
+
} as const
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/* Design tokens come only from the host (`data-cervio-theme-bridge`); do not
|
|
2
|
+
define :root here - a bundled :root would load after the bridge and
|
|
3
|
+
override the host palette. Use var(--background), var(--foreground), etc. */
|
|
4
|
+
|
|
5
|
+
* {
|
|
6
|
+
box-sizing: border-box;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
body {
|
|
10
|
+
margin: 0;
|
|
11
|
+
font-family: var(--font-sans, system-ui, sans-serif);
|
|
12
|
+
color: var(--foreground);
|
|
13
|
+
background: var(--background);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.shell {
|
|
17
|
+
min-height: 100%;
|
|
18
|
+
padding: 1rem 1.25rem 1.25rem;
|
|
19
|
+
display: flex;
|
|
20
|
+
flex-direction: column;
|
|
21
|
+
gap: 1rem;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.shell-header {
|
|
25
|
+
display: flex;
|
|
26
|
+
align-items: baseline;
|
|
27
|
+
justify-content: space-between;
|
|
28
|
+
gap: 1rem;
|
|
29
|
+
flex-wrap: wrap;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.shell-title {
|
|
33
|
+
margin: 0;
|
|
34
|
+
font-size: 1.125rem;
|
|
35
|
+
font-weight: 600;
|
|
36
|
+
letter-spacing: -0.02em;
|
|
37
|
+
color: var(--foreground);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.shell-meta {
|
|
41
|
+
font-size: 0.75rem;
|
|
42
|
+
color: var(--muted-foreground);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.card {
|
|
46
|
+
background: var(--card);
|
|
47
|
+
color: var(--card-foreground);
|
|
48
|
+
border: 1px solid var(--border);
|
|
49
|
+
border-radius: var(--radius-lg);
|
|
50
|
+
padding: 1rem 1.1rem;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.identity {
|
|
54
|
+
font-size: 0.875rem;
|
|
55
|
+
color: var(--muted-foreground);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.error-banner {
|
|
59
|
+
border: 1px solid var(--border);
|
|
60
|
+
border-radius: var(--radius-lg);
|
|
61
|
+
padding: 0.75rem 1rem;
|
|
62
|
+
font-size: 0.875rem;
|
|
63
|
+
color: var(--destructive, oklch(0.577 0.245 27.325));
|
|
64
|
+
background: color-mix(in oklch, var(--destructive, oklch(0.577 0.245 27.325)) 8%, var(--card));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.button-primary {
|
|
68
|
+
align-self: flex-start;
|
|
69
|
+
border: none;
|
|
70
|
+
border-radius: var(--radius-lg);
|
|
71
|
+
padding: 0.5rem 1rem;
|
|
72
|
+
font-size: 0.875rem;
|
|
73
|
+
font-weight: 500;
|
|
74
|
+
color: var(--primary-foreground);
|
|
75
|
+
background: var(--primary);
|
|
76
|
+
cursor: pointer;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.button-primary:hover {
|
|
80
|
+
filter: brightness(0.95);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.surface {
|
|
84
|
+
padding: 1rem 1.25rem;
|
|
85
|
+
display: flex;
|
|
86
|
+
flex-direction: column;
|
|
87
|
+
gap: 0.75rem;
|
|
88
|
+
color: var(--foreground);
|
|
89
|
+
background: var(--background);
|
|
90
|
+
min-height: 100%;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.surface pre {
|
|
94
|
+
background: var(--muted);
|
|
95
|
+
border-radius: var(--radius-lg);
|
|
96
|
+
padding: 0.75rem;
|
|
97
|
+
font-size: 0.75rem;
|
|
98
|
+
overflow: auto;
|
|
99
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="it">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>{{PLUGIN_NAME}} - surface</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="/src/surface-main.tsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"useDefineForClassFields": true,
|
|
5
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"moduleResolution": "bundler",
|
|
9
|
+
"isolatedModules": true,
|
|
10
|
+
"moduleDetection": "force",
|
|
11
|
+
"strict": true,
|
|
12
|
+
"noEmit": true,
|
|
13
|
+
"jsx": "react-jsx",
|
|
14
|
+
"noUnusedLocals": true,
|
|
15
|
+
"noUnusedParameters": true
|
|
16
|
+
},
|
|
17
|
+
"include": ["src"]
|
|
18
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url'
|
|
2
|
+
import { defineConfig } from 'vite'
|
|
3
|
+
import react from '@vitejs/plugin-react'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Route page build: one entry JS + one CSS. Cervio serves `dist/` as a cached
|
|
7
|
+
* document per uploaded version and inlines the entry JS (up to ~384 KB) and
|
|
8
|
+
* CSS into it, so the first paint needs no extra request.
|
|
9
|
+
*
|
|
10
|
+
* Chunks are allowed for dynamic `import()` only: use them for heavy features
|
|
11
|
+
* the user opens rarely (an export, a chart library). A sandboxed iframe does
|
|
12
|
+
* not cache its own requests, so a chunk is downloaded each time it is used;
|
|
13
|
+
* everything on the first screen belongs in the entry.
|
|
14
|
+
*
|
|
15
|
+
* `surface.html` is built by a second pass (`vite.surface.config.ts`) so the
|
|
16
|
+
* two entries never share chunks.
|
|
17
|
+
*/
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
plugins: [react()],
|
|
20
|
+
base: './',
|
|
21
|
+
build: {
|
|
22
|
+
target: 'es2022',
|
|
23
|
+
outDir: 'dist',
|
|
24
|
+
assetsDir: 'assets',
|
|
25
|
+
emptyOutDir: true,
|
|
26
|
+
cssCodeSplit: false,
|
|
27
|
+
modulePreload: false,
|
|
28
|
+
rolldownOptions: {
|
|
29
|
+
input: fileURLToPath(new URL('./index.html', import.meta.url)),
|
|
30
|
+
output: {
|
|
31
|
+
chunkFileNames: 'assets/[name]-[hash].js',
|
|
32
|
+
entryFileNames: 'assets/[name]-[hash].js',
|
|
33
|
+
assetFileNames: 'assets/[name]-[hash][extname]',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url'
|
|
2
|
+
import { defineConfig } from 'vite'
|
|
3
|
+
import react from '@vitejs/plugin-react'
|
|
4
|
+
|
|
5
|
+
/** Second pass for `dist/surface.html`, same rules as `vite.config.ts` (own entry, no shared chunks). */
|
|
6
|
+
export default defineConfig({
|
|
7
|
+
plugins: [react()],
|
|
8
|
+
base: './',
|
|
9
|
+
build: {
|
|
10
|
+
target: 'es2022',
|
|
11
|
+
outDir: 'dist',
|
|
12
|
+
assetsDir: 'assets',
|
|
13
|
+
emptyOutDir: false,
|
|
14
|
+
cssCodeSplit: false,
|
|
15
|
+
modulePreload: false,
|
|
16
|
+
rolldownOptions: {
|
|
17
|
+
input: fileURLToPath(new URL('./surface.html', import.meta.url)),
|
|
18
|
+
output: {
|
|
19
|
+
chunkFileNames: 'assets/[name]-[hash].js',
|
|
20
|
+
entryFileNames: 'assets/[name]-[hash].js',
|
|
21
|
+
assetFileNames: 'assets/[name]-[hash][extname]',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Cervio write-gate plugin
|
|
2
|
+
|
|
3
|
+
A write-gate plugin (`plugin_type: policy`) runs before a record is created or
|
|
4
|
+
updated (`before_create` / `before_update`) and answers allow or deny. It is not a
|
|
5
|
+
record visibility policy: those are configured in Admin → Permissions, without code.
|
|
6
|
+
|
|
7
|
+
The host runs your entry as a **single ESM module** in a server-side isolate, with
|
|
8
|
+
no `node_modules` resolution, no network and no writes.
|
|
9
|
+
|
|
10
|
+
## Workflow
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install # or pnpm / yarn
|
|
14
|
+
# edit src/index.js (uses @cervio/plugin-sdk/policy)
|
|
15
|
+
npm run build # bundles into server/index.js
|
|
16
|
+
npm run validate # build + cervio validate
|
|
17
|
+
npm run pack # build + cervio pack
|
|
18
|
+
npm run push # build + cervio push
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then bind the hook in Admin → Plugins. Disabling the binding works as a kill switch
|
|
22
|
+
without uploading a new version.
|
|
23
|
+
|
|
24
|
+
## Why build?
|
|
25
|
+
|
|
26
|
+
`cervio pack` and `cervio validate` reject bare imports in `server/index.js`
|
|
27
|
+
(for example `from '@cervio/plugin-sdk/policy'`). Always bundle with the provided
|
|
28
|
+
esbuild script.
|
|
29
|
+
|
|
30
|
+
## Manifest
|
|
31
|
+
|
|
32
|
+
`plugin_type` must be `policy`. Hooks point at `server/index.js`.
|
|
33
|
+
|
|
34
|
+
Declare the entities you need to read under `entities` with `access: ["read"]`: the
|
|
35
|
+
host uses that list as the allowlist for `host.getRecord`, `host.listRecords` and
|
|
36
|
+
`host.sumField`.
|
|
37
|
+
|
|
38
|
+
## SDK
|
|
39
|
+
|
|
40
|
+
[`@cervio/plugin-sdk/policy`](https://www.npmjs.com/package/@cervio/plugin-sdk):
|
|
41
|
+
`allow()`, `deny(message, code?)`, `definePolicy((input, host) => ...)`.
|
|
42
|
+
|
|
43
|
+
Host reads run as the user who saves the record, share the hook's `deadlineMs`, and
|
|
44
|
+
cannot change data.
|
|
45
|
+
|
|
46
|
+
Partner docs: [docs.cervio.app](https://docs.cervio.app), Authoring → Policy plugins.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"plugin_key": "example-policy-guard",
|
|
3
|
+
"plugin_name": "Example write-gate plugin",
|
|
4
|
+
"plugin_type": "policy",
|
|
5
|
+
"hooks": [
|
|
6
|
+
{
|
|
7
|
+
"event": "before_create",
|
|
8
|
+
"entities": ["example_entity"],
|
|
9
|
+
"blocking": true,
|
|
10
|
+
"identity": "caller",
|
|
11
|
+
"entry": "server/index.js"
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"settings": [
|
|
15
|
+
{
|
|
16
|
+
"key": "block_message",
|
|
17
|
+
"type": "string",
|
|
18
|
+
"label": "Deny message",
|
|
19
|
+
"default": "Save blocked by example write-gate plugin"
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
"entities": [
|
|
23
|
+
{
|
|
24
|
+
"name": "example_entity",
|
|
25
|
+
"access": ["read"]
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{PLUGIN_KEY}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "esbuild src/index.js --bundle --format=esm --platform=neutral --target=es2022 --outfile=server/index.js",
|
|
7
|
+
"pack": "npm run build && cervio pack",
|
|
8
|
+
"validate": "npm run build && cervio validate",
|
|
9
|
+
"push": "npm run build && cervio push",
|
|
10
|
+
"push:dev": "npm run build && cervio push --dev"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@cervio/plugin-sdk": "^2.0.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@cervio/cli": "^1.0.0",
|
|
17
|
+
"esbuild": "^0.28.0"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// node_modules/@cervio/plugin-sdk/dist/policy/index.js
|
|
2
|
+
function allow() {
|
|
3
|
+
return { allow: true };
|
|
4
|
+
}
|
|
5
|
+
function deny(message, code) {
|
|
6
|
+
const trimmed = String(message ?? "").trim();
|
|
7
|
+
if (!trimmed) {
|
|
8
|
+
throw new Error("deny() requires a non-empty message");
|
|
9
|
+
}
|
|
10
|
+
return code ? { allow: false, message: trimmed, code } : { allow: false, message: trimmed };
|
|
11
|
+
}
|
|
12
|
+
function definePolicy(handler, options) {
|
|
13
|
+
const catchErrors = options?.catchErrors === true;
|
|
14
|
+
return async (input, host = null) => {
|
|
15
|
+
try {
|
|
16
|
+
return await handler(input, host);
|
|
17
|
+
} catch (err) {
|
|
18
|
+
if (!catchErrors) throw err;
|
|
19
|
+
const message = err instanceof Error && err.message.trim() ? err.message.trim() : "Policy Plugin error";
|
|
20
|
+
return deny(message);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/index.js
|
|
26
|
+
var index_default = definePolicy(async (input, host) => {
|
|
27
|
+
const message = (input.settings && typeof input.settings.block_message === "string" ? input.settings.block_message : null) || "Save blocked by example write-gate plugin";
|
|
28
|
+
if (input.record?.data?.blocked === true) {
|
|
29
|
+
return deny(message, "EXAMPLE_BLOCKED");
|
|
30
|
+
}
|
|
31
|
+
return allow();
|
|
32
|
+
});
|
|
33
|
+
export {
|
|
34
|
+
index_default as default
|
|
35
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write-gate plugin source: edit this file, then `npm run build`
|
|
3
|
+
* (bundles into `server/index.js`). The host isolate cannot resolve node_modules.
|
|
4
|
+
*
|
|
5
|
+
* Host-mediated reads: declare entities with access:["read"] in manifest.json,
|
|
6
|
+
* then use the second `host` argument (getRecord / listRecords / sumField).
|
|
7
|
+
* Reads run as the save caller; they share the hook deadlineMs.
|
|
8
|
+
*/
|
|
9
|
+
import { definePolicy, allow, deny } from '@cervio/plugin-sdk/policy'
|
|
10
|
+
|
|
11
|
+
export default definePolicy(async (input, host) => {
|
|
12
|
+
const message =
|
|
13
|
+
(input.settings && typeof input.settings.block_message === 'string'
|
|
14
|
+
? input.settings.block_message
|
|
15
|
+
: null) || 'Save blocked by example write-gate plugin'
|
|
16
|
+
|
|
17
|
+
// Demo: deny when the record payload sets `blocked: true`.
|
|
18
|
+
if (input.record?.data?.blocked === true) {
|
|
19
|
+
return deny(message, 'EXAMPLE_BLOCKED')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Example host reads (requires manifest.entities with access:["read"]):
|
|
23
|
+
//
|
|
24
|
+
// if (host) {
|
|
25
|
+
// const parentId = String(input.record?.data?.parent_id ?? '')
|
|
26
|
+
// if (parentId) {
|
|
27
|
+
// const parent = await host.getRecord('parent_entity', parentId)
|
|
28
|
+
// const related = await host.listRecords('child_entity', {
|
|
29
|
+
// filters: { parent_id: parentId },
|
|
30
|
+
// limit: 50,
|
|
31
|
+
// })
|
|
32
|
+
// const total = await host.sumField('child_entity', 'amount', {
|
|
33
|
+
// filters: { parent_id: parentId },
|
|
34
|
+
// })
|
|
35
|
+
// // Soft budget sketch (hours overrun consumer):
|
|
36
|
+
// // const budget = Number(parent?.data?.budget_ore ?? 0)
|
|
37
|
+
// // const used = await host.sumField('dettaglio_attivita', 'ore', {
|
|
38
|
+
// // filters: { attivita_id: parentId },
|
|
39
|
+
// // })
|
|
40
|
+
// // const next = Number(input.record?.data?.ore ?? 0)
|
|
41
|
+
// // const remaining = budget - used - next
|
|
42
|
+
// // if (remaining < 0) return deny(`Ore rimanenti insufficienti [${remaining}]`, 'OVER_BUDGET')
|
|
43
|
+
// void parent
|
|
44
|
+
// void related
|
|
45
|
+
// void total
|
|
46
|
+
// }
|
|
47
|
+
// }
|
|
48
|
+
|
|
49
|
+
return allow()
|
|
50
|
+
})
|