@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
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkSMAZA2KE_cjs = require('./chunk-SMAZA2KE.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "MESSAGE_SOURCE", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return chunkSMAZA2KE_cjs.MESSAGE_SOURCE; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "isHostMessage", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return chunkSMAZA2KE_cjs.isHostMessage; }
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(exports, "isRuntimeMessage", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return chunkSMAZA2KE_cjs.isRuntimeMessage; }
|
|
18
|
+
});
|
|
19
|
+
//# sourceMappingURL=protocol.cjs.map
|
|
20
|
+
//# sourceMappingURL=protocol.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"protocol.cjs"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
declare const MESSAGE_SOURCE: "proteos-component-runtime";
|
|
2
|
+
interface PageContext {
|
|
3
|
+
record?: Record<string, unknown>;
|
|
4
|
+
entity?: {
|
|
5
|
+
slug: string;
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
};
|
|
8
|
+
params?: Record<string, unknown>;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
interface InitMessage {
|
|
12
|
+
type: 'INIT';
|
|
13
|
+
/** Raw bearer token (the OIDC `id_token`). The runtime's ProteosClient
|
|
14
|
+
* prepends `Bearer ` when building the Authorization header, so the host
|
|
15
|
+
* sends the token verbatim — NOT a full `Bearer …` header value. */
|
|
16
|
+
authToken: string;
|
|
17
|
+
/** API origin the bundle endpoint + SDK calls resolve against. */
|
|
18
|
+
apiBaseUrl: string;
|
|
19
|
+
/** Resolved props for the component (Liquid templating happens host-side). */
|
|
20
|
+
props: Record<string, unknown>;
|
|
21
|
+
pageCtx: PageContext;
|
|
22
|
+
}
|
|
23
|
+
interface AuthRefreshMessage {
|
|
24
|
+
type: 'AUTH_REFRESH';
|
|
25
|
+
token: string;
|
|
26
|
+
}
|
|
27
|
+
interface DataInvalidateMessage {
|
|
28
|
+
type: 'DATA_INVALIDATE';
|
|
29
|
+
queryKey?: unknown[];
|
|
30
|
+
}
|
|
31
|
+
type HostMessage = InitMessage | AuthRefreshMessage | DataInvalidateMessage;
|
|
32
|
+
interface ReadyMessage {
|
|
33
|
+
type: 'READY';
|
|
34
|
+
}
|
|
35
|
+
interface ResizeMessage {
|
|
36
|
+
type: 'RESIZE';
|
|
37
|
+
height: number;
|
|
38
|
+
}
|
|
39
|
+
interface NavigateMessage {
|
|
40
|
+
type: 'NAVIGATE';
|
|
41
|
+
to: string;
|
|
42
|
+
}
|
|
43
|
+
/** Open an in-app path in a peek (slide-over) panel instead of full-page
|
|
44
|
+
* navigation. Same `/…` in-app path grammar as NAVIGATE; the host decides
|
|
45
|
+
* what can peek (today: platform pages) and falls back to NAVIGATE otherwise. */
|
|
46
|
+
interface OpenPeekMessage {
|
|
47
|
+
type: 'OPEN_PEEK';
|
|
48
|
+
to: string;
|
|
49
|
+
}
|
|
50
|
+
interface ErrorMessage {
|
|
51
|
+
type: 'ERROR';
|
|
52
|
+
message: string;
|
|
53
|
+
stack?: string;
|
|
54
|
+
}
|
|
55
|
+
type RuntimeMessage = ReadyMessage | ResizeMessage | NavigateMessage | OpenPeekMessage | ErrorMessage;
|
|
56
|
+
type Envelope<T> = T & {
|
|
57
|
+
source: typeof MESSAGE_SOURCE;
|
|
58
|
+
};
|
|
59
|
+
declare function isHostMessage(data: unknown): data is Envelope<HostMessage>;
|
|
60
|
+
declare function isRuntimeMessage(data: unknown): data is Envelope<RuntimeMessage>;
|
|
61
|
+
|
|
62
|
+
export { type AuthRefreshMessage, type DataInvalidateMessage, type Envelope, type ErrorMessage, type HostMessage, type InitMessage, MESSAGE_SOURCE, type NavigateMessage, type OpenPeekMessage, type PageContext, type ReadyMessage, type ResizeMessage, type RuntimeMessage, isHostMessage, isRuntimeMessage };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
declare const MESSAGE_SOURCE: "proteos-component-runtime";
|
|
2
|
+
interface PageContext {
|
|
3
|
+
record?: Record<string, unknown>;
|
|
4
|
+
entity?: {
|
|
5
|
+
slug: string;
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
};
|
|
8
|
+
params?: Record<string, unknown>;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
interface InitMessage {
|
|
12
|
+
type: 'INIT';
|
|
13
|
+
/** Raw bearer token (the OIDC `id_token`). The runtime's ProteosClient
|
|
14
|
+
* prepends `Bearer ` when building the Authorization header, so the host
|
|
15
|
+
* sends the token verbatim — NOT a full `Bearer …` header value. */
|
|
16
|
+
authToken: string;
|
|
17
|
+
/** API origin the bundle endpoint + SDK calls resolve against. */
|
|
18
|
+
apiBaseUrl: string;
|
|
19
|
+
/** Resolved props for the component (Liquid templating happens host-side). */
|
|
20
|
+
props: Record<string, unknown>;
|
|
21
|
+
pageCtx: PageContext;
|
|
22
|
+
}
|
|
23
|
+
interface AuthRefreshMessage {
|
|
24
|
+
type: 'AUTH_REFRESH';
|
|
25
|
+
token: string;
|
|
26
|
+
}
|
|
27
|
+
interface DataInvalidateMessage {
|
|
28
|
+
type: 'DATA_INVALIDATE';
|
|
29
|
+
queryKey?: unknown[];
|
|
30
|
+
}
|
|
31
|
+
type HostMessage = InitMessage | AuthRefreshMessage | DataInvalidateMessage;
|
|
32
|
+
interface ReadyMessage {
|
|
33
|
+
type: 'READY';
|
|
34
|
+
}
|
|
35
|
+
interface ResizeMessage {
|
|
36
|
+
type: 'RESIZE';
|
|
37
|
+
height: number;
|
|
38
|
+
}
|
|
39
|
+
interface NavigateMessage {
|
|
40
|
+
type: 'NAVIGATE';
|
|
41
|
+
to: string;
|
|
42
|
+
}
|
|
43
|
+
/** Open an in-app path in a peek (slide-over) panel instead of full-page
|
|
44
|
+
* navigation. Same `/…` in-app path grammar as NAVIGATE; the host decides
|
|
45
|
+
* what can peek (today: platform pages) and falls back to NAVIGATE otherwise. */
|
|
46
|
+
interface OpenPeekMessage {
|
|
47
|
+
type: 'OPEN_PEEK';
|
|
48
|
+
to: string;
|
|
49
|
+
}
|
|
50
|
+
interface ErrorMessage {
|
|
51
|
+
type: 'ERROR';
|
|
52
|
+
message: string;
|
|
53
|
+
stack?: string;
|
|
54
|
+
}
|
|
55
|
+
type RuntimeMessage = ReadyMessage | ResizeMessage | NavigateMessage | OpenPeekMessage | ErrorMessage;
|
|
56
|
+
type Envelope<T> = T & {
|
|
57
|
+
source: typeof MESSAGE_SOURCE;
|
|
58
|
+
};
|
|
59
|
+
declare function isHostMessage(data: unknown): data is Envelope<HostMessage>;
|
|
60
|
+
declare function isRuntimeMessage(data: unknown): data is Envelope<RuntimeMessage>;
|
|
61
|
+
|
|
62
|
+
export { type AuthRefreshMessage, type DataInvalidateMessage, type Envelope, type ErrorMessage, type HostMessage, type InitMessage, MESSAGE_SOURCE, type NavigateMessage, type OpenPeekMessage, type PageContext, type ReadyMessage, type ResizeMessage, type RuntimeMessage, isHostMessage, isRuntimeMessage };
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"protocol.js"}
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@proteos/component-sdk",
|
|
3
|
+
"version": "0.18.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"description": "Typed runtime hooks for authoring Proteos custom components — the SDK you import inside a component bundle.",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/proteos-ai/go-sdk.git",
|
|
9
|
+
"directory": "packages/component-sdk"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/proteos-ai/go-sdk",
|
|
12
|
+
"bugs": "https://github.com/proteos-ai/go-sdk/issues",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./dist/index.d.cts",
|
|
22
|
+
"default": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"./protocol": {
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/protocol.d.ts",
|
|
28
|
+
"default": "./dist/protocol.js"
|
|
29
|
+
},
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./dist/protocol.d.cts",
|
|
32
|
+
"default": "./dist/protocol.cjs"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": false
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"src"
|
|
43
|
+
],
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@proteos/sdk": "^0.18.1"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@tanstack/react-query": ">=5.0.0",
|
|
49
|
+
"react": ">=19.0.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@biomejs/biome": "^2.4.13",
|
|
53
|
+
"@tanstack/react-query": "^5.80.0",
|
|
54
|
+
"@testing-library/react": "^16.3.2",
|
|
55
|
+
"@types/react": "^19.2.14",
|
|
56
|
+
"@vitejs/plugin-react": "^6.0.1",
|
|
57
|
+
"jsdom": "^29.0.2",
|
|
58
|
+
"react": "^19.2.5",
|
|
59
|
+
"react-dom": "^19.2.5",
|
|
60
|
+
"tsup": "^8.3.0",
|
|
61
|
+
"typescript": "^6.0.3",
|
|
62
|
+
"vitest": "^3.2.4",
|
|
63
|
+
"@proteos/biome-config": "0.0.0",
|
|
64
|
+
"@proteos/tsconfig": "0.0.0"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsup",
|
|
68
|
+
"dev": "tsup --watch",
|
|
69
|
+
"lint": "biome check .",
|
|
70
|
+
"format": "biome format --write .",
|
|
71
|
+
"typecheck": "tsc -b",
|
|
72
|
+
"test": "vitest run",
|
|
73
|
+
"test:watch": "vitest"
|
|
74
|
+
},
|
|
75
|
+
"main": "./dist/index.cjs",
|
|
76
|
+
"module": "./dist/index.js",
|
|
77
|
+
"types": "./dist/index.d.ts"
|
|
78
|
+
}
|
package/src/context.tsx
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AccountClient,
|
|
3
|
+
AgentClient,
|
|
4
|
+
DataClient,
|
|
5
|
+
FunctionsClient,
|
|
6
|
+
KnowledgeClient,
|
|
7
|
+
MetaClient,
|
|
8
|
+
StorageClient,
|
|
9
|
+
} from '@proteos/sdk'
|
|
10
|
+
import { createContext, type ReactNode, useMemo } from 'react'
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
type Envelope,
|
|
14
|
+
MESSAGE_SOURCE,
|
|
15
|
+
type NavigateMessage,
|
|
16
|
+
type OpenPeekMessage,
|
|
17
|
+
type PageContext,
|
|
18
|
+
} from './protocol'
|
|
19
|
+
|
|
20
|
+
/** Options for {@link RuntimeContextValue.navigate}. */
|
|
21
|
+
export interface NavigateOptions {
|
|
22
|
+
/** Open the target in a peek (slide-over) panel instead of full-page. */
|
|
23
|
+
peek?: boolean
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The authenticated `@proteos/sdk` clients the runtime injects. Same instances
|
|
28
|
+
* the host app uses — pre-wired with a refreshing bearer token, so a component
|
|
29
|
+
* never touches auth headers.
|
|
30
|
+
*/
|
|
31
|
+
export interface ComponentSdk {
|
|
32
|
+
account: AccountClient
|
|
33
|
+
agents: AgentClient
|
|
34
|
+
data: DataClient
|
|
35
|
+
functions: FunctionsClient
|
|
36
|
+
knowledge: KnowledgeClient
|
|
37
|
+
meta: MetaClient
|
|
38
|
+
storage: StorageClient
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The raw runtime payload the shell hands a component at INIT. The shell builds
|
|
43
|
+
* this; component authors read it through the hooks.
|
|
44
|
+
*/
|
|
45
|
+
export interface RuntimeValue {
|
|
46
|
+
sdk: ComponentSdk
|
|
47
|
+
/** Liquid-resolved props (a snapshot — not a live binding). */
|
|
48
|
+
props: Record<string, unknown>
|
|
49
|
+
/** Page context snapshot: the host record, entity, and URL params. */
|
|
50
|
+
pageCtx: PageContext
|
|
51
|
+
/** API origin SDK calls + asset URLs resolve against. */
|
|
52
|
+
apiBaseUrl: string
|
|
53
|
+
/** Current bearer token getter (escape hatch for raw fetches). */
|
|
54
|
+
getToken: () => string
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Runtime value plus the derived helpers the hooks expose. */
|
|
58
|
+
export interface RuntimeContextValue extends RuntimeValue {
|
|
59
|
+
/** Navigate the host SPA to an in-app path (posts a NAVIGATE message), or
|
|
60
|
+
* open it in a peek panel with `{ peek: true }` (posts OPEN_PEEK). */
|
|
61
|
+
navigate: (to: string, options?: NavigateOptions) => void
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const RuntimeContext = createContext<RuntimeContextValue | null>(null)
|
|
65
|
+
|
|
66
|
+
/** Post a NAVIGATE envelope to the host. The host only honours relative,
|
|
67
|
+
* in-app targets (it ignores absolute/external URLs). */
|
|
68
|
+
function postNavigate(to: string): void {
|
|
69
|
+
const envelope: Envelope<NavigateMessage> = { source: MESSAGE_SOURCE, type: 'NAVIGATE', to }
|
|
70
|
+
window.parent?.postMessage(envelope, '*')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Post an OPEN_PEEK envelope to the host — open the in-app path in a peek
|
|
74
|
+
* panel. Same relative-path contract as {@link postNavigate}. */
|
|
75
|
+
function postOpenPeek(to: string): void {
|
|
76
|
+
const envelope: Envelope<OpenPeekMessage> = { source: MESSAGE_SOURCE, type: 'OPEN_PEEK', to }
|
|
77
|
+
window.parent?.postMessage(envelope, '*')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Navigate full-page (default) or into a peek panel (`{ peek: true }`). */
|
|
81
|
+
function navigate(to: string, options?: NavigateOptions): void {
|
|
82
|
+
if (options?.peek) postOpenPeek(to)
|
|
83
|
+
else postNavigate(to)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Provides the runtime value + derived helpers to a component subtree. Mounted
|
|
88
|
+
* by the component-runtime shell around the loaded component — authors never
|
|
89
|
+
* render this themselves. Lives in `@proteos/component-sdk` (a shared,
|
|
90
|
+
* import-mapped singleton) so the context the shell provides is the very same
|
|
91
|
+
* context the component's hooks read, across the bundle boundary.
|
|
92
|
+
*/
|
|
93
|
+
export function RuntimeProvider({ value, children }: { value: RuntimeValue; children: ReactNode }) {
|
|
94
|
+
// The shell builds `value` once at INIT (a stable state object — clients,
|
|
95
|
+
// props and pageCtx are all set once), so memoizing on its identity recreates
|
|
96
|
+
// the context only when the runtime genuinely re-inits.
|
|
97
|
+
const ctx = useMemo<RuntimeContextValue>(() => ({ ...value, navigate }), [value])
|
|
98
|
+
return <RuntimeContext.Provider value={ctx}>{children}</RuntimeContext.Provider>
|
|
99
|
+
}
|
package/src/global.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ComponentSdk } from './context'
|
|
2
|
+
import type { PageContext } from './protocol'
|
|
3
|
+
|
|
4
|
+
// Ambient declaration of the low-level runtime bridge the shell injects at INIT.
|
|
5
|
+
// Importing `@proteos/component-sdk` anywhere registers this, so component
|
|
6
|
+
// authors get a typed `window.__PROTEOS_RUNTIME__` for free and never hand-write
|
|
7
|
+
// a `declare global` block. Prefer the typed hooks (`useSdk`, `usePageContext`, …);
|
|
8
|
+
// the global is the back-compat / escape-hatch path.
|
|
9
|
+
declare global {
|
|
10
|
+
interface Window {
|
|
11
|
+
__PROTEOS_RUNTIME__?: {
|
|
12
|
+
apiBaseUrl: string
|
|
13
|
+
getToken: () => string
|
|
14
|
+
props: Record<string, unknown>
|
|
15
|
+
pageCtx: PageContext
|
|
16
|
+
sdk: ComponentSdk
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
import { RuntimeProvider, type RuntimeValue } from './context'
|
|
6
|
+
import { useApiBaseUrl, useNavigate, usePageContext, useProps, useRuntime, useSdk } from './hooks'
|
|
7
|
+
import { MESSAGE_SOURCE } from './protocol'
|
|
8
|
+
|
|
9
|
+
// A stable stand-in for the injected SDK — the hooks only pass it through.
|
|
10
|
+
const sdk = {} as RuntimeValue['sdk']
|
|
11
|
+
|
|
12
|
+
function makeValue(overrides: Partial<RuntimeValue> = {}): RuntimeValue {
|
|
13
|
+
return {
|
|
14
|
+
sdk,
|
|
15
|
+
props: { title: 'Hello' },
|
|
16
|
+
pageCtx: { record: { id: 'rec_1', name: 'ACME' }, entity: { slug: 'company' } },
|
|
17
|
+
apiBaseUrl: 'https://api.test',
|
|
18
|
+
getToken: () => 'tok_123',
|
|
19
|
+
...overrides,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function wrapper(value: RuntimeValue) {
|
|
24
|
+
return function Wrapper({ children }: { children: ReactNode }) {
|
|
25
|
+
return <RuntimeProvider value={value}>{children}</RuntimeProvider>
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
vi.restoreAllMocks()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('runtime hooks', () => {
|
|
34
|
+
it('useSdk / useProps / usePageContext / useApiBaseUrl expose the runtime value', () => {
|
|
35
|
+
const value = makeValue()
|
|
36
|
+
const { result } = renderHook(
|
|
37
|
+
() => ({
|
|
38
|
+
sdk: useSdk(),
|
|
39
|
+
props: useProps<{ title: string }>(),
|
|
40
|
+
ctx: usePageContext(),
|
|
41
|
+
base: useApiBaseUrl(),
|
|
42
|
+
}),
|
|
43
|
+
{ wrapper: wrapper(value) },
|
|
44
|
+
)
|
|
45
|
+
expect(result.current.sdk).toBe(value.sdk)
|
|
46
|
+
expect(result.current.props.title).toBe('Hello')
|
|
47
|
+
expect(result.current.ctx.record).toEqual({ id: 'rec_1', name: 'ACME' })
|
|
48
|
+
expect(result.current.ctx.entity?.slug).toBe('company')
|
|
49
|
+
expect(result.current.base).toBe('https://api.test')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('usePageContext().record is undefined when the page has no bound record', () => {
|
|
53
|
+
const value = makeValue({ pageCtx: { entity: { slug: 'dashboard' } } })
|
|
54
|
+
const { result } = renderHook(() => usePageContext(), { wrapper: wrapper(value) })
|
|
55
|
+
expect(result.current.record).toBeUndefined()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('useNavigate posts a NAVIGATE envelope to the host', () => {
|
|
59
|
+
const post = vi.spyOn(window.parent, 'postMessage').mockImplementation(() => {})
|
|
60
|
+
const { result } = renderHook(() => useNavigate(), { wrapper: wrapper(makeValue()) })
|
|
61
|
+
result.current('/org/acme/record/task/42')
|
|
62
|
+
expect(post).toHaveBeenCalledWith(
|
|
63
|
+
{ source: MESSAGE_SOURCE, type: 'NAVIGATE', to: '/org/acme/record/task/42' },
|
|
64
|
+
'*',
|
|
65
|
+
)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('throws a clear error when used outside the runtime', () => {
|
|
69
|
+
// Silence the expected React error-boundary console noise.
|
|
70
|
+
vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
71
|
+
expect(() => renderHook(() => useRuntime())).toThrow(/Proteos component runtime/)
|
|
72
|
+
})
|
|
73
|
+
})
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { useContext, useEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type ComponentSdk,
|
|
5
|
+
type NavigateOptions,
|
|
6
|
+
RuntimeContext,
|
|
7
|
+
type RuntimeContextValue,
|
|
8
|
+
} from './context'
|
|
9
|
+
import type { PageContext } from './protocol'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Low-level escape hatch: the whole runtime context (sdk, props, pageCtx,
|
|
13
|
+
* apiBaseUrl, getToken, navigate). Prefer the focused hooks below. Throws a
|
|
14
|
+
* clear error if used outside the component runtime.
|
|
15
|
+
*/
|
|
16
|
+
export function useRuntime(): RuntimeContextValue {
|
|
17
|
+
const ctx = useContext(RuntimeContext)
|
|
18
|
+
if (!ctx) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
'@proteos/component-sdk: hooks must run inside the Proteos component runtime. ' +
|
|
21
|
+
'This usually means the component is rendered outside its iframe, or a ' +
|
|
22
|
+
'second copy of @proteos/component-sdk was bundled instead of using the ' +
|
|
23
|
+
'runtime-provided one (keep it external).',
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
return ctx
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The authenticated SDK clients (account / data / functions / meta). */
|
|
30
|
+
export function useSdk(): ComponentSdk {
|
|
31
|
+
return useRuntime().sdk
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The component's Liquid-resolved props (a snapshot, not a live binding). */
|
|
35
|
+
export function useProps<T extends Record<string, unknown> = Record<string, unknown>>(): T {
|
|
36
|
+
return useRuntime().props as T
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The full page context: host `record`, `entity`, and URL `params`. The
|
|
41
|
+
* `record` is a snapshot taken at mount (`undefined` on record-less platform
|
|
42
|
+
* pages) — fetch via `useSdk().data` for anything that must stay fresh.
|
|
43
|
+
*/
|
|
44
|
+
export function usePageContext(): PageContext {
|
|
45
|
+
return useRuntime().pageCtx
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Navigate the host SPA to an in-app path, e.g.
|
|
50
|
+
* `navigate('/org/acme/record/task/123')`. The host ignores absolute/external
|
|
51
|
+
* URLs. Build org/app-prefixed paths with `useHostLocation().appBasePath`.
|
|
52
|
+
*
|
|
53
|
+
* Pass `{ peek: true }` to open the path in a peek (slide-over) panel instead
|
|
54
|
+
* of full-page navigation — e.g. `navigate(path, { peek: true })`. The host
|
|
55
|
+
* peeks what it can (today: platform pages) and falls back to full navigation.
|
|
56
|
+
*/
|
|
57
|
+
export function useNavigate(): (to: string, options?: NavigateOptions) => void {
|
|
58
|
+
return useRuntime().navigate
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The API origin SDK calls resolve against (for building raw asset URLs). */
|
|
62
|
+
export function useApiBaseUrl(): string {
|
|
63
|
+
return useRuntime().apiBaseUrl
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A getter for the current bearer token. Returns a function (not the token)
|
|
68
|
+
* because the token is refreshed in place — read it at call time for a raw
|
|
69
|
+
* `fetch`. Most components never need this; prefer `useSdk()`.
|
|
70
|
+
*/
|
|
71
|
+
export function useAuthToken(): () => string {
|
|
72
|
+
return useRuntime().getToken
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface HostLocation {
|
|
76
|
+
/** The host tab's pathname (same-origin read), or null if unavailable. */
|
|
77
|
+
pathname: string | null
|
|
78
|
+
/** The org id parsed from `/org/<orgId>/…`, or null. */
|
|
79
|
+
orgId: string | null
|
|
80
|
+
/** The `/org/<orgId>/app/<appId>` prefix, or null when not on an app page. */
|
|
81
|
+
appBasePath: string | null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function readHostLocation(): HostLocation {
|
|
85
|
+
const empty: HostLocation = { pathname: null, orgId: null, appBasePath: null }
|
|
86
|
+
try {
|
|
87
|
+
const parentWindow = window.parent
|
|
88
|
+
if (!parentWindow || parentWindow === window) return empty
|
|
89
|
+
const pathname = parentWindow.location.pathname
|
|
90
|
+
const appMatch = pathname.match(/^\/org\/[^/]+\/app\/[^/]+/)
|
|
91
|
+
const orgMatch = pathname.match(/^\/org\/([^/]+)\//)
|
|
92
|
+
return {
|
|
93
|
+
pathname,
|
|
94
|
+
orgId: orgMatch ? (orgMatch[1] ?? null) : null,
|
|
95
|
+
appBasePath: appMatch ? appMatch[0] : null,
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
// Cross-origin (untrusted-component future) or detached — fall back to null.
|
|
99
|
+
return empty
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Read the host tab's location (same-origin) — the org id and `/org/.../app/...`
|
|
105
|
+
* prefix needed to build navigation targets on platform pages. Read once on
|
|
106
|
+
* mount (the iframe re-inits when the host route changes).
|
|
107
|
+
*/
|
|
108
|
+
export function useHostLocation(): HostLocation {
|
|
109
|
+
const [loc] = useState(readHostLocation)
|
|
110
|
+
return loc
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface FillViewportOptions {
|
|
114
|
+
/** Gap left below the component (px). Default 24. */
|
|
115
|
+
bottomMargin?: number
|
|
116
|
+
/** Floor for the computed height (px). Default 320. */
|
|
117
|
+
minHeight?: number
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Compute a "fill the rest of the viewport" height for full-bleed layouts
|
|
122
|
+
* (boards, canvases). The runtime auto-sizes the iframe to its content, so a
|
|
123
|
+
* component can't use its own `vh` units (circular). The iframe is same-origin,
|
|
124
|
+
* so this measures the parent viewport minus the iframe's offset instead.
|
|
125
|
+
* Returns `null` when it can't measure (fall back to natural height).
|
|
126
|
+
*/
|
|
127
|
+
export function useFillViewportHeight(options: FillViewportOptions = {}): number | null {
|
|
128
|
+
const { bottomMargin = 24, minHeight = 320 } = options
|
|
129
|
+
const [height, setHeight] = useState<number | null>(null)
|
|
130
|
+
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
function compute(): void {
|
|
133
|
+
try {
|
|
134
|
+
const frame = window.frameElement
|
|
135
|
+
const parentWindow = window.parent
|
|
136
|
+
if (!frame || !parentWindow || parentWindow === window) return
|
|
137
|
+
const available =
|
|
138
|
+
parentWindow.innerHeight - frame.getBoundingClientRect().top - bottomMargin
|
|
139
|
+
setHeight(Math.max(available, minHeight))
|
|
140
|
+
} catch {
|
|
141
|
+
setHeight(null) // cross-origin or detached — fall back to natural height
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
compute()
|
|
145
|
+
try {
|
|
146
|
+
window.parent.addEventListener('resize', compute)
|
|
147
|
+
return () => window.parent.removeEventListener('resize', compute)
|
|
148
|
+
} catch {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
}, [bottomMargin, minHeight])
|
|
152
|
+
|
|
153
|
+
return height
|
|
154
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// @proteos/component-sdk — the author-facing runtime SDK for Proteos custom
|
|
2
|
+
// components. Import the hooks inside a component bundle; the runtime shell
|
|
3
|
+
// wires the provider. Kept EXTERNAL in both the shell build and the component
|
|
4
|
+
// build (resolved via the runtime import map) so the provider and the hooks
|
|
5
|
+
// share one context instance across the iframe bundle boundary.
|
|
6
|
+
|
|
7
|
+
// Register the ambient `Window.__PROTEOS_RUNTIME__` augmentation.
|
|
8
|
+
import './global'
|
|
9
|
+
|
|
10
|
+
export type { ComponentSdk, NavigateOptions, RuntimeContextValue, RuntimeValue } from './context'
|
|
11
|
+
export { RuntimeProvider } from './context'
|
|
12
|
+
export type { FillViewportOptions, HostLocation } from './hooks'
|
|
13
|
+
export {
|
|
14
|
+
useApiBaseUrl,
|
|
15
|
+
useAuthToken,
|
|
16
|
+
useFillViewportHeight,
|
|
17
|
+
useHostLocation,
|
|
18
|
+
useNavigate,
|
|
19
|
+
usePageContext,
|
|
20
|
+
useProps,
|
|
21
|
+
useRuntime,
|
|
22
|
+
useSdk,
|
|
23
|
+
} from './hooks'
|
|
24
|
+
export {
|
|
25
|
+
type AuthRefreshMessage,
|
|
26
|
+
type DataInvalidateMessage,
|
|
27
|
+
type Envelope,
|
|
28
|
+
type ErrorMessage,
|
|
29
|
+
type HostMessage,
|
|
30
|
+
type InitMessage,
|
|
31
|
+
isHostMessage,
|
|
32
|
+
isRuntimeMessage,
|
|
33
|
+
MESSAGE_SOURCE,
|
|
34
|
+
type NavigateMessage,
|
|
35
|
+
type OpenPeekMessage,
|
|
36
|
+
type PageContext,
|
|
37
|
+
type ReadyMessage,
|
|
38
|
+
type ResizeMessage,
|
|
39
|
+
type RuntimeMessage,
|
|
40
|
+
} from './protocol'
|