@proteos/component-sdk 0.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,103 @@
1
+ // postMessage protocol between the host page and the component-runtime iframe.
2
+ // This is the single source of truth for the wire contract: the host renderer
3
+ // (clients/web .../component-frame.tsx), the runtime shell
4
+ // (clients/web/component-runtime/shell.tsx), and the author-facing hooks
5
+ // (this package's RuntimeProvider / useNavigate) all import these same types so
6
+ // every end stays in lockstep.
7
+ //
8
+ // Every message carries `source: MESSAGE_SOURCE` so we can ignore the unrelated
9
+ // postMessage traffic that shares the window (OIDC silent-renew, Vite HMR, …).
10
+
11
+ export const MESSAGE_SOURCE = 'proteos-component-runtime' as const
12
+
13
+ export interface PageContext {
14
+ record?: Record<string, unknown>
15
+ entity?: { slug: string; [key: string]: unknown }
16
+ params?: Record<string, unknown>
17
+ [key: string]: unknown
18
+ }
19
+
20
+ // ── Host → runtime ──────────────────────────────────────────────────────────
21
+
22
+ export interface InitMessage {
23
+ type: 'INIT'
24
+ /** Raw bearer token (the OIDC `id_token`). The runtime's ProteosClient
25
+ * prepends `Bearer ` when building the Authorization header, so the host
26
+ * sends the token verbatim — NOT a full `Bearer …` header value. */
27
+ authToken: string
28
+ /** API origin the bundle endpoint + SDK calls resolve against. */
29
+ apiBaseUrl: string
30
+ /** Resolved props for the component (Liquid templating happens host-side). */
31
+ props: Record<string, unknown>
32
+ pageCtx: PageContext
33
+ }
34
+
35
+ export interface AuthRefreshMessage {
36
+ type: 'AUTH_REFRESH'
37
+ token: string
38
+ }
39
+
40
+ export interface DataInvalidateMessage {
41
+ type: 'DATA_INVALIDATE'
42
+ queryKey?: unknown[]
43
+ }
44
+
45
+ export type HostMessage = InitMessage | AuthRefreshMessage | DataInvalidateMessage
46
+
47
+ // ── Runtime → host ──────────────────────────────────────────────────────────
48
+
49
+ export interface ReadyMessage {
50
+ type: 'READY'
51
+ }
52
+
53
+ export interface ResizeMessage {
54
+ type: 'RESIZE'
55
+ height: number
56
+ }
57
+
58
+ export interface NavigateMessage {
59
+ type: 'NAVIGATE'
60
+ to: string
61
+ }
62
+
63
+ /** Open an in-app path in a peek (slide-over) panel instead of full-page
64
+ * navigation. Same `/…` in-app path grammar as NAVIGATE; the host decides
65
+ * what can peek (today: platform pages) and falls back to NAVIGATE otherwise. */
66
+ export interface OpenPeekMessage {
67
+ type: 'OPEN_PEEK'
68
+ to: string
69
+ }
70
+
71
+ export interface ErrorMessage {
72
+ type: 'ERROR'
73
+ message: string
74
+ stack?: string
75
+ }
76
+
77
+ export type RuntimeMessage =
78
+ | ReadyMessage
79
+ | ResizeMessage
80
+ | NavigateMessage
81
+ | OpenPeekMessage
82
+ | ErrorMessage
83
+
84
+ // Wire envelope: the message plus the source discriminator.
85
+ export type Envelope<T> = T & { source: typeof MESSAGE_SOURCE }
86
+
87
+ export function isHostMessage(data: unknown): data is Envelope<HostMessage> {
88
+ return (
89
+ typeof data === 'object' &&
90
+ data !== null &&
91
+ (data as { source?: unknown }).source === MESSAGE_SOURCE &&
92
+ typeof (data as { type?: unknown }).type === 'string'
93
+ )
94
+ }
95
+
96
+ export function isRuntimeMessage(data: unknown): data is Envelope<RuntimeMessage> {
97
+ return (
98
+ typeof data === 'object' &&
99
+ data !== null &&
100
+ (data as { source?: unknown }).source === MESSAGE_SOURCE &&
101
+ typeof (data as { type?: unknown }).type === 'string'
102
+ )
103
+ }