@notionhq/custom-blocks-dev-shell 0.2.0 → 0.3.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/dist/assets/index-CWJ-vFk0.js +12 -0
- package/dist/index.html +1 -1
- package/dist-cli/cli/published-cli.js +158 -103
- package/docs/bridge/protocol.md +18 -17
- package/docs/data-sources.md +6 -4
- package/docs/host/README.md +54 -42
- package/docs/host/docs/handlers.md +36 -143
- package/docs/host/docs/host-contract.md +196 -0
- package/docs/host/docs/lifecycle.md +28 -37
- package/package.json +1 -1
- package/dist/assets/index-n0_PUieY.js +0 -11
package/docs/host/README.md
CHANGED
|
@@ -5,16 +5,23 @@ owns the iframe, permissions, data access, persistence, analytics, and UI. Block
|
|
|
5
5
|
`@notionhq/custom-blocks`.
|
|
6
6
|
|
|
7
7
|
Create the host before navigating the iframe. Pass the full block URL as `sandboxUrl`, and call
|
|
8
|
-
`stop()` when removing the iframe.
|
|
8
|
+
`stop()` when removing the iframe. A host instance belongs to the required `viewerId`. Supply one
|
|
9
|
+
asynchronous `getCurrentUser` profile reader; its returned ID must match that viewer. Create a fresh
|
|
10
|
+
host and iframe when the viewer changes.
|
|
9
11
|
|
|
10
12
|
For configuration and lifecycle details, see [Initialization and lifecycle](docs/lifecycle.md).
|
|
11
13
|
|
|
14
|
+
For platform handlers, signal-based live queries, and explicit partial implementations, see the
|
|
15
|
+
[Host contract](docs/host-contract.md). The host accepts `CustomBlockHostOptions`.
|
|
16
|
+
|
|
12
17
|
Use `BridgeParent` from `@notionhq/custom-blocks-host/protocol` to construct parent references with
|
|
13
18
|
plain string IDs. The host validates the parent before sending it to the block.
|
|
14
19
|
|
|
15
|
-
Implement `CustomBlockHostHandlers` to connect the host to your application.
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
Implement `CustomBlockHostHandlers` to connect the host to your application. Result-returning
|
|
21
|
+
handlers return promises. `CustomBlockHostHandler<Args, Payload>` supplies the shared signature and
|
|
22
|
+
request context. Handlers return shared bridge data using `CustomBlockHostHandlerResult`. Use
|
|
23
|
+
`makeBridgePage` to construct a complete `BridgePage` from your records. See
|
|
24
|
+
[Return bridge data](docs/handlers.md#return-bridge-data).
|
|
18
25
|
|
|
19
26
|
## Create a host
|
|
20
27
|
|
|
@@ -26,7 +33,7 @@ This example embeds a block with no data sources and supplies fixed demo context
|
|
|
26
33
|
than the embedding application.
|
|
27
34
|
|
|
28
35
|
```ts
|
|
29
|
-
import { createCustomBlockHost } from "@notionhq/custom-blocks-host"
|
|
36
|
+
import { createCustomBlockHost, hostHandlerNotImplemented } from "@notionhq/custom-blocks-host"
|
|
30
37
|
|
|
31
38
|
function mountCustomBlock(container: HTMLElement, blockUrl: URL): () => void {
|
|
32
39
|
if (
|
|
@@ -56,46 +63,46 @@ function mountCustomBlock(container: HTMLElement, blockUrl: URL): () => void {
|
|
|
56
63
|
|
|
57
64
|
const host = createCustomBlockHost({
|
|
58
65
|
iframe,
|
|
66
|
+
blockId: "11111111-1111-4111-8111-111111111111",
|
|
67
|
+
viewerId: "33333333-3333-4333-8333-333333333333",
|
|
68
|
+
getCurrentUser: async () => ({
|
|
69
|
+
object: "user",
|
|
70
|
+
id: "33333333-3333-4333-8333-333333333333",
|
|
71
|
+
type: "person",
|
|
72
|
+
name: "Demo viewer",
|
|
73
|
+
avatar_url: null,
|
|
74
|
+
person: { email: "viewer@example.com" },
|
|
75
|
+
}),
|
|
76
|
+
theme: "light",
|
|
77
|
+
contrastMode: "standard",
|
|
78
|
+
containingPage: {
|
|
79
|
+
type: "page",
|
|
80
|
+
id: "22222222-2222-4222-8222-222222222222",
|
|
81
|
+
parent: { type: "workspace", workspace: true },
|
|
82
|
+
},
|
|
83
|
+
parent: {
|
|
84
|
+
type: "page_id",
|
|
85
|
+
page_id: "22222222-2222-4222-8222-222222222222",
|
|
86
|
+
},
|
|
59
87
|
sandboxUrl: blockUrl.href,
|
|
60
88
|
manifestSource: "host",
|
|
61
89
|
getPersistedManifest: async () => ({
|
|
62
90
|
status: "success",
|
|
63
91
|
manifest: { version: 1, dataSources: {} },
|
|
64
92
|
}),
|
|
65
|
-
|
|
66
|
-
status: "success",
|
|
67
|
-
initialState: {
|
|
68
|
-
theme: "light",
|
|
69
|
-
contrastMode: "standard",
|
|
70
|
-
blockId: "11111111-1111-4111-8111-111111111111",
|
|
71
|
-
parent: {
|
|
72
|
-
type: "page_id",
|
|
73
|
-
page_id: "22222222-2222-4222-8222-222222222222",
|
|
74
|
-
},
|
|
75
|
-
page: {
|
|
76
|
-
type: "page",
|
|
77
|
-
id: "22222222-2222-4222-8222-222222222222",
|
|
78
|
-
parent: { type: "workspace", workspace: true },
|
|
79
|
-
},
|
|
80
|
-
currentUser: {
|
|
81
|
-
object: "user",
|
|
82
|
-
id: "33333333-3333-4333-8333-333333333333",
|
|
83
|
-
name: "Demo viewer",
|
|
84
|
-
avatar_url: null,
|
|
85
|
-
type: "person",
|
|
86
|
-
person: { email: "viewer@example.com" },
|
|
87
|
-
},
|
|
88
|
-
dataSources: { bindings: {} },
|
|
89
|
-
},
|
|
90
|
-
}),
|
|
93
|
+
bindings: { pages: {}, dataSources: {} },
|
|
91
94
|
handlers: {
|
|
92
|
-
|
|
95
|
+
getDataSource: async () => ({ status: "success", state: { status: "unavailable" } }),
|
|
96
|
+
subscribeToQuery: ({ onError }) => onError(hostHandlerNotImplemented("subscribeToQuery")),
|
|
97
|
+
getPage: async () => ({ status: "error", error: hostHandlerNotImplemented("getPage") }),
|
|
98
|
+
createPage: async () => ({ status: "error", error: hostHandlerNotImplemented("createPage") }),
|
|
99
|
+
updatePage: async () => ({ status: "error", error: hostHandlerNotImplemented("updatePage") }),
|
|
100
|
+
getUser: async () => ({ status: "error", error: hostHandlerNotImplemented("getUser") }),
|
|
101
|
+
listUsers: async () => ({ status: "error", error: hostHandlerNotImplemented("listUsers") }),
|
|
102
|
+
openPage: async () => ({ status: "error", error: hostHandlerNotImplemented("openPage") }),
|
|
103
|
+
openExternalUrl: async () => ({
|
|
93
104
|
status: "error",
|
|
94
|
-
error:
|
|
95
|
-
code: "unknown_data_source_key",
|
|
96
|
-
message: "This demo has no data sources.",
|
|
97
|
-
isRetryable: false,
|
|
98
|
-
},
|
|
105
|
+
error: hostHandlerNotImplemented("openExternalUrl"),
|
|
99
106
|
}),
|
|
100
107
|
resize: message => setHeight(message.height),
|
|
101
108
|
},
|
|
@@ -110,7 +117,6 @@ function mountCustomBlock(container: HTMLElement, blockUrl: URL): () => void {
|
|
|
110
117
|
status.textContent = `Block failed: ${state.error.code}`
|
|
111
118
|
}
|
|
112
119
|
},
|
|
113
|
-
onQueryUnsubscribed: undefined,
|
|
114
120
|
})
|
|
115
121
|
|
|
116
122
|
// The host must listen before the block starts its handshake.
|
|
@@ -133,6 +139,12 @@ pixels.
|
|
|
133
139
|
Call `dispose()` when your application removes the embed. To retry a failed connection or change the
|
|
134
140
|
block URL, dispose of the current embed and call `mountCustomBlock()` again.
|
|
135
141
|
|
|
142
|
+
Initial `theme`, `contrastMode`, `containingPage`, and `parent` may be `null` while loading. Call
|
|
143
|
+
the corresponding setters as values resolve. The shared host waits for all four before sending
|
|
144
|
+
`init`. The wait counts toward `initialStateTimeoutMs` after `connect`. Setters take resolved values
|
|
145
|
+
only. For containing-page context, `{ type: "unavailable" }` means denied access, distinct from
|
|
146
|
+
loading.
|
|
147
|
+
|
|
136
148
|
For a production integration, replace the demo context and manifest with your application's values.
|
|
137
149
|
Supply data-source bindings and handlers for the APIs your block uses. Your application must enforce
|
|
138
150
|
permissions and record access.
|
|
@@ -173,7 +185,7 @@ import {
|
|
|
173
185
|
`LATEST_BRIDGE_PROTOCOL_VERSION` identifies the latest bridge protocol version defined by the
|
|
174
186
|
bundled protocol.
|
|
175
187
|
|
|
176
|
-
Host APIs, configuration, handler results, and
|
|
188
|
+
Host APIs, configuration, handler results, and domain argument types are available from
|
|
177
189
|
`@notionhq/custom-blocks-host`.
|
|
178
190
|
|
|
179
191
|
These schemas validate wire payloads. Your application still enforces permissions.
|
|
@@ -185,9 +197,9 @@ additional modules, such as under an exact-URL Content Security Policy.
|
|
|
185
197
|
|
|
186
198
|
### Native page icons
|
|
187
199
|
|
|
188
|
-
`CustomBlockHostPageIcon` accepts canonical native icon names from
|
|
189
|
-
|
|
200
|
+
`CustomBlockHostPageIcon` accepts canonical native icon names from hosts. Hosts validate names and
|
|
201
|
+
colors before writing. The runtime validates response payloads.
|
|
190
202
|
|
|
191
|
-
|
|
203
|
+
Hosts must check URL-backed icon and cover writes against the block's network permissions before
|
|
192
204
|
mutating records. Notion permits the submitted HTTPS destination only when its origin is
|
|
193
205
|
allowlisted; that origin is trusted to redirect.
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Handlers connect block requests to your application's data access, persistence, and navigation. The
|
|
4
4
|
runtime validates bridge messages and manages subscriptions. Your application enforces viewer
|
|
5
|
-
permissions and implements the requested operations. This guide covers handler contracts,
|
|
6
|
-
data-source
|
|
5
|
+
permissions and implements the requested operations. This guide covers handler contracts,
|
|
6
|
+
data-source setup, updates, and external navigation.
|
|
7
7
|
|
|
8
8
|
The [dev shell](https://www.npmjs.com/package/@notionhq/custom-blocks-dev-shell) is a working host
|
|
9
9
|
implementation with local sample data. Use it as a reference for a larger integration. Production
|
|
@@ -11,8 +11,11 @@ hosts must provide their own permissions, data access, and persistence.
|
|
|
11
11
|
|
|
12
12
|
## Handler contracts
|
|
13
13
|
|
|
14
|
-
`
|
|
15
|
-
|
|
14
|
+
Every `CustomBlockHostHandlers` method is required. Use `hostHandlerNotImplemented()` in explicit
|
|
15
|
+
wrappers for the
|
|
16
|
+
[operations that can report unsupported](host-contract.md#options-and-partial-implementations).
|
|
17
|
+
Result-returning handlers return promises. Errors carry `{ code, message, isRetryable }`, with open
|
|
18
|
+
string codes.
|
|
16
19
|
|
|
17
20
|
### Return bridge data
|
|
18
21
|
|
|
@@ -23,151 +26,41 @@ The host validates result shapes, matches responses to requests, and converts re
|
|
|
23
26
|
block's protocol version. Your application checks that records exist and that the viewer may perform
|
|
24
27
|
the requested operation.
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
`
|
|
29
|
+
The runtime validates query filters and sorts against the bound data-source schema before
|
|
30
|
+
`subscribeToQuery` runs. See the [wire reference](../../bridge/protocol.md#data-sources) for
|
|
28
31
|
query semantics.
|
|
29
32
|
|
|
30
|
-
For one-shot requests, a
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
For one-shot requests, a handler that throws, rejects, or returns a malformed result produces
|
|
34
|
+
`unknown_error`. Exception details go to `onLog`; the sandbox receives a generic error. Specific
|
|
35
|
+
errors returned by handlers pass through unchanged.
|
|
33
36
|
|
|
34
37
|
## Connect a data source
|
|
35
38
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Keep the iframe, context, and lifecycle callbacks from the embed example. The block must declare the
|
|
41
|
-
same manifest and query `tasks` through the SDK's public APIs.
|
|
42
|
-
|
|
43
|
-
### Declare and bind the data source
|
|
44
|
-
|
|
45
|
-
The manifest uses block-defined keys. Bindings connect those keys to actual data-source and property
|
|
46
|
-
IDs. Add these definitions before creating the host:
|
|
47
|
-
|
|
48
|
-
```ts
|
|
49
|
-
import type {
|
|
50
|
-
CustomBlockHostDataSourcesPayload,
|
|
51
|
-
CustomBlockHostHandlers,
|
|
52
|
-
} from "@notionhq/custom-blocks-host"
|
|
53
|
-
import {
|
|
54
|
-
makeBridgePage,
|
|
55
|
-
type BridgeCustomBlockManifest,
|
|
56
|
-
type BridgePage,
|
|
57
|
-
} from "@notionhq/custom-blocks-host/protocol"
|
|
58
|
-
|
|
59
|
-
const tasksId = "44444444-4444-4444-8444-444444444444"
|
|
60
|
-
|
|
61
|
-
const manifest: BridgeCustomBlockManifest = {
|
|
62
|
-
version: 1,
|
|
63
|
-
dataSources: {
|
|
64
|
-
tasks: {
|
|
65
|
-
name: "Tasks",
|
|
66
|
-
properties: {
|
|
67
|
-
estimate: { name: "Estimate", type: "number" },
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const taskBindings: CustomBlockHostDataSourcesPayload = {
|
|
74
|
-
bindings: {
|
|
75
|
-
tasks: {
|
|
76
|
-
collectionPointer: { table: "collection", id: tasksId },
|
|
77
|
-
collectionSchema: {
|
|
78
|
-
propertiesById: {
|
|
79
|
-
"estimate-id": { name: "Estimate", type: "number" },
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
propertyIdsByKey: { estimate: "estimate-id" },
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
In the embed example, return `{ status: "success", manifest }` from `getPersistedManifest` and
|
|
89
|
-
replace `initialState.dataSources` with `taskBindings`. Here, `tasks` and `estimate` are semantic
|
|
90
|
-
keys; `tasksId` and `estimate-id` identify the bound data source and property.
|
|
91
|
-
|
|
92
|
-
### Return query results
|
|
93
|
-
|
|
94
|
-
Add this handler and use it as the embed example's `handlers.queryDataSource`. Keep its existing
|
|
95
|
-
`resize` handler.
|
|
96
|
-
|
|
97
|
-
```ts
|
|
98
|
-
let estimate = 3
|
|
99
|
-
|
|
100
|
-
const handlers: CustomBlockHostHandlers = {
|
|
101
|
-
queryDataSource: message => {
|
|
102
|
-
if (message.dataSourceId !== tasksId) {
|
|
103
|
-
return {
|
|
104
|
-
status: "error",
|
|
105
|
-
error: {
|
|
106
|
-
code: "unmapped_data_source",
|
|
107
|
-
message: "This data source is not available.",
|
|
108
|
-
isRetryable: false,
|
|
109
|
-
},
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (message.filter || message.sorts?.length) {
|
|
114
|
-
return {
|
|
115
|
-
status: "error",
|
|
116
|
-
error: {
|
|
117
|
-
code: "invalid_data_source_query",
|
|
118
|
-
message: "This demo supports only unfiltered, unsorted queries.",
|
|
119
|
-
isRetryable: false,
|
|
120
|
-
},
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const items: BridgePage[] = [
|
|
125
|
-
makeBridgePage({
|
|
126
|
-
id: "55555555-5555-4555-8555-555555555555",
|
|
127
|
-
parent: { type: "data_source_id", data_source_id: tasksId },
|
|
128
|
-
is_archived: false,
|
|
129
|
-
in_trash: false,
|
|
130
|
-
properties: {
|
|
131
|
-
Estimate: { id: "estimate-id", type: "number", number: estimate },
|
|
132
|
-
},
|
|
133
|
-
}),
|
|
134
|
-
]
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
status: "success",
|
|
138
|
-
items: items.slice(0, message.limit),
|
|
139
|
-
hasMore: items.length > message.limit,
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
The SDK resolves `tasks` to `tasksId` before the host receives the request. Result properties use
|
|
146
|
-
display names as keys (`Estimate`) and include their raw property IDs (`estimate-id`). The runtime
|
|
147
|
-
handles subscription IDs, validates results, and converts them for supported older SDKs.
|
|
148
|
-
|
|
149
|
-
This demo rejects filters and sorts rather than silently ignoring them. A production adapter must
|
|
150
|
-
enforce record access, execute the requested filter and sorts, and honor the result limit. See
|
|
151
|
-
[query semantics](../../bridge/protocol.md#data-sources).
|
|
39
|
+
To extend the [embed example](../README.md#create-a-host) with a data source, add its manifest
|
|
40
|
+
declaration, bindings, metadata handler, and query handler. The block uses the same declaration and
|
|
41
|
+
queries the source through the SDK's public APIs.
|
|
152
42
|
|
|
153
|
-
|
|
43
|
+
Declare `tasks` and its property keys in the manifest. Supply their IDs in `bindings.dataSources`,
|
|
44
|
+
and return the schema from `getDataSource({ dataSourceId, signal })`. Missing resources and denied
|
|
45
|
+
access both return `{ status: "success", state: { status: "unavailable" } }`. Binding failures
|
|
46
|
+
return an error. See [metadata and configuration](host-contract.md#metadata-and-configuration).
|
|
154
47
|
|
|
155
|
-
|
|
48
|
+
`subscribeToQuery` receives the source ID, query options, limit, signal, and callbacks described in
|
|
49
|
+
[queries and cancellation](host-contract.md#queries-and-cancellation). Enforce record access, apply
|
|
50
|
+
the requested filters, sorts, and archive selection, and honor the limit before calling
|
|
51
|
+
`onSnapshot({ items, hasMore })`. Result properties use display names as keys and include raw
|
|
52
|
+
property IDs. The runtime validates results and converts them for supported older SDKs.
|
|
156
53
|
|
|
157
|
-
|
|
158
|
-
estimate = 5
|
|
159
|
-
host.refreshQuery({ dataSourceId: tasksId })
|
|
160
|
-
```
|
|
54
|
+
## Refresh active queries
|
|
161
55
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
embed.
|
|
56
|
+
The platform observer calls `onSnapshot` again when rows change. Release the observer and cancel
|
|
57
|
+
pending setup when the subscription signal aborts. The shared host handles subscription IDs,
|
|
58
|
+
replacement, unsubscribe, and shutdown. Do not return a second cleanup callback.
|
|
166
59
|
|
|
167
|
-
`
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
60
|
+
Use `host.refreshDataSource({ dataSourceId })` when source metadata or access changes. A successful
|
|
61
|
+
refresh sends updated bindings and cancels queries whose bindings changed. Unchanged bindings keep
|
|
62
|
+
their existing subscriptions. Ordinary row changes flow through the existing observer. See
|
|
63
|
+
[queries and cancellation](host-contract.md#queries-and-cancellation).
|
|
171
64
|
|
|
172
65
|
## Page and user operations
|
|
173
66
|
|
|
@@ -182,9 +75,9 @@ CSS pixels. The application owns navigation and iframe sizing.
|
|
|
182
75
|
## External navigation
|
|
183
76
|
|
|
184
77
|
`openExternalUrl` receives an `AbortSignal` and an absolute HTTP or HTTPS URL without credentials.
|
|
185
|
-
The URL is limited to 2,048 JavaScript code units before and after normalization. The
|
|
186
|
-
displays the exact URL, obtains approval, and navigates only after approval. It may
|
|
187
|
-
restrictions. Dismiss pending confirmation UI when the signal aborts.
|
|
78
|
+
The URL is limited to 2,048 JavaScript code units before and after normalization. The host
|
|
79
|
+
implementation displays the exact URL, obtains approval, and navigates only after approval. It may
|
|
80
|
+
apply further restrictions. Dismiss pending confirmation UI when the signal aborts.
|
|
188
81
|
|
|
189
82
|
Each iframe permits one pending navigation. A different request receives `pending_navigation`. A
|
|
190
83
|
duplicate of the active request is ignored. Completed requests are forgotten, so later requests
|
|
@@ -205,6 +98,6 @@ properties, the manifest property stays unmapped.
|
|
|
205
98
|
`toNotionDateValue()` converts `NotionDateInput` into the four `CustomBlockDateValue` variants,
|
|
206
99
|
including reminders. Invalid input throws `InvalidNotionDateError`.
|
|
207
100
|
|
|
208
|
-
The root exports host configuration, handlers, results, and
|
|
101
|
+
The root exports host configuration, handlers, results, and domain argument types. Wire schemas,
|
|
209
102
|
message and page types, and constants such as `LATEST_BRIDGE_PROTOCOL_VERSION` use the
|
|
210
103
|
[protocol subpath](../README.md#protocol-schemas-and-types).
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# Host contract
|
|
2
|
+
|
|
3
|
+
`createCustomBlockHost(options: CustomBlockHostOptions)` returns a running `CustomBlockHost`.
|
|
4
|
+
`CustomBlockHostHandlers` connects that instance to application data and actions.
|
|
5
|
+
|
|
6
|
+
Your application supplies authorized data and actions. The shared host owns initialization, message
|
|
7
|
+
validation, protocol compatibility, subscriptions, cancellation, and shutdown. Each host instance
|
|
8
|
+
belongs to one viewer. Stop the host and create a fresh host and iframe to change viewers.
|
|
9
|
+
|
|
10
|
+
## Options and partial implementations
|
|
11
|
+
|
|
12
|
+
Required options are `blockId`, `viewerId`, `getCurrentUser`, `theme`, `contrastMode`,
|
|
13
|
+
`containingPage`, `parent`, `iframe`, `sandboxUrl`, `manifestSource`, `getPersistedManifest`,
|
|
14
|
+
`onStatusChange`, `bindings`, and `handlers`. `blockId` and `viewerId` stay fixed for the instance.
|
|
15
|
+
Optional settings are `onLog`, `minBridgeProtocolVersion`, `noConnectTimeoutMs`,
|
|
16
|
+
`initialStateTimeoutMs`, and `noInitResultTimeoutMs`.
|
|
17
|
+
|
|
18
|
+
The four initial rendering values (`theme`, `contrastMode`, `containingPage`, and `parent`) are
|
|
19
|
+
required options that accept `null` while unresolved. Supply each resolved value through `setTheme`,
|
|
20
|
+
`setContrastMode`, `setContainingPage`, or `setParent`. Setters accept resolved values only. The
|
|
21
|
+
shared host retains the latest values and waits for all four before sending the `init` payload. This
|
|
22
|
+
wait counts toward `initialStateTimeoutMs` after connection. Stopping or failing the host cancels
|
|
23
|
+
the wait. Unresolved rendering values are never sent to the sandbox.
|
|
24
|
+
|
|
25
|
+
For containing-page context, `null` means not loaded yet. `{ type: "unavailable" }` means the viewer
|
|
26
|
+
cannot access it. The latter is a resolved value, subject to the existing protocol compatibility
|
|
27
|
+
rules. The application observes subsequent context changes and calls the same setters. Creating the
|
|
28
|
+
host does not subscribe to application state.
|
|
29
|
+
|
|
30
|
+
Every handler is required. `hostHandlerNotImplemented` accepts `getPage`, `createPage`,
|
|
31
|
+
`updatePage`, `getUser`, `listUsers`, `openPage`, `openExternalUrl`, and `subscribeToQuery`.
|
|
32
|
+
Failures from these operations leave initialization and unrelated operations available. The
|
|
33
|
+
current-user profile and metadata for declared data sources are needed during initialization.
|
|
34
|
+
`resize` has no error result and can do nothing for a fixed-size embed.
|
|
35
|
+
|
|
36
|
+
A partial integration supplies an explicit wrapper using `hostHandlerNotImplemented`, which creates
|
|
37
|
+
a non-retryable `host_handler_not_implemented` error:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import {
|
|
41
|
+
createCustomBlockHost,
|
|
42
|
+
hostHandlerNotImplemented,
|
|
43
|
+
type CustomBlockHostOptions,
|
|
44
|
+
} from "@notionhq/custom-blocks-host"
|
|
45
|
+
|
|
46
|
+
declare const options: CustomBlockHostOptions
|
|
47
|
+
|
|
48
|
+
options.handlers.getPage = async () => ({
|
|
49
|
+
status: "error",
|
|
50
|
+
error: hostHandlerNotImplemented("getPage"),
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
options.handlers.subscribeToQuery = ({ onError }) => {
|
|
54
|
+
onError(hostHandlerNotImplemented("subscribeToQuery"))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const host = createCustomBlockHost(options)
|
|
58
|
+
host.stop()
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`resize` returns no result and sends no result message. The application enforces authorization and
|
|
62
|
+
obtains any required approval. Neither manifest declarations nor bindings establish access.
|
|
63
|
+
|
|
64
|
+
## Handler inputs and results
|
|
65
|
+
|
|
66
|
+
`CustomBlockHostHandlerResult<Payload>` combines `{ status: "success" }` with named payload fields,
|
|
67
|
+
or returns `{ status: "error", error: { code, message, isRetryable } }`. For example, `getUser`
|
|
68
|
+
returns `{ status: "success", user }` and `listUsers` returns `{ status: "success", list }`.
|
|
69
|
+
`getPersistedManifest` returns `{ status: "success", manifest }`. Actions without payloads return
|
|
70
|
+
`{ status: "success" }`. The shared host validates results and logs unexpected exceptions before
|
|
71
|
+
returning a generic error to the sandbox.
|
|
72
|
+
|
|
73
|
+
Asynchronous providers receive `CustomBlockHostRequestContext` with an `AbortSignal`. Inputs omit
|
|
74
|
+
sandbox message types and correlation IDs. Named domain arguments such as
|
|
75
|
+
`CustomBlockHostCreatePageArgs`, `CustomBlockHostUpdatePageArgs`, and `CustomBlockHostOpenPageArgs`
|
|
76
|
+
contain only operation data. Handlers receive those arguments combined with
|
|
77
|
+
`CustomBlockHostRequestContext`; the runtime supplies `signal`. Page operations retain their
|
|
78
|
+
existing domain payloads, including parent-dependent insertion rules, properties, and appearance.
|
|
79
|
+
Source and page operations use raw IDs. The SDK resolves data-source keys before sending requests.
|
|
80
|
+
|
|
81
|
+
| Handler | Additional input | Success payload |
|
|
82
|
+
| ----------------- | ---------------------------------- | --------------------------------------- |
|
|
83
|
+
| `getDataSource` | `dataSourceId` | `state: CustomBlockHostDataSourceState` |
|
|
84
|
+
| `getPage` | `pageId`, optional `dataSourceId` | `page: BridgePage` |
|
|
85
|
+
| `createPage` | Existing creation payload | `page: BridgePage` |
|
|
86
|
+
| `updatePage` | Existing update payload | `page: BridgePage` |
|
|
87
|
+
| `getUser` | `userId` | `user: BridgeUser` |
|
|
88
|
+
| `listUsers` | Optional `pageSize`, `startCursor` | `list: BridgeUserList` |
|
|
89
|
+
| `openPage` | `pageId`, `mode` | None |
|
|
90
|
+
| `openExternalUrl` | `url` | None |
|
|
91
|
+
|
|
92
|
+
`openPage` completes the requested action without echoing its input. The shared host includes the
|
|
93
|
+
requested page ID in the existing wire response.
|
|
94
|
+
|
|
95
|
+
Result-returning handlers return promises of explicit results.
|
|
96
|
+
`CustomBlockHostHandler<Args, Payload>` combines domain arguments with
|
|
97
|
+
`CustomBlockHostRequestContext` and the shared result type. `subscribeToQuery` and `resize` remain
|
|
98
|
+
synchronous.
|
|
99
|
+
|
|
100
|
+
## Current viewer
|
|
101
|
+
|
|
102
|
+
`viewerId` is a required, nonempty identity supplied before host construction. The application
|
|
103
|
+
handles missing authentication before mounting a host. Anonymous viewers are not modeled by this
|
|
104
|
+
contract.
|
|
105
|
+
|
|
106
|
+
`getCurrentUser({ signal })` is a required constructor callback returning a promise of a complete
|
|
107
|
+
`BridgeUser` profile directly, without a success envelope or separate field readers. The shared host
|
|
108
|
+
validates the profile and rejects initialization if its ID differs from `viewerId`. Rejected reads
|
|
109
|
+
and malformed profiles become a generic initialization error; their details are not sent to the
|
|
110
|
+
sandbox.
|
|
111
|
+
|
|
112
|
+
Existing protocols require the full profile in `init`, so initialization invokes the callback and
|
|
113
|
+
waits for it. The callback may run before `connect` when the selected manifest is already available.
|
|
114
|
+
Changing this startup dependency requires a later minimal-init protocol. The host adds no retained
|
|
115
|
+
profile cache or new field-grant behavior.
|
|
116
|
+
|
|
117
|
+
## Metadata and configuration
|
|
118
|
+
|
|
119
|
+
`customBlockHostBindingsSchema` and `customBlockHostDataSourceStateSchema` are host-only Valibot
|
|
120
|
+
schemas exported with their types. They describe application configuration and source metadata
|
|
121
|
+
separately from wire payloads.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
type CustomBlockHostBindings = {
|
|
125
|
+
pages: Record<string, { pageId: string }>
|
|
126
|
+
dataSources: Record<
|
|
127
|
+
string,
|
|
128
|
+
{
|
|
129
|
+
dataSourceId: string
|
|
130
|
+
propertyIdsByKey: Record<string, string | undefined>
|
|
131
|
+
}
|
|
132
|
+
>
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The host validates configuration shape. `pages` accepts page keys and IDs, but the runtime does not
|
|
137
|
+
use them to resolve page requests or include them in `init`. The host converts `dataSources` to
|
|
138
|
+
bridge bindings for initialization and updates. The shared host reads every declared source and
|
|
139
|
+
deduplicates aliases of the same source ID within each read, without executing a row query for its
|
|
140
|
+
schema. Initialization and each configuration refresh read metadata independently. The host does not
|
|
141
|
+
reuse metadata promises across reads.
|
|
142
|
+
|
|
143
|
+
`CustomBlockHostDataSourceState` is `{ status: "unavailable" }` or
|
|
144
|
+
`{ status: "available", dataSource: { collectionSchema, capabilities } }`. Capabilities contain
|
|
145
|
+
`read`, `create`, `update`, and `archive`. These flags describe platform access. The runtime
|
|
146
|
+
validates the flags but does not use them to authorize requests or send them to the sandbox.
|
|
147
|
+
Handlers enforce viewer permissions on each operation.
|
|
148
|
+
|
|
149
|
+
Missing and unreadable sources both use `{ status: "unavailable" }`. A metadata load keeps its
|
|
150
|
+
promise pending until it succeeds or fails. Binding errors return error results. The host converts
|
|
151
|
+
an unavailable source to `{ inaccessible: true }` without a schema, pointer, or property mappings.
|
|
152
|
+
|
|
153
|
+
## Queries and cancellation
|
|
154
|
+
|
|
155
|
+
`subscribeToQuery(args: CustomBlockHostSubscribeToQueryArgs): void` receives `dataSourceId`,
|
|
156
|
+
optional `filter`, `sorts`, and `archive_status`, plus `limit`, `signal`, `onSnapshot`, and
|
|
157
|
+
`onError`. `archive_status` accepts `"active"`, `"archived"`, or `"all"`. See the
|
|
158
|
+
[query semantics](../../bridge/protocol.md#data-sources) for archive selection defaults. A
|
|
159
|
+
`CustomBlockHostQuerySnapshot` contains `items: BridgePage[]` and `hasMore`. The shared host
|
|
160
|
+
validates filters and sorts against the bound schema before starting the observer.
|
|
161
|
+
|
|
162
|
+
The signal controls the subscription lifetime, including asynchronous setup. The application checks
|
|
163
|
+
for an already-aborted signal and cancels pending setup and observers on abort. Replacement,
|
|
164
|
+
unsubscribe, invalidation, non-retryable errors, and shutdown abort the signal. Retryable errors can
|
|
165
|
+
leave the observer active. Callbacks may run synchronously; late callbacks after cancellation are
|
|
166
|
+
ignored. There is no returned cleanup function or top-level query-unsubscribed callback. The SDK
|
|
167
|
+
returns its own unsubscribe function, which sends the cancellation request to the host.
|
|
168
|
+
|
|
169
|
+
`resize({ height })` requests an iframe height in CSS pixels. The application applies the height.
|
|
170
|
+
The [lifecycle reference](lifecycle.md#callbacks) describes when to size and reveal the iframe.
|
|
171
|
+
|
|
172
|
+
## Updates
|
|
173
|
+
|
|
174
|
+
`stop()`, `setTheme()`, `setContrastMode()`, `setParent()`, and `setContainingPage()` return `void`.
|
|
175
|
+
`setBindings()` and `refreshDataSource()` return promises of `CustomBlockHostHandlerResult`:
|
|
176
|
+
|
|
177
|
+
- `setBindings(nextBindings)` reads metadata and validates the candidate before committing it. A
|
|
178
|
+
rejected update leaves the accepted configuration, pending operations, and subscriptions active. A
|
|
179
|
+
successful update commits the bindings and cancels queries whose bridge bindings changed. Queries
|
|
180
|
+
with unchanged bindings keep their observers.
|
|
181
|
+
- `refreshDataSource({ dataSourceId })` rereads metadata for all declared sources and validates the
|
|
182
|
+
configuration before notifying the sandbox. Queries whose bridge bindings changed are canceled.
|
|
183
|
+
Queries with unchanged bindings remain active, including queries for the refreshed source.
|
|
184
|
+
|
|
185
|
+
Only the latest configuration refresh can commit. Superseded refreshes are aborted. Before the
|
|
186
|
+
manifest is available, `setBindings()` stores the bindings and returns success. Initialization
|
|
187
|
+
validates those bindings later. Once the manifest is available, a successful update supplies the
|
|
188
|
+
metadata used for initialization, even if the initial metadata read failed while user loading was
|
|
189
|
+
pending. Malformed input to `setBindings()` rejects its promise with a schema validation error
|
|
190
|
+
rather than returning an error result.
|
|
191
|
+
|
|
192
|
+
A successful configuration change cancels outstanding one-shot host operations, including unrelated
|
|
193
|
+
reads. A late completion returns non-retryable `host_state_changed`. A dispatched write may already
|
|
194
|
+
have committed. Cancellation does not roll back a write. Stopping or failing the host aborts
|
|
195
|
+
initialization, pending operations, metadata refreshes, and subscriptions before reporting its final
|
|
196
|
+
status.
|