@notionhq/custom-blocks 0.2.10 → 0.2.12
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 +73 -69
- package/bin/notion-custom-blocks/cli.js +0 -0
- package/dist/version.js +1 -1
- package/docs/block-location.md +71 -45
- package/docs/data-sources.md +242 -141
- package/docs/deployment.md +30 -20
- package/docs/errors.md +33 -27
- package/docs/lifecycle.md +82 -72
- package/docs/pages.md +137 -81
- package/docs/users.md +65 -51
- package/package.json +65 -65
package/docs/pages.md
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
# `pages` API
|
|
2
2
|
|
|
3
|
-
Helpers for creating, reading, updating, archiving, and opening Notion pages from inside a custom
|
|
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.
|
|
4
5
|
|
|
5
6
|
```ts
|
|
6
|
-
import { pages } from "@notionhq/custom-blocks"
|
|
7
|
+
import { pages } from "@notionhq/custom-blocks"
|
|
7
8
|
```
|
|
8
9
|
|
|
9
|
-
Every helper follows the SDK's [error-handling contract](./errors.md). Check `result.status` before
|
|
10
|
+
Every helper follows the SDK's [error-handling contract](./errors.md). Check `result.status` before
|
|
11
|
+
reading the success payload.
|
|
10
12
|
|
|
11
13
|
## Creating pages
|
|
12
14
|
|
|
13
|
-
`pages.create` accepts a parent, a property map, and an optional position. Setting an icon or cover
|
|
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`.
|
|
14
17
|
|
|
15
18
|
```ts
|
|
16
19
|
const result = await pages.create({
|
|
@@ -22,16 +25,17 @@ const result = await pages.create({
|
|
|
22
25
|
},
|
|
23
26
|
dueDate: { type: "date", date: { start: "2026-06-01" } },
|
|
24
27
|
},
|
|
25
|
-
})
|
|
28
|
+
})
|
|
26
29
|
|
|
27
30
|
if (result.status === "success") {
|
|
28
|
-
console.log("created", result.page.id)
|
|
31
|
+
console.log("created", result.page.id)
|
|
29
32
|
}
|
|
30
33
|
```
|
|
31
34
|
|
|
32
35
|
### Choosing a parent
|
|
33
36
|
|
|
34
|
-
`parent` is the **destination for the new page**. It does not describe the custom block's own
|
|
37
|
+
`parent` is the **destination for the new page**. It does not describe the custom block's own
|
|
38
|
+
location.
|
|
35
39
|
|
|
36
40
|
- A data source parent creates a new row in that data source.
|
|
37
41
|
- A page parent creates a child page (subpage) inside that page.
|
|
@@ -42,62 +46,85 @@ The accepted shape is `CreatePageParent`:
|
|
|
42
46
|
type CreatePageParent =
|
|
43
47
|
| { type: "page_id"; page_id: NotionPageId }
|
|
44
48
|
| { type: "data_source_id"; data_source_id: NotionDataSourceId }
|
|
45
|
-
| { type: "data_source_key"; key: string }
|
|
49
|
+
| { type: "data_source_key"; key: string }
|
|
46
50
|
```
|
|
47
51
|
|
|
48
|
-
Use `data_source_key` for a data source key declared in the custom block's manifest. When the block
|
|
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.
|
|
49
55
|
|
|
50
|
-
Use `page_id` when you want a subpage under a specific page. For example,
|
|
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`.
|
|
51
59
|
|
|
52
|
-
Use `data_source_id` when you already have a raw Notion data source ID. In normal custom-block code,
|
|
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.
|
|
53
63
|
|
|
54
64
|
### Property keys
|
|
55
65
|
|
|
56
|
-
`properties` is a `NotionPagePropertyInputMap`. Two niceties versus the raw API when the create
|
|
66
|
+
`properties` is a `NotionPagePropertyInputMap`. Two niceties versus the raw API when the create
|
|
67
|
+
parent resolves to a configured data source:
|
|
57
68
|
|
|
58
|
-
- **Keys** can be either raw Notion property IDs _or_ the data source property keys you declared in
|
|
59
|
-
|
|
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.
|
|
60
76
|
|
|
61
|
-
So if your manifest declares `title` and `dueDate`, you can write them by name (as in the example
|
|
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.
|
|
62
79
|
|
|
63
80
|
### Where the new page lands
|
|
64
81
|
|
|
65
|
-
For a `page_id` parent, `position` is a `NotionCreatePagePosition` and controls placement inside the
|
|
82
|
+
For a `page_id` parent, `position` is a `NotionCreatePagePosition` and controls placement inside the
|
|
83
|
+
parent:
|
|
66
84
|
|
|
67
85
|
- `{ type: "start" }` / `{ type: "end" }` — prepend or append (default).
|
|
68
|
-
- `{ type: "before", blockId }` / `{ type: "after", blockId }` — insert as a sibling of the given
|
|
86
|
+
- `{ type: "before", blockId }` / `{ type: "after", blockId }` — insert as a sibling of the given
|
|
87
|
+
block (which can be nested anywhere under the parent).
|
|
69
88
|
|
|
70
|
-
Do not pass `position` with a `data_source_id` or `data_source_key` parent. Data source rows are
|
|
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.
|
|
71
91
|
|
|
72
92
|
## Reading pages
|
|
73
93
|
|
|
74
94
|
`pages.get(pageId)` fetches a single page by ID:
|
|
75
95
|
|
|
76
96
|
```ts
|
|
77
|
-
const result = await pages.get(pageId)
|
|
78
|
-
if (result.status === "error") return
|
|
97
|
+
const result = await pages.get(pageId)
|
|
98
|
+
if (result.status === "error") return
|
|
79
99
|
|
|
80
|
-
const page = result.page
|
|
81
|
-
console.log(page.properties)
|
|
100
|
+
const page = result.page // NotionPage
|
|
101
|
+
console.log(page.properties)
|
|
82
102
|
```
|
|
83
103
|
|
|
84
|
-
`page` mirrors Notion's public API shape, with `id`, `parent`, `properties`, optional `icon` /
|
|
104
|
+
`page` mirrors Notion's public API shape, with `id`, `parent`, `properties`, optional `icon` /
|
|
105
|
+
`cover`, etc. — see `NotionPage` in the types list below.
|
|
85
106
|
|
|
86
107
|
### Archive
|
|
87
108
|
|
|
88
|
-
Archiving keeps content you no longer actively use without deleting it.
|
|
109
|
+
Archiving keeps content you no longer actively use without deleting it.
|
|
110
|
+
[Learn about Archive](https://www.notion.com/help/archive-pages).
|
|
89
111
|
|
|
90
|
-
Read `is_archived` on a page or data source row. It is `true` when the page or an ancestor is
|
|
112
|
+
Read `is_archived` on a page or data source row. It is `true` when the page or an ancestor is
|
|
113
|
+
archived.
|
|
91
114
|
|
|
92
115
|
### Trash
|
|
93
116
|
|
|
94
|
-
Pages in Trash are scheduled for permanent deletion. By default, Notion permanently deletes them 30
|
|
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).
|
|
95
121
|
|
|
96
122
|
Read `in_trash` on a page or data source row. It is `true` when the page or an ancestor is in Trash.
|
|
97
123
|
|
|
98
124
|
## Updating pages
|
|
99
125
|
|
|
100
|
-
`pages.update` writes back to a page. The optional fields (`properties`, `icon`, `cover`,
|
|
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:
|
|
101
128
|
|
|
102
129
|
```ts
|
|
103
130
|
const result = await pages.update({
|
|
@@ -110,32 +137,38 @@ const result = await pages.update({
|
|
|
110
137
|
},
|
|
111
138
|
},
|
|
112
139
|
icon: { type: "emoji", emoji: "✅" },
|
|
113
|
-
})
|
|
140
|
+
})
|
|
114
141
|
```
|
|
115
142
|
|
|
116
|
-
The `properties` map (a `NotionPagePropertyWriteMap`) is keyed by **raw Notion property ID**, and
|
|
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.
|
|
117
148
|
|
|
118
|
-
If you call `pages.update` with no fields to change, it short-circuits and resolves with
|
|
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.
|
|
119
152
|
|
|
120
153
|
## Archiving and unarchiving pages
|
|
121
154
|
|
|
122
155
|
`pages.archive(pageId)` archives a page. `pages.unarchive(pageId)` removes its archive state.
|
|
123
156
|
|
|
124
157
|
```ts
|
|
125
|
-
const result = await pages.archive(pageId)
|
|
158
|
+
const result = await pages.archive(pageId)
|
|
126
159
|
if (result.status === "error") {
|
|
127
|
-
console.error(result.error.message)
|
|
160
|
+
console.error(result.error.message)
|
|
128
161
|
}
|
|
129
162
|
|
|
130
|
-
const restored = await pages.unarchive(pageId)
|
|
163
|
+
const restored = await pages.unarchive(pageId)
|
|
131
164
|
if (restored.status === "error") {
|
|
132
|
-
console.error(restored.error.message)
|
|
165
|
+
console.error(restored.error.message)
|
|
133
166
|
}
|
|
134
167
|
```
|
|
135
168
|
|
|
136
|
-
`pages.archive` is shorthand for calling `pages.update` with `is_archived: true`.
|
|
137
|
-
|
|
138
|
-
|
|
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`.
|
|
139
172
|
|
|
140
173
|
## Trashing and untrashing pages
|
|
141
174
|
|
|
@@ -143,9 +176,11 @@ This is not currently supported by the custom blocks SDK.
|
|
|
143
176
|
|
|
144
177
|
## Icons, covers, and file uploads
|
|
145
178
|
|
|
146
|
-
`pages.update` can set icons and covers, and returned `NotionPage` objects can contain both fields.
|
|
179
|
+
`pages.update` can set icons and covers, and returned `NotionPage` objects can contain both fields.
|
|
180
|
+
`pages.create` cannot set them yet.
|
|
147
181
|
|
|
148
|
-
File-upload references aren't enabled for custom blocks yet. When updating icons or covers, use one
|
|
182
|
+
File-upload references aren't enabled for custom blocks yet. When updating icons or covers, use one
|
|
183
|
+
of:
|
|
149
184
|
|
|
150
185
|
- An emoji icon — `{ type: "emoji", emoji: "🟢" }`
|
|
151
186
|
- An external URL — `{ type: "external", external: { url: "https://example.com/cover.png" } }`
|
|
@@ -157,66 +192,82 @@ Do **not** send `{ type: "file_upload", file_upload: { id } }`; the host will re
|
|
|
157
192
|
|
|
158
193
|
This table shows property support for custom blocks running in Notion.
|
|
159
194
|
|
|
160
|
-
- **Read value** shows `page.properties` in responses from `pages.get`, `pages.create`, and
|
|
195
|
+
- **Read value** shows `page.properties` in responses from `pages.get`, `pages.create`, and
|
|
196
|
+
`pages.update`. The table identifies top-level fields separately. Unavailable values are absent
|
|
197
|
+
from `page.properties`.
|
|
161
198
|
- **Create** covers property writes through `pages.create`.
|
|
162
199
|
- **Update** covers property writes through `pages.update`.
|
|
163
200
|
|
|
164
|
-
Pages outside a data source support only `title`. Other property writes require a data source row
|
|
201
|
+
Pages outside a data source support only `title`. Other property writes require a data source row
|
|
202
|
+
and a property in its schema.
|
|
165
203
|
|
|
166
204
|
- 🟢 **Supported:** The SDK supports this operation in Notion.
|
|
167
205
|
- 🟡 **Limited:** The SDK supports this operation with the restrictions shown in the cell.
|
|
168
206
|
- 🔴 **Unavailable:** The SDK does not expose this operation.
|
|
169
207
|
- ⚫ **Not supported in Notion:** Notion itself does not support this operation in the stated case.
|
|
170
208
|
|
|
171
|
-
| Property type
|
|
172
|
-
|
|
|
173
|
-
| `title`
|
|
174
|
-
| `rich_text`
|
|
175
|
-
| `number`
|
|
176
|
-
| `checkbox`
|
|
177
|
-
| `url`
|
|
178
|
-
| `email`
|
|
179
|
-
| `phone_number`
|
|
180
|
-
| `select`
|
|
181
|
-
| `multi_select`
|
|
182
|
-
| `status`
|
|
183
|
-
| `date`
|
|
184
|
-
| `people`
|
|
185
|
-
| `files`
|
|
186
|
-
| `unique_id`
|
|
187
|
-
| `relation`
|
|
188
|
-
| `place`
|
|
189
|
-
| `formula`
|
|
190
|
-
| `rollup`
|
|
191
|
-
| `button`
|
|
192
|
-
| `verification`
|
|
193
|
-
| `last_visited_time` | 🔴 Unavailable
|
|
194
|
-
| `location`
|
|
195
|
-
| `created_time`
|
|
196
|
-
| `last_edited_time`
|
|
197
|
-
| `created_by`
|
|
198
|
-
| `last_edited_by`
|
|
209
|
+
| Property type | Read value (`pages.get`) | Create (`pages.create`) | Update (`pages.update`) |
|
|
210
|
+
| ------------------- | ---------------------------------------------------- | -------------------------------------------- | -------------------------------------------- |
|
|
211
|
+
| `title` | 🟡 Text array with plain text only | 🟡 Plain text only | 🟡 Plain text only |
|
|
212
|
+
| `rich_text` | 🟡 Text array with plain text only | 🟡 Plain text only | 🟡 Plain text only |
|
|
213
|
+
| `number` | 🟢 Number or `null` | 🟢 Supported | 🟢 Supported |
|
|
214
|
+
| `checkbox` | 🟢 Boolean | 🟢 Supported | 🟢 Supported |
|
|
215
|
+
| `url` | 🟢 String or `null` | 🟢 Supported | 🟢 Supported |
|
|
216
|
+
| `email` | 🟢 String or `null` | 🟢 Supported | 🟢 Supported |
|
|
217
|
+
| `phone_number` | 🟢 String or `null` | 🟢 Supported | 🟢 Supported |
|
|
218
|
+
| `select` | 🟡 Option ID/name object or `null` | 🟡 Set value only | 🟡 Set value only |
|
|
219
|
+
| `multi_select` | 🟡 Array of option ID/name objects | 🟡 Set value only | 🟡 Set value only |
|
|
220
|
+
| `status` | 🟡 Option ID/name object or `null` | 🟡 Set value only | 🟡 Set value only |
|
|
221
|
+
| `date` | 🟢 `start` and optional `end` strings, or `null` | 🟡 Minute precision | 🟡 Minute precision |
|
|
222
|
+
| `people` | 🟢 Array of user/group ID objects | 🟢 Supported | 🟢 Supported |
|
|
223
|
+
| `files` | 🟡 External file links only | 🟡 File URLs only | 🟡 File URLs only |
|
|
224
|
+
| `unique_id` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
225
|
+
| `relation` | 🟢 Array of page ID objects | 🟡 Replaces value. Rejects `has_more: true`. | 🟡 Replaces value. Rejects `has_more: true`. |
|
|
226
|
+
| `place` | 🟢 Coordinates and optional place details, or `null` | 🟢 Supported | 🟢 Supported |
|
|
227
|
+
| `formula` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
228
|
+
| `rollup` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
229
|
+
| `button` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
230
|
+
| `verification` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
231
|
+
| `last_visited_time` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
232
|
+
| `location` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
233
|
+
| `created_time` | 🟡 Top-level ISO timestamp only | 🔴 Unavailable | 🔴 Unavailable |
|
|
234
|
+
| `last_edited_time` | 🟡 Top-level ISO timestamp only | 🔴 Unavailable | 🔴 Unavailable |
|
|
235
|
+
| `created_by` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
236
|
+
| `last_edited_by` | 🔴 Unavailable | 🔴 Unavailable | 🔴 Unavailable |
|
|
199
237
|
|
|
200
238
|
### Notes
|
|
201
239
|
|
|
202
|
-
- Select, multi-select, and status reads return option names. They also return IDs when the options
|
|
240
|
+
- Select, multi-select, and status reads return option names. They also return IDs when the options
|
|
241
|
+
match the schema, but they omit colors. Writes accept an existing option ID or a name and set the
|
|
242
|
+
selected value. They do not edit option definitions or colors. If you supply both an ID and a
|
|
243
|
+
name, the host uses the ID. The host rejects unknown IDs.
|
|
203
244
|
- Date-time writes preserve hours and minutes but discard seconds and milliseconds.
|
|
204
|
-
- Title and rich text use text arrays, but reads and writes preserve only plain text. Reads and
|
|
205
|
-
|
|
245
|
+
- Title and rich text use text arrays, but reads and writes preserve only plain text. Reads and
|
|
246
|
+
writes do not preserve formatting, links, mentions, or equations as structured rich text.
|
|
247
|
+
- File reads include link items only. File writes accept `external` URLs and existing `file` URLs
|
|
248
|
+
and store them as links. Upload references (`file_upload`) are unsupported.
|
|
206
249
|
- Relation writes replace the value and reject `has_more: true`.
|
|
207
|
-
- `created_time` and `last_edited_time` are read-only top-level fields on `page`, when available.
|
|
250
|
+
- `created_time` and `last_edited_time` are read-only top-level fields on `page`, when available.
|
|
251
|
+
The host omits them from `page.properties`. The host does not return `created_by` or
|
|
252
|
+
`last_edited_by`, even as top-level fields. The host does not return structured page values for
|
|
253
|
+
formula, rollup, unique ID, or the other omitted types.
|
|
208
254
|
|
|
209
|
-
For flattened query values, filters, and sorts, see
|
|
255
|
+
For flattened query values, filters, and sorts, see
|
|
256
|
+
[Data source property support](./data-sources.md#property-support).
|
|
210
257
|
|
|
211
258
|
## Types
|
|
212
259
|
|
|
213
260
|
- `NotionPage` — the page record returned by every successful call.
|
|
214
261
|
- `NotionPageId` — branded string ID for a page.
|
|
215
262
|
- `NotionPageIcon` / `NotionPageCover` — icon and cover variants Notion supports.
|
|
216
|
-
- `NotionParent` — a block's parent reference (`page_id` / `block_id` / `data_source_id` /
|
|
263
|
+
- `NotionParent` — a block's parent reference (`page_id` / `block_id` / `data_source_id` /
|
|
264
|
+
`workspace` / `agent_id` / `unavailable`), as returned on `NotionPage.parent` and by
|
|
265
|
+
`useParent()`.
|
|
217
266
|
- `NotionPagePropertyValue` — a single property value as returned on a `NotionPage`.
|
|
218
|
-
- `NotionPagePropertyInputValue` — a single property value as accepted by `pages.create` /
|
|
219
|
-
|
|
267
|
+
- `NotionPagePropertyInputValue` — a single property value as accepted by `pages.create` /
|
|
268
|
+
`pages.update` (may omit `id` for `create`).
|
|
269
|
+
- `NotionPagePropertyInputMap` — input map for `pages.create` (keys can be raw property IDs, or
|
|
270
|
+
semantic keys when creating into a mapped data source).
|
|
220
271
|
- `NotionPagePropertyWriteMap` — input map for `pages.update` (raw property IDs only).
|
|
221
272
|
- `NotionCreatePagePosition` — `start` / `end` / `before` / `after` insertion variants.
|
|
222
273
|
- `OpenPageMode` — `"side_peek"` / `"center_peek"`.
|
|
@@ -230,13 +281,18 @@ For flattened query values, filters, and sorts, see [Data source property suppor
|
|
|
230
281
|
`pages.open(pageId, { mode })` asks Notion to open a readable page using host-owned navigation:
|
|
231
282
|
|
|
232
283
|
```ts
|
|
233
|
-
const result = await pages.open(pageId, { mode: "side_peek" })
|
|
284
|
+
const result = await pages.open(pageId, { mode: "side_peek" })
|
|
234
285
|
|
|
235
286
|
if (result.status === "error") {
|
|
236
|
-
console.warn(result.error.code, result.error.message)
|
|
287
|
+
console.warn(result.error.code, result.error.message)
|
|
237
288
|
}
|
|
238
289
|
```
|
|
239
290
|
|
|
240
|
-
When the block is inside a peek, opening another page in that peek replaces it and unmounts the
|
|
291
|
+
When the block is inside a peek, opening another page in that peek replaces it and unmounts the
|
|
292
|
+
block, losing its local state. The request result may not be observable after the block unmounts.
|
|
241
293
|
|
|
242
|
-
Modes are `"side_peek"` and `"center_peek"`. Success means the host handed the request to Notion
|
|
294
|
+
Modes are `"side_peek"` and `"center_peek"`. Success means the host handed the request to Notion
|
|
295
|
+
navigation; it does not wait for route-leave confirmation or for the page to finish loading. A
|
|
296
|
+
surface that cannot host the requested peek may fall back to full-page navigation. The success
|
|
297
|
+
result contains `{ pageId }` only — call `pages.get(pageId)` separately when you need readable page
|
|
298
|
+
metadata.
|
package/docs/users.md
CHANGED
|
@@ -3,26 +3,25 @@
|
|
|
3
3
|
Read the current user, fetch a user by ID, or list workspace users from inside a custom block.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
initCustomBlock,
|
|
9
|
-
users,
|
|
10
|
-
type NotionUserId,
|
|
11
|
-
} from "@notionhq/custom-blocks";
|
|
12
|
-
import { useCurrentUser } from "@notionhq/custom-blocks/react";
|
|
6
|
+
import { customBlock, initCustomBlock, users, type NotionUserId } from "@notionhq/custom-blocks"
|
|
7
|
+
import { useCurrentUser } from "@notionhq/custom-blocks/react"
|
|
13
8
|
```
|
|
14
9
|
|
|
15
|
-
`users.get()` and `users.list()` follow the SDK's [error-handling contract](./errors.md). Check
|
|
10
|
+
`users.get()` and `users.list()` follow the SDK's [error-handling contract](./errors.md). Check
|
|
11
|
+
`result.status` before reading `result.user` or `result.list`.
|
|
16
12
|
|
|
17
13
|
## Reading the current user
|
|
18
14
|
|
|
19
|
-
`customBlock.getCurrentUser()` and `useCurrentUser()` return the user viewing the custom block. Both
|
|
15
|
+
`customBlock.getCurrentUser()` and `useCurrentUser()` return the user viewing the custom block. Both
|
|
16
|
+
read the user from the block’s state without a separate request.
|
|
20
17
|
|
|
21
|
-
Wait for `initCustomBlock()` to resolve before calling the getter or mounting components that use
|
|
18
|
+
Wait for `initCustomBlock()` to resolve before calling the getter or mounting components that use
|
|
19
|
+
the hook. See [initialization](./lifecycle.md).
|
|
22
20
|
|
|
23
21
|
### TypeScript API
|
|
24
22
|
|
|
25
|
-
Use `customBlock.getCurrentUser()` to read the current user in any framework. Subscribe to context
|
|
23
|
+
Use `customBlock.getCurrentUser()` to read the current user in any framework. Subscribe to context
|
|
24
|
+
changes to keep your UI updated:
|
|
26
25
|
|
|
27
26
|
```typescript
|
|
28
27
|
import { customBlock, initCustomBlock } from "@notionhq/custom-blocks"
|
|
@@ -32,12 +31,13 @@ const user = customBlock.getCurrentUser()
|
|
|
32
31
|
console.log(user.name)
|
|
33
32
|
|
|
34
33
|
const unsubscribe = customBlock.subscribeToCurrentUser(nextUser => {
|
|
35
|
-
|
|
34
|
+
console.log(nextUser.name)
|
|
36
35
|
})
|
|
37
36
|
```
|
|
38
37
|
|
|
39
|
-
Call `unsubscribe()` when you no longer need updates. The callback receives the initial profile and
|
|
40
|
-
See [context subscriptions](./block-location.md#reading-values-and-following-changes)
|
|
38
|
+
Call `unsubscribe()` when you no longer need updates. The callback receives the initial profile and
|
|
39
|
+
later changes. See [context subscriptions](./block-location.md#reading-values-and-following-changes)
|
|
40
|
+
for initialization and equality rules.
|
|
41
41
|
|
|
42
42
|
### React hook
|
|
43
43
|
|
|
@@ -47,8 +47,8 @@ In React, use `useCurrentUser()` to re-render when the current user changes:
|
|
|
47
47
|
import { useCurrentUser } from "@notionhq/custom-blocks/react"
|
|
48
48
|
|
|
49
49
|
function Greeting() {
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const user = useCurrentUser()
|
|
51
|
+
return <p>Hello, {user.name ?? "there"}</p>
|
|
52
52
|
}
|
|
53
53
|
```
|
|
54
54
|
|
|
@@ -56,66 +56,72 @@ Both approaches return the same `NotionUser` shape.
|
|
|
56
56
|
|
|
57
57
|
## User object
|
|
58
58
|
|
|
59
|
-
`NotionUser` is based on the [public API User object](https://developers.notion.com/reference/user).
|
|
59
|
+
`NotionUser` is based on the [public API User object](https://developers.notion.com/reference/user).
|
|
60
|
+
`users.get()` and the current-user APIs return a `NotionUser`. `users.list()` returns a page of
|
|
61
|
+
these objects. Custom blocks return only people, not bots or integrations.
|
|
60
62
|
|
|
61
|
-
All fields except `name` are always present. `name` may be omitted. `avatar_url` is present but can
|
|
63
|
+
All fields except `name` are always present. `name` may be omitted. `avatar_url` is present but can
|
|
64
|
+
be `null`.
|
|
62
65
|
|
|
63
|
-
| Property
|
|
64
|
-
|
|
|
65
|
-
| `object`
|
|
66
|
-
| `id`
|
|
67
|
-
| `name`
|
|
68
|
-
| `avatar_url`
|
|
69
|
-
| `type`
|
|
70
|
-
| `person`
|
|
71
|
-
| `person.email` | `string`
|
|
66
|
+
| Property | Type | Description |
|
|
67
|
+
| -------------- | ------------------- | ----------------------------------------------------------- |
|
|
68
|
+
| `object` | `"user"` | Identifies a user object. |
|
|
69
|
+
| `id` | `string` (UUID) | The user's ID. Typed as `NotionUserId` in the SDK. |
|
|
70
|
+
| `name` | `string` (optional) | The user's name, if available. |
|
|
71
|
+
| `avatar_url` | `string` (nullable) | The user's avatar URL, or `null` if no avatar is available. |
|
|
72
|
+
| `type` | `"person"` | Custom blocks return people. |
|
|
73
|
+
| `person` | `object` | The user's person details. |
|
|
74
|
+
| `person.email` | `string` | The user's email address. |
|
|
72
75
|
|
|
73
76
|
## Listing users
|
|
74
77
|
|
|
75
|
-
Each `users.list()` call returns one page of workspace users visible to the viewing user. The SDK
|
|
78
|
+
Each `users.list()` call returns one page of workspace users visible to the viewing user. The SDK
|
|
79
|
+
does not fetch the remaining pages automatically.
|
|
76
80
|
|
|
77
81
|
```ts
|
|
78
|
-
const result = await users.list({ pageSize: 50 })
|
|
82
|
+
const result = await users.list({ pageSize: 50 })
|
|
79
83
|
if (result.status === "success") {
|
|
80
84
|
for (const user of result.list.results) {
|
|
81
|
-
console.log(user.id, user.name, user.person.email)
|
|
85
|
+
console.log(user.id, user.name, user.person.email)
|
|
82
86
|
}
|
|
83
87
|
} else {
|
|
84
|
-
console.error(result.error)
|
|
88
|
+
console.error(result.error)
|
|
85
89
|
}
|
|
86
90
|
```
|
|
87
91
|
|
|
88
92
|
`ListUsersArgs` accepts the following options:
|
|
89
93
|
|
|
90
|
-
| Option
|
|
91
|
-
|
|
|
92
|
-
| `pageSize`
|
|
94
|
+
| Option | Description |
|
|
95
|
+
| ------------- | --------------------------------------------------------------------------------------- |
|
|
96
|
+
| `pageSize` | Maximum users per response. Optional integer from 1 to 100. Defaults to 100. |
|
|
93
97
|
| `startCursor` | Optional cursor from the previous response's `next_cursor`. Omit it for the first page. |
|
|
94
98
|
|
|
95
|
-
`users.list()` returns a promise that resolves to a `ListUsersResult`: either
|
|
99
|
+
`users.list()` returns a promise that resolves to a `ListUsersResult`: either
|
|
100
|
+
`{ status: "success", list }` or `{ status: "error", error }`.
|
|
96
101
|
|
|
97
102
|
On success, `list` is a `NotionUserList` with `results`, `next_cursor`, and `has_more`.
|
|
98
103
|
|
|
99
|
-
Pass `next_cursor` as `startCursor` to request the next page. Use the cursor exactly as returned. Do
|
|
104
|
+
Pass `next_cursor` as `startCursor` to request the next page. Use the cursor exactly as returned. Do
|
|
105
|
+
not parse it or create your own. Stop when `has_more` is false.
|
|
100
106
|
|
|
101
107
|
```ts
|
|
102
|
-
import { users, type ListUsersArgs } from "@notionhq/custom-blocks"
|
|
108
|
+
import { users, type ListUsersArgs } from "@notionhq/custom-blocks"
|
|
103
109
|
|
|
104
|
-
const args: ListUsersArgs = { pageSize: 50 }
|
|
110
|
+
const args: ListUsersArgs = { pageSize: 50 }
|
|
105
111
|
|
|
106
112
|
while (true) {
|
|
107
|
-
const result = await users.list(args)
|
|
113
|
+
const result = await users.list(args)
|
|
108
114
|
if (result.status === "error") {
|
|
109
|
-
console.error(result.error)
|
|
110
|
-
break
|
|
115
|
+
console.error(result.error)
|
|
116
|
+
break
|
|
111
117
|
}
|
|
112
118
|
|
|
113
119
|
for (const user of result.list.results) {
|
|
114
|
-
console.log(user.id, user.name)
|
|
120
|
+
console.log(user.id, user.name)
|
|
115
121
|
}
|
|
116
122
|
|
|
117
|
-
if (!result.list.has_more || result.list.next_cursor === null) break
|
|
118
|
-
args.startCursor = result.list.next_cursor
|
|
123
|
+
if (!result.list.has_more || result.list.next_cursor === null) break
|
|
124
|
+
args.startCursor = result.list.next_cursor
|
|
119
125
|
}
|
|
120
126
|
```
|
|
121
127
|
|
|
@@ -124,23 +130,31 @@ while (true) {
|
|
|
124
130
|
`users.get(userId)` fetches one `NotionUser` by `NotionUserId`:
|
|
125
131
|
|
|
126
132
|
```ts
|
|
127
|
-
const result = await users.get(userId)
|
|
133
|
+
const result = await users.get(userId)
|
|
128
134
|
if (result.status === "success") {
|
|
129
|
-
console.log(result.user.name)
|
|
135
|
+
console.log(result.user.name)
|
|
130
136
|
}
|
|
131
137
|
```
|
|
132
138
|
|
|
133
|
-
`users.get()` returns a promise that resolves to a `GetUserResult`: either
|
|
139
|
+
`users.get()` returns a promise that resolves to a `GetUserResult`: either
|
|
140
|
+
`{ status: "success", user }` or `{ status: "error", error }`.
|
|
134
141
|
|
|
135
|
-
A `people` property can contain both users and groups (`object: "group"`). `users.get()` and
|
|
142
|
+
A `people` property can contain both users and groups (`object: "group"`). `users.get()` and
|
|
143
|
+
`users.list()` only support people. You cannot use these APIs to resolve a group or list its
|
|
144
|
+
members.
|
|
136
145
|
|
|
137
|
-
Skip group entries when you need a person’s name and avatar. Match the person’s ID against results
|
|
146
|
+
Skip group entries when you need a person’s name and avatar. Match the person’s ID against results
|
|
147
|
+
from `users.list()`. Reuse those results if your UI already lists workspace users. IDs in a `people`
|
|
148
|
+
property are plain strings. `users.get()` requires a `NotionUserId`, such as an ID from the user
|
|
149
|
+
APIs.
|
|
138
150
|
|
|
139
151
|
## Permissions
|
|
140
152
|
|
|
141
|
-
A user returned by `users.list()` does not necessarily have access to the current page or data
|
|
153
|
+
A user returned by `users.list()` does not necessarily have access to the current page or data
|
|
154
|
+
source. Do not use this list to check page or data source permissions.
|
|
142
155
|
|
|
143
|
-
If a request returns `status: "error"`, handle the error before reading `result.user` or
|
|
156
|
+
If a request returns `status: "error"`, handle the error before reading `result.user` or
|
|
157
|
+
`result.list`. See [error handling](./errors.md).
|
|
144
158
|
|
|
145
159
|
## Types
|
|
146
160
|
|