@notionhq/custom-blocks-dev-shell 0.1.116 → 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/README.md +8 -2
- package/dist/assets/index-CWJ-vFk0.js +12 -0
- package/dist/index.html +1 -1
- package/dist-cli/cli/published-cli.js +271 -111
- package/docs/bridge/changelog.md +28 -1
- package/docs/bridge/protocol.md +51 -15
- package/docs/bridge/versioning.md +1 -0
- package/docs/data-sources.md +6 -4
- package/docs/host/README.md +64 -36
- package/docs/host/docs/compatibility.md +15 -7
- package/docs/host/docs/handlers.md +45 -141
- package/docs/host/docs/host-contract.md +196 -0
- package/docs/host/docs/lifecycle.md +28 -23
- package/package.json +1 -1
- package/dist/assets/index-DSaseWP_.js +0 -11
|
@@ -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.
|
|
@@ -14,12 +14,13 @@ messages and target outgoing messages. `CustomBlockManifestSource` selects the m
|
|
|
14
14
|
| `"host"` | The manifest returned by `getPersistedManifest({ signal })` |
|
|
15
15
|
| `"sandbox"` | The local declaration supplied in `connect.manifest`, subject to the [local URL policy](#local-block-urls) |
|
|
16
16
|
|
|
17
|
-
`getPersistedManifest` is required in both modes and starts when the host is created. It returns
|
|
18
|
-
`
|
|
19
|
-
`{ status: "error", error }`.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
and
|
|
17
|
+
`getPersistedManifest` is required in both modes and starts when the host is created. It returns a
|
|
18
|
+
`CustomBlockHostHandlerResult` with either `{ status: "success", manifest }` or
|
|
19
|
+
`{ status: "error", error }`. The manifest follows `manifestSchema` from the protocol subpath. The
|
|
20
|
+
callback must not resolve to `null` or `undefined`. A definition that is still loading keeps the
|
|
21
|
+
promise pending; a failed load returns an error result. The host validates the result and normalizes
|
|
22
|
+
the manifest before reading the current user and source metadata. Omitted `dataSources` and each
|
|
23
|
+
source's omitted `properties` default to `{}`.
|
|
23
24
|
|
|
24
25
|
The sandbox loads independently and sends `connect`. In sandbox mode, the SDK reads the manifest
|
|
25
26
|
from JSON metadata injected into the local block HTML by dev tooling. The host validates the
|
|
@@ -28,18 +29,22 @@ determines initialization. Sandbox mode has no persisted fallback. The
|
|
|
28
29
|
[wire reference](../../bridge/protocol.md#initialization) describes the declaration and its
|
|
29
30
|
validation errors.
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
`
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
Supply initial `theme`, `contrastMode`, `containingPage`, and `parent` as required constructor
|
|
33
|
+
options, using `null` for values that have not loaded yet. Set resolved values with the
|
|
34
|
+
corresponding setters. The shared host retains the latest values and waits for all four before
|
|
35
|
+
sending `init`. The wait counts toward `initialStateTimeoutMs` after `connect`. A containing-page
|
|
36
|
+
value of `{ type: "unavailable" }` means denied access, not loading. Once the selected manifest is
|
|
37
|
+
ready, the host reads the current user and source metadata through the handlers. The shared host
|
|
38
|
+
assembles and validates the existing init payload. See the [host contract](host-contract.md) for
|
|
39
|
+
handler results and cancellation. Host-mode preparation runs concurrently with connection setup.
|
|
40
|
+
Sandbox-mode preparation starts after `connect` supplies the declaration. The host sends `init`
|
|
41
|
+
after preparation and a valid connection succeed. A preparation failure is retained until the
|
|
42
|
+
sandbox connects.
|
|
38
43
|
|
|
39
44
|
Errors from the selected manifest source retain their `code`, `message`, and `isRetryable`. A
|
|
40
45
|
missing sandbox declaration produces `manifest_unavailable`. A sandbox `manifest_invalid` error is
|
|
41
46
|
ignored in host mode because that mode uses the persisted manifest; other connect errors remain
|
|
42
|
-
fatal. Unexpected exceptions or invalid results
|
|
47
|
+
fatal. Unexpected exceptions or invalid results during preparation produce `unknown_error`.
|
|
43
48
|
Diagnostics go to `onLog("warn", payload)`. Stopping aborts preparation and suppresses late results
|
|
44
49
|
without a cancellation warning.
|
|
45
50
|
|
|
@@ -52,13 +57,16 @@ without a cancellation warning.
|
|
|
52
57
|
| `noInitResultTimeoutMs` | `initResult` after `init` | 5 seconds | `no_init_result` |
|
|
53
58
|
|
|
54
59
|
The SDK waits for `init` without its own timeout. Handshake failures abort preparation and suppress
|
|
55
|
-
late results.
|
|
60
|
+
late results. `getPersistedManifest`, `getCurrentUser`, and `getDataSource` must respect their
|
|
61
|
+
`signal`.
|
|
56
62
|
|
|
57
63
|
## Live state
|
|
58
64
|
|
|
59
|
-
`setTheme()`, `setContrastMode()`, `setParent()`, `
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
`setTheme()`, `setContrastMode()`, `setParent()`, and `setContainingPage()` hold updates during
|
|
66
|
+
initialization and send them after `initResult.success`. Use `setBindings()` and
|
|
67
|
+
`refreshDataSource()` to refresh source configuration. Each host instance belongs to one viewer.
|
|
68
|
+
Stop it and create a fresh host and iframe when the viewer changes. See
|
|
69
|
+
[updates](host-contract.md#updates) for their results and cancellation behavior.
|
|
62
70
|
|
|
63
71
|
## Callbacks
|
|
64
72
|
|
|
@@ -74,7 +82,7 @@ The required `onStatusChange(state)` callback receives the `CustomBlockHostStatu
|
|
|
74
82
|
| `stopped` | The application explicitly disposed of the host | None |
|
|
75
83
|
|
|
76
84
|
`localManifest` is optional and comes only from a permitted local sandbox. It is available before
|
|
77
|
-
initialization completes, so
|
|
85
|
+
initialization completes, so the application can use its requirements to prepare bindings.
|
|
78
86
|
`resolvedManifest` is the selected declaration sent in `init`. Rejected connections can skip
|
|
79
87
|
intermediate statuses. Both `failed` and `stopped` are terminal; stopping a failed host releases
|
|
80
88
|
resources without replacing its failure status.
|
|
@@ -87,10 +95,7 @@ At `initialized`, apply a supplied `initialHeight` in CSS pixels before revealin
|
|
|
87
95
|
height is absent, use your application's default size. The `resize` handler applies later height
|
|
88
96
|
changes. Reveal, sizing, and failure UI belong to the embedding application.
|
|
89
97
|
|
|
90
|
-
|
|
91
|
-
| ------------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
92
|
-
| `onQueryUnsubscribed(subscriptionId)` | Runs once when a subscription ends through unsubscribe, replacement, terminal error, or stop |
|
|
93
|
-
| `onLog(direction, payload)` | Receives bridge messages and warnings |
|
|
98
|
+
The optional `onLog(direction, payload)` callback receives bridge messages and warnings.
|
|
94
99
|
|
|
95
100
|
## Iframe lifetime
|
|
96
101
|
|