@notionhq/custom-blocks 0.2.24 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/README.md +8 -5
  2. package/dist/bridge/SandboxBridge.d.ts +15 -14
  3. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  4. package/dist/bridge/SandboxBridge.js +117 -35
  5. package/dist/bridge/dataSources/resolve.d.ts +1 -3
  6. package/dist/bridge/dataSources/resolve.d.ts.map +1 -1
  7. package/dist/bridge/dataSources/resolve.js +1 -11
  8. package/dist/bridge/dataSources/subscribe.d.ts +2 -1
  9. package/dist/bridge/dataSources/subscribe.d.ts.map +1 -1
  10. package/dist/bridge/dataSources/subscribe.js +6 -1
  11. package/dist/bridge/hostState.d.ts +11 -39
  12. package/dist/bridge/hostState.d.ts.map +1 -1
  13. package/dist/bridge/hostState.js +5 -50
  14. package/dist/bridge/page.d.ts +14 -0
  15. package/dist/bridge/page.d.ts.map +1 -0
  16. package/dist/bridge/page.js +59 -0
  17. package/dist/bridge/sandboxClient.d.ts +8 -14
  18. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  19. package/dist/bridge/sandboxClient.js +13 -21
  20. package/dist/dataSources.d.ts +8 -0
  21. package/dist/dataSources.d.ts.map +1 -0
  22. package/dist/dataSources.js +103 -0
  23. package/dist/index.d.ts +6 -6
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +3 -2
  26. package/dist/protocol/dataSources/dataSourcePage.d.ts +3 -5
  27. package/dist/protocol/dataSources/dataSourcePage.js +2 -3
  28. package/dist/protocol/dataSources/propertySchema.d.ts +1 -0
  29. package/dist/protocol/index.d.ts +1 -0
  30. package/dist/protocol/index.js +1 -0
  31. package/dist/protocol/messages/createPage.js +2 -2
  32. package/dist/protocol/messages/createPageResult.d.ts +468 -1
  33. package/dist/protocol/messages/createPageResult.js +17 -2
  34. package/dist/protocol/messages/getPage.d.ts +468 -1
  35. package/dist/protocol/messages/getPage.js +18 -3
  36. package/dist/protocol/messages/hostToSandbox.d.ts +2587 -178
  37. package/dist/protocol/messages/hostToSandbox.js +24 -12
  38. package/dist/protocol/messages/openExternalUrl.d.ts +32 -0
  39. package/dist/protocol/messages/openExternalUrl.js +26 -0
  40. package/dist/protocol/messages/queryDataSourceResult.d.ts +468 -1
  41. package/dist/protocol/messages/queryDataSourceResult.js +18 -2
  42. package/dist/protocol/messages/sandboxToHost.d.ts +6 -0
  43. package/dist/protocol/messages/sandboxToHost.js +2 -0
  44. package/dist/protocol/messages/updatePage.d.ts +1 -0
  45. package/dist/protocol/messages/updatePage.js +4 -3
  46. package/dist/protocol/messages/updatePageResult.d.ts +467 -1
  47. package/dist/protocol/messages/updatePageResult.js +16 -2
  48. package/dist/protocol/pages/page.d.ts +386 -6
  49. package/dist/protocol/pages/page.js +23 -2
  50. package/dist/protocol/protocolVersion.d.ts +2 -1
  51. package/dist/protocol/protocolVersion.js +4 -1
  52. package/dist/react/useDataSource.d.ts +1 -1
  53. package/dist/react/useDataSource.d.ts.map +1 -1
  54. package/dist/react/useDataSource.js +1 -3
  55. package/dist/types.d.ts +55 -96
  56. package/dist/types.d.ts.map +1 -1
  57. package/dist/version.js +1 -1
  58. package/docs/block-location.md +3 -3
  59. package/docs/data-sources.md +167 -133
  60. package/docs/errors.md +9 -9
  61. package/docs/links.md +44 -0
  62. package/docs/pages.md +113 -182
  63. package/package.json +1 -1
  64. package/src/bridge/SandboxBridge.ts +189 -62
  65. package/src/bridge/dataSources/resolve.ts +1 -20
  66. package/src/bridge/dataSources/subscribe.ts +10 -5
  67. package/src/bridge/hostState.ts +21 -100
  68. package/src/bridge/page.ts +90 -0
  69. package/src/bridge/sandboxClient.ts +15 -25
  70. package/src/dataSources.ts +140 -0
  71. package/src/index.ts +8 -19
  72. package/src/react/useDataSource.ts +1 -3
  73. package/src/types.ts +61 -109
