@notionhq/custom-blocks 0.3.1 → 0.3.3

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 (46) hide show
  1. package/README.md +2 -8
  2. package/dist/bridge/SandboxBridge.d.ts +469 -15
  3. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  4. package/dist/bridge/SandboxBridge.js +119 -88
  5. package/dist/bridge/dataSources/resolveProperty.d.ts +1 -20
  6. package/dist/bridge/dataSources/resolveProperty.d.ts.map +1 -1
  7. package/dist/bridge/dataSources/resolveProperty.js +37 -32
  8. package/dist/bridge/page.d.ts +11 -7
  9. package/dist/bridge/page.d.ts.map +1 -1
  10. package/dist/bridge/page.js +20 -50
  11. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  12. package/dist/bridge/sandboxClient.js +32 -6
  13. package/dist/dataSources.d.ts.map +1 -1
  14. package/dist/dataSources.js +4 -3
  15. package/dist/index.d.ts +1 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/protocol/dataSources/dateValue.d.ts +2 -2
  18. package/dist/protocol/dataSources/dateValue.js +9 -7
  19. package/dist/protocol/messages/createPage.d.ts +64 -2
  20. package/dist/protocol/messages/createPageResult.d.ts +192 -6
  21. package/dist/protocol/messages/getPage.d.ts +192 -6
  22. package/dist/protocol/messages/hostToSandbox.d.ts +704 -22
  23. package/dist/protocol/messages/queryDataSourceResult.d.ts +128 -4
  24. package/dist/protocol/messages/sandboxToHost.d.ts +128 -4
  25. package/dist/protocol/messages/updatePage.d.ts +64 -2
  26. package/dist/protocol/messages/updatePageResult.d.ts +192 -6
  27. package/dist/protocol/pages/page.d.ts +360 -12
  28. package/dist/protocol/pages/page.js +48 -2
  29. package/dist/protocol/protocolVersion.d.ts +2 -1
  30. package/dist/protocol/protocolVersion.js +3 -1
  31. package/dist/types.d.ts +1 -1
  32. package/dist/types.d.ts.map +1 -1
  33. package/dist/version.js +1 -1
  34. package/docs/build-tools.md +55 -0
  35. package/docs/data-sources.md +12 -9
  36. package/docs/deployment.md +16 -34
  37. package/docs/errors.md +65 -52
  38. package/docs/pages.md +85 -78
  39. package/package.json +2 -18
  40. package/src/bridge/SandboxBridge.ts +168 -129
  41. package/src/bridge/dataSources/resolveProperty.ts +48 -39
  42. package/src/bridge/page.ts +32 -75
  43. package/src/bridge/sandboxClient.ts +40 -7
  44. package/src/dataSources.ts +7 -9
  45. package/src/index.ts +6 -0
  46. package/src/types.ts +6 -0
package/docs/pages.md CHANGED
@@ -1,8 +1,8 @@
1
- # Pages and unified page objects
1
+ # Pages
2
2
 
3
- `pages.get` and `pages.create` return `{ status: "success", page: NotionPage }` on success. Failures
4
- return `{ status: "error", error }`. Data source methods use the same status envelopes and page
5
- type.
3
+ `pages.get` and `pages.create` return `{ status: "success", page }` or `{ status: "error", error }`.
4
+ Both the Pages and Data sources namespaces return the same `NotionPage` type, with methods to read
5
+ and update properties.
6
6
 
7
7
  ```ts
8
8
  import { pages, dataSources } from "@notionhq/custom-blocks"
@@ -14,23 +14,22 @@ if (result.status === "success") {
14
14
  }
15
15
  ```
16
16
 
17
- `NotionPageData` contains serializable page fields. The SDK constructs `NotionPage` by adding
18
- property getters and mutation methods. These methods send bridge messages and are never serialized.
19
- Both namespaces use this same page type.
17
+ `NotionPageData` contains the serializable page fields. `NotionPage` adds methods, which are not
18
+ serialized. Reads use loaded values. Writes send bridge messages to Notion.
20
19
 
21
20
  ## Page fields
22
21
 
