@notionhq/custom-blocks 0.3.5 → 0.4.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 (124) hide show
  1. package/README.md +2 -2
  2. package/dist/bridge/SandboxBridge.d.ts +4 -23
  3. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  4. package/dist/bridge/SandboxBridge.js +36 -104
  5. package/dist/bridge/requests.d.ts +35 -0
  6. package/dist/bridge/requests.d.ts.map +1 -0
  7. package/dist/bridge/requests.js +97 -0
  8. package/dist/dataSources/bridge/resolveDataSourceQuery.d.ts.map +1 -1
  9. package/dist/dataSources/bridge/resolveDataSourceQuery.js +15 -1
  10. package/dist/dataSources/createDataSourcePage.d.ts.map +1 -1
  11. package/dist/dataSources/createDataSourcePage.js +5 -1
  12. package/dist/dataSources/types.d.ts +2 -0
  13. package/dist/dataSources/types.d.ts.map +1 -1
  14. package/dist/index.d.ts +2 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/manifest/types.d.ts +1 -1
  17. package/dist/manifest/types.d.ts.map +1 -1
  18. package/dist/navigation/openExternalUrl.d.ts.map +1 -1
  19. package/dist/navigation/openExternalUrl.js +2 -1
  20. package/dist/pages/bridge/getPageForDataSource.d.ts.map +1 -1
  21. package/dist/pages/bridge/getPageForDataSource.js +2 -1
  22. package/dist/pages/bridge/sendCreatePageRequest.d.ts.map +1 -1
  23. package/dist/pages/bridge/sendCreatePageRequest.js +2 -1
  24. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.d.ts +11 -2
  25. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.d.ts.map +1 -1
  26. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.js +0 -1
  27. package/dist/pages/bridge/toNotionPagePropertyFromBridgeProperty.d.ts.map +1 -1
  28. package/dist/pages/bridge/toNotionPagePropertyFromBridgeProperty.js +0 -6
  29. package/dist/pages/bridge/updatePage.d.ts.map +1 -1
  30. package/dist/pages/bridge/updatePage.js +19 -4
  31. package/dist/pages/createPage.d.ts.map +1 -1
  32. package/dist/pages/createPage.js +5 -1
  33. package/dist/pages/openPage.d.ts.map +1 -1
  34. package/dist/pages/openPage.js +2 -1
  35. package/dist/pages/types.d.ts +5 -7
  36. package/dist/pages/types.d.ts.map +1 -1
  37. package/dist/protocol/compatibility/protocol3To5/bridgePage.d.ts +7 -5
  38. package/dist/protocol/context/parent.d.ts +2 -0
  39. package/dist/protocol/dataSources/bridgeArchiveStatus.d.ts +3 -0
  40. package/dist/protocol/dataSources/bridgeArchiveStatus.js +6 -0
  41. package/dist/protocol/dataSources/bridgeDataSourceBinding.d.ts +0 -4
  42. package/dist/protocol/dataSources/bridgeDataSourceBindings.d.ts +0 -4
  43. package/dist/protocol/dataSources/collectionSchema.d.ts +0 -4
  44. package/dist/protocol/dataSources/messages/dataSourcesChanged.d.ts +0 -4
  45. package/dist/protocol/dataSources/messages/queryDataSource.d.ts +17 -14
  46. package/dist/protocol/dataSources/messages/queryDataSource.js +2 -0
  47. package/dist/protocol/dataSources/notionDataSource.d.ts +0 -8
  48. package/dist/protocol/dataSources/properties/propertySchema.d.ts +3 -7
  49. package/dist/protocol/dataSources/properties/propertySchema.js +1 -3
  50. package/dist/protocol/handshake/connect.d.ts +15 -4
  51. package/dist/protocol/handshake/init.d.ts +15 -8
  52. package/dist/protocol/icons.d.ts +17 -0
  53. package/dist/protocol/icons.js +27 -0
  54. package/dist/protocol/index.d.ts +13 -9
  55. package/dist/protocol/index.js +8 -5
  56. package/dist/protocol/manifest/grants.d.ts +48 -0
  57. package/dist/protocol/manifest/grants.js +19 -0
  58. package/dist/protocol/manifest/manifest.d.ts +33 -10
  59. package/dist/protocol/manifest/manifest.js +11 -5
  60. package/dist/protocol/messages/hostToSandbox.d.ts +109 -87
  61. package/dist/protocol/messages/invalidHostMessage.d.ts +2 -0
  62. package/dist/protocol/messages/invalidHostMessage.js +2 -0
  63. package/dist/protocol/messages/invalidSandboxMessage.d.ts +2 -0
  64. package/dist/protocol/messages/invalidSandboxMessage.js +2 -0
  65. package/dist/protocol/messages/sandboxToHost.d.ts +104 -17
  66. package/dist/protocol/navigation/openExternalUrl.d.ts +2 -2
  67. package/dist/protocol/pages/bridgePage.d.ts +7 -6
  68. package/dist/protocol/pages/bridgePage.js +0 -1
  69. package/dist/protocol/pages/makeBridgePage.d.ts +3 -1
  70. package/dist/protocol/pages/messages/createPage.d.ts +103 -27
  71. package/dist/protocol/pages/messages/createPage.js +3 -1
  72. package/dist/protocol/pages/messages/getPage.d.ts +23 -19
  73. package/dist/protocol/pages/messages/openPage.d.ts +2 -2
  74. package/dist/protocol/pages/messages/updatePage.d.ts +30 -24
  75. package/dist/protocol/pages/pageAppearance.d.ts +36 -1
  76. package/dist/protocol/pages/pageAppearance.js +9 -0
  77. package/dist/protocol/pages/properties/bridgePagePropertyMaps.d.ts +0 -8
  78. package/dist/protocol/pages/properties/bridgePagePropertyValue.d.ts +0 -4
  79. package/dist/protocol/pages/properties/bridgePagePropertyValue.js +0 -5
  80. package/dist/protocol/users/makeBridgeUser.d.ts +3 -1
  81. package/dist/protocol/users/messages/getUser.d.ts +2 -2
  82. package/dist/protocol/users/messages/listUsers.d.ts +2 -2
  83. package/dist/protocol/utils/errors.d.ts +6 -4
  84. package/dist/protocol/utils/errors.js +3 -3
  85. package/dist/protocol/utils/protocolVersion.d.ts +2 -1
  86. package/dist/protocol/utils/protocolVersion.js +3 -1
  87. package/dist/protocol/utils/readRequestId.d.ts +2 -0
  88. package/dist/protocol/utils/readRequestId.js +7 -0
  89. package/dist/users/getUser.d.ts.map +1 -1
  90. package/dist/users/getUser.js +2 -1
  91. package/dist/users/listUsers.d.ts.map +1 -1
  92. package/dist/users/listUsers.js +4 -2
  93. package/dist/utils/version.js +1 -1
  94. package/docs/data-sources.md +38 -34
  95. package/docs/errors.md +47 -5
  96. package/docs/grants.md +127 -0
  97. package/docs/pages.md +45 -32
  98. package/docs/users.md +2 -2
  99. package/package.json +1 -1
  100. package/src/bridge/SandboxBridge.ts +44 -188
  101. package/src/bridge/requests.ts +138 -0
  102. package/src/dataSources/bridge/resolveDataSourceQuery.ts +21 -1
  103. package/src/dataSources/createDataSourcePage.ts +5 -1
  104. package/src/dataSources/types.ts +3 -0
  105. package/src/index.ts +8 -4
  106. package/src/manifest/types.ts +1 -1
  107. package/src/navigation/openExternalUrl.ts +5 -1
  108. package/src/pages/bridge/getPageForDataSource.ts +5 -1
  109. package/src/pages/bridge/sendCreatePageRequest.ts +5 -1
  110. package/src/pages/bridge/toNotionPageFieldsFromBridgePage.ts +0 -1
  111. package/src/pages/bridge/toNotionPagePropertyFromBridgeProperty.ts +0 -6
  112. package/src/pages/bridge/updatePage.ts +28 -8
  113. package/src/pages/createPage.ts +5 -1
  114. package/src/pages/openPage.ts +5 -1
  115. package/src/pages/types.ts +7 -5
  116. package/src/users/getUser.ts +5 -1
  117. package/src/users/listUsers.ts +9 -4
  118. package/dist/bridge/pendingRequests.d.ts +0 -14
  119. package/dist/bridge/pendingRequests.d.ts.map +0 -1
  120. package/dist/bridge/pendingRequests.js +0 -28
  121. package/dist/protocol/manifest/capabilities.d.ts +0 -37
  122. package/dist/protocol/manifest/capabilities.js +0 -13
  123. package/docs/capabilities.md +0 -111
  124. package/src/bridge/pendingRequests.ts +0 -28
