@notionhq/custom-blocks 0.3.2 → 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.
- package/README.md +2 -8
- package/dist/bridge/SandboxBridge.d.ts.map +1 -1
- package/dist/bridge/SandboxBridge.js +12 -32
- package/dist/bridge/dataSources/resolveProperty.d.ts +1 -20
- package/dist/bridge/dataSources/resolveProperty.d.ts.map +1 -1
- package/dist/bridge/dataSources/resolveProperty.js +37 -32
- package/dist/bridge/page.d.ts +6 -0
- package/dist/bridge/page.d.ts.map +1 -1
- package/dist/bridge/page.js +17 -0
- package/dist/bridge/sandboxClient.d.ts.map +1 -1
- package/dist/bridge/sandboxClient.js +5 -2
- package/dist/protocol/protocolVersion.d.ts +2 -1
- package/dist/protocol/protocolVersion.js +3 -1
- package/dist/version.js +1 -1
- package/docs/build-tools.md +55 -0
- package/docs/data-sources.md +3 -1
- package/docs/deployment.md +16 -34
- package/docs/errors.md +65 -52
- package/docs/pages.md +79 -75
- package/package.json +2 -18
- package/src/bridge/SandboxBridge.ts +14 -39
- package/src/bridge/dataSources/resolveProperty.ts +48 -39
- package/src/bridge/page.ts +24 -0
- package/src/bridge/sandboxClient.ts +6 -3
package/docs/errors.md
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
# Error handling
|
|
2
2
|
|
|
3
|
-
Pages
|
|
4
|
-
Success returns `{ status: "success",
|
|
5
|
-
`{ status: "error", error }`.
|
|
6
|
-
|
|
7
|
-
Page instance mutations, navigation, and Users methods also retain their result envelopes.
|
|
8
|
-
Initialization still rejects on failure. Hooks and subscriptions keep their existing error behavior.
|
|
3
|
+
Pages, Users, navigation, and asynchronous Data sources methods return a result with a `status`
|
|
4
|
+
field. Success returns `{ status: "success", ... }` with the requested data. Failure returns
|
|
5
|
+
`{ status: "error", error }`. Initialization rejects on failure. Hooks and subscriptions report
|
|
6
|
+
errors separately.
|
|
9
7
|
|
|
10
8
|
## Error format
|
|
11
9
|
|
|
@@ -19,75 +17,90 @@ type CustomBlockErrorInfo<TCode extends string = string> = {
|
|
|
19
17
|
}
|
|
20
18
|
```
|
|
21
19
|
|
|
22
|
-
- `code` is a stable identifier
|
|
23
|
-
- `message` provides
|
|
24
|
-
- `isRetryable` indicates whether
|
|
25
|
-
request or
|
|
26
|
-
|
|
20
|
+
- `code` is a stable identifier for application logic.
|
|
21
|
+
- `message` provides details for developers. Its text can change.
|
|
22
|
+
- `isRetryable` indicates whether the operation could succeed if repeated without changes to the
|
|
23
|
+
request or configuration. A `false` value does not rule out success after you fix the query,
|
|
24
|
+
schema, or bindings.
|
|
27
25
|
|
|
28
|
-
Each API
|
|
29
|
-
|
|
26
|
+
Each API defines known `code` values for autocomplete and accepts other strings. This lets older
|
|
27
|
+
SDKs receive new codes from newer hosts.
|
|
30
28
|
|
|
31
29
|
## API-specific error types
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
31
|
+
Each operation has an error code type and an error info type:
|
|
32
|
+
|
|
33
|
+
| Operation | Code type | Info type |
|
|
34
|
+
| ------------------- | ------------------------------------------ | ------------------------------------------ |
|
|
35
|
+
| Create page | `CustomBlockCreatePageErrorCode` | `CustomBlockCreatePageErrorInfo` |
|
|
36
|
+
| Get page | `CustomBlockGetPageErrorCode` | `CustomBlockGetPageErrorInfo` |
|
|
37
|
+
| Open page | `CustomBlockOpenPageErrorCode` | `CustomBlockOpenPageErrorInfo` |
|
|
38
|
+
| Update page | `CustomBlockUpdatePageErrorCode` | `CustomBlockUpdatePageErrorInfo` |
|
|
39
|
+
| Get user | `CustomBlockGetUserErrorCode` | `CustomBlockGetUserErrorInfo` |
|
|
40
|
+
| List users | `CustomBlockListUsersErrorCode` | `CustomBlockListUsersErrorInfo` |
|
|
41
|
+
| Open external URL | `CustomBlockOpenExternalUrlErrorCode` | `CustomBlockOpenExternalUrlErrorInfo` |
|
|
42
|
+
| Query data source | `CustomBlockQueryDataSourceErrorCode` | `CustomBlockQueryDataSourceErrorInfo` |
|
|
43
|
+
| Resolve property | `CustomBlockPropertyErrorCode` | `CustomBlockPropertyErrorInfo` |
|
|
44
|
+
| Resolve data source | `CustomBlockDataSourceResolutionErrorCode` | `CustomBlockDataSourceResolutionErrorInfo` |
|
|
45
|
+
|
|
46
|
+
## Local page update errors
|
|
47
|
+
|
|
48
|
+
The SDK validates all properties before sending an update. These local errors have
|
|
49
|
+
`isRetryable: false`:
|
|
50
|
+
|
|
51
|
+
| Code | Cause |
|
|
52
|
+
| ------------------------ | ------------------------------------------------------------------------------ |
|
|
53
|
+
| `invalid_page_update` | No properties, icon, cover, or archive change. |
|
|
54
|
+
| `invalid_property_value` | Malformed property map or value. |
|
|
55
|
+
| `unmapped_property` | Missing data source context, an invalid or unbound key, or a missing property. |
|
|
56
|
+
| `property_id_mismatch` | A supplied value ID differs from the resolved raw property ID. |
|
|
57
|
+
| `duplicate_property` | Multiple keys resolve to the same raw property ID. |
|
|
43
58
|
|
|
44
59
|
## Data source errors
|
|
45
60
|
|
|
46
|
-
- `data_source_schema_unavailable`:
|
|
47
|
-
|
|
48
|
-
|
|
61
|
+
- `data_source_schema_unavailable`: A query uses filters or sorts, but the host did not provide the
|
|
62
|
+
database schema. Both the SDK and the generic host use this code. Queries without filters or sorts
|
|
63
|
+
can run without a schema.
|
|
49
64
|
- `unmapped_data_source`: The data source has no database binding.
|
|
50
65
|
- `invalid_data_source_query_filter`: The host rejects a filter because its property ID or condition
|
|
51
66
|
does not match the database schema.
|
|
52
67
|
- `invalid_data_source_query_sort`: The host rejects a sort, for example, because its property ID is
|
|
53
68
|
unknown, its property type is unsupported, or the query repeats a sort property.
|
|
54
69
|
|
|
55
|
-
`useDataSource`
|
|
56
|
-
|
|
57
|
-
|
|
70
|
+
On query failure, `useDataSource` sets `error` and `isLoading: false`, keeping the last successful
|
|
71
|
+
rows for that query. A later snapshot clears the error. Framework-neutral subscriptions report
|
|
72
|
+
failures through their optional error callback.
|
|
58
73
|
|
|
59
|
-
|
|
60
|
-
|
|
74
|
+
When the host supplies a changed schema or binding, data source subscriptions repeat the query
|
|
75
|
+
regardless of `isRetryable`.
|
|
61
76
|
|
|
62
77
|
## Initialization errors
|
|
63
78
|
|
|
64
|
-
`initCustomBlock` rejects with `CustomBlockInitializationError`. Its
|
|
65
|
-
`CustomBlockInitializationErrorCode
|
|
66
|
-
|
|
79
|
+
`initCustomBlock` rejects with `CustomBlockInitializationError`. Its
|
|
80
|
+
`CustomBlockInitializationErrorCode` covers handshake failures from the SDK or host, plus local
|
|
81
|
+
iframe errors.
|
|
67
82
|
|
|
68
|
-
`CustomBlockInitializationErrorInfo`
|
|
83
|
+
`CustomBlockInitializationErrorInfo` describes the `{ code, message, isRetryable }` object.
|
|
69
84
|
|
|
70
|
-
|
|
85
|
+
The error code identifies which side failed:
|
|
71
86
|
|
|
72
|
-
- `invalid_connect_payload
|
|
73
|
-
|
|
74
|
-
- `invalid_init_payload
|
|
75
|
-
|
|
76
|
-
- `invalid_init_bindings
|
|
77
|
-
|
|
78
|
-
- `invalid_init_result_payload
|
|
79
|
-
|
|
80
|
-
- `initial_state_timeout
|
|
81
|
-
|
|
82
|
-
- `no_init_result` means that the host did not receive a valid `initResult` before its timeout.
|
|
87
|
+
- `invalid_connect_payload`: The host could not parse the SDK's `connect` message. The host sends
|
|
88
|
+
this code in `init.error` when it can read the `initializationId`.
|
|
89
|
+
- `invalid_init_payload`: The SDK could not parse the host's `init` message. The SDK sends this code
|
|
90
|
+
in `initResult.error` when it can read the expected `initializationId`.
|
|
91
|
+
- `invalid_init_bindings`: The SDK could not apply the data source bindings in the host's valid
|
|
92
|
+
`init` message. The SDK sends this code in `initResult.error`.
|
|
93
|
+
- `invalid_init_result_payload`: The host could not parse the SDK's `initResult`. Only the host
|
|
94
|
+
reports this error.
|
|
95
|
+
- `initial_state_timeout`: The host could not prepare the initial state before its timeout.
|
|
96
|
+
- `no_init_result`: The host did not receive a valid `initResult` before its timeout.
|
|
83
97
|
|
|
84
98
|
See [Lifecycle and initialization](./lifecycle.md) for initialization-specific error codes.
|
|
85
99
|
|
|
86
100
|
## Handling errors
|
|
87
101
|
|
|
88
|
-
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
- Treat `isRetryable` as
|
|
92
|
-
- Use `message` for developer-facing details, not as a stable identifier.
|
|
102
|
+
- Use `code` for error handling and `message` for developer-facing details. Do not branch on
|
|
103
|
+
`message`.
|
|
104
|
+
- Keep a default branch when switching on `code` to handle codes from newer hosts.
|
|
105
|
+
- Treat `isRetryable` as advice when deciding whether to offer or schedule a retry.
|
|
93
106
|
- `"unknown_error"` means the host encountered an unexpected or unclassified failure.
|
package/docs/pages.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# Pages
|
|
1
|
+
# Pages
|
|
2
2
|
|
|
3
|
-
`pages.get` and `pages.create` return `{ status: "success", page
|
|
4
|
-
|
|
5
|
-
|
|
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.
|
|
18
|
-
|
|
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
|
|
24
|
-
property ID. `getPropertyById`
|
|
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
|
-
|
|
28
|
-
`last_visited_time
|
|
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
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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`
|
|
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
|
|
60
|
-
`
|
|
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
|
-
|
|
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
|
-
|
|
80
|
-
|
|
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
|
-
|
|
89
|
-
|
|
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
|
-
|
|
95
|
-
|
|
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
|
-
|
|
122
|
+
The table below shows property support in Notion:
|
|
112
123
|
|
|
113
|
-
- **Read value** shows `page.properties` after
|
|
114
|
-
|
|
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
|
|
119
|
-
|
|
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
|
|
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
|
|
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
|
| ------------------- | ---------------------------------------------------- | -------------------------------------------- | -------------------------------------------- |
|
|
@@ -154,30 +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
|
|
158
|
-
|
|
159
|
-
|
|
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
|
|
163
|
-
|
|
164
|
-
- `pages.get` resolves Files links and readable file
|
|
165
|
-
source. Create
|
|
166
|
-
existing `file` URLs
|
|
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`
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
string value.
|
|
172
|
-
- These strings contain stored text only. Formulas and rollups are not evaluated. Missing values
|
|
173
|
-
return `""`.
|
|
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 `""`.
|
|
174
181
|
|
|
175
182
|
For query properties, filters, and sorts, see
|
|
176
183
|
[Data source property support](./data-sources.md#property-support).
|
|
177
184
|
|
|
178
185
|
## Opening pages
|
|
179
186
|
|
|
180
|
-
`pages.open(pageId, { mode })` asks Notion to open a
|
|
187
|
+
`pages.open(pageId, { mode })` asks Notion to open a page the viewer can read:
|
|
181
188
|
|
|
182
189
|
```ts
|
|
183
190
|
const result = await pages.open(pageId, { mode: "side_peek" })
|
|
@@ -187,31 +194,31 @@ if (result.status === "error") {
|
|
|
187
194
|
}
|
|
188
195
|
```
|
|
189
196
|
|
|
190
|
-
|
|
191
|
-
|
|
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.
|
|
192
199
|
|
|
193
|
-
Modes are `"side_peek"` and `"center_peek"`. Success means the host
|
|
194
|
-
navigation
|
|
195
|
-
surface
|
|
196
|
-
result contains `{ pageId }
|
|
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
|
|
197
204
|
metadata.
|
|
198
205
|
|
|
199
206
|
### Files reads
|
|
200
207
|
|
|
201
|
-
Each Files entry is either an external link (`type: "external"`) or a
|
|
208
|
+
Each Files entry is either an external link (`type: "external"`) or a file hosted by Notion
|
|
202
209
|
(`type: "file"`), in the order shown in Notion. A hosted file's `file.url` is a signed URL that
|
|
203
210
|
expires at `file.expiry_time`. Call `pages.get` again when you need a fresh URL. `expiry_time` can
|
|
204
211
|
be missing on older hosts.
|
|
205
212
|
|
|
206
|
-
An empty Files value returns `[]`. If the host
|
|
207
|
-
|
|
208
|
-
|
|
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.
|
|
209
216
|
|
|
210
217
|
## Mutation results
|
|
211
218
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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.
|
|
215
222
|
|
|
216
223
|
## Namespace methods
|
|
217
224
|
|
|
@@ -224,8 +231,8 @@ methods refresh the same page object before resolving. Use `updateById` for raw
|
|
|
224
231
|
| `dataSources.getPage(key, pageId)` | `{ status: "success", page, propertyIdsByKey }` |
|
|
225
232
|
| `dataSources.create(key, payload)` | `{ status: "success", page, propertyIdsByKey }` |
|
|
226
233
|
|
|
227
|
-
The Pages namespace exposes `get`, `create`, and `open`. Use page
|
|
228
|
-
and archive
|
|
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:
|
|
229
236
|
|
|
230
237
|
```ts
|
|
231
238
|
const fetched = await pages.get(pageId)
|
|
@@ -238,6 +245,3 @@ if (fetched.status === "success") {
|
|
|
238
245
|
if (result.status === "success") console.log(page.properties.Score)
|
|
239
246
|
}
|
|
240
247
|
```
|
|
241
|
-
|
|
242
|
-
The successful write refreshes this page before the promise resolves. Separately fetched pages
|
|
243
|
-
remain independent objects.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@notionhq/custom-blocks",
|
|
3
|
-
"version": "0.3.
|
|
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.
|
|
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"
|
|
@@ -72,7 +72,7 @@ import {
|
|
|
72
72
|
import type { ManifestLoadResult } from "./loadManifest.js"
|
|
73
73
|
import type { MessageInterceptor } from "./messageInterceptor.js"
|
|
74
74
|
import { notifyListener } from "./notifyListener.js"
|
|
75
|
-
import { toNotionPageFromNotionPageData } from "./page.js"
|
|
75
|
+
import { makeUpdatePageByKey, toNotionPageFromNotionPageData } from "./page.js"
|
|
76
76
|
import { PendingRequests } from "./pendingRequests.js"
|
|
77
77
|
import { SandboxInitialization } from "./SandboxInitialization.js"
|
|
78
78
|
|
|
@@ -783,7 +783,6 @@ export class SandboxBridge {
|
|
|
783
783
|
const resolvedProperties = resolvePropertyWriteMapForDataSource({
|
|
784
784
|
dataSource: propertyDataSource,
|
|
785
785
|
properties: args.properties,
|
|
786
|
-
operationName: "createPage",
|
|
787
786
|
})
|
|
788
787
|
if (resolvedProperties.status === "error") {
|
|
789
788
|
resolve(resolvedProperties)
|
|
@@ -813,8 +812,11 @@ export class SandboxBridge {
|
|
|
813
812
|
this.getPropertyByKey({ page, dataSource, propertyKey }),
|
|
814
813
|
updateById: payload =>
|
|
815
814
|
update({ ...payload, pageId: page.id, dataSource: undefined }),
|
|
816
|
-
updateByKey:
|
|
817
|
-
|
|
815
|
+
updateByKey: makeUpdatePageByKey({
|
|
816
|
+
dataSource,
|
|
817
|
+
update: payload =>
|
|
818
|
+
update({ ...payload, pageId: page.id, dataSource }),
|
|
819
|
+
}),
|
|
818
820
|
})
|
|
819
821
|
resolve({ status: "success", page })
|
|
820
822
|
})
|
|
@@ -863,8 +865,11 @@ export class SandboxBridge {
|
|
|
863
865
|
this.getPropertyByKey({ page, dataSource, propertyKey }),
|
|
864
866
|
updateById: payload =>
|
|
865
867
|
update({ ...payload, pageId: page.id, dataSource: undefined }),
|
|
866
|
-
updateByKey:
|
|
867
|
-
|
|
868
|
+
updateByKey: makeUpdatePageByKey({
|
|
869
|
+
dataSource,
|
|
870
|
+
update: payload =>
|
|
871
|
+
update({ ...payload, pageId: page.id, dataSource }),
|
|
872
|
+
}),
|
|
868
873
|
})
|
|
869
874
|
resolve({ status: "success", page })
|
|
870
875
|
})
|
|
@@ -980,32 +985,9 @@ export class SandboxBridge {
|
|
|
980
985
|
> => {
|
|
981
986
|
const { dataSource } = args
|
|
982
987
|
const dataSourceId = dataSource?.collectionPointer?.id
|
|
983
|
-
const propertyIdsByKey = dataSource?.propertyIdsByKey ?? {}
|
|
984
|
-
if (dataSource !== undefined) {
|
|
985
|
-
for (const propertyKey of Object.keys(args.properties ?? {})) {
|
|
986
|
-
const propertyId = Object.hasOwn(propertyIdsByKey, propertyKey)
|
|
987
|
-
? propertyIdsByKey[propertyKey]
|
|
988
|
-
: undefined
|
|
989
|
-
if (
|
|
990
|
-
propertyId &&
|
|
991
|
-
dataSource?.collectionSchema?.propertiesById[propertyId]
|
|
992
|
-
) {
|
|
993
|
-
continue
|
|
994
|
-
}
|
|
995
|
-
return {
|
|
996
|
-
status: "error",
|
|
997
|
-
error: {
|
|
998
|
-
code: "unmapped_property",
|
|
999
|
-
message: `Cannot resolve property key "${propertyKey}" on this page.`,
|
|
1000
|
-
isRetryable: false,
|
|
1001
|
-
},
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
988
|
const resolved = resolvePropertyWriteMapForDataSource({
|
|
1006
989
|
dataSource,
|
|
1007
|
-
properties: args.properties
|
|
1008
|
-
operationName: "page.update",
|
|
990
|
+
properties: args.properties === undefined ? {} : args.properties,
|
|
1009
991
|
})
|
|
1010
992
|
if (resolved.status === "error") {
|
|
1011
993
|
return resolved
|
|
@@ -1079,15 +1061,8 @@ export class SandboxBridge {
|
|
|
1079
1061
|
switch (parent.type) {
|
|
1080
1062
|
case "page_id":
|
|
1081
1063
|
return { status: "ok", parent, dataSource: undefined }
|
|
1082
|
-
case "data_source_id":
|
|
1083
|
-
|
|
1084
|
-
this.hostState.status === "initialized"
|
|
1085
|
-
? this.hostState.dataSources.find(
|
|
1086
|
-
entry => entry.collectionPointer?.id === parent.data_source_id,
|
|
1087
|
-
)
|
|
1088
|
-
: undefined
|
|
1089
|
-
return { status: "ok", parent, dataSource }
|
|
1090
|
-
}
|
|
1064
|
+
case "data_source_id":
|
|
1065
|
+
return { status: "ok", parent, dataSource: undefined }
|
|
1091
1066
|
case "data_source_key": {
|
|
1092
1067
|
if (this.hostState.status !== "initialized") {
|
|
1093
1068
|
return {
|