@notionhq/custom-blocks 0.5.2 → 0.6.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.
Files changed (555) hide show
  1. package/README.md +43 -43
  2. package/bin/notion-custom-blocks/tar.js +1 -1
  3. package/dist/bridge/SandboxBridge.d.ts +96 -8
  4. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  5. package/dist/bridge/SandboxBridge.js +310 -54
  6. package/dist/{context/applyContextMessage.d.ts → bridge/applyHostStateMessage.d.ts} +3 -3
  7. package/dist/bridge/applyHostStateMessage.d.ts.map +1 -0
  8. package/dist/{context/applyContextMessage.js → bridge/applyHostStateMessage.js} +7 -8
  9. package/dist/bridge/getInitializedHostState.d.ts.map +1 -0
  10. package/dist/bridge/getInitializedHostState.js +8 -0
  11. package/dist/bridge/hostState.d.ts +29 -0
  12. package/dist/bridge/hostState.d.ts.map +1 -0
  13. package/dist/bridge/initialization/SandboxInitialization.d.ts +43 -21
  14. package/dist/bridge/initialization/SandboxInitialization.d.ts.map +1 -1
  15. package/dist/bridge/initialization/SandboxInitialization.js +258 -84
  16. package/dist/bridge/requests.d.ts +1 -1
  17. package/dist/bridge/requests.d.ts.map +1 -1
  18. package/dist/bridge/requests.js +4 -3
  19. package/dist/bridge/subscribeToHostStateField.d.ts +11 -0
  20. package/dist/bridge/subscribeToHostStateField.d.ts.map +1 -0
  21. package/dist/{context/subscribeToContext.js → bridge/subscribeToHostStateField.js} +8 -8
  22. package/dist/data-sources.d.ts +10 -0
  23. package/dist/data-sources.d.ts.map +1 -0
  24. package/dist/data-sources.js +1 -0
  25. package/dist/dataSources/NotionDataSource.d.ts +35 -0
  26. package/dist/dataSources/NotionDataSource.d.ts.map +1 -0
  27. package/dist/dataSources/NotionDataSource.js +50 -0
  28. package/dist/dataSources/bridge/applyDataSourceBindings.d.ts +1 -1
  29. package/dist/dataSources/bridge/applyDataSourceBindings.d.ts.map +1 -1
  30. package/dist/dataSources/bridge/makeDataSourceReadResponseSchema.d.ts +2001 -0
  31. package/dist/dataSources/bridge/makeDataSourceReadResponseSchema.d.ts.map +1 -0
  32. package/dist/dataSources/bridge/makeDataSourceReadResponseSchema.js +19 -0
  33. package/dist/dataSources/bridge/resolveDataSourceQuery.d.ts +2 -2
  34. package/dist/dataSources/bridge/resolveDataSourceQuery.d.ts.map +1 -1
  35. package/dist/dataSources/bridge/resolveDataSources.d.ts +1 -1
  36. package/dist/dataSources/bridge/resolveDataSources.js +1 -1
  37. package/dist/dataSources/dataSourceState.d.ts +26 -0
  38. package/dist/dataSources/dataSourceState.d.ts.map +1 -0
  39. package/dist/dataSources/dataSourceState.js +39 -0
  40. package/dist/dataSources/ensureDataSource.d.ts +554 -0
  41. package/dist/dataSources/ensureDataSource.d.ts.map +1 -0
  42. package/dist/dataSources/ensureDataSource.js +24 -0
  43. package/dist/dataSources/getDataSourcePage.d.ts.map +1 -1
  44. package/dist/dataSources/getDataSourcePage.js +8 -3
  45. package/dist/dataSources/observeDataSource.d.ts +8 -0
  46. package/dist/dataSources/observeDataSource.d.ts.map +1 -0
  47. package/dist/dataSources/observeDataSource.js +76 -0
  48. package/dist/dataSources/properties/resolvePropertyWriteMapForDataSource.d.ts +1 -1
  49. package/dist/dataSources/properties/resolvePropertyWriteMapForDataSource.js +1 -1
  50. package/dist/dataSources/resolvePageDataSource.d.ts +1 -1
  51. package/dist/dataSources/resolvePageDataSource.d.ts.map +1 -1
  52. package/dist/dataSources/subscribeToDataSource.d.ts +1 -1
  53. package/dist/dataSources/subscribeToDataSource.d.ts.map +1 -1
  54. package/dist/dataSources/subscribeToDataSource.js +72 -48
  55. package/dist/dataSources/types.d.ts +18 -15
  56. package/dist/dataSources/types.d.ts.map +1 -1
  57. package/dist/index.d.ts +18 -33
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +10 -9
  60. package/dist/initialization/initCustomBlock.d.ts +6 -4
  61. package/dist/initialization/initCustomBlock.d.ts.map +1 -1
  62. package/dist/initialization/initCustomBlock.js +6 -4
  63. package/dist/initialization/notifyReady.d.ts +14 -0
  64. package/dist/initialization/notifyReady.d.ts.map +1 -0
  65. package/dist/initialization/notifyReady.js +9 -0
  66. package/dist/initialization/react/useCustomBlockInit.d.ts +18 -36
  67. package/dist/initialization/react/useCustomBlockInit.d.ts.map +1 -1
  68. package/dist/initialization/react/useCustomBlockInit.js +9 -22
  69. package/dist/initialization/resolveInitialState.d.ts +3 -12
  70. package/dist/initialization/resolveInitialState.d.ts.map +1 -1
  71. package/dist/initialization/resolveInitialState.js +8 -25
  72. package/dist/initialization/types.d.ts +17 -14
  73. package/dist/initialization/types.d.ts.map +1 -1
  74. package/dist/layout/autoResize.d.ts +1 -0
  75. package/dist/layout/autoResize.d.ts.map +1 -1
  76. package/dist/layout/autoResize.js +57 -37
  77. package/dist/location/getContainingPage.d.ts +4 -0
  78. package/dist/location/getContainingPage.d.ts.map +1 -0
  79. package/dist/location/getContainingPage.js +10 -0
  80. package/dist/location/getInstance.d.ts +7 -0
  81. package/dist/location/getInstance.d.ts.map +1 -0
  82. package/dist/location/getInstance.js +8 -0
  83. package/dist/location/getParent.d.ts +4 -0
  84. package/dist/location/getParent.d.ts.map +1 -0
  85. package/dist/location/getParent.js +10 -0
  86. package/dist/location/mapContainingPage.d.ts +4 -0
  87. package/dist/location/mapContainingPage.d.ts.map +1 -0
  88. package/dist/location/mapContainingPage.js +10 -0
  89. package/dist/location/mapParent.d.ts +4 -0
  90. package/dist/location/mapParent.d.ts.map +1 -0
  91. package/dist/location/mapParent.js +20 -0
  92. package/dist/location/subscribeToContainingPage.d.ts +5 -0
  93. package/dist/location/subscribeToContainingPage.d.ts.map +1 -0
  94. package/dist/location/subscribeToContainingPage.js +9 -0
  95. package/dist/location/subscribeToParent.d.ts +5 -0
  96. package/dist/location/subscribeToParent.d.ts.map +1 -0
  97. package/dist/location/subscribeToParent.js +9 -0
  98. package/dist/location/types.d.ts +45 -0
  99. package/dist/location/types.d.ts.map +1 -0
  100. package/dist/location/types.js +1 -0
  101. package/dist/manifest/getManifest.d.ts +1 -1
  102. package/dist/manifest/getManifest.js +2 -2
  103. package/dist/navigation/openExternalUrl.d.ts +1 -1
  104. package/dist/navigation/openExternalUrl.d.ts.map +1 -1
  105. package/dist/navigation/openExternalUrl.js +1 -1
  106. package/dist/navigation.d.ts +16 -0
  107. package/dist/navigation.d.ts.map +1 -0
  108. package/dist/navigation.js +21 -0
  109. package/dist/pages/NotionPage.d.ts +34 -0
  110. package/dist/pages/NotionPage.d.ts.map +1 -0
  111. package/dist/pages/NotionPage.js +105 -0
  112. package/dist/pages/bridge/getPageForDataSource.d.ts +1 -1
  113. package/dist/pages/bridge/getPageForDataSource.d.ts.map +1 -1
  114. package/dist/pages/bridge/getPageForDataSource.js +2 -2
  115. package/dist/pages/bridge/resolveCreatePageDestination.d.ts +1 -1
  116. package/dist/pages/bridge/resolveCreatePageDestination.d.ts.map +1 -1
  117. package/dist/pages/bridge/sendCreatePageRequest.d.ts.map +1 -1
  118. package/dist/pages/bridge/sendCreatePageRequest.js +3 -2
  119. package/dist/pages/bridge/toNotionPageFromBridgePage.d.ts +3 -9
  120. package/dist/pages/bridge/toNotionPageFromBridgePage.d.ts.map +1 -1
  121. package/dist/pages/bridge/toNotionPageFromBridgePage.js +36 -11
  122. package/dist/pages/bridge/updatePage.d.ts +4 -3
  123. package/dist/pages/bridge/updatePage.d.ts.map +1 -1
  124. package/dist/pages/bridge/updatePage.js +3 -3
  125. package/dist/pages/createPage.d.ts.map +1 -1
  126. package/dist/pages/createPage.js +9 -1
  127. package/dist/pages/getPage.d.ts +2 -2
  128. package/dist/pages/getPage.d.ts.map +1 -1
  129. package/dist/pages/getPage.js +1 -1
  130. package/dist/pages/identityBlockKey.d.ts +4 -0
  131. package/dist/pages/identityBlockKey.d.ts.map +1 -0
  132. package/dist/pages/identityBlockKey.js +6 -0
  133. package/dist/pages/identityPageKey.d.ts +7 -0
  134. package/dist/pages/identityPageKey.d.ts.map +1 -0
  135. package/dist/pages/identityPageKey.js +9 -0
  136. package/dist/pages/parent.d.ts +0 -2
  137. package/dist/pages/parent.d.ts.map +1 -1
  138. package/dist/pages/types.d.ts +20 -48
  139. package/dist/pages/types.d.ts.map +1 -1
  140. package/dist/pages.d.ts +56 -0
  141. package/dist/pages.d.ts.map +1 -0
  142. package/dist/pages.js +69 -0
  143. package/dist/preferences/backgroundColors.d.ts.map +1 -0
  144. package/dist/preferences/getContrastMode.d.ts.map +1 -0
  145. package/dist/{context → preferences}/getContrastMode.js +1 -1
  146. package/dist/preferences/getTheme.d.ts.map +1 -0
  147. package/dist/{context → preferences}/getTheme.js +1 -1
  148. package/dist/preferences/react/useContrastMode.d.ts.map +1 -0
  149. package/dist/preferences/react/useTheme.d.ts.map +1 -0
  150. package/dist/{context → preferences}/subscribeToContrastMode.d.ts +1 -1
  151. package/dist/preferences/subscribeToContrastMode.d.ts.map +1 -0
  152. package/dist/{context → preferences}/subscribeToContrastMode.js +2 -2
  153. package/dist/{context → preferences}/subscribeToTheme.d.ts +1 -1
  154. package/dist/preferences/subscribeToTheme.d.ts.map +1 -0
  155. package/dist/{context → preferences}/subscribeToTheme.js +2 -2
  156. package/dist/preferences/syncDocumentAppearance.d.ts +11 -0
  157. package/dist/preferences/syncDocumentAppearance.d.ts.map +1 -0
  158. package/dist/{context → preferences}/syncDocumentAppearance.js +2 -5
  159. package/dist/preferences.d.ts +7 -0
  160. package/dist/preferences.d.ts.map +1 -0
  161. package/dist/preferences.js +4 -0
  162. package/dist/protocol/context/containingPage.d.ts +2 -0
  163. package/dist/protocol/context/containingPage.js +4 -2
  164. package/dist/protocol/context/instance.d.ts +13 -0
  165. package/dist/protocol/context/instance.js +8 -0
  166. package/dist/protocol/context/messages/pageChanged.d.ts +1 -0
  167. package/dist/protocol/context/messages/pageError.d.ts +11 -0
  168. package/dist/protocol/context/messages/pageError.js +7 -0
  169. package/dist/protocol/context/messages/parentError.d.ts +11 -0
  170. package/dist/protocol/context/messages/parentError.js +7 -0
  171. package/dist/protocol/context/parent.d.ts +6 -6
  172. package/dist/protocol/dataSources/bridgeDataSourceBinding.d.ts +4 -4
  173. package/dist/protocol/dataSources/bridgeDataSourceBindings.d.ts +4 -4
  174. package/dist/protocol/dataSources/dataSourceSchema.d.ts +4 -4
  175. package/dist/protocol/dataSources/errors.d.ts +27 -3
  176. package/dist/protocol/dataSources/messages/getDataSource.d.ts +1857 -0
  177. package/dist/protocol/dataSources/messages/getDataSource.js +27 -0
  178. package/dist/protocol/dataSources/messages/queryDataSource.d.ts +37 -383
  179. package/dist/protocol/dataSources/messages/queryDataSource.js +0 -17
  180. package/dist/protocol/dataSources/notionDataSource.d.ts +8 -8
  181. package/dist/protocol/dataSources/properties/propertySchema.d.ts +15 -20
  182. package/dist/protocol/dataSources/properties/propertySchema.js +5 -10
  183. package/dist/protocol/handshake/connect.d.ts +11 -23
  184. package/dist/protocol/handshake/connect.js +12 -34
  185. package/dist/protocol/handshake/connectResult.d.ts +19 -0
  186. package/dist/protocol/handshake/connectResult.js +18 -0
  187. package/dist/protocol/handshake/init.d.ts +41 -219
  188. package/dist/protocol/handshake/init.js +11 -30
  189. package/dist/protocol/handshake/initResult.d.ts +9 -14
  190. package/dist/protocol/handshake/initResult.js +7 -10
  191. package/dist/protocol/handshake/isInitHandshakeMessage.d.ts +3 -3
  192. package/dist/protocol/handshake/isInitHandshakeMessage.js +1 -0
  193. package/dist/protocol/handshake/ready.d.ts +11 -0
  194. package/dist/protocol/handshake/ready.js +10 -0
  195. package/dist/protocol/icons.d.ts +1 -1
  196. package/dist/protocol/icons.js +1 -1
  197. package/dist/protocol/manifest/manifest.d.ts +2 -3
  198. package/dist/protocol/manifest/manifest.js +2 -3
  199. package/dist/protocol/messages/hostToSandbox.d.ts +1889 -2963
  200. package/dist/protocol/messages/hostToSandbox.js +21 -24
  201. package/dist/protocol/messages/incomingType.d.ts +2 -6
  202. package/dist/protocol/messages/incomingType.js +2 -6
  203. package/dist/protocol/messages/invalidHostMessage.d.ts +2 -0
  204. package/dist/protocol/messages/invalidHostMessage.js +2 -0
  205. package/dist/protocol/messages/invalidSandboxMessage.d.ts +2 -0
  206. package/dist/protocol/messages/invalidSandboxMessage.js +2 -0
  207. package/dist/protocol/messages/sandboxToHost.d.ts +26 -28
  208. package/dist/protocol/messages/sandboxToHost.js +13 -8
  209. package/dist/protocol/pages/bridgePage.d.ts +12 -6
  210. package/dist/protocol/pages/bridgePage.js +6 -1
  211. package/dist/protocol/pages/messages/createPage.d.ts +49 -596
  212. package/dist/protocol/pages/messages/createPage.js +0 -16
  213. package/dist/protocol/pages/messages/getPage.d.ts +12 -586
  214. package/dist/protocol/pages/messages/getPage.js +0 -15
  215. package/dist/protocol/pages/messages/updatePage.d.ts +15 -589
  216. package/dist/protocol/pages/messages/updatePage.js +1 -16
  217. package/dist/protocol/pages/properties/bridgePagePropertyMaps.d.ts +4 -4
  218. package/dist/protocol/pages/properties/bridgePagePropertyValue.d.ts +2 -2
  219. package/dist/protocol/pages/properties/metadataActor.d.ts +1 -1
  220. package/dist/protocol/pages/properties/metadataActor.js +0 -1
  221. package/dist/protocol/users/bridgeUser.d.ts +2 -1
  222. package/dist/protocol/users/bridgeUser.js +2 -1
  223. package/dist/protocol/users/bridgeUserList.d.ts +1 -1
  224. package/dist/protocol/users/makeBridgeUser.d.ts +1 -1
  225. package/dist/protocol/users/makeBridgeUser.js +2 -1
  226. package/dist/protocol/users/messages/getCurrentUser.d.ts +32 -0
  227. package/dist/protocol/users/messages/getCurrentUser.js +22 -0
  228. package/dist/protocol/users/messages/getUser.d.ts +1 -1
  229. package/dist/protocol/users/messages/getViewer.d.ts +29 -0
  230. package/dist/protocol/users/messages/getViewer.js +23 -0
  231. package/dist/protocol/users/messages/listUsers.d.ts +1 -1
  232. package/dist/protocol/users/viewer.d.ts +9 -0
  233. package/dist/protocol/users/viewer.js +10 -0
  234. package/dist/protocol/utils/errors.d.ts +33 -3
  235. package/dist/protocol/utils/ids.d.ts +3 -0
  236. package/dist/protocol/utils/ids.js +2 -0
  237. package/dist/protocol/utils/protocolVersion.d.ts +2 -5
  238. package/dist/protocol/utils/protocolVersion.js +3 -10
  239. package/dist/protocol/utils/readSubscriptionId.d.ts +2 -0
  240. package/dist/protocol/utils/readSubscriptionId.js +7 -0
  241. package/dist/react/NotionCustomBlock.d.ts +6 -26
  242. package/dist/react/NotionCustomBlock.d.ts.map +1 -1
  243. package/dist/react/NotionCustomBlock.js +15 -41
  244. package/dist/react/NotionTokenScope.js +2 -2
  245. package/dist/react/index.d.ts +12 -14
  246. package/dist/react/index.d.ts.map +1 -1
  247. package/dist/react/index.js +11 -14
  248. package/dist/react/useAsyncRead.d.ts +16 -0
  249. package/dist/react/useAsyncRead.d.ts.map +1 -0
  250. package/dist/react/useAsyncRead.js +25 -0
  251. package/dist/react/useContainingPage.d.ts +5 -0
  252. package/dist/react/useContainingPage.d.ts.map +1 -0
  253. package/dist/react/useContainingPage.js +16 -0
  254. package/dist/react/useLiveLocationRead.d.ts +14 -0
  255. package/dist/react/useLiveLocationRead.d.ts.map +1 -0
  256. package/dist/react/useLiveLocationRead.js +35 -0
  257. package/dist/react/usePage.d.ts +7 -0
  258. package/dist/react/usePage.d.ts.map +1 -0
  259. package/dist/react/usePage.js +38 -0
  260. package/dist/react/useParent.d.ts +7 -0
  261. package/dist/react/useParent.d.ts.map +1 -0
  262. package/dist/react/useParent.js +18 -0
  263. package/dist/react/useQuerySubscription.d.ts +22 -0
  264. package/dist/react/useQuerySubscription.d.ts.map +1 -0
  265. package/dist/react/useQuerySubscription.js +43 -0
  266. package/dist/react/useUserFields.d.ts +9 -0
  267. package/dist/react/useUserFields.d.ts.map +1 -0
  268. package/dist/react/useUserFields.js +14 -0
  269. package/dist/react/useViewer.d.ts +3 -0
  270. package/dist/react/useViewer.d.ts.map +1 -0
  271. package/dist/react/useViewer.js +6 -0
  272. package/dist/users/NotionUser.d.ts +21 -0
  273. package/dist/users/NotionUser.d.ts.map +1 -0
  274. package/dist/users/NotionUser.js +51 -0
  275. package/dist/users/bridge/toNotionUserDataFromBridgeUser.d.ts +4 -0
  276. package/dist/users/bridge/toNotionUserDataFromBridgeUser.d.ts.map +1 -0
  277. package/dist/users/bridge/{toNotionUserFromBridgeUser.js → toNotionUserDataFromBridgeUser.js} +2 -2
  278. package/dist/users/getUser.d.ts +1 -1
  279. package/dist/users/getUser.js +3 -3
  280. package/dist/users/getViewer.d.ts +19 -0
  281. package/dist/users/getViewer.d.ts.map +1 -0
  282. package/dist/users/getViewer.js +21 -0
  283. package/dist/users/listUsers.d.ts.map +1 -1
  284. package/dist/users/listUsers.js +15 -5
  285. package/dist/users/types.d.ts +9 -7
  286. package/dist/users/types.d.ts.map +1 -1
  287. package/dist/users.d.ts +9 -0
  288. package/dist/users.d.ts.map +1 -0
  289. package/dist/users.js +3 -0
  290. package/dist/utils/makeUuid.d.ts +2 -0
  291. package/dist/utils/makeUuid.d.ts.map +1 -0
  292. package/dist/utils/makeUuid.js +3 -0
  293. package/dist/utils/unreachable.d.ts +1 -5
  294. package/dist/utils/unreachable.d.ts.map +1 -1
  295. package/dist/utils/unreachable.js +1 -5
  296. package/dist/utils/version.js +1 -1
  297. package/docs/block-location.md +114 -134
  298. package/docs/data-sources.md +243 -380
  299. package/docs/deployment.md +8 -8
  300. package/docs/errors.md +87 -68
  301. package/docs/grants.md +18 -27
  302. package/docs/lifecycle.md +94 -188
  303. package/docs/links.md +14 -16
  304. package/docs/pages.md +136 -148
  305. package/docs/users.md +81 -126
  306. package/package.json +21 -1
  307. package/src/bridge/SandboxBridge.ts +387 -66
  308. package/src/{context/applyContextMessage.ts → bridge/applyHostStateMessage.ts} +9 -9
  309. package/src/{context → bridge}/getInitializedHostState.ts +2 -2
  310. package/src/bridge/hostState.ts +31 -0
  311. package/src/bridge/initialization/SandboxInitialization.ts +323 -117
  312. package/src/bridge/requests.ts +6 -3
  313. package/src/{context/subscribeToContext.ts → bridge/subscribeToHostStateField.ts} +11 -9
  314. package/src/data-sources.ts +24 -0
  315. package/src/dataSources/NotionDataSource.ts +114 -0
  316. package/src/dataSources/bridge/applyDataSourceBindings.ts +1 -1
  317. package/src/dataSources/bridge/makeDataSourceReadResponseSchema.ts +29 -0
  318. package/src/dataSources/bridge/resolveDataSourceQuery.ts +2 -2
  319. package/src/dataSources/bridge/resolveDataSources.ts +1 -1
  320. package/src/dataSources/dataSourceState.ts +65 -0
  321. package/src/dataSources/ensureDataSource.ts +33 -0
  322. package/src/dataSources/getDataSourcePage.ts +8 -3
  323. package/src/dataSources/observeDataSource.ts +94 -0
  324. package/src/dataSources/properties/resolvePropertyWriteMapForDataSource.ts +1 -1
  325. package/src/dataSources/resolvePageDataSource.ts +1 -1
  326. package/src/dataSources/subscribeToDataSource.ts +78 -49
  327. package/src/dataSources/types.ts +16 -16
  328. package/src/index.ts +25 -66
  329. package/src/initialization/initCustomBlock.ts +9 -5
  330. package/src/initialization/notifyReady.ts +15 -0
  331. package/src/initialization/react/useCustomBlockInit.ts +22 -37
  332. package/src/initialization/resolveInitialState.ts +13 -32
  333. package/src/initialization/types.ts +20 -14
  334. package/src/layout/autoResize.ts +57 -40
  335. package/src/location/getContainingPage.ts +14 -0
  336. package/src/location/getInstance.ts +10 -0
  337. package/src/location/getParent.ts +14 -0
  338. package/src/location/mapContainingPage.ts +13 -0
  339. package/src/location/mapParent.ts +23 -0
  340. package/src/location/subscribeToContainingPage.ts +14 -0
  341. package/src/location/subscribeToParent.ts +14 -0
  342. package/src/location/types.ts +29 -0
  343. package/src/manifest/getManifest.ts +2 -2
  344. package/src/navigation/openExternalUrl.ts +1 -1
  345. package/src/navigation.ts +45 -0
  346. package/src/pages/NotionPage.ts +144 -0
  347. package/src/pages/bridge/getPageForDataSource.ts +3 -3
  348. package/src/pages/bridge/resolveCreatePageDestination.ts +1 -1
  349. package/src/pages/bridge/sendCreatePageRequest.ts +3 -2
  350. package/src/pages/bridge/toNotionPageFromBridgePage.ts +45 -19
  351. package/src/pages/bridge/updatePage.ts +6 -6
  352. package/src/pages/createPage.ts +14 -1
  353. package/src/pages/getPage.ts +2 -2
  354. package/src/pages/identityBlockKey.ts +10 -0
  355. package/src/pages/identityPageKey.ts +13 -0
  356. package/src/pages/parent.ts +0 -10
  357. package/src/pages/types.ts +23 -42
  358. package/src/pages.ts +144 -0
  359. package/src/{context → preferences}/getContrastMode.ts +1 -1
  360. package/src/{context → preferences}/getTheme.ts +1 -1
  361. package/src/{context → preferences}/subscribeToContrastMode.ts +3 -3
  362. package/src/{context → preferences}/subscribeToTheme.ts +3 -3
  363. package/src/{context → preferences}/syncDocumentAppearance.ts +2 -5
  364. package/src/preferences.ts +6 -0
  365. package/src/react/NotionCustomBlock.tsx +21 -52
  366. package/src/react/NotionTokenScope.tsx +2 -2
  367. package/src/react/index.ts +20 -14
  368. package/src/react/useAsyncRead.ts +44 -0
  369. package/src/react/useContainingPage.ts +20 -0
  370. package/src/react/useLiveLocationRead.ts +53 -0
  371. package/src/react/usePage.ts +61 -0
  372. package/src/react/useParent.ts +22 -0
  373. package/src/react/useQuerySubscription.ts +71 -0
  374. package/src/react/useUserFields.ts +24 -0
  375. package/src/react/useViewer.ts +7 -0
  376. package/src/users/NotionUser.ts +70 -0
  377. package/src/users/bridge/{toNotionUserFromBridgeUser.ts → toNotionUserDataFromBridgeUser.ts} +6 -3
  378. package/src/users/getUser.ts +3 -3
  379. package/src/users/getViewer.ts +36 -0
  380. package/src/users/listUsers.ts +15 -5
  381. package/src/users/types.ts +9 -7
  382. package/src/users.ts +20 -0
  383. package/src/utils/makeUuid.ts +3 -0
  384. package/src/utils/unreachable.ts +1 -6
  385. package/dist/bridge/parseHostToSandboxMessage.d.ts +0 -3678
  386. package/dist/bridge/parseHostToSandboxMessage.d.ts.map +0 -1
  387. package/dist/bridge/parseHostToSandboxMessage.js +0 -47
  388. package/dist/context/applyContextMessage.d.ts.map +0 -1
  389. package/dist/context/backgroundColors.d.ts.map +0 -1
  390. package/dist/context/getBlockId.d.ts +0 -7
  391. package/dist/context/getBlockId.d.ts.map +0 -1
  392. package/dist/context/getBlockId.js +0 -8
  393. package/dist/context/getContrastMode.d.ts.map +0 -1
  394. package/dist/context/getInitializedHostState.d.ts.map +0 -1
  395. package/dist/context/getInitializedHostState.js +0 -8
  396. package/dist/context/getPage.d.ts +0 -7
  397. package/dist/context/getPage.d.ts.map +0 -1
  398. package/dist/context/getPage.js +0 -8
  399. package/dist/context/getParent.d.ts +0 -7
  400. package/dist/context/getParent.d.ts.map +0 -1
  401. package/dist/context/getParent.js +0 -8
  402. package/dist/context/getTheme.d.ts.map +0 -1
  403. package/dist/context/hostState.d.ts +0 -15
  404. package/dist/context/hostState.d.ts.map +0 -1
  405. package/dist/context/index.d.ts +0 -42
  406. package/dist/context/index.d.ts.map +0 -1
  407. package/dist/context/index.js +0 -41
  408. package/dist/context/react/useBlockId.d.ts +0 -11
  409. package/dist/context/react/useBlockId.d.ts.map +0 -1
  410. package/dist/context/react/useBlockId.js +0 -14
  411. package/dist/context/react/useContrastMode.d.ts.map +0 -1
  412. package/dist/context/react/usePage.d.ts +0 -12
  413. package/dist/context/react/usePage.d.ts.map +0 -1
  414. package/dist/context/react/usePage.js +0 -15
  415. package/dist/context/react/useParent.d.ts +0 -12
  416. package/dist/context/react/useParent.d.ts.map +0 -1
  417. package/dist/context/react/useParent.js +0 -15
  418. package/dist/context/react/useTheme.d.ts.map +0 -1
  419. package/dist/context/subscribeToBlockId.d.ts +0 -8
  420. package/dist/context/subscribeToBlockId.d.ts.map +0 -1
  421. package/dist/context/subscribeToBlockId.js +0 -8
  422. package/dist/context/subscribeToContext.d.ts +0 -12
  423. package/dist/context/subscribeToContext.d.ts.map +0 -1
  424. package/dist/context/subscribeToContrastMode.d.ts.map +0 -1
  425. package/dist/context/subscribeToPage.d.ts +0 -8
  426. package/dist/context/subscribeToPage.d.ts.map +0 -1
  427. package/dist/context/subscribeToPage.js +0 -8
  428. package/dist/context/subscribeToParent.d.ts +0 -8
  429. package/dist/context/subscribeToParent.d.ts.map +0 -1
  430. package/dist/context/subscribeToParent.js +0 -8
  431. package/dist/context/subscribeToTheme.d.ts.map +0 -1
  432. package/dist/context/syncDocumentAppearance.d.ts +0 -14
  433. package/dist/context/syncDocumentAppearance.d.ts.map +0 -1
  434. package/dist/dataSources/createDataSourcePage.d.ts +0 -4
  435. package/dist/dataSources/createDataSourcePage.d.ts.map +0 -1
  436. package/dist/dataSources/createDataSourcePage.js +0 -44
  437. package/dist/dataSources/index.d.ts +0 -9
  438. package/dist/dataSources/index.d.ts.map +0 -1
  439. package/dist/dataSources/index.js +0 -8
  440. package/dist/dataSources/queryDataSource.d.ts +0 -3
  441. package/dist/dataSources/queryDataSource.d.ts.map +0 -1
  442. package/dist/dataSources/queryDataSource.js +0 -56
  443. package/dist/dataSources/react/useDataSource.d.ts +0 -13
  444. package/dist/dataSources/react/useDataSource.d.ts.map +0 -1
  445. package/dist/dataSources/react/useDataSource.js +0 -58
  446. package/dist/initialization/getInitBindingError.d.ts +0 -5
  447. package/dist/initialization/getInitBindingError.d.ts.map +0 -1
  448. package/dist/initialization/getInitBindingError.js +0 -26
  449. package/dist/layout/postResize.d.ts +0 -2
  450. package/dist/layout/postResize.d.ts.map +0 -1
  451. package/dist/layout/postResize.js +0 -12
  452. package/dist/manifest/react/useManifest.d.ts +0 -15
  453. package/dist/manifest/react/useManifest.d.ts.map +0 -1
  454. package/dist/manifest/react/useManifest.js +0 -18
  455. package/dist/manifest/subscribeToManifest.d.ts +0 -8
  456. package/dist/manifest/subscribeToManifest.d.ts.map +0 -1
  457. package/dist/manifest/subscribeToManifest.js +0 -8
  458. package/dist/navigation/index.d.ts +0 -5
  459. package/dist/navigation/index.d.ts.map +0 -1
  460. package/dist/navigation/index.js +0 -4
  461. package/dist/pages/bridge/makeUpdatePageByKey.d.ts +0 -8
  462. package/dist/pages/bridge/makeUpdatePageByKey.d.ts.map +0 -1
  463. package/dist/pages/bridge/makeUpdatePageByKey.js +0 -17
  464. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.d.ts +0 -89
  465. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.d.ts.map +0 -1
  466. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.js +0 -24
  467. package/dist/pages/bridge/updateNotionPageFromBridgePage.d.ts +0 -4
  468. package/dist/pages/bridge/updateNotionPageFromBridgePage.d.ts.map +0 -1
  469. package/dist/pages/bridge/updateNotionPageFromBridgePage.js +0 -4
  470. package/dist/pages/index.d.ts +0 -9
  471. package/dist/pages/index.d.ts.map +0 -1
  472. package/dist/pages/index.js +0 -8
  473. package/dist/pages/makeNotionPage.d.ts +0 -11
  474. package/dist/pages/makeNotionPage.d.ts.map +0 -1
  475. package/dist/pages/makeNotionPage.js +0 -34
  476. package/dist/pages/properties/getPropertyById.d.ts +0 -6
  477. package/dist/pages/properties/getPropertyById.d.ts.map +0 -1
  478. package/dist/pages/properties/getPropertyById.js +0 -3
  479. package/dist/pages/properties/getPropertyByKey.d.ts +0 -9
  480. package/dist/pages/properties/getPropertyByKey.d.ts.map +0 -1
  481. package/dist/pages/properties/getPropertyByKey.js +0 -16
  482. package/dist/protocol/compatibility/protocol3To10/dataSourceBindings.d.ts +0 -530
  483. package/dist/protocol/compatibility/protocol3To10/dataSourceBindings.js +0 -25
  484. package/dist/protocol/compatibility/protocol3To5/bridgePage.d.ts +0 -282
  485. package/dist/protocol/compatibility/protocol3To5/bridgePage.js +0 -21
  486. package/dist/protocol/compatibility/protocol3To5/dataSourcePage.d.ts +0 -73
  487. package/dist/protocol/compatibility/protocol3To5/dataSourcePage.js +0 -14
  488. package/dist/protocol/compatibility/protocol3To5/dataSourceValue.d.ts +0 -66
  489. package/dist/protocol/compatibility/protocol3To5/dataSourceValue.js +0 -14
  490. package/dist/protocol/dataSources/messages/dataSourcesChanged.d.ts +0 -150
  491. package/dist/protocol/dataSources/messages/dataSourcesChanged.js +0 -13
  492. package/dist/protocol/index.d.ts +0 -158
  493. package/dist/protocol/index.js +0 -80
  494. package/dist/protocol/users/messages/currentUserChanged.d.ts +0 -18
  495. package/dist/protocol/users/messages/currentUserChanged.js +0 -9
  496. package/dist/users/bridge/toNotionUserFromBridgeUser.d.ts +0 -4
  497. package/dist/users/bridge/toNotionUserFromBridgeUser.d.ts.map +0 -1
  498. package/dist/users/getCurrentUser.d.ts +0 -7
  499. package/dist/users/getCurrentUser.d.ts.map +0 -1
  500. package/dist/users/getCurrentUser.js +0 -8
  501. package/dist/users/index.d.ts +0 -7
  502. package/dist/users/index.d.ts.map +0 -1
  503. package/dist/users/index.js +0 -6
  504. package/dist/users/react/useCurrentUser.d.ts +0 -12
  505. package/dist/users/react/useCurrentUser.d.ts.map +0 -1
  506. package/dist/users/react/useCurrentUser.js +0 -15
  507. package/dist/users/subscribeToCurrentUser.d.ts +0 -8
  508. package/dist/users/subscribeToCurrentUser.d.ts.map +0 -1
  509. package/dist/users/subscribeToCurrentUser.js +0 -8
  510. package/src/bridge/parseHostToSandboxMessage.ts +0 -54
  511. package/src/context/getBlockId.ts +0 -10
  512. package/src/context/getPage.ts +0 -10
  513. package/src/context/getParent.ts +0 -10
  514. package/src/context/hostState.ts +0 -17
  515. package/src/context/index.ts +0 -42
  516. package/src/context/react/useBlockId.ts +0 -16
  517. package/src/context/react/usePage.ts +0 -17
  518. package/src/context/react/useParent.ts +0 -17
  519. package/src/context/subscribeToBlockId.ts +0 -13
  520. package/src/context/subscribeToPage.ts +0 -13
  521. package/src/context/subscribeToParent.ts +0 -13
  522. package/src/dataSources/createDataSourcePage.ts +0 -50
  523. package/src/dataSources/index.ts +0 -9
  524. package/src/dataSources/queryDataSource.ts +0 -62
  525. package/src/dataSources/react/useDataSource.ts +0 -71
  526. package/src/initialization/getInitBindingError.ts +0 -39
  527. package/src/layout/postResize.ts +0 -15
  528. package/src/manifest/react/useManifest.ts +0 -20
  529. package/src/manifest/subscribeToManifest.ts +0 -13
  530. package/src/navigation/index.ts +0 -5
  531. package/src/pages/bridge/makeUpdatePageByKey.ts +0 -25
  532. package/src/pages/bridge/toNotionPageFieldsFromBridgePage.ts +0 -27
  533. package/src/pages/bridge/updateNotionPageFromBridgePage.ts +0 -10
  534. package/src/pages/index.ts +0 -9
  535. package/src/pages/makeNotionPage.ts +0 -55
  536. package/src/pages/properties/getPropertyById.ts +0 -12
  537. package/src/pages/properties/getPropertyByKey.ts +0 -30
  538. package/src/styles/nds.css.metadata.json +0 -6
  539. package/src/users/getCurrentUser.ts +0 -10
  540. package/src/users/index.ts +0 -7
  541. package/src/users/react/useCurrentUser.ts +0 -17
  542. package/src/users/subscribeToCurrentUser.ts +0 -13
  543. /package/dist/{context → bridge}/getInitializedHostState.d.ts +0 -0
  544. /package/dist/{context → bridge}/hostState.js +0 -0
  545. /package/dist/{context → preferences}/backgroundColors.d.ts +0 -0
  546. /package/dist/{context → preferences}/backgroundColors.js +0 -0
  547. /package/dist/{context → preferences}/getContrastMode.d.ts +0 -0
  548. /package/dist/{context → preferences}/getTheme.d.ts +0 -0
  549. /package/dist/{context → preferences}/react/useContrastMode.d.ts +0 -0
  550. /package/dist/{context → preferences}/react/useContrastMode.js +0 -0
  551. /package/dist/{context → preferences}/react/useTheme.d.ts +0 -0
  552. /package/dist/{context → preferences}/react/useTheme.js +0 -0
  553. /package/src/{context → preferences}/backgroundColors.ts +0 -0
  554. /package/src/{context → preferences}/react/useContrastMode.ts +0 -0
  555. /package/src/{context → preferences}/react/useTheme.ts +0 -0