package/docs/pages.md CHANGED
@@ -1,201 +1,119 @@
1
- # `pages` API
1
+ # Pages and unified page objects
2
2
 
3
- Helpers for creating, reading, updating, archiving, and opening Notion pages from inside a custom
4
- block. The SDK forwards each call to Notion on your behalf.
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.
5
6
 
6
7
  ```ts
7
- import { pages } from "@notionhq/custom-blocks"
8
- ```
9
-
10
- Every helper follows the SDK's [error-handling contract](./errors.md). Check `result.status` before
11
- reading the success payload.
12
-
13
- ## Creating pages
14
-
15
- `pages.create` accepts a parent, a property map, and an optional position. Setting an icon or cover
16
- at creation time is not supported yet; create the page first, then use `pages.update`.
17
-
18
- ```ts
19
- const result = await pages.create({
20
- parent: { type: "data_source_key", key: "tasks" },
21
- properties: {
22
- title: {
23
- type: "title",
24
- title: [{ type: "text", text: { content: "New task" } }],
25
- },
26
- dueDate: { type: "date", date: { start: "2026-06-01" } },
27
- },
28
- })
8
+ import { pages, dataSources } from "@notionhq/custom-blocks"
29
9
 
10
+ const result = await pages.get(pageId)
30
11
  if (result.status === "success") {
31
- console.log("created", result.page.id)
12
+ const score = result.page.getPropertyById("score-id")
13
+ await result.page.updateById({ properties: { "score-id": { type: "number", number: 10 } } })
32
14
  }
33
15
  ```
34
16
 
35
- ### Choosing a parent
36
-
37
- `parent` is the **destination for the new page**. It does not describe the custom block's own
38
- location.
39
-
40
- - A data source parent creates a new row in that data source.
41
- - A page parent creates a child page (subpage) inside that page.
42
-
43
- The accepted shape is `CreatePageParent`:
44
-
45
- ```ts
46
- type CreatePageParent =
47
- | { type: "page_id"; page_id: NotionPageId }
48
- | { type: "data_source_id"; data_source_id: NotionDataSourceId }
49
- | { type: "data_source_key"; key: string }
50
- ```
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.
51
20
 
52
- Use `data_source_key` for a data source key declared in the custom block's manifest. When the block
53
- is configured, that slot is bound to an actual Notion data source. The SDK resolves the binding and
54
- creates a row there.
21
+ ## Page fields
55
22
 
56
- Use `page_id` when you want a subpage under a specific page. For example,
57
- `parent: { type: "page_id", page_id: page.id }` creates a child of the page returned by `usePage()`.
58
- Only page parents support `position`.
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.
59
26
 
60
- Use `data_source_id` when you already have a raw Notion data source ID. In normal custom-block code,
61
- prefer `data_source_key` so the block uses its configured binding rather than a deployment-specific
62
- ID.
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
29
+ properties.
63
30
 
64
- ### Property keys
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.
65
34
 
66
- `properties` is a `NotionPagePropertyInputMap`. Two niceties versus the raw API when the create
67
- parent resolves to a configured data source:
68
-
69
- - **Keys** can be either raw Notion property IDs _or_ the data source property keys you declared in
70
- the manifest. The SDK resolves keys → IDs before forwarding the request. Semantic keys work for
71
- `parent.type: "data_source_key"` and for `parent.type: "data_source_id"` when that ID matches a
72
- data source delivered by `init` / `dataSourcesChanged`; use raw property IDs for `page_id` parents
73
- or unmapped data sources.
74
- - **Values** (`NotionPagePropertyInputValue`) may omit `id` — the SDK fills in the final raw ID for
75
- you.
76
-
77
- So if your manifest declares `title` and `dueDate`, you can write them by name (as in the example
78
- above) instead of looking up the raw IDs.
79
-
80
- ### Where the new page lands
81
-
82
- For a `page_id` parent, `position` is a `NotionCreatePagePosition` and controls placement inside the
83
- parent:
84
-
85
- - `{ type: "start" }` / `{ type: "end" }` — prepend or append (default).
86
- - `{ type: "before", blockId }` / `{ type: "after", blockId }` — insert as a sibling of the given
87
- block (which can be nested anywhere under the parent).
88
-
89
- Do not pass `position` with a `data_source_id` or `data_source_key` parent. Data source rows are
90
- ordered by the view rather than by sibling anchors.
91
-
92
- ## Reading pages
93
-
94
- `pages.get(pageId)` fetches a single page by ID:
35
+ ## Data source context
95
36
 
