@notionhq/custom-blocks 0.4.0 → 0.5.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.
Files changed (133) hide show
  1. package/dist/bridge/SandboxBridge.d.ts +0 -16
  2. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  3. package/dist/bridge/SandboxBridge.js +2 -41
  4. package/dist/bridge/parseHostToSandboxMessage.d.ts +3658 -0
  5. package/dist/bridge/parseHostToSandboxMessage.d.ts.map +1 -0
  6. package/dist/bridge/parseHostToSandboxMessage.js +47 -0
  7. package/dist/dataSources/bridge/resolveDataSourceQuery.js +3 -3
  8. package/dist/dataSources/bridge/resolveDataSources.js +5 -5
  9. package/dist/dataSources/filters/resolveFilter.d.ts.map +1 -1
  10. package/dist/dataSources/filters/resolveFilter.js +13 -3
  11. package/dist/dataSources/filters/types.d.ts +9 -3
  12. package/dist/dataSources/filters/types.d.ts.map +1 -1
  13. package/dist/dataSources/properties/resolvePropertyAddress.js +1 -1
  14. package/dist/dataSources/properties/resolvePropertyWriteMapForDataSource.js +1 -1
  15. package/dist/dataSources/resolvePageDataSource.js +2 -2
  16. package/dist/dataSources/subscribeToDataSource.d.ts +1 -4
  17. package/dist/dataSources/subscribeToDataSource.d.ts.map +1 -1
  18. package/dist/dataSources/subscribeToDataSource.js +2 -3
  19. package/dist/files/files.d.ts +2 -0
  20. package/dist/files/files.d.ts.map +1 -0
  21. package/dist/files/files.js +1 -0
  22. package/dist/files/icons.d.ts +2 -0
  23. package/dist/files/icons.d.ts.map +1 -0
  24. package/dist/files/icons.js +1 -0
  25. package/dist/index.d.ts +4 -4
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/initialization/getInitBindingError.js +2 -2
  28. package/dist/manifest/types.d.ts +4 -3
  29. package/dist/manifest/types.d.ts.map +1 -1
  30. package/dist/pages/bridge/getPageForDataSource.js +1 -1
  31. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.d.ts +4 -1
  32. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.d.ts.map +1 -1
  33. package/dist/pages/bridge/toNotionPagePropertyFromBridgeProperty.d.ts.map +1 -1
  34. package/dist/pages/bridge/toNotionPagePropertyFromBridgeProperty.js +0 -2
  35. package/dist/pages/bridge/updatePage.js +1 -1
  36. package/dist/pages/properties/getPropertyByKey.js +2 -2
  37. package/dist/pages/types.d.ts +8 -10
  38. package/dist/pages/types.d.ts.map +1 -1
  39. package/dist/protocol/compatibility/protocol3To10/dataSourceBindings.d.ts +530 -0
  40. package/dist/protocol/compatibility/protocol3To10/dataSourceBindings.js +25 -0
  41. package/dist/protocol/compatibility/protocol3To5/bridgePage.d.ts +6 -7
  42. package/dist/protocol/compatibility/protocol3To5/bridgePage.js +3 -3
  43. package/dist/protocol/context/containingPage.d.ts +3 -0
  44. package/dist/protocol/context/messages/pageChanged.d.ts +3 -0
  45. package/dist/protocol/context/messages/parentChanged.d.ts +3 -0
  46. package/dist/protocol/context/parent.d.ts +3 -0
  47. package/dist/protocol/context/parent.js +6 -1
  48. package/dist/protocol/dataSources/bridgeDataSourceBinding.d.ts +3 -7
  49. package/dist/protocol/dataSources/bridgeDataSourceBinding.js +5 -5
  50. package/dist/protocol/dataSources/bridgeDataSourceBindings.d.ts +2 -6
  51. package/dist/protocol/dataSources/{collectionPointer.d.ts → dataSourcePointer.d.ts} +2 -2
  52. package/dist/protocol/dataSources/{collectionPointer.js → dataSourcePointer.js} +1 -1
  53. package/dist/protocol/dataSources/{collectionSchema.d.ts → dataSourceSchema.d.ts} +2 -6
  54. package/dist/protocol/dataSources/{collectionSchema.js → dataSourceSchema.js} +1 -1
  55. package/dist/protocol/dataSources/filters/bridgeDataSourceFilter.d.ts +1458 -12
  56. package/dist/protocol/dataSources/filters/bridgeDataSourceFilterCondition.d.ts +729 -6
  57. package/dist/protocol/dataSources/filters/bridgeDataSourceFilterCondition.js +14 -2
  58. package/dist/protocol/dataSources/filters/peopleFilterOperator.d.ts +3 -3
  59. package/dist/protocol/dataSources/filters/peopleFilterOperator.js +1 -1
  60. package/dist/protocol/dataSources/filters/timestampFilterOperator.d.ts +354 -0
  61. package/dist/protocol/dataSources/filters/timestampFilterOperator.js +4 -0
  62. package/dist/protocol/dataSources/messages/dataSourcesChanged.d.ts +2 -6
  63. package/dist/protocol/dataSources/messages/queryDataSource.d.ts +1470 -26
  64. package/dist/protocol/dataSources/notionDataSource.d.ts +3 -11
  65. package/dist/protocol/dataSources/notionDataSource.js +5 -5
  66. package/dist/protocol/dataSources/properties/propertySchema.d.ts +4 -9
  67. package/dist/protocol/dataSources/properties/propertySchema.js +2 -4
  68. package/dist/protocol/dataSources/resolveDataSourceBinding.d.ts +4 -4
  69. package/dist/protocol/dataSources/resolveDataSourceBinding.js +2 -2
  70. package/dist/protocol/files.d.ts +32 -0
  71. package/dist/protocol/files.js +17 -0
  72. package/dist/protocol/handshake/connect.d.ts +1 -1
  73. package/dist/protocol/handshake/init.d.ts +9 -7
  74. package/dist/protocol/icons.d.ts +22 -7
  75. package/dist/protocol/icons.js +19 -7
  76. package/dist/protocol/index.d.ts +16 -14
  77. package/dist/protocol/index.js +10 -7
  78. package/dist/protocol/manifest/manifest.d.ts +9 -9
  79. package/dist/protocol/manifest/manifest.js +7 -7
  80. package/dist/protocol/messages/hostToSandbox.d.ts +100 -103
  81. package/dist/protocol/messages/sandboxToHost.d.ts +1471 -37
  82. package/dist/protocol/pages/bridgePage.d.ts +6 -7
  83. package/dist/protocol/pages/bridgePage.js +3 -3
  84. package/dist/protocol/pages/messages/createPage.d.ts +547 -40
  85. package/dist/protocol/pages/messages/createPage.js +27 -15
  86. package/dist/protocol/pages/messages/getPage.d.ts +18 -21
  87. package/dist/protocol/pages/messages/updatePage.d.ts +21 -28
  88. package/dist/protocol/pages/messages/updatePage.js +3 -3
  89. package/dist/protocol/pages/pageAppearance.d.ts +5 -59
  90. package/dist/protocol/pages/pageAppearance.js +11 -40
  91. package/dist/protocol/pages/properties/bridgePagePropertyMaps.d.ts +4 -12
  92. package/dist/protocol/pages/properties/bridgePagePropertyValue.d.ts +2 -6
  93. package/dist/protocol/pages/properties/bridgePagePropertyValue.js +2 -7
  94. package/dist/protocol/utils/ids.d.ts +2 -0
  95. package/dist/protocol/utils/ids.js +1 -0
  96. package/dist/protocol/utils/protocolVersion.d.ts +2 -1
  97. package/dist/protocol/utils/protocolVersion.js +3 -1
  98. package/dist/react/NotionCustomBlock.d.ts.map +1 -1
  99. package/dist/react/NotionCustomBlock.js +1 -15
  100. package/dist/utils/version.js +1 -1
  101. package/docs/data-sources.md +75 -76
  102. package/docs/deployment.md +4 -5
  103. package/docs/pages.md +90 -56
  104. package/package.json +1 -1
  105. package/src/bridge/SandboxBridge.ts +3 -58
  106. package/src/bridge/parseHostToSandboxMessage.ts +54 -0
  107. package/src/dataSources/bridge/resolveDataSourceQuery.ts +3 -3
  108. package/src/dataSources/bridge/resolveDataSources.ts +6 -6
  109. package/src/dataSources/filters/resolveFilter.ts +12 -3
  110. package/src/dataSources/filters/types.ts +7 -4
  111. package/src/dataSources/properties/resolvePropertyAddress.ts +1 -1
  112. package/src/dataSources/properties/resolvePropertyWriteMapForDataSource.ts +1 -1
  113. package/src/dataSources/resolvePageDataSource.ts +2 -2
  114. package/src/dataSources/subscribeToDataSource.ts +7 -19
  115. package/src/files/files.ts +5 -0
  116. package/src/files/icons.ts +9 -0
  117. package/src/index.ts +6 -11
  118. package/src/initialization/getInitBindingError.ts +2 -2
  119. package/src/manifest/types.ts +9 -3
  120. package/src/pages/bridge/getPageForDataSource.ts +1 -1
  121. package/src/pages/bridge/toNotionPagePropertyFromBridgeProperty.ts +0 -2
  122. package/src/pages/bridge/updatePage.ts +1 -1
  123. package/src/pages/properties/getPropertyByKey.ts +2 -2
  124. package/src/pages/types.ts +9 -13
  125. package/src/react/NotionCustomBlock.tsx +1 -16
  126. package/dist/protocol/dataSources/filters/createdByFilterOperator.d.ts +0 -17
  127. package/dist/protocol/dataSources/filters/createdByFilterOperator.js +0 -11
  128. package/dist/protocol/pages/properties/fileInput.d.ts +0 -16
  129. package/dist/protocol/pages/properties/fileInput.js +0 -16
  130. package/dist/react/DebugMessageLog.d.ts +0 -2
  131. package/dist/react/DebugMessageLog.d.ts.map +0 -1
  132. package/dist/react/DebugMessageLog.js +0 -23
  133. package/src/react/DebugMessageLog.tsx +0 -34