23
- `properties` maps property display names to typed values or `null`. Each typed value retains its raw
24
- property ID. `getPropertyById` scans loaded values and returns the matching value or `null`. Neither
25
- getter fetches data.
22
+ `properties` maps property display names to typed values or `null`. Each typed value includes its
23
+ raw property ID. `getPropertyById` returns the loaded value with that ID, or `null`. Neither
24
+ property getter fetches data.
26
25
 
27
- All pages contain `created_time`, `last_edited_time`, `created_by`, `last_edited_by`, and
28
- `last_visited_time`. Unavailable metadata uses `null`. These fields are separate from schema
26
+ Every page has `created_time`, `last_edited_time`, `created_by`, `last_edited_by`, and
27
+ `last_visited_time` fields. Unavailable values are `null`. These fields are separate from database
29
28
  properties.
30
29
 
31
- Parent, icon, cover, URLs, timestamps, and archive states remain page metadata. Absent icons,
32
- covers, URLs, and timestamps use explicit `null` values. `is_archived` and `in_trash` include
33
- inherited state. The SDK does not support trash mutations.
30
+ Parent, icon, cover, URLs, timestamps, and archive state are page metadata. Missing icons, covers,
31
+ URLs, and timestamps are `null`. `is_archived` and `in_trash` include inherited state. The SDK
32
+ cannot move pages to or from the trash.
34
33
 
35
34
  ## Data source context
36
35
 
@@ -43,21 +42,29 @@ if (result.status === "success") {
43
42
  }
44
43
  ```
45
44
 
46
- `dataSourceKey` identifies the retained manifest data source context. It is `null` for pages
47
- returned by the Pages namespace. `getPropertyByKey` returns `null` without context or for invalid,
48
- unbound, or missing properties. The shared `propertyIdsByKey` map converts manifest keys to raw
49
- property IDs. Unbound keys map to `undefined`. Pages contain no binding maps.
45
+ Pages returned by Data sources methods retain their manifest key in `dataSourceKey`. Pages returned
46
+ by the Pages namespace have `dataSourceKey: null`.
47
+
48
+ `getPropertyByKey` uses the retained data source binding to resolve a manifest property key. It
49
+ returns `null` if the page has no context, the key is invalid or unbound, or the property is
50
+ missing. The result's `propertyIdsByKey` map exposes these bindings, with `undefined` for unbound
51
+ keys. The page itself has no binding map.
50
52
 
51
53
  ## Create pages
52
54
 
53
- `pages.create` retains `CreatePageArgs` and all `CreatePageParent` variants:
55
+ `pages.create` accepts `CreatePageArgs` with one of three `CreatePageParent` variants:
54
56
 
55
57
  - `{ type: "page_id", page_id }` creates a child page.
56
58
  - `{ type: "data_source_id", data_source_id }` creates a data source row.
57
59
  - `{ type: "data_source_key", key }` creates a row using a manifest binding.
58
60
 
59
- Only page parents accept `NotionCreatePagePosition`. Its variants are `start`, `end`, `before`, and
60
- `after`. Before and after positions require a `blockId`.
61
+ Only a `page_id` parent accepts `NotionCreatePagePosition`: `start`, `end`, `before`, or `after`.
62
+ The `before` and `after` positions require a `blockId`.
63
+
64
+ A `page_id` or `data_source_id` parent uses raw property IDs. A `data_source_key` parent uses
65
+ manifest property keys bound to properties in the loaded database schema.
66
+
67
+ In this example, `tasks` has a `title` manifest key bound to the database's title property:
61
68
 
62
69
  ```ts