96
37
  ```ts
97
- const result = await pages.get(pageId)
98
- if (result.status === "error") return
99
-
100
- const page = result.page // NotionPage
101
- console.log(page.properties)
38
+ const result = await dataSources.getPage("tasks", pageId)
39
+ if (result.status === "success") {
40
+ const { page, propertyIdsByKey } = result
41
+ const score = page.getPropertyByKey("score")
42
+ await page.updateByKey({ properties: { score: { type: "number", number: 10 } } })
43
+ }
102
44
  ```
103
45
 
104
- `page` mirrors Notion's public API shape, with `id`, `parent`, `properties`, optional `icon` /
105
- `cover`, etc. — see `NotionPage` in the types list below.
106
-
107
- ### Archive
108
-
109
- Archiving keeps content you no longer actively use without deleting it.
110
- [Learn about Archive](https://www.notion.com/help/archive-pages).
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.
111
50
 
112
- Read `is_archived` on a page or data source row. It is `true` when the page or an ancestor is
113
- archived.
51
+ ## Create pages
114
52
 
115
- ### Trash
53
+ `pages.create` retains `CreatePageArgs` and all `CreatePageParent` variants:
116
54
 
117
- Pages in Trash are scheduled for permanent deletion. By default, Notion permanently deletes them 30
118
- days after they enter Trash, unless you restore them first. Enterprise workspaces can configure a
119
- different retention period.
120
- [Learn about Trash retention](https://www.notion.com/help/custom-data-retention-settings).
55
+ - `{ type: "page_id", page_id }` creates a child page.
56
+ - `{ type: "data_source_id", data_source_id }` creates a data source row.
57
+ - `{ type: "data_source_key", key }` creates a row using a manifest binding.
121
58
 
122
- Read `in_trash` on a page or data source row. It is `true` when the page or an ancestor is in Trash.
123
-
124
- ## Updating pages
125
-
126
- `pages.update` writes back to a page. The optional fields (`properties`, `icon`, `cover`,
127
- `is_archived`) are independent — supply only what you want to change:
59
+ Only page parents accept `NotionCreatePagePosition`. Its variants are `start`, `end`, `before`, and
60
+ `after`. Before and after positions require a `blockId`.
128
61
 
129
62
  ```ts
130
- const result = await pages.update({
131
- pageId,
132
- properties: {
133
- "%5C%3FX%3D": {
134
- id: "%5C%3FX%3D",
135
- type: "checkbox",
136
- checkbox: true,
137
- },
138
- },
139
- icon: { type: "emoji", emoji: "✅" },
63
+ const page = await pages.create({
64
+ parent: { type: "page_id", page_id: parentPageId },
65
+ properties: { title: { type: "title", title: [{ text: { content: "New page" } }] } },
140
66
  })
141
- ```
142
-
143
- The `properties` map (a `NotionPagePropertyWriteMap`) is keyed by **raw Notion property ID**, and
144
- each value must repeat that ID as its own `id` field — semantic data source keys aren't accepted
145
- here. To update a row by configured custom-block key without writing out the raw IDs, use the
146
- `update` helper on the row returned from `useDataSource` instead; the SDK handles the key → ID
147
- resolution for you.
148
-
149
- If you call `pages.update` with no fields to change, it short-circuits and resolves with
150
- `{ status: "error", error: { code: "invalid_page_update", message: "updatePage requires at least one property.", isRetryable: false } }`
151
- — no request is sent.
152
-
153
- ## Archiving and unarchiving pages
154
-
155
- `pages.archive(pageId)` archives a page. `pages.unarchive(pageId)` removes its archive state.
156
67
 
157
- ```ts
158
- const result = await pages.archive(pageId)
159
- if (result.status === "error") {
160
- console.error(result.error.message)
161
- }
162
-
163
- const restored = await pages.unarchive(pageId)
164
- if (restored.status === "error") {
165
- console.error(restored.error.message)
166
- }
68
+ const row = await pages.create({
69
+ parent: { type: "data_source_key", key: "tasks" },
70
+ properties: { title: { type: "title", title: [{ text: { content: "New task" } }] } },
71
+ })
167
72
  ```
168
73
 
169
- `pages.archive` is shorthand for calling `pages.update` with `is_archived: true`. `pages.unarchive`
170
- is shorthand for calling `pages.update` with `is_archived: false`. Both methods return an
171
- `UpdatePageResult`.
74
+ Create payloads retain existing property formats and semantic key resolution. Create does not accept
75
+ icons or covers.
172
76
 
173
- ## Trashing and untrashing pages
77
+ ## Mutate pages
174
78
 
175
- This is not currently supported by the custom blocks SDK.
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.
176
82
 
177
- ## Icons, covers, and file uploads
83
+ - `page.updateById` interprets property keys as raw property IDs.
84
+ - `page.updateByKey` interprets property keys as manifest keys.
85
+ - `page.archive()` sets `is_archived` to `true`.
86
+ - `page.unarchive()` sets `is_archived` to `false`.
178
87
 
179
- `pages.update` can set icons and covers, and returned `NotionPage` objects can contain both fields.
180
- `pages.create` cannot set them yet.
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.
181
93
 
182
- File-upload references aren't enabled for custom blocks yet. When updating icons or covers, use one
183
- of:
94
+ Icons use `NotionPageIcon` and covers use `NotionPageCover`. Use emoji, external URLs, or existing
95
+ hosted file URLs. File upload references remain unsupported.
184
96
 
185
- - An emoji icon — `{ type: "emoji", emoji: "🟢" }`
186
- - An external URL — `{ type: "external", external: { url: "https://example.com/cover.png" } }`
187
- - An existing hosted file URL returned by Notion — `{ type: "file", file: { url: existingUrl } }`
97
+ ## Types
188
98
 
189
- Do **not** send `{ type: "file_upload", file_upload: { id } }`; the host will reject it.
99
+ - `NotionPage`, `NotionPageId`, and `NotionParent` describe a page and its location.
100
+ - `NotionPagePropertyValue` describes a typed property read.
101
+ - `NotionPagePropertyReadMap` describes the bridge property map.
102
+ - `NotionPagePropertyWriteMap` describes resolved writes with raw IDs.
103
+ - `NotionPagePropertyInputValue` and `NotionPagePropertyInputMap` describe SDK writes.
104
+ - `CreatePageArgs` and `CreatePageParent` describe page creation inputs.
105
+ - `GetPageResult` and `CreatePageResult` contain a successful `NotionPage` or a structured error.
106
+ - `PageUpdatePayload` and `UpdatePageResult` describe page instance updates.
107
+ - `OpenPageOptions`, `OpenPageResult`, and `OpenPageMode` describe navigation.
190
108
 
191
109
  ## Property support
192
110
 
193
111
  This table shows property support for custom blocks running in Notion.
194
112
 
195
- - **Read value** shows `page.properties` in responses from `pages.get`, `pages.create`, and
196
- `pages.update`. Unsupported property types are omitted from `page.properties`.
113
+ - **Read value** shows `page.properties` after reads, creates, and successful instance writes.
114
+ Unsupported properties return `null`.
197
115
  - **Create** covers property writes through `pages.create`.
198
- - **Update** covers property writes through `pages.update`.
116
+ - **Update** covers property writes through `page.updateById`.
199
117
 
200
118
  Pages outside a data source support only `title`. Other property writes require a data source row
201
119
  and a property in its schema.
@@ -205,7 +123,7 @@ and a property in its schema.
205
123
  - 🔴 **Unavailable:** The SDK does not expose this operation.
206
124
  - ⚫ **Not supported in Notion:** Notion itself does not support this operation in the stated case.
207
125
 
208
- | Property type | Read value (`pages.get`) | Create (`pages.create`) | Update (`pages.update`) |
126
+ | Property type | Read value (`pages.get`) | Create (`pages.create`) | Update (`page.updateById`) |
209
127
  | ------------------- | ---------------------------------------------------- | -------------------------------------------- | -------------------------------------------- |
210
128
  | `title` | 🟡 Text array with plain text only | 🟡 Plain text only | 🟡 Plain text only |
211
129
  | `rich_text` | 🟡 Text array with plain text only | 🟡 Plain text only | 🟡 Plain text only |
@@ -251,31 +169,9 @@ and a property in its schema.
251
169
  properties.
252
170
  - Formula, rollup, unique ID, and other omitted types do not return structured page values.
253
171
 
254
- For flattened query values, filters, and sorts, see
172
+ For query properties, filters, and sorts, see
255
173
  [Data source property support](./data-sources.md#property-support).
256
174
 
257
- ## Types
258
-
259
- - `NotionPage` — the page record returned by every successful call.
260
- - `NotionPageId` — branded string ID for a page.
261
- - `NotionPageIcon` / `NotionPageCover` — icon and cover variants Notion supports.
262
- - `NotionParent` — a block's parent reference (`page_id` / `block_id` / `data_source_id` /
263
- `workspace` / `agent_id` / `unavailable`), as returned on `NotionPage.parent` and by
264
- `useParent()`.
265
- - `NotionPagePropertyValue` — a single property value as returned on a `NotionPage`.
266
- - `NotionPagePropertyInputValue` — a single property value as accepted by `pages.create` /
267
- `pages.update` (may omit `id` for `create`).
268
- - `NotionPagePropertyInputMap` — input map for `pages.create` (keys can be raw property IDs, or
269
- semantic keys when creating into a mapped data source).
270
- - `NotionPagePropertyReadMap`: maps property IDs to returned property values.
271
- - `NotionPagePropertyWriteMap` — input map for `pages.update` (raw property IDs only).
272
- - `NotionCreatePagePosition` — `start` / `end` / `before` / `after` insertion variants.
273
- - `OpenPageMode` — `"side_peek"` / `"center_peek"`.
274
- - `OpenPageOptions` / `OpenPageResult`.
275
- - `CreatePageArgs` / `CreatePageParent` / `CreatePageResult`.
276
- - `GetPageResult`.
277
- - `UpdatePageArgs` / `UpdatePageResult`.
278
-
279
175
  ## Opening pages
280
176
 
281
177
  `pages.open(pageId, { mode })` asks Notion to open a readable page using host-owned navigation:
@@ -307,3 +203,38 @@ be missing on older hosts.
307
203
  An empty Files value returns `[]`. If the host can't resolve an entry (for example, you can't read
308
204
  the referenced file, or it points to a folder), `pages.get` fails with an error instead of returning
309
205
  a partial list.
206
+
207
+ ## Mutation results
208
+
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.
212
+
213
+ ## Namespace methods
214
+
215
+ | Method | Successful return |
216
+ | ---------------------------------- | -------------------------------------------------------------------- |
217
+ | `pages.get(pageId)` | `{ status: "success", page }` |
218
+ | `pages.create(args)` | `{ status: "success", page }` |
219
+ | `pages.open(pageId, options)` | Existing `OpenPageResult` |
220
+ | `dataSources.query(key, options?)` | `{ status: "success", items, propertyIdsByKey, isLoading, hasMore }` |
221
+ | `dataSources.getPage(key, pageId)` | `{ status: "success", page, propertyIdsByKey }` |
222
+ | `dataSources.create(key, payload)` | `{ status: "success", page, propertyIdsByKey }` |
223
+
224
+ The Pages namespace exposes `get`, `create`, and `open`. Use page instance methods for all writes
225
+ and archive changes.
226
+
227
+ ```ts
228
+ const fetched = await pages.get(pageId)
229
+ if (fetched.status === "success") {
230
+ const page = fetched.page
231
+ console.log(page.properties.Score)
232
+ const result = await page.updateById({
233
+ properties: { "score-id": { type: "number", number: 10 } },
234
+ })
235
+ if (result.status === "success") console.log(page.properties.Score)
236
+ }
237
+ ```
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.2.24",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "publishConfig": {