package/docs/pages.md CHANGED
@@ -103,8 +103,7 @@ The SDK validates all properties before sending an update. Invalid values, misma
103
103
  targets, and unresolved keys return local errors. An update must include at least one property or
104
104
  metadata change. See [Local page update errors](errors.md#local-page-update-errors).
105
105
 
106
- Icons use `NotionPageIcon` and covers use `NotionPageCover`. Supported values include emoji,
107
- external URLs, and existing hosted file URLs. File upload references are unsupported.
106
+ For the supported icon, cover, and file property values, see [Files and icons](#files-and-icons).
108
107
 
109
108
  ## Types
110
109
 
@@ -120,64 +119,71 @@ external URLs, and existing hosted file URLs. File upload references are unsuppo
120
119
 
121
120
  ## Property support
122
121
 
123
- The table below shows property support in Notion:
122
+ This table shows property support for custom blocks running in Notion.
124
123
 
125
- - **Read value** shows `page.properties` after a read, create, or successful update. Unsupported
126
- properties return `null`.
127
- - **Create** covers property writes through `pages.create`.
128
- - **Update** covers property writes through `page.updateById`.
124
+ Read page shows property values returned by reads, creates, and successful updates. Create page and
125
+ Update page cover property writes.
129
126
 
130
- Pages outside a data source support only `title`. Other properties require a data source row and a
131
- matching property in its schema.
127
+ Pages outside a data source support only `title`. Other property writes require a data source row
128
+ and a property in its schema.
129
+
130
+ Property values can be `null` when empty or unavailable.
132
131
 
133
132
  - 🟢 **Supported:** The SDK supports this operation in Notion.
134
- - 🟡 **Limited:** The SDK supports this operation with the listed restrictions.
135
- - 🔴 **Unavailable:** The SDK does not expose this operation.
136
- - ⚫ **Not supported in Notion:** Notion does not support this operation in the stated case.
137
-
138
- | Property type | Read value (`pages.get`) | Create (`pages.create`) | Update (`page.updateById`) |
139
- | ------------------ | ---------------------------------------------------- | -------------------------------------------- | -------------------------------------------- |
140
- | `title` | 🟡 Text array with plain text only | 🟡 Plain text only | 🟡 Plain text only |
141
- | `rich_text` | 🟡 Text array with plain text only | 🟡 Plain text only | 🟡 Plain text only |
142
- | `number` | 🟢 Number or `null` | 🟢 Supported | 🟢 Supported |
143
- | `checkbox` | 🟢 Boolean | 🟢 Supported | 🟢 Supported |
144
- | `url` | 🟢 String or `null` | 🟢 Supported | 🟢 Supported |
145
- | `email` | 🟢 String or `null` | 🟢 Supported | 🟢 Supported |
146
- | `phone_number` | 🟢 String or `null` | 🟢 Supported | 🟢 Supported |
147
- | `select` | 🟡 Does not return available options | 🟡 Set value only | 🟡 Set value only |
148
- | `multi_select` | 🟡 Does not return available options | 🟡 Set value only | 🟡 Set value only |
149
- | `status` | 🟡 Does not return available options | 🟡 Set value only | 🟡 Set value only |
150
- | `date` | 🟢 `start` and optional `end` strings, or `null` | 🟡 Minute precision | 🟡 Minute precision |
151
- | `people` | 🟢 Array of user/group ID objects | 🟢 Supported | 🟢 Supported |
152
- | `files` | 🟢 External links and hosted files | 🟡 File URLs only | 🟡 File URLs only |
153
- | `unique_id` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
154
- | `relation` | 🟢 Array of page ID objects | 🟡 Replaces value. Rejects `has_more: true`. | 🟡 Replaces value. Rejects `has_more: true`. |
155
- | `place` | 🟢 Coordinates and optional place details, or `null` | 🟢 Supported | 🟢 Supported |
156
- | `formula` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
157
- | `rollup` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
158
- | `button` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
159
- | `verification` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
160
- | `location` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
161
- | `created_time` | 🟢 ISO timestamp in UTC | 🔴 Unavailable | 🔴 Unavailable |
162
- | `last_edited_time` | 🟢 ISO timestamp in UTC | 🔴 Unavailable | 🔴 Unavailable |
163
- | `created_by` | 🟢 Actor record pointer | 🔴 Unavailable | 🔴 Unavailable |
164
- | `last_edited_by` | 🟢 Actor record pointer | 🔴 Unavailable | 🔴 Unavailable |
133
+ - 🟡 **Limited:** The SDK supports this operation with the restrictions shown in the table and
134
+ notes.
135
+ - 🔒 **Read-only:** Notion sets this value. You cannot set it when creating or updating a page.
136
+ - 🔴 **Unavailable:** The SDK does not support this operation.
137
+
138
+ | Property type | Read page | Create page | Update page |
139
+ | ------------------ | ----------------------------------------- | ------------------ | ------------------ |
140
+ | `title` | 🟡 Plain text array | 🟡 Plain text only | 🟡 Plain text only |
141
+ | `rich_text` | 🟡 Plain text array | 🟡 Plain text only | 🟡 Plain text only |
142
+ | `number` | 🟢 Number | 🟢 Supported | 🟢 Supported |
143
+ | `checkbox` | 🟢 Boolean | 🟢 Supported | 🟢 Supported |
144
+ | `url` | 🟢 String | 🟢 Supported | 🟢 Supported |
145
+ | `email` | 🟢 String | 🟢 Supported | 🟢 Supported |
146
+ | `phone_number` | 🟢 String | 🟢 Supported | 🟢 Supported |
147
+ | `select` | 🟢 Selected option | 🟢 Supported | 🟢 Supported |
148
+ | `multi_select` | 🟢 Selected options | 🟢 Supported | 🟢 Supported |
149
+ | `status` | 🟢 Selected option | 🟢 Supported | 🟢 Supported |
150
+ | `date` | 🟢 `start` and optional `end` strings | 🟢 Supported | 🟢 Supported |
151
+ | `people` | 🟢 Array of user or group ID objects | 🟢 Supported | 🟢 Supported |
152
+ | `relation` | 🟢 Array of page ID objects | 🟡 Replaces value | 🟡 Replaces value |
153
+ | `place` | 🟢 Coordinates and optional place details | 🟢 Supported | 🟢 Supported |
154
+ | `files` | 🟢 External links and hosted files | 🔴 Unavailable | 🔴 Unavailable |
155
+ | `unique_id` | 🟡 Formatted ID | 🔴 Unavailable | 🔴 Unavailable |
156
+ | `formula` | 🟡 Stored text | 🔴 Unavailable | 🔴 Unavailable |
157
+ | `rollup` | 🟡 Stored text | 🔴 Unavailable | 🔴 Unavailable |
158
+ | `button` | 🟡 Button label | 🔴 Unavailable | 🔴 Unavailable |
159
+ | `verification` | 🟡 Status text | 🔴 Unavailable | 🔴 Unavailable |
160
+ | `created_time` | 🟢 Timestamp | 🔒 Read-only | 🔒 Read-only |
161
+ | `last_edited_time` | 🟢 Timestamp | 🔒 Read-only | 🔒 Read-only |
162
+ | `created_by` | 🟢 Actor reference | 🔒 Read-only | 🔒 Read-only |
163
+ | `last_edited_by` | 🟢 Actor reference | 🔒 Read-only | 🔒 Read-only |
165
164
 
166
165
  ### Notes
167
166
 
168
- - Select, multi-select, and status reads return available option names and colors, plus IDs for
169
- options that match the schema. Writes select options by ID or name without editing their
170
- definitions. If you supply both, the host uses the ID. Unknown IDs fail.
171
- - Date-time writes preserve hours and minutes but discard seconds and milliseconds.
172
- - Title and rich text use text arrays that preserve only plain text. Formatting, links, mentions,
173
- and equations are not preserved as structured rich text.
174
- - `pages.get` resolves Files links and readable file, image, PDF, audio, and video references with a
175
- source. Create and update responses include only links. File writes store `external` URLs and
176
- existing `file` URLs as links. Upload references (`file_upload`) are unsupported.
167
+ - Timestamps are ISO 8601 strings in UTC.
168
+ - Actor references identify who created or last edited the page.
169
+ - Unique IDs return a formatted string, including the data source prefix when available. Missing
170
+ values return `""`.
171
+ - Formula and rollup reads return stored plain text. They do not evaluate formulas or aggregations.
172
+ Missing values return `""`.
173
+ - Button reads return the automation name when readable, or the property name as a fallback.
174
+ - Verification reads return a localized status label, such as "Verified" or "Expired". Missing or
175
+ invalid values return `""`.
176
+ - Select, multi-select, and status reads return option names and colors when available. They also
177
+ return IDs when the options match the schema.
178
+ - Select, multi-select, and status writes select existing options by ID or name. They do not create
179
+ or edit schema options. If you supply both an ID and a name, the host uses the ID. The host
180
+ rejects unknown IDs.
181
+ - Date-time writes support minute precision. Seconds and milliseconds are not preserved.
182
+ - Title and rich text use text arrays, but reads and writes preserve only plain text. Reads and
183
+ writes do not preserve formatting, links, mentions, or equations as structured rich text.
184
+ - Files reads return external links and signed URLs for Notion-hosted files.
177
185
  - Relation writes replace the value and reject `has_more: true`.
178
- - Top-level `created_time` and `last_edited_time` do not depend on the database schema.
179
- - Formula, rollup, unique ID, button, verification, and location reads keep their type but return
180
- stored text as a string. Formulas and rollups are not evaluated. Missing values return `""`.
186
+ - Top-level `created_time` and `last_edited_time` do not depend on the data source schema.
181
187
 
182
188
  For query properties, filters, and sorts, see
183
189
  [Data source property support](data-sources.md#property-support).
@@ -246,15 +252,43 @@ if (fetched.status === "success") {
246
252
  }
247
253
  ```
248
254
 
255
+ ## Files and icons
256
+
257
+ The SDK provides five types for icons and file references. Page icons, covers, and file properties
258
+ accept different combinations of these types. Import them from `@notionhq/custom-blocks`.
259
+
260
+ ### File and icon types
261
+
262
+ - `NotionIcon` — an icon from Notion's built-in catalog, identified by name with an optional color.
263
+ - `NotionEmoji` — a Unicode emoji.
264
+ - `NotionCustomEmoji` — a workspace custom emoji, identified by ID with an optional name and image
265
+ URL.
266
+ - `NotionExternalFile` — a file hosted outside Notion, referenced by URL.
267
+ - `NotionHostedFile` — a file hosted by Notion, referenced by a URL with an optional expiration
268
+ time.
269
+
270
+ ### Where these types are used
271
+
272
+ - **Page icons:** `NotionPageIcon` accepts any of the five types. The same type describes
273
+ `page.icon` and the `icon` field used to create or update a page.
274
+ - **Page covers:** `NotionPageCover` accepts `NotionExternalFile` or `NotionHostedFile`. The same
275
+ type describes `page.cover` and the `cover` field used to create or update a page.
276
+ - **Page file properties:** A property with `type: "files"` contains a `files` array of
277
+ `NotionFilePropertyItem` entries. Each entry combines `NotionExternalFile` or `NotionHostedFile`
278
+ with a required display name (`name`).
279
+
280
+ File upload references are unsupported. File references point to existing URLs.
281
+
249
282
  ### Native page icons
250
283
 
251
284
  `pages.create`, `dataSources.create`, `page.updateById`, and `page.updateByKey` accept
252
- `{ type: "icon", icon: { name: "star", color: "blue" } }`.
285
+ `{ type: "icon", icon: { name: "star", color: "blue" } }` in the `icon` field.
253
286
 
254
287
  The host resolves aliases and defaults omitted colors to gray. Native icons require a protocol 9
255
- host. `NotionIconName` accepts any string. Reads and writes share `NotionPageIcon`, including
256
- optional color and future native icon fields. `NotionIconColor` accepts any string for reads and
257
- writes and suggests known colors for autocomplete.
288
+ host. `NotionIconName` accepts any string. Reads and writes preserve optional colors and future icon
289
+ fields. `NotionIconColor` accepts any string and suggests known colors for autocomplete.
290
+
291
+ ### URL permissions
258
292
 
259
293
  External icon and cover writes require an HTTPS origin allowed by the block's network permissions.
260
294
  The host checks the submitted URL; allowlisted origins are trusted to redirect. Without network
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notionhq/custom-blocks",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -3,12 +3,11 @@ import type { BridgeDataSourceBindings } from "@notionhq/custom-blocks-protocol/
3
3
  import type { QueryDataSourceResultMessage } from "@notionhq/custom-blocks-protocol/dataSources/messages/queryDataSource.js"
4
4
  import type { InitMessage } from "@notionhq/custom-blocks-protocol/handshake/init.js"
5
5
  import type { CustomBlockInitResultErrorInfo } from "@notionhq/custom-blocks-protocol/handshake/initResult.js"
6
- import { hostToSandboxMessageSchema } from "@notionhq/custom-blocks-protocol/messages/hostToSandbox.js"
7
6
  import { readIncomingType } from "@notionhq/custom-blocks-protocol/messages/incomingType.js"
8
7
  import type { InvalidHostMessage } from "@notionhq/custom-blocks-protocol/messages/invalidHostMessage.js"
9
8
  import type { SandboxToHostMessage } from "@notionhq/custom-blocks-protocol/messages/sandboxToHost.js"
10
9
  import { readRequestId } from "@notionhq/custom-blocks-protocol/utils/readRequestId.js"
11
- import * as v from "valibot"
10
+ import type * as v from "valibot"
12
11
  import { applyContextMessage } from "../context/applyContextMessage.js"
13
12
  import type { CustomBlockHostState } from "../context/hostState.js"
14
13
  import { syncDocumentAppearance } from "../context/syncDocumentAppearance.js"
@@ -19,22 +18,13 @@ import { notifyListener } from "../utils/notifyListener.js"
19
18
  import { unreachable } from "../utils/unreachable.js"
20
19
  import { SandboxInitialization } from "./initialization/SandboxInitialization.js"
21
20
  import type { MessageInterceptor } from "./messageInterceptor.js"
21
+ import { parseHostToSandboxMessage } from "./parseHostToSandboxMessage.js"
22
22
  import {
23
23
  type RequestMessage,
24
24
  Requests,
25
25
  type ResponseSchema,
26
26
  } from "./requests.js"
27
27
 
28
- /**
29
- * A single entry in the bridge message log. Kept intentionally plain so the log is copy-pasteable
30
- * to a local coding agent without needing extra context.
31
- */
32
- type MessageLogEntry = {
33
- timestamp: string
34
- direction: "sent" | "received"
35
- data: unknown
36
- }
37
-
38
28
  export class SandboxBridge {
39
29
  private hostState: CustomBlockHostState = {
40
30
  status: "uninitialized",
@@ -48,8 +38,6 @@ export class SandboxBridge {
48
38
  (message: QueryDataSourceResultMessage) => void
49
39
  >()
50
40
  private messageInterceptors = new Set<MessageInterceptor>()
51
- private messageLog: MessageLogEntry[] = []
52
- private messageLogListeners = new Set<() => void>()
53
41
  private readonly requests = new Requests(message => this.postToHost(message))
54
42
  private latestDataSourceBindings: BridgeDataSourceBindings = {}
55
43
  private isListening = false
@@ -83,7 +71,6 @@ export class SandboxBridge {
83
71
  }
84
72
  this.listeners.clear()
85
73
  this.dataSourceQueryListeners.clear()
86
- this.messageLogListeners.clear()
87
74
  }
88
75
 
89
76
  startListening() {
@@ -94,31 +81,6 @@ export class SandboxBridge {
94
81
  window.addEventListener("message", this.handleMessage)
95
82
  }
96
83
 
97
- private static MAX_LOG_ENTRIES = 100
98
-
99
- private logMessage(direction: "sent" | "received", data: unknown) {
100
- if (this.messageLog.length >= SandboxBridge.MAX_LOG_ENTRIES) {
101
- this.messageLog.shift()
102
- }
103
- this.messageLog.push({
104
- timestamp: new Date().toISOString(),
105
- direction,
106
- data: redactExternalUrl(data),
107
- })
108
- for (const listener of this.messageLogListeners) {
109
- listener()
110
- }
111
- }
112
-
113
- getMessageLog = (): readonly MessageLogEntry[] => {
114
- return this.messageLog
115
- }
116
-
117
- subscribeToMessageLog = (listener: () => void) => {
118
- this.messageLogListeners.add(listener)
119
- return () => this.messageLogListeners.delete(listener)
120
- }
121
-
122
84
  registerMessageInterceptor(interceptor: MessageInterceptor): () => void {
123
85
  this.messageInterceptors.add(interceptor)
124
86
  return () => this.messageInterceptors.delete(interceptor)
@@ -136,7 +98,6 @@ export class SandboxBridge {
136
98
  return
137
99
  }
138
100
  }
139
- this.logMessage("sent", message)
140
101
  window.parent.postMessage(message, "*")
141
102
  }
142
103
 
@@ -151,9 +112,8 @@ export class SandboxBridge {
151
112
  if (this.isDisposed) {
152
113
  return
153
114
  }
154
- this.logMessage("received", data)
155
115
 
156
- const parsed = v.safeParse(hostToSandboxMessageSchema, data)
116
+ const parsed = parseHostToSandboxMessage(data)
157
117
  if (!parsed.success) {
158
118
  console.warn(
159
119
  "[custom-blocks-sdk] ignoring malformed host message",
@@ -356,21 +316,6 @@ function readInitializationId(data: unknown): string | undefined {
356
316
  return undefined
357
317
  }
358
318
 
359
- function redactExternalUrl(data: unknown): unknown {
360
- if (
361
- typeof data === "object" &&
362
- data !== null &&
363
- "type" in data &&
364
- data.type === "openExternalUrl"
365
- ) {
366
- return {
367
- type: "openExternalUrl",
368
- ...("requestId" in data ? { requestId: data.requestId } : {}),
369
- }
370
- }
371
- return data
372
- }
373
-
374
319
  function formatInvalidHostReason(
375
320
  incomingType: string | undefined,
376
321
  issues: readonly v.BaseIssue<unknown>[],
@@ -0,0 +1,54 @@
1
+ import { bridgeDataSourceBindingSchema } from "@notionhq/custom-blocks-protocol/dataSources/bridgeDataSourceBinding.js"
2
+ import { dataSourcePointerValidator } from "@notionhq/custom-blocks-protocol/dataSources/dataSourcePointer.js"
3
+ import { notionDataSourceSchemaValidator } from "@notionhq/custom-blocks-protocol/dataSources/dataSourceSchema.js"
4
+ import { initMessageSchema } from "@notionhq/custom-blocks-protocol/handshake/init.js"
5
+ import { hostToSandboxMessageSchema } from "@notionhq/custom-blocks-protocol/messages/hostToSandbox.js"
6
+ import { readIncomingType } from "@notionhq/custom-blocks-protocol/messages/incomingType.js"
7
+ import * as v from "valibot"
8
+
9
+ /**
10
+ * Older hosts such as 0.9.0 send `collectionPointer` and `collectionSchema` during
11
+ * initialization and live updates. Normalize these pre-protocol-11 names to
12
+ * `dataSourcePointer` and `dataSourceSchema` before exposing bindings to SDK consumers.
13
+ * Current fields take precedence when both names are present. Malformed fields and
14
+ * invalid property mappings still fail validation; this compatibility does not add
15
+ * host operations or repair bindings the Notion client cannot read from storage.
16
+ */
17
+ export function parseHostToSandboxMessage(data: unknown) {
18
+ const type = readIncomingType(data)
19
+ if (type === "init" || type === "dataSourcesChanged") {
20
+ return v.safeParse(bindingMessageSchema, data)
21
+ }
22
+ return v.safeParse(hostToSandboxMessageSchema, data)
23
+ }
24
+
25
+ const bindingSchema = v.pipe(
26
+ v.object({
27
+ ...bridgeDataSourceBindingSchema.entries,
28
+ collectionPointer: v.optional(dataSourcePointerValidator),
29
+ collectionSchema: v.optional(notionDataSourceSchemaValidator),
30
+ }),
31
+ v.transform(({ collectionPointer, collectionSchema, ...binding }) => ({
32
+ ...binding,
33
+ dataSourcePointer: binding.dataSourcePointer ?? collectionPointer,
34
+ dataSourceSchema: binding.dataSourceSchema ?? collectionSchema,
35
+ })),
36
+ )
37
+
38
+ const dataSourcesSchema = v.object({
39
+ bindings: v.record(v.string(), bindingSchema),
40
+ })
41
+
42
+ const bindingMessageSchema = v.variant("type", [
43
+ v.variant("status", [
44
+ v.object({
45
+ ...initMessageSchema.options[0].entries,
46
+ dataSources: dataSourcesSchema,
47
+ }),
48
+ initMessageSchema.options[1],
49
+ ]),
50
+ v.object({
51
+ type: v.literal("dataSourcesChanged"),
52
+ dataSources: dataSourcesSchema,
53
+ }),
54
+ ])
@@ -44,7 +44,7 @@ export function resolveDataSourceQuery(args: {
44
44
  if (dataSource.inaccessible === true) {
45
45
  return { status: "error", error: dataSourceInaccessibleError(key) }
46
46
  }
47
- if (dataSource.collectionPointer === undefined) {
47
+ if (dataSource.dataSourcePointer === undefined) {
48
48
  return {
49
49
  status: "error",
50
50
  error: {
@@ -68,7 +68,7 @@ export function resolveDataSourceQuery(args: {
68
68
  if (
69
69
  (queryOptions?.filter !== undefined ||
70
70
  (Array.isArray(queryOptions?.sorts) && queryOptions.sorts.length > 0)) &&
71
- dataSource.collectionSchema === undefined
71
+ dataSource.dataSourceSchema === undefined
72
72
  ) {
73
73
  return {
74
74
  status: "error",
@@ -119,7 +119,7 @@ export function resolveDataSourceQuery(args: {
119
119
  return {
120
120
  status: "ok",
121
121
  query: {
122
- dataSourceId: dataSource.collectionPointer.id,
122
+ dataSourceId: dataSource.dataSourcePointer.id,
123
123
  limit: limit.limit,
124
124
  filter: filter.filter,
125
125
  sorts: sorts.sorts,
@@ -17,12 +17,12 @@ export function resolveDataSources(args: {
17
17
  return Object.entries(args.manifest.dataSources).map(
18
18
  ([key, manifestDataSource]) => {
19
19
  const binding = args.dataSourceBindings[key]
20
- const collectionProperties =
21
- binding?.collectionSchema?.propertiesById ?? {}
20
+ const dataSourceProperties =
21
+ binding?.dataSourceSchema?.propertiesById ?? {}
22
22
 
23
23
  const resolvedBinding = resolveDataSourceBinding({
24
24
  manifestDataSource,
25
- collectionSchema: binding?.collectionSchema,
25
+ dataSourceSchema: binding?.dataSourceSchema,
26
26
  propertyIdsByKey: binding?.propertyIdsByKey,
27
27
  })
28
28
  const propertyIdsByKey: Record<string, string | undefined> = {}
@@ -33,10 +33,10 @@ export function resolveDataSources(args: {
33
33
 
34
34
  return {
35
35
  key,
36
- collectionPointer: binding?.collectionPointer,
37
- collectionSchema: binding?.collectionSchema,
36
+ dataSourcePointer: binding?.dataSourcePointer,
37
+ dataSourceSchema: binding?.dataSourceSchema,
38
38
  propertyIdsByKey,
39
- propertySchemasById: collectionProperties,
39
+ propertySchemasById: dataSourceProperties,
40
40
  ...(binding?.inaccessible === true && { inaccessible: true }),
41
41
  }
42
42
  },
@@ -1,6 +1,7 @@
1
1
  import type { BridgeDataSourceFilter } from "@notionhq/custom-blocks-protocol/dataSources/filters/bridgeDataSourceFilter.js"
2
2
  import type { BridgeDataSourceFilterCondition } from "@notionhq/custom-blocks-protocol/dataSources/filters/bridgeDataSourceFilterCondition.js"
3
3
  import { bridgeDataSourceFilterConditionSchema } from "@notionhq/custom-blocks-protocol/dataSources/filters/bridgeDataSourceFilterCondition.js"
4
+ import type { CustomBlockDateFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/dateFilterOperator.js"
4
5
  import { customBlockRelativeDateValueSchema } from "@notionhq/custom-blocks-protocol/dataSources/filters/dateFilterOperator.js"
5
6
  import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/notionDataSource.js"
6
7
  import * as v from "valibot"
@@ -139,9 +140,17 @@ function parsePropertyFilter(args: {
139
140
  ) {
140
141
  return invalidOperator("number")
141
142
  }
143
+ let dateOperator: CustomBlockDateFilterOperator | undefined
144
+ if ("date" in filter) {
145
+ dateOperator = filter.date
146
+ } else if ("created_time" in filter) {
147
+ dateOperator = filter.created_time
148
+ } else if ("last_edited_time" in filter) {
149
+ dateOperator = filter.last_edited_time
150
+ }
142
151
  if (
143
- "date" in filter &&
144
- !Object.entries(filter.date).every(
152
+ dateOperator !== undefined &&
153
+ !Object.entries(dateOperator).every(
145
154
  ([key, entry]) =>
146
155
  key === "target" ||
147
156
  entry === true ||
@@ -150,7 +159,7 @@ function parsePropertyFilter(args: {
150
159
  isValidIsoDate(entry),
151
160
  )
152
161
  ) {
153
- return invalidOperator("date")
162
+ return invalidOperator(branch)
154
163
  }
155
164
  return { status: "ok", filter }
156
165
  }
@@ -1,6 +1,5 @@
1
1
  import type { CustomBlockCheckboxFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/checkboxFilterOperator.js"
2
2
  import type { CustomBlockContainsFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/containsFilterOperator.js"
3
- import type { CustomBlockCreatedByFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/createdByFilterOperator.js"
4
3
  import type { CustomBlockDateFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/dateFilterOperator.js"
5
4
  import type { CustomBlockNumberFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/numberFilterOperator.js"
6
5
  import type { CustomBlockOptionFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/optionFilterOperator.js"
@@ -11,6 +10,7 @@ import type {
11
10
  import type { CustomBlockRelationFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/relationFilterOperator.js"
12
11
  import type { CustomBlockStatusFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/statusFilterOperator.js"
13
12
  import type { CustomBlockTextFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/textFilterOperator.js"
13
+ import type { CustomBlockTimestampFilterOperator } from "@notionhq/custom-blocks-protocol/dataSources/filters/timestampFilterOperator.js"
14
14
  import type { NotionSortDirection } from "@notionhq/custom-blocks-protocol/dataSources/sortDirection.js"
15
15
  import type { ExclusiveUnion } from "../../utils/typeUtils.js"
16
16
 
@@ -35,8 +35,8 @@ export type NotionDataSourceContainsFilterOperator =
35
35
  export type NotionDataSourceDateFilterOperator =
36
36
  ExclusiveUnion<CustomBlockDateFilterOperator>
37
37
 
38
- export type NotionDataSourceCreatedByFilterOperator =
39
- ExclusiveUnion<CustomBlockCreatedByFilterOperator>
38
+ export type NotionDataSourceTimestampFilterOperator =
39
+ ExclusiveUnion<CustomBlockTimestampFilterOperator>
40
40
 
41
41
  export type NotionDataSourcePersonFilterValue = CustomBlockPersonFilterValue
42
42
 
@@ -63,9 +63,12 @@ export type NotionDataSourcePropertyFilter = NotionDataSourcePropertyAddress &
63
63
  | { multi_select: NotionDataSourceContainsFilterOperator }
64
64
  | { status: NotionDataSourceStatusFilterOperator }
65
65
  | { date: NotionDataSourceDateFilterOperator }
66
+ | { created_time: NotionDataSourceTimestampFilterOperator }
67
+ | { last_edited_time: NotionDataSourceTimestampFilterOperator }
66
68
  | { people: NotionDataSourcePeopleFilterOperator }
67
69
  | { relation: NotionDataSourceRelationFilterOperator }
68
- | { created_by: NotionDataSourceCreatedByFilterOperator }
70
+ | { created_by: NotionDataSourcePeopleFilterOperator }
71
+ | { last_edited_by: NotionDataSourcePeopleFilterOperator }
69
72
  >
70
73
 
71
74
  export type NotionDataSourceSort = NotionDataSourcePropertyAddress & {
@@ -46,7 +46,7 @@ export function resolvePropertyAddress(
46
46
  }
47
47
  propertyId = value.propertyId
48
48
  }
49
- const properties = dataSource.collectionSchema?.propertiesById ?? {}
49
+ const properties = dataSource.dataSourceSchema?.propertiesById ?? {}
50
50
  if (!Object.hasOwn(properties, propertyId)) {
51
51
  return {
52
52
  status: "error",
@@ -118,7 +118,7 @@ function resolveWriteIdentifier(args: {
118
118
  : undefined
119
119
  if (
120
120
  propertyId === undefined ||
121
- !dataSource.collectionSchema?.propertiesById[propertyId]
121
+ !dataSource.dataSourceSchema?.propertiesById[propertyId]
122
122
  ) {
123
123
  return {
124
124
  status: "error",
@@ -39,7 +39,7 @@ export function resolvePageDataSource(args: {
39
39
  if (dataSource.inaccessible === true) {
40
40
  return { status: "error", error: dataSourceInaccessibleError(key) }
41
41
  }
42
- if (dataSource.collectionPointer === undefined) {
42
+ if (dataSource.dataSourcePointer === undefined) {
43
43
  return {
44
44
  status: "error",
45
45
  error: {
@@ -52,6 +52,6 @@ export function resolvePageDataSource(args: {
52
52
  return {
53
53
  status: "success",
54
54
  dataSource,
55
- dataSourceId: dataSource.collectionPointer.id,
55
+ dataSourceId: dataSource.dataSourcePointer.id,
56
56
  }
57
57
  }
@@ -9,24 +9,12 @@ import { notifyListener } from "../utils/notifyListener.js"
9
9
  import { resolveDataSourceQuery } from "./bridge/resolveDataSourceQuery.js"
10
10
  import type { SubscribeToDataSourceArgs } from "./types.js"
11
11
 
12
- export function subscribeToDataSource(
13
- args: SubscribeToDataSourceArgs,
14
- ): () => void
15
- /** @deprecated Pass onError in the first argument instead. */
16
- export function subscribeToDataSource(
17
- args: SubscribeToDataSourceArgs,
18
- onError?: (error: CustomBlockQueryDataSourceErrorInfo) => void,
19
- ): () => void
20
- export function subscribeToDataSource(
21
- {
22
- key,
23
- onSnapshot,
24
- onError: providedOnError,
25
- options: providedOptions = {},
26
- }: SubscribeToDataSourceArgs,
27
- legacyOnError?: (error: CustomBlockQueryDataSourceErrorInfo) => void,
28
- ): () => void {
29
- const onError = providedOnError ?? legacyOnError
12
+ export function subscribeToDataSource({
13
+ key,
14
+ onSnapshot,
15
+ onError,
16
+ options: providedOptions = {},
17
+ }: SubscribeToDataSourceArgs): () => void {
30
18
  const options = structuredClone(providedOptions)
31
19
  const bridge = getBridge()
32
20
  const subscriptionId = `data-source:${globalThis.crypto.randomUUID()}`
@@ -82,7 +70,7 @@ export function subscribeToDataSource(
82
70
  }
83
71
  const backingSourceChanged =
84
72
  bindingSignature !== undefined &&
85
- dataSource?.collectionPointer?.id !== nextSource?.collectionPointer?.id
73
+ dataSource?.dataSourcePointer?.id !== nextSource?.dataSourcePointer?.id
86
74
  bindingSignature = signature
87
75
  dataSource = nextSource
88
76
  if (backingSourceChanged) {
@@ -0,0 +1,5 @@
1
+ export type {
2
+ BridgeExternalFile as NotionExternalFile,
3
+ BridgeFilePropertyItem as NotionFilePropertyItem,
4
+ BridgeNotionHostedFile as NotionHostedFile,
5
+ } from "@notionhq/custom-blocks-protocol/files.js"
@@ -0,0 +1,9 @@
1
+ // Icons and emoji cross the bridge unchanged. Reusing the inferred types keeps
2
+ // SDK types aligned with validation, including support for future icon fields.
3
+ export type {
4
+ BridgeCustomEmoji as NotionCustomEmoji,
5
+ BridgeEmoji as NotionEmoji,
6
+ BridgeIcon as NotionIcon,
7
+ BridgeIconColor as NotionIconColor,
8
+ BridgeIconName as NotionIconName,
9
+ } from "@notionhq/custom-blocks-protocol/icons.js"