@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.
- package/LICENSE +40 -0
- package/dist/chunk-NMXHDDL6.js +12 -0
- package/dist/chunk-NMXHDDL6.js.map +1 -0
- package/dist/chunk-SMAZA2KE.cjs +16 -0
- package/dist/chunk-SMAZA2KE.cjs.map +1 -0
- package/dist/index.cjs +121 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +134 -0
- package/dist/index.d.ts +134 -0
- package/dist/index.js +99 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.cjs +20 -0
- package/dist/protocol.cjs.map +1 -0
- package/dist/protocol.d.cts +62 -0
- package/dist/protocol.d.ts +62 -0
- package/dist/protocol.js +3 -0
- package/dist/protocol.js.map +1 -0
- package/package.json +78 -0
- package/src/context.tsx +99 -0
- package/src/global.ts +19 -0
- package/src/hooks.test.tsx +73 -0
- package/src/hooks.ts +154 -0
- package/src/index.ts +40 -0
- package/src/protocol.ts +103 -0
package/src/protocol.ts
ADDED
|
@@ -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
|
+
}
|