63
70
  const page = await pages.create({
@@ -71,28 +78,32 @@ const row = await pages.create({
71
78
  })
72
79
  ```
73
80
 
74
- Create payloads retain existing property formats and semantic key resolution. Create does not accept
75
- icons or covers.
81
+ If a property value includes `id`, it must match the target's raw property ID. Unknown or unbound
82
+ manifest keys return `unmapped_property`. Creation does not accept icons or covers.
76
83
 
77
84
  ## Mutate pages
78
85
 
79
- `PageUpdatePayload` accepts properties, icon, cover, and `is_archived`. Each method retains the page
80
- ID and data source context. Mutation methods return `UpdatePageResult` with the updated page or a
81
- structured error. Successful writes refresh the same page object.
86
+ Page methods use the page's ID and retained data source context. `PageUpdatePayload` accepts
87
+ `properties`, `icon`, `cover`, and `is_archived`.
82
88
 
83
89
  - `page.updateById` interprets property keys as raw property IDs.
84
90
  - `page.updateByKey` interprets property keys as manifest keys.
85
91
  - `page.archive()` sets `is_archived` to `true`.
86
92
  - `page.unarchive()` sets `is_archived` to `false`.
87
93
 
88
- ID writes accept `NotionPagePropertyInputMap` and fill omitted value IDs. Mismatched value IDs and
89
- duplicate properties fail before sending a request. Keyed writes require valid bindings and loaded
90
- properties. The SDK validates every key before sending any write. A missing context, invalid key,
91
- unbound key, or missing property produces a local error. An empty update also produces a local
92
- error.
94
+ Both update methods accept `NotionPagePropertyInputMap`. The SDK fills in omitted value IDs. If a
95
+ value includes `id`, it must match the target's raw property ID, even in a key-based update.
93
96
 
94
- Icons use `NotionPageIcon` and covers use `NotionPageCover`. Use emoji, external URLs, or existing
95
- hosted file URLs. File upload references remain unsupported.
97
+ Property writes through `updateByKey` require a page returned by a Data sources method and keys
98
+ bound to properties in the loaded database schema. Updates to only `icon`, `cover`, or `is_archived`
99
+ do not require data source context.
100
+
101
+ The SDK validates all properties before sending an update. Invalid values, mismatched IDs, duplicate
102
+ targets, and unresolved keys return local errors. An update must include at least one property or
103
+ metadata change. See [Local page update errors](./errors.md#local-page-update-errors).
104
+
105
+ Icons use `NotionPageIcon` and covers use `NotionPageCover`. Supported values include emoji,
106
+ external URLs, and existing hosted file URLs. File upload references are unsupported.
96
107
 
97
108
  ## Types
98
109
 
@@ -108,20 +119,20 @@ hosted file URLs. File upload references remain unsupported.
108
119
 
109
120
  ## Property support
110
121
 
111
- This table shows property support for custom blocks running in Notion.
122
+ The table below shows property support in Notion:
112
123
 
113
- - **Read value** shows `page.properties` after reads, creates, and successful instance writes.
114
- Unsupported properties return `null`.
124
+ - **Read value** shows `page.properties` after a read, create, or successful update. Unsupported
125
+ properties return `null`.
115
126
  - **Create** covers property writes through `pages.create`.
116
127
  - **Update** covers property writes through `page.updateById`.
117
128
 
118
- Pages outside a data source support only `title`. Other property writes require a data source row
119
- and a property in its schema.
129
+ Pages outside a data source support only `title`. Other properties require a data source row and a
130
+ matching property in its schema.
120
131
 
121
132
  - 🟢 **Supported:** The SDK supports this operation in Notion.
122
- - 🟡 **Limited:** The SDK supports this operation with the restrictions shown in the cell.
133
+ - 🟡 **Limited:** The SDK supports this operation with the listed restrictions.
123
134
  - 🔴 **Unavailable:** The SDK does not expose this operation.
124
- - ⚫ **Not supported in Notion:** Notion itself does not support this operation in the stated case.
135
+ - ⚫ **Not supported in Notion:** Notion does not support this operation in the stated case.
125
136
 
126
137
  | Property type | Read value (`pages.get`) | Create (`pages.create`) | Update (`page.updateById`) |
127
138
  | ------------------- | ---------------------------------------------------- | -------------------------------------------- | -------------------------------------------- |
@@ -138,15 +149,15 @@ and a property in its schema.
138
149
  | `date` | 🟢 `start` and optional `end` strings, or `null` | 🟡 Minute precision | 🟡 Minute precision |
139
150
  | `people` | 🟢 Array of user/group ID objects | 🟢 Supported | 🟢 Supported |
140
151
  | `files` | 🟢 External links and hosted files | 🟡 File URLs only | 🟡 File URLs only |
141
- | `unique_id` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
152
+ | `unique_id` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
142
153
  | `relation` | 🟢 Array of page ID objects | 🟡 Replaces value. Rejects `has_more: true`. | 🟡 Replaces value. Rejects `has_more: true`. |
143
154
  | `place` | 🟢 Coordinates and optional place details, or `null` | 🟢 Supported | 🟢 Supported |
144
- | `formula` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
145
- | `rollup` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
146
- | `button` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
147
- | `verification` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
155
+ | `formula` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
156
+ | `rollup` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
157
+ | `button` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
158
+ | `verification` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
148
159
  | `last_visited_time` | 🟡 Current viewer’s ISO timestamp | 🔴 Unavailable | 🔴 Unavailable |
149
- | `location` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
160
+ | `location` | 🟡 Text fallback | 🔴 Unavailable | 🔴 Unavailable |
150
161
  | `created_time` | 🟢 ISO timestamp in UTC | 🔴 Unavailable | 🔴 Unavailable |
151
162
  | `last_edited_time` | 🟢 ISO timestamp in UTC | 🔴 Unavailable | 🔴 Unavailable |
152
163
  | `created_by` | 🟢 Actor record pointer | 🔴 Unavailable | 🔴 Unavailable |
@@ -154,27 +165,26 @@ and a property in its schema.
154
165
 
155
166
  ### Notes
156
167
 
157
- - Select, multi-select, and status reads return option names and colors when available. They also
158
- return IDs when the options match the schema. Writes accept an existing option ID or a name and
159
- set the selected value. They do not edit option definitions. If you supply both an ID and a name,
160
- the host uses the ID. The host rejects unknown IDs.
168
+ - Select, multi-select, and status reads return available option names and colors, plus IDs for
169
+ options that match the schema. Writes select options by ID or name without editing their
170
+ definitions. If you supply both, the host uses the ID. Unknown IDs fail.
161
171
  - Date-time writes preserve hours and minutes but discard seconds and milliseconds.
162
- - Title and rich text use text arrays, but reads and writes preserve only plain text. Reads and
163
- writes do not preserve formatting, links, mentions, or equations as structured rich text.
164
- - `pages.get` resolves Files links and readable file/image/pdf/audio/video references that have a
165
- source. Create/update responses include link items only. File writes accept `external` URLs and
166
- existing `file` URLs and store them as links. Upload references (`file_upload`) are unsupported.
172
+ - Title and rich text use text arrays that preserve only plain text. Formatting, links, mentions,
173
+ and equations are not preserved as structured rich text.
174
+ - `pages.get` resolves Files links and readable file, image, PDF, audio, and video references with a
175
+ source. Create and update responses include only links. File writes store `external` URLs and
176
+ existing `file` URLs as links. Upload references (`file_upload`) are unsupported.
167
177
  - Relation writes replace the value and reject `has_more: true`.
168
- - Top-level `created_time` and `last_edited_time` remain available independently of schema
169
- properties.
170
- - Formula, rollup, unique ID, and other omitted types do not return structured page values.
178
+ - Top-level `created_time` and `last_edited_time` do not depend on the database schema.
179
+ - Formula, rollup, unique ID, button, verification, and location reads keep their type but return
180
+ stored text as a string. Formulas and rollups are not evaluated. Missing values return `""`.
171
181
 
172
182
  For query properties, filters, and sorts, see
173
183
  [Data source property support](./data-sources.md#property-support).
174
184
 
175
185
  ## Opening pages
176
186
 
177
- `pages.open(pageId, { mode })` asks Notion to open a readable page using host-owned navigation:
187
+ `pages.open(pageId, { mode })` asks Notion to open a page the viewer can read:
178
188
 
179
189
  ```ts
180
190
  const result = await pages.open(pageId, { mode: "side_peek" })
@@ -184,31 +194,31 @@ if (result.status === "error") {
184
194
  }
185
195
  ```
186
196
 
187
- When the block is inside a peek, opening another page in that peek replaces it and unmounts the
188
- block, losing its local state. The request result may not be observable after the block unmounts.
197
+ If the block is inside a peek, opening another page in that peek unmounts the block and loses its
198
+ local state. The block may unmount before it receives the request result.
189
199
 
190
- Modes are `"side_peek"` and `"center_peek"`. Success means the host handed the request to Notion
191
- navigation; it does not wait for route-leave confirmation or for the page to finish loading. A
192
- surface that cannot host the requested peek may fall back to full-page navigation. The success
193
- result contains `{ pageId }` only — call `pages.get(pageId)` separately when you need readable page
200
+ Modes are `"side_peek"` and `"center_peek"`. Success means the host passed the request to Notion's
201
+ navigation system. The method does not wait for confirmation to leave the current page or for the
202
+ new page to load. If the current surface cannot show the requested peek, Notion may open a full page
203
+ instead. The success result contains only `{ pageId }`. Use `pages.get(pageId)` to read the page's
194
204
  metadata.
195
205
 
196
206
  ### Files reads
197
207
 
198
- Each Files entry is either an external link (`type: "external"`) or a Notion-hosted file
208
+ Each Files entry is either an external link (`type: "external"`) or a file hosted by Notion
199
209
  (`type: "file"`), in the order shown in Notion. A hosted file's `file.url` is a signed URL that
200
210
  expires at `file.expiry_time`. Call `pages.get` again when you need a fresh URL. `expiry_time` can
201
211
  be missing on older hosts.
202
212
 
203
- An empty Files value returns `[]`. If the host can't resolve an entry (for example, you can't read
204
- the referenced file, or it points to a folder), `pages.get` fails with an error instead of returning
205
- a partial list.
213
+ An empty Files value returns `[]`. If the host cannot resolve an entry, `pages.get` fails instead of
214
+ returning a partial list. For example, an entry may point to a folder or a file the viewer cannot
215
+ read.
206
216
 
207
217
  ## Mutation results
208
218
 
209
- Page instance methods return `UpdatePageResult`: the updated page or a structured error. Successful
210
- methods refresh the same page object before resolving. Use `updateById` for raw property IDs and
211
- `updateByKey` for manifest keys.
219
+ Update and archive methods return `UpdatePageResult`: `{ status: "success", page }` or
220
+ `{ status: "error", error }`. A successful update refreshes the same page object before the promise
221
+ resolves. Separately fetched pages remain independent objects.
212
222
 
213
223
  ## Namespace methods
214
224
 
@@ -221,8 +231,8 @@ methods refresh the same page object before resolving. Use `updateById` for raw
221
231
  | `dataSources.getPage(key, pageId)` | `{ status: "success", page, propertyIdsByKey }` |
222
232
  | `dataSources.create(key, payload)` | `{ status: "success", page, propertyIdsByKey }` |
223
233
 
224
- The Pages namespace exposes `get`, `create`, and `open`. Use page instance methods for all writes
225
- and archive changes.
234
+ The Pages namespace exposes `get`, `create`, and `open`. Use page methods to update properties or
235
+ metadata and to archive or unarchive a page:
226
236
 
227
237
  ```ts
228
238
  const fetched = await pages.get(pageId)
@@ -235,6 +245,3 @@ if (fetched.status === "success") {
235
245
  if (result.status === "success") console.log(page.properties.Score)
236
246
  }
237
247
  ```
238
-
239
- The successful write refreshes this page before the promise resolves. Separately fetched pages
240
- remain independent objects.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notionhq/custom-blocks",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -41,24 +41,8 @@
41
41
  "optional": true
42
42
  }
43
43
  },
44
- "devDependencies": {
45
- "@testing-library/react": "^16.3.3",
46
- "@types/node": "^26.5.0",
47
- "@types/react": "^19.2.18",
48
- "@types/react-dom": "^19.2.7",
49
- "@vitest/coverage-v8": "^5.0.0",
50
- "jsdom": "^30.0.1",
51
- "react": "^19.2.8",
52
- "react-dom": "^19.2.8",
53
- "vitest": "^5.0.0"
54
- },
55
44
  "dependencies": {
56
- "valibot": "^1.4.2"
57
- },
58
- "scripts": {
59
- "test": "vitest run --environment jsdom",
60
- "test:coverage": "vitest run --environment jsdom --coverage --coverage.provider=v8 --coverage.reporter=json",
61
- "typecheck": "tsc --project tsconfig.json && tsc --project cli/tsconfig.json"
45
+ "valibot": "^1.5.0"
62
46
  },
63
47
  "bin": {
64
48
  "notion-custom-blocks": "./bin/notion-custom-blocks/cli.js"