package/docs/grants.md ADDED
@@ -0,0 +1,127 @@
1
+ # Grants
2
+
3
+ The block manifest's `grants` lists what a custom block needs to do. Notion asks each viewer to
4
+ approve the declared grants before it loads the block.
5
+
6
+ Hosts do not restrict bridge requests by grant yet. A request that the approved grants do not cover
7
+ still runs. Declare what the block uses anyway, so viewers see it before approving.
8
+
9
+ Each grant pairs an `action` with a `resource`. The action names what the block does, and the
10
+ resource names what it acts on:
11
+
12
+ ```json
13
+ {
14
+ "version": 1,
15
+ "dataSources": {
16
+ "tasks": { "name": "Tasks" },
17
+ "projects": { "name": "Projects" }
18
+ },
19
+ "grants": [
20
+ { "action": "page", "resource": "data_source:tasks" },
21
+ { "action": "page.read", "resource": ["data_source:tasks", "data_source:projects"] },
22
+ { "action": "user.read", "resource": "workspace" }
23
+ ]
24
+ }
25
+ ```
26
+
27
+ `resource` is one resource or a non-empty array of resources. An array applies the action to each
28
+ resource, so the second entry above declares `page.read` on `data_source:tasks` and on
29
+ `data_source:projects`. `action` is always one action: declare one entry per action. A resource
30
+ string is never split on commas, so `"data_source:tasks,projects"` names one key.
31
+
32
+ ## Vocabulary
33
+
34
+ | Action | Resource | Covers |
35
+ | ----------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
36
+ | `user.read` | `current_user` | `users.get` for the viewer. |
37
+ | `user.read` | `workspace` | `users.get` for any user and `users.list`. |
38
+ | `user.read_email` | `current_user` | The viewer's `person.email` in `users.get` results. |
39
+ | `user.read_email` | `workspace` | Every user's `person.email` in `users.get` and `users.list` results. Requires `user.read` at `workspace`. |
40
+ | `user` | any user resource | Shorthand for `user.read` and `user.read_email` on the same resource. |
41
+ | `page.read` | `containing_page` | `pages.get` and `pages.open` for the page that contains the block. |
42
+ | `page.read` | `data_source:<key>` | Queries of the bound data source, plus `pages.get` and `pages.open` for its rows. |
43
+ | `page.read` | `workspace` | Reads of any page or data source that the viewer can access. |
44
+ | `page.insert` | `containing_page` | `pages.create` under the containing page. |
45
+ | `page.insert` | `data_source:<key>` | `pages.create` in the bound data source. |
46
+ | `page.insert` | `workspace` | `pages.create` under any page or in any data source that the viewer can edit. |
47
+ | `page.update` | `containing_page` | `page.updateById()` and `page.updateByKey()` for the containing page. |
48
+ | `page.update` | `data_source:<key>` | `row.updateById()`, `row.updateByKey()`, `page.updateById()`, and `page.updateByKey()` for rows of the bound data source. |
49
+ | `page.update` | `workspace` | `page.updateById()` and `page.updateByKey()` for any page that the viewer can edit. |
50
+ | `page.archive` | `containing_page` | Archiving or unarchiving the containing page. See [Archive](#archive). |
51
+ | `page.archive` | `data_source:<key>` | Archiving or unarchiving rows of the bound data source. |
52
+ | `page.archive` | `workspace` | Archiving or unarchiving any page that the viewer can edit. |
53
+ | any page action | `data_source` | The same action on `data_source:<key>` for every key in `dataSources`. |
54
+ | `page` | any page resource | Shorthand for `page.read`, `page.insert`, `page.update`, and `page.archive` on the same resource. |
55
+ | `network.fetch` | an HTTPS origin | Loading content from and connecting to that origin, such as `https://api.example.com`. See [Network access](#network-access). |
56
+
57
+ For user actions, `workspace` is every member of the workspace. For page actions, it's every page
58
+ the viewer can access. Either way it covers every narrower resource of the same action. A
59
+ `containing_page` resource applies to the page that the block instance sits on, so viewers approve
60
+ it again after the block moves to another page.
61
+
62
+ `data_source:<key>` applies to the data source bound to that key of the manifest's `dataSources`,
63
+ such as `data_source:tasks`. Viewers approve each bound data source separately, and an approval
64
+ stays with that data source: after a key is bound to a different data source, viewers approve it
65
+ again. A bound data source that the viewer hasn't approved stays unbound for them. If two keys are
66
+ bound to the same data source, the block holds the grants of both keys on it.
67
+
68
+ A bare `data_source` resource is shorthand for every declared key. Prefer explicit keys, so each
69
+ data source gets only the actions the block uses on it. User actions do not take data source
70
+ resources. The `private` page resource is reserved and not accepted yet.
71
+
72
+ Deploys reject unknown actions, unknown resources, pairs that the table does not list, empty
73
+ `resource` arrays, and `data_source:<key>` resources whose key is not in `dataSources`. In an array,
74
+ every resource must be valid for the action.
75
+
76
+ Every key in `dataSources` needs a page grant that covers it: one on `data_source:<key>`, on
77
+ `data_source`, or on `workspace`, such as `{ action: "page.read", resource: "data_source:tasks" }`.
78
+ Otherwise the deploy fails.
79
+
80
+ ## Archive
81
+
82
+ `page.update` does not cover archiving or unarchiving a page. Declare `page.archive`, or the `page`
83
+ shorthand, on the same resource.
84
+
85
+ `page.archive` covers updates that set `is_archived`: `page.archive()`, `page.unarchive()`,
86
+ `row.archive()`, `row.unarchive()`, and any update that passes `is_archived`. An update that sets
87
+ `is_archived` and other fields needs both `page.archive` and `page.update`.
88
+
89
+ `page.trash` is reserved for a future call that moves pages to Trash. Deploys do not accept it yet.
90
+
91
+ ## Network access
92
+
93
+ `network.fetch` takes a bare HTTPS origin as its resource and lets the block load content from and
94
+ connect to that origin, for example
95
+ `{ action: "network.fetch", resource: "https://api.example.com" }`. It covers `fetch` and other
96
+ connections, plus images, media, fonts, stylesheets, and frames loaded from the origin. An
97
+ `https://*.example.com` resource covers subdomains. Deploys reject resources that are not bare HTTPS
98
+ origins. Notion allows an approved origin only while it is also on the workspace's custom block
99
+ network allowlist. The dev shell does not limit network access.
100
+
101
+ The declared origins, written as a JSON array such as `["https://api.example.com"]`, can use at most
102
+ 2,048 characters. Deploys reject longer lists. Combine origins that share a domain into one
103
+ wildcard, such as `https://*.example.com`, to stay under the limit. The workspace allowlist has the
104
+ same limit.
105
+
106
+ ## Blocks without grants
107
+
108
+ A manifest that omits `grants` loads without an approval step. Notion may require `grants` in a
109
+ future release. An empty `grants` array declares nothing.
110
+
111
+ ## Grant errors
112
+
113
+ The protocol defines the `not_granted` error code for requests that the approved grants do not
114
+ cover. No host sends it yet. When a host does, the result will resolve with `status: "error"` and
115
+ `isRetryable: false`. See [error handling](errors.md).
116
+
117
+ ## Dev shell
118
+
119
+ The dev shell does not ask for approval. It loads every block as if its grants were approved.
120
+
121
+ ## Types
122
+
123
+ - `CustomBlockGrant`: one `{ action, resource }` entry. `action` is a string and `resource` is a
124
+ string or a non-empty array of strings, so newer names do not break older SDKs.
125
+ - `CustomBlockGrantAction`: the actions in the vocabulary table.
126
+ - `CustomBlockGrantResourceName`: the resource names in the vocabulary table, including
127
+ `data_source:${string}`.
package/docs/pages.md CHANGED
@@ -24,9 +24,8 @@ if (result.status === "success") {
24
24
  raw property ID. `getPropertyById` returns the loaded value with that ID, or `null`. Neither
25
25
  property getter fetches data.
26
26
 
27
- Every page has `created_time`, `last_edited_time`, `created_by`, `last_edited_by`, and
28
- `last_visited_time` fields. Unavailable values are `null`. These fields are separate from database
29
- properties.
27
+ Every page has `created_time`, `last_edited_time`, `created_by`, and `last_edited_by` fields.
28
+ Unavailable values are `null`. These fields are separate from database properties.
30
29
 
31
30
  Parent, icon, cover, URLs, timestamps, and archive state are page metadata. Missing icons, covers,
32
31
  URLs, and timestamps are `null`. `is_archived` and `in_trash` include inherited state. The SDK
@@ -80,7 +79,8 @@ const row = await pages.create({
80
79
  ```
81
80
 
82
81
  If a property value includes `id`, it must match the target's raw property ID. Unknown or unbound
83
- manifest keys return `unmapped_property`. Creation does not accept icons or covers.
82
+ manifest keys return `unmapped_property`. Creation accepts native icons, emoji, custom emoji, and
83
+ URL-backed icons and covers. URL-backed inputs require host network permission.
84
84
 
85
85
  ## Mutate pages
86
86
 
@@ -135,34 +135,33 @@ matching property in its schema.
135
135
  - 🔴 **Unavailable:** The SDK does not expose this operation.
136
136
  - ⚫ **Not supported in Notion:** Notion does not support this operation in the stated case.
137
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
- | `last_visited_time` | 🟡 Current viewer’s ISO timestamp | 🔴 Unavailable | 🔴 Unavailable |
161
- | `location` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
162
- | `created_time` | 🟢 ISO timestamp in UTC | 🔴 Unavailable | 🔴 Unavailable |
163
- | `last_edited_time` | 🟢 ISO timestamp in UTC | 🔴 Unavailable | 🔴 Unavailable |
164
- | `created_by` | 🟢 Actor record pointer | 🔴 Unavailable | 🔴 Unavailable |
165
- | `last_edited_by` | 🟢 Actor record pointer | 🔴 Unavailable | 🔴 Unavailable |
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 |
166
165
 
167
166
  ### Notes
168
167
 
@@ -246,3 +245,17 @@ if (fetched.status === "success") {
246
245
  if (result.status === "success") console.log(page.properties.Score)
247
246
  }
248
247
  ```
248
+
249
+ ### Native page icons
250
+
251
+ `pages.create`, `dataSources.create`, `page.updateById`, and `page.updateByKey` accept
252
+ `{ type: "icon", icon: { name: "star", color: "blue" } }`.
253
+
254
+ 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.
258
+
259
+ External icon and cover writes require an HTTPS origin allowed by the block's network permissions.
260
+ The host checks the submitted URL; allowlisted origins are trusted to redirect. Without network
261
+ permission, URL-backed writes fail before changing the page.
package/docs/users.md CHANGED
@@ -153,8 +153,8 @@ APIs.
153
153
  A user returned by `users.list()` does not necessarily have access to the current page or data
154
154
  source. Do not use this list to check page or data source permissions.
155
155
 
156
- Declare `{ capability: "user.read", resource: "workspace" }` for `users.get()` on another user and
157
- for `users.list()`, and `user.read_email` for `person.email`. See [capabilities](capabilities.md).
156
+ Declare `{ action: "user.read", resource: "workspace" }` for `users.get()` on another user and for
157
+ `users.list()`, and `user.read_email` for `person.email`. See [grants](grants.md).
158
158
 
159
159
  If a request returns `status: "error"`, handle the error before reading `result.user` or
160
160
  `result.list`. See [error handling](errors.md).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notionhq/custom-blocks",
3
- "version": "0.3.5",
3
+ "version": "0.4.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -6,36 +6,8 @@ import type { CustomBlockInitResultErrorInfo } from "@notionhq/custom-blocks-pro
6
6
  import { hostToSandboxMessageSchema } from "@notionhq/custom-blocks-protocol/messages/hostToSandbox.js"
7
7
  import { readIncomingType } from "@notionhq/custom-blocks-protocol/messages/incomingType.js"
8
8
  import type { InvalidHostMessage } from "@notionhq/custom-blocks-protocol/messages/invalidHostMessage.js"
9
- import type { BridgeMessagePayload } from "@notionhq/custom-blocks-protocol/messages/messagePayload.js"
10
9
  import type { SandboxToHostMessage } from "@notionhq/custom-blocks-protocol/messages/sandboxToHost.js"
11
- import type {
12
- OpenExternalUrlMessage,
13
- OpenExternalUrlResultMessage,
14
- } from "@notionhq/custom-blocks-protocol/navigation/openExternalUrl.js"
15
- import type {
16
- CreatePageMessage,
17
- CreatePageResultMessage,
18
- } from "@notionhq/custom-blocks-protocol/pages/messages/createPage.js"
19
- import type {
20
- GetPageMessage,
21
- GetPageResultMessage,
22
- } from "@notionhq/custom-blocks-protocol/pages/messages/getPage.js"
23
- import type {
24
- OpenPageMessage,
25
- OpenPageResultMessage,
26
- } from "@notionhq/custom-blocks-protocol/pages/messages/openPage.js"
27
- import type {
28
- UpdatePageMessage,
29
- UpdatePageResultMessage,
30
- } from "@notionhq/custom-blocks-protocol/pages/messages/updatePage.js"
31
- import type {
32
- GetUserMessage,
33
- GetUserResultMessage,
34
- } from "@notionhq/custom-blocks-protocol/users/messages/getUser.js"
35
- import type {
36
- ListUsersMessage,
37
- ListUsersResultMessage,
38
- } from "@notionhq/custom-blocks-protocol/users/messages/listUsers.js"
10
+ import { readRequestId } from "@notionhq/custom-blocks-protocol/utils/readRequestId.js"
39
11
  import * as v from "valibot"
40
12
  import { applyContextMessage } from "../context/applyContextMessage.js"
41
13
  import type { CustomBlockHostState } from "../context/hostState.js"
@@ -47,7 +19,11 @@ import { notifyListener } from "../utils/notifyListener.js"
47
19
  import { unreachable } from "../utils/unreachable.js"
48
20
  import { SandboxInitialization } from "./initialization/SandboxInitialization.js"
49
21
  import type { MessageInterceptor } from "./messageInterceptor.js"
50
- import { PendingRequests } from "./pendingRequests.js"
22
+ import {
23
+ type RequestMessage,
24
+ Requests,
25
+ type ResponseSchema,
26
+ } from "./requests.js"
51
27
 
52
28
  /**
53
29
  * A single entry in the bridge message log. Kept intentionally plain so the log is copy-pasteable
@@ -74,27 +50,7 @@ export class SandboxBridge {
74
50
  private messageInterceptors = new Set<MessageInterceptor>()
75
51
  private messageLog: MessageLogEntry[] = []
76
52
  private messageLogListeners = new Set<() => void>()
77
- private readonly pendingCreatePage = new PendingRequests<
78
- BridgeMessagePayload<CreatePageResultMessage>
79
- >("custom-block-create-page")
80
- private readonly pendingGetPage = new PendingRequests<
81
- BridgeMessagePayload<GetPageResultMessage>
82
- >("custom-block-get-page")
83
- private readonly pendingGetUser = new PendingRequests<
84
- BridgeMessagePayload<GetUserResultMessage>
85
- >("custom-block-get-user")
86
- private readonly pendingListUsers = new PendingRequests<
87
- BridgeMessagePayload<ListUsersResultMessage>
88
- >("custom-block-list-users")
89
- private readonly pendingOpenPage = new PendingRequests<
90
- BridgeMessagePayload<OpenPageResultMessage>
91
- >("custom-block-open-page")
92
- private readonly pendingOpenExternalUrl = new PendingRequests<
93
- BridgeMessagePayload<OpenExternalUrlResultMessage>
94
- >("custom-block-open-url")
95
- private readonly pendingUpdatePage = new PendingRequests<
96
- BridgeMessagePayload<UpdatePageResultMessage>
97
- >("custom-block-update-page")
53
+ private readonly requests = new Requests(message => this.postToHost(message))
98
54
  private latestDataSourceBindings: BridgeDataSourceBindings = {}
99
55
  private isListening = false
100
56
  private isDisposed = false
@@ -115,10 +71,11 @@ export class SandboxBridge {
115
71
  * acknowledgment callbacks, removes the message listener, and discards queued
116
72
  * outbound messages. Further host messages are ignored.
117
73
  *
118
- * Pending page and user requests are not settled by disposal.
74
+ * Pending requests complete with request_disposed, and their timers are cleared.
119
75
  */
120
76
  dispose() {
121
77
  this.isDisposed = true
78
+ this.requests.disposeAll()
122
79
  this.initialization.dispose()
123
80
  if (this.isListening) {
124
81
  window.removeEventListener("message", this.handleMessage)
@@ -212,6 +169,15 @@ export class SandboxBridge {
212
169
  ) {
213
170
  return
214
171
  }
172
+ const requestId = readRequestId(data)
173
+ const reason = formatInvalidHostReason(incomingType, parsed.issues)
174
+ if (requestId !== undefined) {
175
+ this.requests.fail(requestId, {
176
+ code: "invalid_host_message",
177
+ message: reason,
178
+ isRetryable: false,
179
+ })
180
+ }
215
181
  // Loop guard: never NACK a NACK. Excludes both directions —
216
182
  // `invalidSandboxMessage` is what the host normally sends, but
217
183
  // a buggy host that echoes our own `invalidHostMessage` back
@@ -222,7 +188,8 @@ export class SandboxBridge {
222
188
  ) {
223
189
  const nack: InvalidHostMessage = {
224
190
  type: "invalidHostMessage",
225
- reason: formatInvalidHostReason(incomingType, parsed.issues),
191
+ ...(requestId !== undefined ? { requestId } : {}),
192
+ reason,
226
193
  }
227
194
  this.postToHost(nack)
228
195
  }
@@ -230,11 +197,14 @@ export class SandboxBridge {
230
197
  }
231
198
  const message = parsed.output
232
199
 
233
- // If the host couldn't parse one of our outbound sandbox-to-host messages, it sends back an
234
- // `invalidSandboxMessage` NACK with a human-readable `reason`. Log it for developer
235
- // visibility and stop. We don't retry as the sandbox can't recover from a host-side parse
236
- // failure on its own.
237
200
  if (message.type === "invalidSandboxMessage") {
201
+ if (message.requestId !== undefined) {
202
+ this.requests.fail(message.requestId, {
203
+ code: "invalid_sandbox_message",
204
+ message: message.reason,
205
+ isRetryable: false,
206
+ })
207
+ }
238
208
  console.warn(
239
209
  "[custom-blocks-sdk] host reported invalid sandbox message:",
240
210
  message.reason,
@@ -258,6 +228,18 @@ export class SandboxBridge {
258
228
  return
259
229
  }
260
230
 
231
+ if ("requestId" in message) {
232
+ const error = this.requests.receive(message)
233
+ if (error !== undefined) {
234
+ this.postToHost({
235
+ type: "invalidHostMessage",
236
+ requestId: message.requestId,
237
+ reason: error.message,
238
+ })
239
+ }
240
+ return
241
+ }
242
+
261
243
  switch (message.type) {
262
244
  case "themeChanged":
263
245
  case "contrastModeChanged":
@@ -278,76 +260,6 @@ export class SandboxBridge {
278
260
  return
279
261
  }
280
262
 
281
- case "createPageResult": {
282
- const { type: _, requestId, ...result } = message
283
- if (!this.pendingCreatePage.resolve(requestId, result)) {
284
- console.warn(
285
- `[custom-blocks-sdk] createPageResult for unknown requestId ${message.requestId}`,
286
- )
287
- }
288
- return
289
- }
290
-
291
- case "getPageResult": {
292
- const { type: _, requestId, ...result } = message
293
- if (!this.pendingGetPage.resolve(requestId, result)) {
294
- console.warn(
295
- `[custom-blocks-sdk] getPageResult for unknown requestId ${message.requestId}`,
296
- )
297
- }
298
- return
299
- }
300
-
301
- case "getUserResult": {
302
- const { type: _, requestId, ...result } = message
303
- if (!this.pendingGetUser.resolve(requestId, result)) {
304
- console.warn(
305
- `[custom-blocks-sdk] getUserResult for unknown requestId ${message.requestId}`,
306
- )
307
- }
308
- return
309
- }
310
-
311
- case "listUsersResult": {
312
- const { type: _, requestId, ...result } = message
313
- if (!this.pendingListUsers.resolve(requestId, result)) {
314
- console.warn(
315
- `[custom-blocks-sdk] listUsersResult for unknown requestId ${message.requestId}`,
316
- )
317
- }
318
- return
319
- }
320
-
321
- case "openPageResult": {
322
- const { type: _, requestId, ...result } = message
323
- if (!this.pendingOpenPage.resolve(requestId, result)) {
324
- console.warn(
325
- `[custom-blocks-sdk] openPageResult for unknown requestId ${message.requestId}`,
326
- )
327
- }
328
- return
329
- }
330
-
331
- case "openExternalUrlResult": {
332
- const { type: _, requestId, ...result } = message
333
- if (!this.pendingOpenExternalUrl.resolve(requestId, result)) {
334
- console.warn(
335
- `[custom-blocks-sdk] openExternalUrlResult for unknown requestId ${message.requestId}`,
336
- )
337
- }
338
- return
339
- }
340
-
341
- case "updatePageResult": {
342
- const { type: _, requestId, ...result } = message
343
- if (!this.pendingUpdatePage.resolve(requestId, result)) {
344
- console.warn(
345
- `[custom-blocks-sdk] updatePageResult for unknown requestId ${message.requestId}`,
346
- )
347
- }
348
- return
349
- }
350
-
351
263
  case "queryDataSourceResult": {
352
264
  const listener = this.dataSourceQueryListeners.get(
353
265
  message.subscriptionId,
@@ -414,67 +326,11 @@ export class SandboxBridge {
414
326
  return undefined
415
327
  }
416
328
 
417
- requestCreatePage(
418
- args: BridgeMessagePayload<CreatePageMessage>,
419
- ): Promise<BridgeMessagePayload<CreatePageResultMessage>> {
420
- return new Promise(resolve => {
421
- const requestId = this.pendingCreatePage.allocate(resolve)
422
- this.postToHost({ type: "createPage", requestId, ...args })
423
- })
424
- }
425
-
426
- requestGetPage(
427
- args: Omit<GetPageMessage, "type" | "requestId">,
428
- ): Promise<BridgeMessagePayload<GetPageResultMessage>> {
429
- return new Promise(resolve => {
430
- const requestId = this.pendingGetPage.allocate(resolve)
431
- this.postToHost({ type: "getPage", requestId, ...args })
432
- })
433
- }
434
-
435
- requestUpdatePage(
436
- args: Omit<UpdatePageMessage, "type" | "requestId">,
437
- ): Promise<BridgeMessagePayload<UpdatePageResultMessage>> {
438
- return new Promise(resolve => {
439
- const requestId = this.pendingUpdatePage.allocate(resolve)
440
- this.postToHost({ type: "updatePage", requestId, ...args })
441
- })
442
- }
443
-
444
- requestGetUser(
445
- args: Omit<GetUserMessage, "type" | "requestId">,
446
- ): Promise<BridgeMessagePayload<GetUserResultMessage>> {
447
- return new Promise(resolve => {
448
- const requestId = this.pendingGetUser.allocate(resolve)
449
- this.postToHost({ type: "getUser", requestId, ...args })
450
- })
451
- }
452
-
453
- requestListUsers(
454
- args: Omit<ListUsersMessage, "type" | "requestId">,
455
- ): Promise<BridgeMessagePayload<ListUsersResultMessage>> {
456
- return new Promise(resolve => {
457
- const requestId = this.pendingListUsers.allocate(resolve)
458
- this.postToHost({ type: "listUsers", requestId, ...args })
459
- })
460
- }
461
-
462
- requestOpenPage(
463
- args: Omit<OpenPageMessage, "type" | "requestId">,
464
- ): Promise<BridgeMessagePayload<OpenPageResultMessage>> {
465
- return new Promise(resolve => {
466
- const requestId = this.pendingOpenPage.allocate(resolve)
467
- this.postToHost({ type: "openPage", requestId, ...args })
468
- })
469
- }
470
-
471
- requestOpenExternalUrl(
472
- args: Omit<OpenExternalUrlMessage, "type" | "requestId">,
473
- ): Promise<BridgeMessagePayload<OpenExternalUrlResultMessage>> {
474
- return new Promise(resolve => {
475
- const requestId = this.pendingOpenExternalUrl.allocate(resolve)
476
- this.postToHost({ type: "openExternalUrl", requestId, ...args })
477
- })
329
+ request<Schema extends ResponseSchema>(
330
+ message: RequestMessage,
331
+ responseSchema: Schema,
332
+ ) {
333
+ return this.requests.send(message, responseSchema)
478
334
  }
479
335
  }
480
336