@@ -0,0 +1,31 @@
1
+ import type { CustomBlockPage } from "@notionhq/custom-blocks-protocol/context/containingPage.js"
2
+ import type { NotionContrastMode } from "@notionhq/custom-blocks-protocol/context/contrast.js"
3
+ import type { CustomBlockInstance } from "@notionhq/custom-blocks-protocol/context/instance.js"
4
+ import type { NotionParent } from "@notionhq/custom-blocks-protocol/context/parent.js"
5
+ import type { NotionTheme } from "@notionhq/custom-blocks-protocol/context/theme.js"
6
+ import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/notionDataSource.js"
7
+ import type { BridgeCustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest/manifest.js"
8
+ import type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/utils/errors.js"
9
+
10
+ export type CustomBlockHostState = UninitializedHostState | InitializedHostState
11
+
12
+ export type UninitializedHostState = {
13
+ status: "uninitialized"
14
+ theme: NotionTheme
15
+ contrastMode: NotionContrastMode
16
+ }
17
+
18
+ export type InitializedHostState = {
19
+ /** The version selected in the successful `connectResult`, retained for this connection. */
20
+ protocolVersion: number
21
+ instance: CustomBlockInstance
22
+ theme: NotionTheme
23
+ contrastMode: NotionContrastMode
24
+ parent?: NotionParent
25
+ page?: CustomBlockPage
26
+ parentError?: CustomBlockErrorInfo
27
+ pageError?: CustomBlockErrorInfo
28
+ dataSources: NotionDataSource[]
29
+ status: "initialized"
30
+ manifest: BridgeCustomBlockManifest
31
+ }
@@ -1,63 +1,89 @@
1
- import type { ConnectMessage } from "@notionhq/custom-blocks-protocol/handshake/connect.js"
2
- import type { InitMessage } from "@notionhq/custom-blocks-protocol/handshake/init.js"
1
+ import type { ConnectRequestMessage } from "@notionhq/custom-blocks-protocol/handshake/connect.js"
2
+ import type { ConnectResultMessage } from "@notionhq/custom-blocks-protocol/handshake/connectResult.js"
3
+ import type { SetupInitMessage } from "@notionhq/custom-blocks-protocol/handshake/init.js"
3
4
  import { CustomBlockInitializationError } from "@notionhq/custom-blocks-protocol/handshake/init.js"
4
5
  import type {
5
6
  CustomBlockInitResultErrorInfo,
6
- InitResultMessage,
7
+ InitResultRequestMessage,
7
8
  } from "@notionhq/custom-blocks-protocol/handshake/initResult.js"
9
+ import type { ReadyMessage } from "@notionhq/custom-blocks-protocol/handshake/ready.js"
8
10
  import type { SandboxToHostMessage } from "@notionhq/custom-blocks-protocol/messages/sandboxToHost.js"
9
- import { LATEST_BRIDGE_PROTOCOL_VERSION } from "@notionhq/custom-blocks-protocol/utils/protocolVersion.js"
10
- import type { CustomBlockHostState } from "../../context/hostState.js"
11
+ import type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/utils/errors.js"
11
12
  import { NotInIframeError } from "../../initialization/NotInIframeError.js"
12
- import type { CustomBlockInitPayload } from "../../initialization/types.js"
13
+ import type { CustomBlockInitData } from "../../initialization/types.js"
13
14
  import { readLocalManifest } from "../../manifest/readLocalManifest.js"
15
+ import { makeUuid } from "../../utils/makeUuid.js"
14
16
  import { CUSTOM_BLOCKS_SDK_VERSION } from "../../utils/version.js"
17
+ import type { CustomBlockHostState } from "../hostState.js"
18
+ import { DEFAULT_REQUEST_TIMEOUT_MS } from "../requests.js"
15
19
 
16
- const INITIALIZATION_ID = "custom-block-initialization-1"
20
+ const SUPPORTED_PROTOCOL_VERSION = 12
17
21
 
18
- /** Tracks the startup handshake and when outbound messages can be sent. */
22
+ /** Startup handshake states, including setup acknowledgment and render readiness. */
19
23
  type SandboxInitializationPhase =
20
- /** No connect message has been sent. */
24
+ /** Startup has not been requested and no `connect` has been sent. */
25
+
21
26
  | "idle"
22
- /** Startup is stopped, acknowledgment callbacks are cancelled, and outbound messages are dropped. */
27
+
28
+ /** Startup has stopped permanently. Queued messages are discarded and pending initialization rejects. */
23
29
  | "disposed"
24
- /** Connect has been sent; the SDK is waiting for a matching init response. */
25
- | "waiting"
26
- /** A matching init is being processed. Outbound messages from subscribers are queued. */
30
+
31
+ /** `connect` has been sent. The SDK is waiting for the matching `connectResult`. */
32
+ | "connecting"
33
+
34
+ /** The host selected a supported protocol. The SDK is waiting for the host's `init` request. */
35
+ | "waitingForInit"
36
+
37
+ /** The SDK is applying `init`. Subscriber messages are queued until `initResult` is sent. */
27
38
  | "applying"
28
- /** Host state is applied and startup is resolved. Outbound messages wait for acknowledgment after the initial render. */
29
- | "rendering"
30
- /** Successful initialization is acknowledged. Outbound messages can be sent immediately. */
39
+
40
+ /** Setup is acknowledged and data operations can run. Render readiness has not been reported. */
41
+ | "setupComplete"
42
+
43
+ /** The SDK sent `ready` with the initial height. Repeated readiness calls return success without sending again. */
31
44
  | "ready"
32
- /** A matching init was rejected by the host or SDK. Further init responses are ignored. */
45
+
46
+ /** Startup failed. Further startup responses are ignored. */
33
47
  | "failed"
34
48
 
35
- /** Bridge callbacks used by the initialization controller. */
49
+ /** Connects startup sequencing to the bridge's transport, state, and height measurement. */
36
50
  export type InitializationOptions = {
37
- /** Starts listening for host messages on the first call to initialize, before connect is sent. */
51
+ /** Starts listening for host messages on the first initialization call, before `connect` is sent. */
38
52
  start: () => void
39
- /** Sends a message immediately, bypassing the controller's outbound queue. */
53
+
54
+ /** Sends directly to the host, bypassing the outbound queue. */
40
55
  post: (message: SandboxToHostMessage) => void
56
+
41
57
  /**
42
- * Applies a validated, matching init response and notifies state subscribers.
43
- * Returns an error if the host context cannot be accepted, or undefined on success.
58
+ * Applies validated setup with the version selected by `connectResult` and notifies state subscribers.
59
+ * Returns an error if setup cannot be accepted, or `undefined` on success.
44
60
  */
45
61
  apply: (
46
- message: Extract<InitMessage, { status: "success" }>,
62
+ message: SetupInitMessage,
63
+ protocolVersion: number,
47
64
  ) => CustomBlockInitResultErrorInfo | undefined
48
- /** Reads the current host state to build the context returned after initialization. */
65
+
66
+ /** Reads the applied host state to build the result returned by initialization. */
49
67
  getState: () => CustomBlockHostState
50
- /** Measures the initial content height in pixels when sending initResult.success. */
68
+
69
+ /** Measures the initial content height in CSS pixels for `ready`. */
70
+
51
71
  getInitialHeight: () => number
52
72
  }
53
73
 
54
- /** Owns one document's startup, acknowledgement, and messages queued during rendering. */
74
+ /**
75
+ * Coordinates explicit startup, setup acknowledgment, and render readiness for one document.
76
+ * Queues outbound messages while setup is being applied so they follow `initResult`.
77
+ */
55
78
  export class SandboxInitialization {
56
79
  private phase: SandboxInitializationPhase = "idle"
57
80
  private pendingMessages: SandboxToHostMessage[] = []
58
- private initPromise: Promise<CustomBlockInitPayload> | undefined
59
- private frameId: number | undefined
60
- private fallbackId: ReturnType<typeof setTimeout> | undefined
81
+ private initPromise: Promise<CustomBlockInitData> | undefined
82
+ private connectRequestId: string | undefined
83
+ private negotiatedProtocolVersion: number | undefined
84
+ private initRequestId: string | undefined
85
+ private earlyInit: SetupInitMessage | undefined
86
+ private hostResponseTimeout: ReturnType<typeof setTimeout> | undefined
61
87
  private resolveInit: (() => void) | undefined
62
88
  private rejectInit: ((error: Error) => void) | undefined
63
89
  private readonly initialized = new Promise<void>((resolve, reject) => {
@@ -66,19 +92,48 @@ export class SandboxInitialization {
66
92
  })
67
93
 
68
94
  constructor(private readonly options: InitializationOptions) {
69
- // A host can report an error before a caller starts awaiting initialization.
70
95
  void this.initialized.catch(() => {})
71
96
  }
72
97
 
73
- initialize(): Promise<CustomBlockInitPayload> {
98
+ initialize(onSetupComplete?: () => void): Promise<CustomBlockInitData> {
74
99
  if (this.initPromise === undefined) {
75
100
  this.options.start()
76
- this.initPromise = this.run()
101
+ this.initPromise = this.run(onSetupComplete)
77
102
  }
78
103
  return this.initPromise
79
104
  }
80
105
 
81
- private async run(): Promise<CustomBlockInitPayload> {
106
+ /**
107
+ * Waits for the requested startup attempt. Returns `not_initialized` immediately
108
+ * if startup has not been requested; this method does not start the handshake.
109
+ */
110
+ async waitForInitialization(): Promise<CustomBlockErrorInfo | undefined> {
111
+ if (this.initPromise === undefined) {
112
+ return {
113
+ code: "not_initialized",
114
+ message:
115
+ "Call initCustomBlock() or mount NotionCustomBlock before requesting data.",
116
+ isRetryable: false,
117
+ }
118
+ }
119
+ try {
120
+ await this.initPromise
121
+ return undefined
122
+ } catch (error) {
123
+ if (!(error instanceof CustomBlockInitializationError)) {
124
+ throw error
125
+ }
126
+ return {
127
+ code: error.code,
128
+ message: error.message,
129
+ isRetryable: error.isRetryable,
130
+ }
131
+ }
132
+ }
133
+
134
+ private async run(
135
+ onSetupComplete: (() => void) | undefined,
136
+ ): Promise<CustomBlockInitData> {
82
137
  try {
83
138
  if (typeof window === "undefined" || window.parent === window) {
84
139
  throw new NotInIframeError()
@@ -88,19 +143,20 @@ export class SandboxInitialization {
88
143
  const state = this.options.getState()
89
144
  if (state.status !== "initialized") {
90
145
  throw new CustomBlockInitializationError({
91
- code: "context_unavailable",
92
- message: "Host block payload is unavailable.",
146
+ code: "initialization_state_invalid",
147
+ message:
148
+ "The SDK acknowledged setup, but its stored runtime state is not initialized.",
93
149
  isRetryable: true,
94
150
  })
95
151
  }
152
+ onSetupComplete?.()
96
153
  return {
97
- theme: state.theme,
98
- contrastMode: state.contrastMode,
99
- blockId: state.blockId,
100
- parent: state.parent,
101
- page: state.page,
102
- currentUser: state.currentUser,
103
- dataSources: state.dataSources,
154
+ instance: state.instance,
155
+ manifest: state.manifest,
156
+ preferences: {
157
+ theme: state.theme,
158
+ contrastMode: state.contrastMode,
159
+ },
104
160
  }
105
161
  } catch (error) {
106
162
  if (error instanceof CustomBlockInitializationError) {
@@ -123,112 +179,239 @@ export class SandboxInitialization {
123
179
  document: window.document,
124
180
  url: new URL(window.location.href),
125
181
  })
126
- this.phase = "waiting"
127
- const identity = {
128
- type: "connect" as const,
129
- initializationId: INITIALIZATION_ID,
130
- bridgeProtocolVersion: LATEST_BRIDGE_PROTOCOL_VERSION,
182
+ if (result.status === "error") {
183
+ this.phase = "failed"
184
+ this.reject(new CustomBlockInitializationError(result.error))
185
+ return
186
+ }
187
+ const message: ConnectRequestMessage = {
188
+ type: "connect",
189
+ requestId: makeUuid(),
190
+ supportedProtocolVersions: [SUPPORTED_PROTOCOL_VERSION],
191
+
131
192
  sdkVersion: CUSTOM_BLOCKS_SDK_VERSION,
193
+ ...(result.manifest !== undefined ? { manifest: result.manifest } : {}),
194
+ }
195
+ this.connectRequestId = message.requestId
196
+ this.phase = "connecting"
197
+ this.waitForHostResponse()
198
+ try {
199
+ this.options.post(message)
200
+ } catch {
201
+ this.fail({
202
+ code: "request_send_failed",
203
+ message: "The SDK could not send the connect request to the host.",
204
+ isRetryable: false,
205
+ })
132
206
  }
133
- const message: ConnectMessage =
134
- result.status === "error"
135
- ? { ...identity, status: "error", error: result.error }
136
- : {
137
- ...identity,
138
- status: "success",
139
- ...(result.manifest !== undefined
140
- ? { manifest: result.manifest }
141
- : {}),
142
- }
143
- this.options.post(message)
144
207
  }
145
208
 
146
- handleInit(message: InitMessage) {
209
+ /** Validates the version selected by the host in the matching `connectResult` before accepting `init`. */
210
+ handleConnectResult(message: ConnectResultMessage) {
147
211
  if (
148
- this.phase === "idle" ||
149
- this.phase === "disposed" ||
150
- message.initializationId !== INITIALIZATION_ID
212
+ this.phase !== "connecting" ||
213
+ message.requestId !== this.connectRequestId
151
214
  ) {
152
215
  console.warn(
153
- `[custom-blocks-sdk] ignoring init for unknown initializationId ${message.initializationId}`,
216
+ `[custom-blocks-sdk] ignoring connectResult for unknown requestId ${message.requestId}`,
154
217
  )
155
218
  return
156
219
  }
157
- if (this.phase !== "waiting") {
158
- console.warn("[custom-blocks-sdk] ignoring duplicate init message")
159
- return
160
- }
161
- this.phase = "applying"
220
+ this.clearHostResponseTimeout()
162
221
  if (message.status === "error") {
163
- console.error(
164
- `[custom-blocks-sdk] host reported init error (${message.error.code}): ${message.error.message}`,
165
- )
166
- this.phase = "failed"
167
- this.reject(new CustomBlockInitializationError(message.error))
222
+ this.fail(message.error)
168
223
  return
169
224
  }
170
- const error = this.options.apply(message)
171
- if (error !== undefined) {
172
- this.fail(error)
225
+ if (message.protocolVersion !== SUPPORTED_PROTOCOL_VERSION) {
226
+ this.fail({
227
+ code: "unsupported_protocol_version",
228
+ message: `This SDK requires host protocol ${SUPPORTED_PROTOCOL_VERSION}; the host selected protocol ${message.protocolVersion}.`,
229
+
230
+ isRetryable: false,
231
+ })
173
232
  return
174
233
  }
175
- // Subscribers can complete or dispose initialization while apply notifies them.
176
- if (this.phase !== "applying") {
234
+ this.negotiatedProtocolVersion = message.protocolVersion
235
+ this.phase = "waitingForInit"
236
+ this.waitForHostResponse()
237
+ const earlyInit = this.earlyInit
238
+ this.earlyInit = undefined
239
+ if (earlyInit !== undefined) {
240
+ this.applyInit(earlyInit)
241
+ }
242
+ }
243
+
244
+ handleInit(message: SetupInitMessage) {
245
+ if (this.phase === "connecting") {
246
+ // Buffer a host that delivers `init` before its `connectResult` event is observed.
247
+ if (this.earlyInit === undefined) {
248
+ this.earlyInit = message
249
+ }
177
250
  return
178
251
  }
179
- this.phase = "rendering"
180
- this.resolve()
181
- this.fallbackId = setTimeout(() => this.complete(), 100)
182
- if (typeof requestAnimationFrame === "function") {
183
- this.frameId = requestAnimationFrame(() => this.complete())
252
+ if (this.phase !== "waitingForInit") {
253
+ console.warn("[custom-blocks-sdk] ignoring init outside startup")
254
+ return
184
255
  }
256
+ this.applyInit(message)
185
257
  }
186
258
 
187
259
  handleInvalidInit(
188
- id: string | undefined,
260
+ requestId: string | undefined,
189
261
  error: CustomBlockInitResultErrorInfo,
190
262
  ): boolean {
191
- if (this.phase !== "waiting" || INITIALIZATION_ID !== id) {
263
+ if (this.phase !== "connecting" && this.phase !== "waitingForInit") {
192
264
  return false
193
265
  }
266
+ if (requestId === undefined) {
267
+ this.fail({
268
+ code: "unsupported_protocol_version",
269
+ message:
270
+ "The host did not send a protocol 12 init request. Update the Notion host to use this SDK.",
271
+ isRetryable: false,
272
+ })
273
+ return true
274
+ }
275
+ this.initRequestId = requestId
194
276
  this.fail(error)
195
277
  return true
196
278
  }
197
279
 
280
+ handleInvalidConnectResult(requestId: string | undefined) {
281
+ if (this.phase !== "connecting" || requestId !== this.connectRequestId) {
282
+ return false
283
+ }
284
+ this.fail({
285
+ code: "invalid_host_message",
286
+ message: "The host returned an invalid connect result.",
287
+ isRetryable: false,
288
+ })
289
+ return true
290
+ }
291
+
292
+ private applyInit(message: SetupInitMessage) {
293
+ const protocolVersion = this.negotiatedProtocolVersion
294
+ if (this.phase !== "waitingForInit" || protocolVersion === undefined) {
295
+ return
296
+ }
297
+ this.clearHostResponseTimeout()
298
+ this.phase = "applying"
299
+ this.initRequestId = message.requestId
300
+ const error = this.options.apply(message, protocolVersion)
301
+
302
+ if (error !== undefined) {
303
+ this.fail(error)
304
+ return
305
+ }
306
+ if (this.phase !== "applying") {
307
+ return
308
+ }
309
+ const response: InitResultRequestMessage = {
310
+ type: "initResult",
311
+ requestId: message.requestId,
312
+ status: "success",
313
+ }
314
+ try {
315
+ this.options.post(response)
316
+ } catch {
317
+ this.fail({
318
+ code: "request_send_failed",
319
+ message: "The SDK could not acknowledge the host init request.",
320
+ isRetryable: false,
321
+ })
322
+ return
323
+ }
324
+ this.phase = "setupComplete"
325
+ this.initRequestId = undefined
326
+ this.resolve()
327
+ this.flushPendingMessages()
328
+ }
329
+
198
330
  post(message: SandboxToHostMessage) {
199
331
  if (this.phase === "disposed") {
200
332
  return
201
333
  }
202
- if (this.phase === "rendering" || this.phase === "applying") {
334
+ if (this.phase === "applying") {
203
335
  this.pendingMessages.push(message)
204
336
  } else {
205
337
  this.options.post(message)
206
338
  }
207
339
  }
208
340
 
209
- complete() {
210
- if (this.phase !== "rendering") {
211
- return
341
+ notifyReady():
342
+ | { status: "success" }
343
+ | { status: "error"; error: CustomBlockErrorInfo } {
344
+ if (this.phase === "ready") {
345
+ return { status: "success" }
212
346
  }
213
- const message: InitResultMessage = {
214
- type: "initResult",
215
- initializationId: INITIALIZATION_ID,
216
- status: "success",
217
- initialHeight: this.options.getInitialHeight(),
347
+ if (this.phase !== "setupComplete") {
348
+ return {
349
+ status: "error",
350
+ error: {
351
+ code: "not_initialized",
352
+ message: "Call initCustomBlock() before notifyReady().",
353
+ isRetryable: false,
354
+ },
355
+ }
356
+ }
357
+ let initialHeight: number
358
+ try {
359
+ initialHeight = this.options.getInitialHeight()
360
+ } catch (error) {
361
+ return {
362
+ status: "error",
363
+ error: {
364
+ code: "height_measurement_failed",
365
+ message:
366
+ error instanceof Error
367
+ ? error.message
368
+ : "The SDK could not measure the block height.",
369
+ isRetryable: true,
370
+ },
371
+ }
372
+ }
373
+ if (!Number.isFinite(initialHeight) || initialHeight < 0) {
374
+ return {
375
+ status: "error",
376
+ error: {
377
+ code: "invalid_height",
378
+ message:
379
+ "The SDK could not measure a finite, nonnegative block height.",
380
+ isRetryable: true,
381
+ },
382
+ }
383
+ }
384
+ const message: ReadyMessage = {
385
+ type: "ready",
386
+ initialHeight,
387
+ }
388
+ try {
389
+ this.options.post(message)
390
+ } catch {
391
+ return {
392
+ status: "error",
393
+ error: {
394
+ code: "request_send_failed",
395
+ message: "The SDK could not send its ready notification to the host.",
396
+ isRetryable: true,
397
+ },
398
+ }
218
399
  }
219
400
  this.phase = "ready"
220
- this.clearAcknowledgement()
221
- this.options.post(message)
401
+ return { status: "success" }
402
+ }
403
+
404
+ private flushPendingMessages() {
222
405
  const pending = this.pendingMessages
223
406
  this.pendingMessages = []
224
- for (const message of pending) {
225
- this.post(message)
407
+ for (const queued of pending) {
408
+ this.post(queued)
226
409
  }
227
410
  }
228
411
 
229
412
  dispose() {
230
413
  this.phase = "disposed"
231
- this.clearAcknowledgement()
414
+ this.clearHostResponseTimeout()
232
415
  this.pendingMessages = []
233
416
  this.reject(
234
417
  new CustomBlockInitializationError({
@@ -239,18 +422,48 @@ export class SandboxInitialization {
239
422
  )
240
423
  }
241
424
 
242
- private fail(error: CustomBlockInitResultErrorInfo) {
243
- if (this.phase !== "waiting" && this.phase !== "applying") {
425
+ private fail(error: CustomBlockErrorInfo) {
426
+ if (
427
+ this.phase !== "connecting" &&
428
+ this.phase !== "waitingForInit" &&
429
+ this.phase !== "applying"
430
+ ) {
244
431
  return
245
432
  }
246
433
  this.phase = "failed"
247
- this.options.post({
248
- type: "initResult",
249
- initializationId: INITIALIZATION_ID,
250
- status: "error",
251
- error,
252
- } satisfies InitResultMessage)
434
+ this.clearHostResponseTimeout()
435
+ this.pendingMessages = []
436
+ this.earlyInit = undefined
437
+ // A broken transport must not prevent local startup from settling.
438
+
253
439
  this.reject(new CustomBlockInitializationError(error))
440
+ if (this.initRequestId !== undefined) {
441
+ const response: InitResultRequestMessage = {
442
+ type: "initResult",
443
+ requestId: this.initRequestId,
444
+ status: "error",
445
+ error,
446
+ }
447
+ try {
448
+ this.options.post(response)
449
+ } catch (reportingError) {
450
+ console.warn(
451
+ "[custom-blocks-sdk] could not report initialization failure to host",
452
+ reportingError,
453
+ )
454
+ }
455
+ }
456
+ }
457
+
458
+ private waitForHostResponse() {
459
+ this.clearHostResponseTimeout()
460
+ this.hostResponseTimeout = setTimeout(() => {
461
+ this.fail({
462
+ code: "init_timeout",
463
+ message: `The host did not complete the startup handshake within ${DEFAULT_REQUEST_TIMEOUT_MS} ms.`,
464
+ isRetryable: true,
465
+ })
466
+ }, DEFAULT_REQUEST_TIMEOUT_MS)
254
467
  }
255
468
 
256
469
  private resolve() {
@@ -265,15 +478,8 @@ export class SandboxInitialization {
265
478
  this.rejectInit = undefined
266
479
  }
267
480
 
268
- private clearAcknowledgement() {
269
- clearTimeout(this.fallbackId)
270
- this.fallbackId = undefined
271
- if (
272
- this.frameId !== undefined &&
273
- typeof cancelAnimationFrame === "function"
274
- ) {
275
- cancelAnimationFrame(this.frameId)
276
- this.frameId = undefined
277
- }
481
+ private clearHostResponseTimeout() {
482
+ clearTimeout(this.hostResponseTimeout)
483
+ this.hostResponseTimeout = undefined
278
484
  }
279
485
  }
@@ -1,6 +1,9 @@
1
1
  import type { SandboxToHostMessage } from "@notionhq/custom-blocks-protocol/messages/sandboxToHost.js"
2
2
  import type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/utils/errors.js"
3
3
  import * as v from "valibot"
4
+ import { makeUuid } from "../utils/makeUuid.js"
5
+
6
+ export const DEFAULT_REQUEST_TIMEOUT_MS = 30_000
4
7
 
5
8
  export type RequestMessage<Message = SandboxToHostMessage> = Message extends {
6
9
  requestId: string
@@ -20,6 +23,7 @@ export type ResponseSchema = v.GenericSchema<
20
23
  unknown,
21
24
  { type: string; requestId: string; status: "success" } | RequestError
22
25
  >
26
+
23
27
  export type RequestResult<Schema extends ResponseSchema> =
24
28
  | Omit<
25
29
  Extract<v.InferOutput<Schema>, { status: "success" }>,
@@ -29,13 +33,12 @@ export type RequestResult<Schema extends ResponseSchema> =
29
33
 
30
34
  /** Owns correlation, response validation, and completion for one bridge's requests. */
31
35
  export class Requests {
32
- private nextId = 1
33
36
  private isDisposed = false
34
37
  private readonly pending = new Map<string, PendingRequest>()
35
38
 
36
39
  constructor(
37
40
  private readonly post: (message: SandboxToHostMessage) => void,
38
- private readonly timeoutMs = 60_000,
41
+ private readonly timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS,
39
42
  ) {}
40
43
 
41
44
  send<Schema extends ResponseSchema>(
@@ -46,7 +49,7 @@ export class Requests {
46
49
  return Promise.resolve({ status: "error", error: disposedError() })
47
50
  }
48
51
  return new Promise(resolve => {
49
- const requestId = `custom-block-request-${this.nextId++}`
52
+ const requestId = makeUuid()
50
53
  const timeout = setTimeout(() => {
51
54
  this.fail(requestId, {
52
55
  code: "request_timeout",