@selfhelp/shared 1.0.1 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/dist/index.d.mts +19 -13
- package/dist/index.d.ts +19 -13
- package/dist/index.js +96 -6
- package/dist/index.mjs +88 -6
- package/dist/plugin-sdk/index.d.mts +906 -0
- package/dist/plugin-sdk/index.d.ts +906 -0
- package/dist/plugin-sdk/index.js +490 -0
- package/dist/plugin-sdk/index.mjs +452 -0
- package/dist/registry/index.d.mts +56 -1
- package/dist/registry/index.d.ts +56 -1
- package/dist/registry/index.js +94 -5
- package/dist/registry/index.mjs +85 -4
- package/dist/{index-Bg60YTsF.d.mts → styles.registry-C3bux91U.d.mts} +569 -28
- package/dist/{index-DEOVT86P.d.ts → styles.registry-Cr961HQ0.d.ts} +569 -28
- package/package.json +19 -3
- package/src/plugin-sdk/define.ts +89 -0
- package/src/plugin-sdk/index.ts +91 -0
- package/src/plugin-sdk/lock.ts +124 -0
- package/src/plugin-sdk/manifest.ts +295 -0
- package/src/plugin-sdk/realtime.ts +277 -0
- package/src/plugin-sdk/registry.ts +95 -0
- package/src/plugin-sdk/types.ts +265 -0
- package/src/plugin-sdk/version.ts +209 -0
- package/src/registry/index.ts +68 -11
- package/src/registry/styles.registry.ts +149 -12
- package/src/types/pages.ts +2 -2
- package/src/utils/transformPageData.ts +18 -12
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `usePluginRealtime` — universal React hook for plugin Mercure topic
|
|
7
|
+
* subscription.
|
|
8
|
+
*
|
|
9
|
+
* Implements the contract laid out in the plan section 10 "Realtime
|
|
10
|
+
* infrastructure & no-polling policy":
|
|
11
|
+
*
|
|
12
|
+
* const { data, isOnline, lastEventAt, error } = usePluginRealtime<TPayload>({
|
|
13
|
+
* pluginId: 'sh2-shp-survey-js',
|
|
14
|
+
* topic: 'surveys/{surveyId}/responses',
|
|
15
|
+
* topicParams: { surveyId: 42 },
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* The hook:
|
|
19
|
+
*
|
|
20
|
+
* - opens a same-origin `EventSource` against
|
|
21
|
+
* `/api/plugins/events?pluginId=…&topic=…` (the host BFF route),
|
|
22
|
+
* - listens for messages on the requested topic event-name,
|
|
23
|
+
* - exposes the latest payload + connection state,
|
|
24
|
+
* - reconnects with exponential backoff on transient errors,
|
|
25
|
+
* - is a no-op in environments without `EventSource` (server / RN).
|
|
26
|
+
*
|
|
27
|
+
* Mobile apps must inject their own `react-native-sse`-backed
|
|
28
|
+
* implementation through the optional `transportFactory` argument;
|
|
29
|
+
* this keeps the shared SDK free of React Native imports while still
|
|
30
|
+
* letting the same plugin code re-use the same hook signature on web
|
|
31
|
+
* and on device. The mobile package documents the wrapper.
|
|
32
|
+
*
|
|
33
|
+
* IMPORTANT: this hook never polls. If `EventSource` cannot connect, it
|
|
34
|
+
* surfaces `isOnline = false` and the consumer is expected to show an
|
|
35
|
+
* "offline" banner — not to set up a `setInterval` fallback.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import { useEffect, useRef, useState } from 'react';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Subset of the WHATWG `EventSource` API the hook actually relies on.
|
|
42
|
+
* Declared inline so the SDK does not pull DOM types into projects that
|
|
43
|
+
* disable `dom` (e.g. some Expo TypeScript setups).
|
|
44
|
+
*/
|
|
45
|
+
export interface IRealtimeTransport {
|
|
46
|
+
addEventListener(type: 'open' | 'error' | string, listener: (evt: unknown) => void): void;
|
|
47
|
+
removeEventListener(type: 'open' | 'error' | string, listener: (evt: unknown) => void): void;
|
|
48
|
+
close(): void;
|
|
49
|
+
readyState: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type TRealtimeTransportFactory = (url: string) => IRealtimeTransport;
|
|
53
|
+
|
|
54
|
+
export interface IUsePluginRealtimeOptions<TPayload = unknown> {
|
|
55
|
+
/** Plugin id (`plugin.json#id`). */
|
|
56
|
+
pluginId: string;
|
|
57
|
+
/** Topic key as declared in the manifest (e.g. `surveys/{surveyId}/responses`). */
|
|
58
|
+
topic: string;
|
|
59
|
+
/** Values that fill the `{placeholders}` in `topic`. */
|
|
60
|
+
topicParams?: Record<string, string | number>;
|
|
61
|
+
/** Disable subscription without unmounting the consumer. */
|
|
62
|
+
enabled?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Override the SSE endpoint. Defaults to `/api/plugins/events`. The
|
|
65
|
+
* host BFF proxies it through to Symfony's per-plugin Mercure
|
|
66
|
+
* bootstrap so the subscriber JWT never reaches the browser.
|
|
67
|
+
*/
|
|
68
|
+
endpoint?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Optional transport factory. Used by the mobile package to plug in
|
|
71
|
+
* `react-native-sse` (or any other SSE-compatible client) without
|
|
72
|
+
* pulling RN imports into the shared SDK.
|
|
73
|
+
*/
|
|
74
|
+
transportFactory?: TRealtimeTransportFactory;
|
|
75
|
+
/** Parser for the event payload. Defaults to `JSON.parse`. */
|
|
76
|
+
parse?: (raw: string) => TPayload;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface IUsePluginRealtimeResult<TPayload = unknown> {
|
|
80
|
+
/** Latest payload emitted on the topic, or `null` before the first event. */
|
|
81
|
+
data: TPayload | null;
|
|
82
|
+
/** `true` once the SSE handshake has succeeded for the current subscription. */
|
|
83
|
+
isOnline: boolean;
|
|
84
|
+
/** Wall-clock time of the last successful event, or `null`. */
|
|
85
|
+
lastEventAt: Date | null;
|
|
86
|
+
/** Most recent transport-level error, or `null` when healthy. */
|
|
87
|
+
error: Error | null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const DEFAULT_ENDPOINT = '/api/plugins/events';
|
|
91
|
+
const MAX_RECONNECT_DELAY_MS = 30_000;
|
|
92
|
+
const INITIAL_RECONNECT_DELAY_MS = 1_000;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Replace `{placeholders}` in the topic key with the provided params and
|
|
96
|
+
* URL-encode every value. Throws if any placeholder is unresolved so
|
|
97
|
+
* misuse fails loudly rather than subscribing to a wildcard topic.
|
|
98
|
+
*/
|
|
99
|
+
function expandTopic(topic: string, params: Record<string, string | number>): string {
|
|
100
|
+
return topic.replace(/\{([^}]+)\}/g, (_match, name: string) => {
|
|
101
|
+
const value = params[name];
|
|
102
|
+
if (value === undefined) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`usePluginRealtime: topic "${topic}" references {${name}} but topicParams.${name} was not provided.`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
return encodeURIComponent(String(value));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function buildSseUrl(
|
|
112
|
+
endpoint: string,
|
|
113
|
+
pluginId: string,
|
|
114
|
+
expandedTopic: string,
|
|
115
|
+
): string {
|
|
116
|
+
const params = new URLSearchParams({
|
|
117
|
+
pluginId,
|
|
118
|
+
topic: expandedTopic,
|
|
119
|
+
});
|
|
120
|
+
const separator = endpoint.includes('?') ? '&' : '?';
|
|
121
|
+
return `${endpoint}${separator}${params.toString()}`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function isEventSourceAvailable(): boolean {
|
|
125
|
+
return typeof globalThis !== 'undefined' && typeof (globalThis as Record<string, unknown>).EventSource !== 'undefined';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function defaultTransportFactory(url: string): IRealtimeTransport {
|
|
129
|
+
const Ctor = (globalThis as Record<string, unknown>).EventSource as new (url: string) => IRealtimeTransport;
|
|
130
|
+
return new Ctor(url);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Subscribe a React/React-Native component to one Mercure plugin topic
|
|
135
|
+
* and re-render whenever a new payload arrives.
|
|
136
|
+
*
|
|
137
|
+
* Returns `{ data, isOnline, lastEventAt, error }`. Always cleans up on
|
|
138
|
+
* unmount and on prop change.
|
|
139
|
+
*/
|
|
140
|
+
export function usePluginRealtime<TPayload = unknown>(
|
|
141
|
+
options: IUsePluginRealtimeOptions<TPayload>,
|
|
142
|
+
): IUsePluginRealtimeResult<TPayload> {
|
|
143
|
+
const {
|
|
144
|
+
pluginId,
|
|
145
|
+
topic,
|
|
146
|
+
topicParams = {},
|
|
147
|
+
enabled = true,
|
|
148
|
+
endpoint = DEFAULT_ENDPOINT,
|
|
149
|
+
transportFactory,
|
|
150
|
+
parse,
|
|
151
|
+
} = options;
|
|
152
|
+
|
|
153
|
+
const [data, setData] = useState<TPayload | null>(null);
|
|
154
|
+
const [isOnline, setIsOnline] = useState<boolean>(false);
|
|
155
|
+
const [lastEventAt, setLastEventAt] = useState<Date | null>(null);
|
|
156
|
+
const [error, setError] = useState<Error | null>(null);
|
|
157
|
+
|
|
158
|
+
// Stable refs so the effect closures always read the latest callback /
|
|
159
|
+
// parser without forcing a reconnection on every render.
|
|
160
|
+
const parseRef = useRef(parse);
|
|
161
|
+
parseRef.current = parse;
|
|
162
|
+
|
|
163
|
+
// Stringify topicParams once so React effect dependency comparison is
|
|
164
|
+
// deterministic regardless of object identity churn in the caller.
|
|
165
|
+
const topicParamsKey = JSON.stringify(topicParams);
|
|
166
|
+
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
if (!enabled) {
|
|
169
|
+
setIsOnline(false);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const factory = transportFactory ?? (isEventSourceAvailable() ? defaultTransportFactory : null);
|
|
174
|
+
if (factory === null) {
|
|
175
|
+
// No transport available (e.g. SSR or RN without injected
|
|
176
|
+
// `react-native-sse`). Stay quiet — the plan forbids polling
|
|
177
|
+
// here; the consumer must render an offline banner instead.
|
|
178
|
+
setIsOnline(false);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let cancelled = false;
|
|
183
|
+
let transport: IRealtimeTransport | null = null;
|
|
184
|
+
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
185
|
+
let reconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
186
|
+
|
|
187
|
+
let expandedTopic: string;
|
|
188
|
+
try {
|
|
189
|
+
expandedTopic = expandTopic(topic, JSON.parse(topicParamsKey) as Record<string, string | number>);
|
|
190
|
+
} catch (err) {
|
|
191
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
192
|
+
setIsOnline(false);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const eventName = `plugin/${pluginId}/${expandedTopic}`;
|
|
197
|
+
const url = buildSseUrl(endpoint, pluginId, expandedTopic);
|
|
198
|
+
|
|
199
|
+
const onOpen = (): void => {
|
|
200
|
+
if (cancelled) return;
|
|
201
|
+
reconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
202
|
+
setIsOnline(true);
|
|
203
|
+
setError(null);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const onEvent = (evt: unknown): void => {
|
|
207
|
+
if (cancelled) return;
|
|
208
|
+
const raw = (evt as { data?: string }).data;
|
|
209
|
+
if (typeof raw !== 'string') return;
|
|
210
|
+
try {
|
|
211
|
+
const payload = parseRef.current
|
|
212
|
+
? parseRef.current(raw)
|
|
213
|
+
: (JSON.parse(raw) as TPayload);
|
|
214
|
+
setData(payload);
|
|
215
|
+
setLastEventAt(new Date());
|
|
216
|
+
} catch (parseErr) {
|
|
217
|
+
setError(parseErr instanceof Error ? parseErr : new Error(String(parseErr)));
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const onError = (evt: unknown): void => {
|
|
222
|
+
if (cancelled || !transport) return;
|
|
223
|
+
setIsOnline(false);
|
|
224
|
+
setError(evt instanceof Error ? evt : new Error('Plugin realtime transport error'));
|
|
225
|
+
// readyState === 2 (CLOSED) means the browser will NOT
|
|
226
|
+
// auto-reconnect (4xx/network); restart manually with backoff.
|
|
227
|
+
if (transport.readyState === 2) {
|
|
228
|
+
try {
|
|
229
|
+
transport.close();
|
|
230
|
+
} catch {
|
|
231
|
+
// ignore — already closed.
|
|
232
|
+
}
|
|
233
|
+
transport = null;
|
|
234
|
+
reconnectTimer = setTimeout(connect, reconnectDelay);
|
|
235
|
+
reconnectDelay = Math.min(reconnectDelay * 2, MAX_RECONNECT_DELAY_MS);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const connect = (): void => {
|
|
240
|
+
if (cancelled) return;
|
|
241
|
+
try {
|
|
242
|
+
transport = factory(url);
|
|
243
|
+
} catch (createErr) {
|
|
244
|
+
setError(createErr instanceof Error ? createErr : new Error(String(createErr)));
|
|
245
|
+
setIsOnline(false);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
transport.addEventListener('open', onOpen);
|
|
249
|
+
transport.addEventListener(eventName, onEvent);
|
|
250
|
+
transport.addEventListener('message', onEvent);
|
|
251
|
+
transport.addEventListener('error', onError);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
connect();
|
|
255
|
+
|
|
256
|
+
return (): void => {
|
|
257
|
+
cancelled = true;
|
|
258
|
+
if (reconnectTimer !== null) {
|
|
259
|
+
clearTimeout(reconnectTimer);
|
|
260
|
+
}
|
|
261
|
+
if (transport) {
|
|
262
|
+
try {
|
|
263
|
+
transport.removeEventListener('open', onOpen);
|
|
264
|
+
transport.removeEventListener(eventName, onEvent);
|
|
265
|
+
transport.removeEventListener('message', onEvent);
|
|
266
|
+
transport.removeEventListener('error', onError);
|
|
267
|
+
transport.close();
|
|
268
|
+
} catch {
|
|
269
|
+
// ignore — best-effort cleanup.
|
|
270
|
+
}
|
|
271
|
+
transport = null;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}, [enabled, endpoint, pluginId, topic, topicParamsKey, transportFactory]);
|
|
275
|
+
|
|
276
|
+
return { data, isOnline, lastEventAt, error };
|
|
277
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* TypeScript mirror of `registry.json` served by a plugin registry.
|
|
7
|
+
* The host installer fetches registries (public and private), merges
|
|
8
|
+
* per-channel metadata, and shows update availability + changelogs in
|
|
9
|
+
* the admin UI.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type TPluginRegistryChannel = 'stable' | 'beta' | 'alpha' | 'nightly';
|
|
13
|
+
|
|
14
|
+
export interface IPluginRegistryVersionEntry {
|
|
15
|
+
/** SemVer version. */
|
|
16
|
+
version: string;
|
|
17
|
+
/** Channel this version was published to. */
|
|
18
|
+
channel: TPluginRegistryChannel;
|
|
19
|
+
/** ISO datetime the version was published. */
|
|
20
|
+
publishedAt: string;
|
|
21
|
+
/** Whether this version is a breaking change vs the previous one. */
|
|
22
|
+
breaking?: boolean;
|
|
23
|
+
/** Whether the host installer should block this version. */
|
|
24
|
+
blocked?: boolean;
|
|
25
|
+
/** Optional reason for `blocked: true`. */
|
|
26
|
+
blockedReason?: string;
|
|
27
|
+
/** Composer / npm tarball or git source URL. */
|
|
28
|
+
sources: {
|
|
29
|
+
backend?: { type: 'composer' | 'git'; package?: string; ref?: string; url?: string };
|
|
30
|
+
frontend?: { type: 'npm' | 'git'; package?: string; url?: string };
|
|
31
|
+
mobile?: { type: 'npm' | 'git'; package?: string; url?: string };
|
|
32
|
+
};
|
|
33
|
+
/** SHA-256 checksum of the published artifact. */
|
|
34
|
+
checksumSha256: string;
|
|
35
|
+
/** Optional Ed25519 signature, base64. */
|
|
36
|
+
signatureEd25519?: string;
|
|
37
|
+
/** Compatibility metadata mirrored from the manifest. */
|
|
38
|
+
compatibility: {
|
|
39
|
+
selfhelp: string;
|
|
40
|
+
php?: string;
|
|
41
|
+
node?: string;
|
|
42
|
+
react?: string;
|
|
43
|
+
reactNative?: string;
|
|
44
|
+
expoSdk?: string;
|
|
45
|
+
pluginApiVersion: string;
|
|
46
|
+
};
|
|
47
|
+
/** Plain-text changelog. Markdown supported. */
|
|
48
|
+
changelog?: string;
|
|
49
|
+
/** Mirror of `dependencies` from the manifest, for resolver-only consumption. */
|
|
50
|
+
dependencies?: { pluginId: string; version: string; required: boolean }[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface IPluginRegistryEntry {
|
|
54
|
+
/** Plugin id (e.g. `sh2-shp-survey-js`). */
|
|
55
|
+
id: string;
|
|
56
|
+
/** Display name. */
|
|
57
|
+
name: string;
|
|
58
|
+
/** Short description. */
|
|
59
|
+
description?: string;
|
|
60
|
+
/** Homepage / repo URL. */
|
|
61
|
+
homepage?: string;
|
|
62
|
+
/** Trust level published by the registry. */
|
|
63
|
+
trustLevel: 'official' | 'reviewed' | 'untrusted';
|
|
64
|
+
/** Available versions. */
|
|
65
|
+
versions: IPluginRegistryVersionEntry[];
|
|
66
|
+
/** Optional public icon URL. */
|
|
67
|
+
iconUrl?: string;
|
|
68
|
+
/** Optional categories. */
|
|
69
|
+
categories?: string[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Registry root document.
|
|
74
|
+
*
|
|
75
|
+
* Mirrors `plugin-registry.schema.json`. The schema document calls the
|
|
76
|
+
* version field `registryVersion` (integer, bumped only on breaking
|
|
77
|
+
* registry-document changes); we keep the legacy `schemaVersion` field
|
|
78
|
+
* as a compatibility alias so existing consumers do not break.
|
|
79
|
+
*/
|
|
80
|
+
export interface IPluginRegistry {
|
|
81
|
+
/** Registry schema generation. Bumped only on breaking registry-document changes. */
|
|
82
|
+
registryVersion: number;
|
|
83
|
+
/** Registry display name. */
|
|
84
|
+
name: string;
|
|
85
|
+
/** Registry homepage / maintainer URL. */
|
|
86
|
+
homepage?: string;
|
|
87
|
+
/** ISO timestamp this registry was published. */
|
|
88
|
+
publishedAt?: string;
|
|
89
|
+
/** Public key used to verify per-version signatures. */
|
|
90
|
+
trustKey?: { algo: 'ed25519'; publicKeyId: string; publicKey: string };
|
|
91
|
+
/** Channels this registry exposes (subset of stable/beta/alpha/nightly). */
|
|
92
|
+
channels?: TPluginRegistryChannel[];
|
|
93
|
+
/** Listed plugins. */
|
|
94
|
+
plugins: IPluginRegistryEntry[];
|
|
95
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Runtime types used by `definePlugin()` / `defineMobilePlugin()` calls.
|
|
7
|
+
*
|
|
8
|
+
* Plugins are loaded by `PluginRuntime` at boot. The runtime imports the
|
|
9
|
+
* plugin's npm package, calls its exported `register()` function and
|
|
10
|
+
* passes it an `IPluginApi` instance. The plugin returns an
|
|
11
|
+
* `IPluginRegistration` describing what it contributes (styles, admin
|
|
12
|
+
* pages, menu items, health checks, etc.).
|
|
13
|
+
*
|
|
14
|
+
* Frontend (`react`) and mobile (`react-native`) plugins share most of
|
|
15
|
+
* the surface; mobile-only types are suffixed with `Mobile`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { IStyleRegistryEntry } from '../registry/styles.registry';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Generic component renderer. We avoid pulling in React types here to
|
|
22
|
+
* keep the SDK framework-agnostic; the runtime casts to the appropriate
|
|
23
|
+
* component type at registration time.
|
|
24
|
+
*/
|
|
25
|
+
export type TPluginComponent<TProps = unknown> = (props: TProps) => unknown;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Definition of a CMS style contributed by a plugin.
|
|
29
|
+
*/
|
|
30
|
+
export interface IStyleDefinition extends Omit<IStyleRegistryEntry, 'category'> {
|
|
31
|
+
/** Style name (becomes the `style_name` discriminator). */
|
|
32
|
+
name: string;
|
|
33
|
+
/**
|
|
34
|
+
* Category. Plugins should usually pick `'plugin'`, but may opt into
|
|
35
|
+
* an existing category (e.g. `'forms'`) when the style is a
|
|
36
|
+
* drop-in replacement.
|
|
37
|
+
*/
|
|
38
|
+
category: IStyleRegistryEntry['category'];
|
|
39
|
+
/** Style implementation component (React or React Native). */
|
|
40
|
+
component: TPluginComponent;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Admin page contributed by a plugin. Mounted under
|
|
45
|
+
* `/admin/plugins-host/{pluginId}/{slug}` in the frontend.
|
|
46
|
+
*/
|
|
47
|
+
export interface IAdminPageDefinition {
|
|
48
|
+
/** URL slug relative to the host shell. */
|
|
49
|
+
slug: string;
|
|
50
|
+
/** Display title in tabs / breadcrumbs. */
|
|
51
|
+
title: string;
|
|
52
|
+
/** Required permission key. */
|
|
53
|
+
permission?: string;
|
|
54
|
+
/** Page component (React). */
|
|
55
|
+
component: TPluginComponent;
|
|
56
|
+
/** Optional preloader hook called during SSR/prefetch. */
|
|
57
|
+
prefetch?: () => Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Menu item contributed by a plugin.
|
|
62
|
+
*/
|
|
63
|
+
export interface IMenuItemDefinition {
|
|
64
|
+
/** Stable key. */
|
|
65
|
+
key: string;
|
|
66
|
+
/** Display label. */
|
|
67
|
+
label: string;
|
|
68
|
+
/** Optional Tabler icon name. */
|
|
69
|
+
icon?: string;
|
|
70
|
+
/** Target href; defaults to the admin page slug. */
|
|
71
|
+
href?: string;
|
|
72
|
+
/** Required permission key. */
|
|
73
|
+
permission?: string;
|
|
74
|
+
/** Position relative to existing host menus. */
|
|
75
|
+
position?: { section: 'admin' | 'cms' | 'tools'; order: number };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Health-check declaration. Invoked through the doctor command and the
|
|
80
|
+
* Plugin detail page's Health tab.
|
|
81
|
+
*/
|
|
82
|
+
export interface IPluginHealthCheck {
|
|
83
|
+
/** Stable key (e.g. `surveyjs.license`). */
|
|
84
|
+
key: string;
|
|
85
|
+
/** Display label. */
|
|
86
|
+
label: string;
|
|
87
|
+
/** Optional severity threshold. */
|
|
88
|
+
severity?: 'info' | 'warning' | 'error';
|
|
89
|
+
/** Runs the check; returns ok/warn/error + detail. */
|
|
90
|
+
run: () => Promise<{ status: 'ok' | 'warn' | 'error'; detail?: string }>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Feature-flag declaration. The host installer seeds the flag based on
|
|
95
|
+
* `defaultEnabled`; admins toggle through the Plugin detail page.
|
|
96
|
+
*/
|
|
97
|
+
export interface IPluginFeatureFlag {
|
|
98
|
+
key: string;
|
|
99
|
+
label: string;
|
|
100
|
+
description?: string;
|
|
101
|
+
defaultEnabled: boolean;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Realtime topic declared by the plugin. Wraps Mercure topics under the
|
|
106
|
+
* scoped path `selfhelp/plugin/{pluginId}/{topicKey}`. Plugins never
|
|
107
|
+
* talk to Mercure directly.
|
|
108
|
+
*/
|
|
109
|
+
export interface IPluginRealtimeTopic<TPayload = unknown> {
|
|
110
|
+
/** Topic key, unique within the plugin. */
|
|
111
|
+
key: string;
|
|
112
|
+
/** Optional permission required to subscribe. */
|
|
113
|
+
requiredPermission?: string;
|
|
114
|
+
/**
|
|
115
|
+
* Optional payload validator. Receives the SSE event data and must
|
|
116
|
+
* either return the typed payload or throw. The SDK runs this on
|
|
117
|
+
* the receiving side; the publisher runs the same validator before
|
|
118
|
+
* publishing.
|
|
119
|
+
*/
|
|
120
|
+
validate?: (raw: unknown) => TPayload;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Realtime publisher exposed to plugin server-side code (mostly inside
|
|
125
|
+
* the plugin's Symfony bundle). Plugins must never instantiate Mercure
|
|
126
|
+
* clients directly.
|
|
127
|
+
*/
|
|
128
|
+
export interface IPluginRealtimePublisher {
|
|
129
|
+
/** Publish a payload to a topic registered by the plugin. */
|
|
130
|
+
publish<TPayload>(topicKey: string, payload: TPayload, options?: {
|
|
131
|
+
/** Audience scope. Defaults to `permission`. */
|
|
132
|
+
audience?: 'permission' | 'broadcast' | 'admins';
|
|
133
|
+
/** Per-publish topic parameters (e.g. survey id). */
|
|
134
|
+
topicParams?: Record<string, string | number>;
|
|
135
|
+
}): Promise<void>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Rich text editor adapter. Both the SurveyJS plugin's `rich-text`
|
|
140
|
+
* question type and the CMS core's `rich-text-editor` style consume
|
|
141
|
+
* this adapter so a single Tiptap-based editor backs both.
|
|
142
|
+
*
|
|
143
|
+
* Plugins must provide a sanitization step on the backend; this
|
|
144
|
+
* adapter only owns the editor UI.
|
|
145
|
+
*/
|
|
146
|
+
export interface IRichTextEditorAdapterOptions {
|
|
147
|
+
/** Optional editor mode hint. */
|
|
148
|
+
mode?: 'inline' | 'block';
|
|
149
|
+
/**
|
|
150
|
+
* Allowed format for the stored value:
|
|
151
|
+
* - `markdown` — string of Markdown.
|
|
152
|
+
* - `sanitized-html` — HTML string passed through DOMPurify on the
|
|
153
|
+
* backend.
|
|
154
|
+
* - `prosemirror-json` — JSON shape produced by Tiptap.
|
|
155
|
+
*/
|
|
156
|
+
format: 'markdown' | 'sanitized-html' | 'prosemirror-json';
|
|
157
|
+
/** Optional toolbar restriction. */
|
|
158
|
+
toolbar?: ('bold' | 'italic' | 'underline' | 'strike' | 'code' | 'link' | 'heading' | 'bulletList' | 'orderedList' | 'blockquote' | 'image')[];
|
|
159
|
+
/** Whether to allow image upload. */
|
|
160
|
+
allowImages?: boolean;
|
|
161
|
+
/** Whether to allow links. */
|
|
162
|
+
allowLinks?: boolean;
|
|
163
|
+
/** Custom placeholder text. */
|
|
164
|
+
placeholder?: string;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface IRichTextEditorValue {
|
|
168
|
+
/** Versioned format token, matches the adapter `format`. */
|
|
169
|
+
format: 'markdown' | 'sanitized-html' | 'prosemirror-json';
|
|
170
|
+
/** Stringified content (Markdown / HTML) or stringified JSON. */
|
|
171
|
+
content: string;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The host-side editor adapter passed to plugins through `IPluginApi`.
|
|
176
|
+
*/
|
|
177
|
+
export interface IRichTextEditorAdapter {
|
|
178
|
+
/**
|
|
179
|
+
* Mount the editor at the given DOM node (web) or component node
|
|
180
|
+
* (mobile) with the initial value. The returned object exposes a
|
|
181
|
+
* setter and a destroyer.
|
|
182
|
+
*/
|
|
183
|
+
mount(target: unknown, options: IRichTextEditorAdapterOptions & {
|
|
184
|
+
initialValue?: IRichTextEditorValue;
|
|
185
|
+
onChange?: (value: IRichTextEditorValue) => void;
|
|
186
|
+
}): {
|
|
187
|
+
setValue(value: IRichTextEditorValue): void;
|
|
188
|
+
destroy(): void;
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Host API passed to plugins at registration time.
|
|
194
|
+
*/
|
|
195
|
+
export interface IPluginApi {
|
|
196
|
+
/** Plugin id (provided by the host so the plugin does not duplicate it). */
|
|
197
|
+
pluginId: string;
|
|
198
|
+
/** Plugin version, mirrored from the manifest. */
|
|
199
|
+
pluginVersion: string;
|
|
200
|
+
/** Live feature-flag accessor. */
|
|
201
|
+
isFeatureEnabled(flagKey: string): boolean;
|
|
202
|
+
/** Host realtime publisher (only available on the backend side). */
|
|
203
|
+
realtime?: IPluginRealtimePublisher;
|
|
204
|
+
/** Host rich-text editor adapter. */
|
|
205
|
+
richTextEditor: IRichTextEditorAdapter;
|
|
206
|
+
/**
|
|
207
|
+
* Logger that respects the host's debug config. Avoid `console.log`
|
|
208
|
+
* in plugin code; route everything through this.
|
|
209
|
+
*/
|
|
210
|
+
log: {
|
|
211
|
+
debug(message: string, context?: Record<string, unknown>): void;
|
|
212
|
+
info(message: string, context?: Record<string, unknown>): void;
|
|
213
|
+
warn(message: string, context?: Record<string, unknown>): void;
|
|
214
|
+
error(message: string, context?: Record<string, unknown>): void;
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Same shape on mobile, minus the rich-text editor adapter when the
|
|
220
|
+
* plugin only supports read-only mobile rendering.
|
|
221
|
+
*/
|
|
222
|
+
export interface IMobilePluginApi extends Omit<IPluginApi, 'richTextEditor'> {
|
|
223
|
+
/** Mobile builds without rich-text support set this to `null`. */
|
|
224
|
+
richTextEditor: IRichTextEditorAdapter | null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Returned by `definePlugin()` for the frontend / admin side of a
|
|
229
|
+
* plugin.
|
|
230
|
+
*/
|
|
231
|
+
export interface IPluginRegistration {
|
|
232
|
+
/** Plugin id, must match the manifest. */
|
|
233
|
+
id: string;
|
|
234
|
+
/** Plugin version, must match the manifest. */
|
|
235
|
+
version: string;
|
|
236
|
+
/** SDK contract version the plugin targets. */
|
|
237
|
+
pluginApiVersion: string;
|
|
238
|
+
/** CMS styles the plugin contributes. */
|
|
239
|
+
styles?: IStyleDefinition[];
|
|
240
|
+
/** Admin pages the plugin contributes. */
|
|
241
|
+
adminPages?: IAdminPageDefinition[];
|
|
242
|
+
/** Menu items the plugin contributes. */
|
|
243
|
+
menuItems?: IMenuItemDefinition[];
|
|
244
|
+
/** Health checks the plugin exposes to the host. */
|
|
245
|
+
healthChecks?: IPluginHealthCheck[];
|
|
246
|
+
/** Feature flags the plugin declares. */
|
|
247
|
+
featureFlags?: IPluginFeatureFlag[];
|
|
248
|
+
/** Realtime topics the plugin publishes / subscribes to. */
|
|
249
|
+
realtimeTopics?: IPluginRealtimeTopic[];
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Returned by `defineMobilePlugin()` for the mobile side of a plugin.
|
|
254
|
+
*/
|
|
255
|
+
export interface IMobilePluginRegistration {
|
|
256
|
+
id: string;
|
|
257
|
+
version: string;
|
|
258
|
+
pluginApiVersion: string;
|
|
259
|
+
/** Mobile-only style implementations. */
|
|
260
|
+
styles?: IStyleDefinition[];
|
|
261
|
+
/** Mobile-only realtime topics. */
|
|
262
|
+
realtimeTopics?: IPluginRealtimeTopic[];
|
|
263
|
+
/** Mobile feature flags (mirror of the manifest flags). */
|
|
264
|
+
featureFlags?: IPluginFeatureFlag[];
|
|
265
|
+
}
|