@notionhq/custom-blocks 0.2.23 → 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 (77) 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/resolveProperty.d.ts +3 -3
  9. package/dist/bridge/dataSources/resolveProperty.d.ts.map +1 -1
  10. package/dist/bridge/dataSources/resolveProperty.js +1 -1
  11. package/dist/bridge/dataSources/subscribe.d.ts +2 -1
  12. package/dist/bridge/dataSources/subscribe.d.ts.map +1 -1
  13. package/dist/bridge/dataSources/subscribe.js +6 -1
  14. package/dist/bridge/hostState.d.ts +11 -39
  15. package/dist/bridge/hostState.d.ts.map +1 -1
  16. package/dist/bridge/hostState.js +5 -50
  17. package/dist/bridge/page.d.ts +14 -0
  18. package/dist/bridge/page.d.ts.map +1 -0
  19. package/dist/bridge/page.js +59 -0
  20. package/dist/bridge/sandboxClient.d.ts +8 -14
  21. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  22. package/dist/bridge/sandboxClient.js +13 -21
  23. package/dist/dataSources.d.ts +8 -0
  24. package/dist/dataSources.d.ts.map +1 -0
  25. package/dist/dataSources.js +103 -0
  26. package/dist/index.d.ts +6 -6
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +3 -2
  29. package/dist/protocol/dataSources/dataSourcePage.d.ts +3 -5
  30. package/dist/protocol/dataSources/dataSourcePage.js +2 -3
  31. package/dist/protocol/dataSources/propertySchema.d.ts +1 -0
  32. package/dist/protocol/index.d.ts +1 -0
  33. package/dist/protocol/index.js +1 -0
  34. package/dist/protocol/messages/createPage.d.ts +28 -0
  35. package/dist/protocol/messages/createPageResult.d.ts +496 -1
  36. package/dist/protocol/messages/createPageResult.js +17 -2
  37. package/dist/protocol/messages/getPage.d.ts +496 -1
  38. package/dist/protocol/messages/getPage.js +18 -3
  39. package/dist/protocol/messages/hostToSandbox.d.ts +2647 -154
  40. package/dist/protocol/messages/hostToSandbox.js +24 -12
  41. package/dist/protocol/messages/openExternalUrl.d.ts +32 -0
  42. package/dist/protocol/messages/openExternalUrl.js +26 -0
  43. package/dist/protocol/messages/queryDataSourceResult.d.ts +468 -1
  44. package/dist/protocol/messages/queryDataSourceResult.js +18 -2
  45. package/dist/protocol/messages/sandboxToHost.d.ts +62 -0
  46. package/dist/protocol/messages/sandboxToHost.js +2 -0
  47. package/dist/protocol/messages/updatePage.d.ts +29 -0
  48. package/dist/protocol/messages/updatePage.js +2 -1
  49. package/dist/protocol/messages/updatePageResult.d.ts +495 -1
  50. package/dist/protocol/messages/updatePageResult.js +16 -2
  51. package/dist/protocol/pages/page.d.ts +470 -3
  52. package/dist/protocol/pages/page.js +53 -1
  53. package/dist/protocol/protocolVersion.d.ts +2 -1
  54. package/dist/protocol/protocolVersion.js +4 -1
  55. package/dist/react/useDataSource.d.ts +1 -1
  56. package/dist/react/useDataSource.d.ts.map +1 -1
  57. package/dist/react/useDataSource.js +1 -3
  58. package/dist/types.d.ts +55 -96
  59. package/dist/types.d.ts.map +1 -1
  60. package/dist/version.js +1 -1
  61. package/docs/block-location.md +3 -3
  62. package/docs/data-sources.md +159 -122
  63. package/docs/errors.md +9 -9
  64. package/docs/links.md +44 -0
  65. package/docs/pages.md +121 -191
  66. package/package.json +1 -1
  67. package/src/bridge/SandboxBridge.ts +189 -62
  68. package/src/bridge/dataSources/resolve.ts +1 -20
  69. package/src/bridge/dataSources/resolveProperty.ts +4 -4
  70. package/src/bridge/dataSources/subscribe.ts +10 -5
  71. package/src/bridge/hostState.ts +21 -100
  72. package/src/bridge/page.ts +90 -0
  73. package/src/bridge/sandboxClient.ts +15 -25
  74. package/src/dataSources.ts +140 -0
  75. package/src/index.ts +9 -19
  76. package/src/react/useDataSource.ts +1 -3
  77. package/src/types.ts +61 -109
package/src/types.ts CHANGED
@@ -1,20 +1,11 @@
1
- import type { NotionCollectionSchema } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
2
- import type { NotionDataSourceValue } from "@notionhq/custom-blocks-protocol/dataSources/dataSourceValue.js"
3
- import type { NotionPropertySchema } from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
4
1
  import type { NotionSortDirection } from "@notionhq/custom-blocks-protocol/dataSources/sortDirection.js"
5
2
  import type {
6
3
  NotionDataSourceId,
7
4
  NotionPageId,
8
5
  } from "@notionhq/custom-blocks-protocol/ids.js"
9
6
  import type { NotionCreatePagePosition } from "@notionhq/custom-blocks-protocol/messages/createPage.js"
10
- import type {
11
- CreatePageResultMessage,
12
- CustomBlockCreatePageErrorInfo,
13
- } from "@notionhq/custom-blocks-protocol/messages/createPageResult.js"
14
- import type {
15
- CustomBlockGetPageErrorInfo,
16
- GetPageResultMessage,
17
- } from "@notionhq/custom-blocks-protocol/messages/getPage.js"
7
+ import type { CustomBlockCreatePageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/createPageResult.js"
8
+ import type { CustomBlockGetPageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/getPage.js"
18
9
  import type {
19
10
  CustomBlockGetUserErrorInfo,
20
11
  GetUserResultMessage,
@@ -25,6 +16,10 @@ import type {
25
16
  ListUsersResultMessage,
26
17
  } from "@notionhq/custom-blocks-protocol/messages/listUsers.js"
27
18
  import type { BridgeMessagePayload } from "@notionhq/custom-blocks-protocol/messages/messagePayload.js"
19
+ import type {
20
+ CustomBlockOpenExternalUrlErrorInfo,
21
+ OpenExternalUrlResultMessage,
22
+ } from "@notionhq/custom-blocks-protocol/messages/openExternalUrl.js"
28
23
  import type {
29
24
  CustomBlockOpenPageErrorInfo,
30
25
  OpenPageMode,
@@ -44,16 +39,15 @@ import type {
44
39
  CustomBlockTextFilterOperator,
45
40
  } from "@notionhq/custom-blocks-protocol/messages/queryDataSource.js"
46
41
  import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
47
- import type { UpdatePageMessage } from "@notionhq/custom-blocks-protocol/messages/updatePage.js"
48
- import type {
49
- CustomBlockUpdatePageErrorInfo,
50
- UpdatePageResultMessage,
51
- } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
42
+ import type { CustomBlockUpdatePageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
52
43
  import type {
53
44
  NotionPageCover,
54
45
  NotionPageIcon,
55
46
  NotionPagePropertyValue,
47
+ NotionPagePropertyWriteMap,
48
+ notionPageDataSchema,
56
49
  } from "@notionhq/custom-blocks-protocol/pages/page.js"
50
+ import type { InferOutput } from "valibot"
57
51
 
58
52
  import type { ExclusiveUnion } from "./typeUtils.js"
59
53
 
@@ -72,6 +66,7 @@ export type {
72
66
  CustomBlockGetPageErrorInfo,
73
67
  CustomBlockGetUserErrorInfo,
74
68
  CustomBlockListUsersErrorInfo,
69
+ CustomBlockOpenExternalUrlErrorInfo,
75
70
  CustomBlockOpenPageErrorInfo,
76
71
  CustomBlockQueryDataSourceErrorInfo,
77
72
  CustomBlockUpdatePageErrorInfo,
@@ -83,7 +78,17 @@ export type {
83
78
  * from the map key before sending the bridge message.
84
79
  */
85
80
  export type NotionPagePropertyInputValue =
86
- NotionPagePropertyValue extends infer PropertyValue
81
+ Exclude<
82
+ NotionPagePropertyWriteMap[string],
83
+ {
84
+ type:
85
+ | "created_time"
86
+ | "last_edited_time"
87
+ | "created_by"
88
+ | "last_edited_by"
89
+ | "last_visited_time"
90
+ }
91
+ > extends infer PropertyValue
87
92
  ? PropertyValue extends { id: string }
88
93
  ? Omit<PropertyValue, "id"> & { id?: string }
89
94
  : never
@@ -93,46 +98,46 @@ export type NotionPagePropertyInputMap = {
93
98
  [propertyIdOrKey: string]: NotionPagePropertyInputValue
94
99
  }
95
100
 
96
- /**
97
- * Consumer-facing page shape returned from a data source snapshot. Derived from the bridge payload
98
- * plus the data source's `propertyIdsByKey`.
99
- */
100
- export type NotionDataSourcePage = {
101
- id: NotionPageId
102
- /** Whether the page or an ancestor is archived. */
103
- is_archived: boolean
104
- /** Whether the page or an ancestor is in Trash. */
105
- in_trash: boolean
106
- /**
107
- * Keyed mapping of raw property IDs to their value. Includes all properties on the data source,
108
- * including the four built-ins (`created_time`, `last_edited_time`, `created_by`,
109
- * `last_edited_by`).
110
- */
111
- propertiesById: { [propertyId: string]: NotionDataSourceValue | undefined }
112
- /**
113
- * Keyed mapping of user-defined keys to their value. Only includes properties that are mapped
114
- * to a raw property ID in the data source's `propertyIdsByKey`.
115
- */
116
- propertiesByKey: { [key: string]: NotionDataSourceValue | undefined }
117
- /**
118
- * Updates this page using the data source's property-key bindings. The SDK resolves property
119
- * keys to raw property IDs before sending the bridge message to the host.
120
- */
121
- update: (args: NotionDataSourcePageUpdateArgs) => Promise<UpdatePageResult>
122
- /** Archives this page. Shorthand for update({ is_archived: true }). */
101
+ export type NotionPage = InferOutput<typeof notionPageDataSchema> & {
102
+ dataSourceKey: string | null
103
+ getPropertyById: (propertyId: string) => NotionPagePropertyValue | null
104
+ getPropertyByKey: (propertyKey: string) => NotionPagePropertyValue | null
105
+ updateById: (payload: PageUpdatePayload) => Promise<UpdatePageResult>
106
+ updateByKey: (payload: PageUpdatePayload) => Promise<UpdatePageResult>
123
107
  archive: () => Promise<UpdatePageResult>
124
- /** Unarchives this page. Shorthand for update({ is_archived: false }). */
125
108
  unarchive: () => Promise<UpdatePageResult>
126
109
  }
127
110
 
128
- export type NotionDataSourcePageUpdateArgs = {
111
+ export type PageUpdatePayload = {
129
112
  properties?: NotionPagePropertyInputMap
130
113
  icon?: NotionPageIcon
131
114
  cover?: NotionPageCover
132
- /** Whether to archive this page. */
133
115
  is_archived?: boolean
134
116
  }
135
117
 
118
+ export type GetPageResult =
119
+ | { status: "success"; page: NotionPage }
120
+ | { status: "error"; error: CustomBlockGetPageErrorInfo }
121
+
122
+ export type CreatePageResult =
123
+ | { status: "success"; page: NotionPage }
124
+ | { status: "error"; error: CustomBlockCreatePageErrorInfo }
125
+
126
+ export type DataSourcesPageResult =
127
+ | {
128
+ status: "success"
129
+ page: NotionPage
130
+ propertyIdsByKey: Record<string, string | undefined>
131
+ }
132
+ | {
133
+ status: "error"
134
+ error: CustomBlockGetPageErrorInfo | CustomBlockCreatePageErrorInfo
135
+ }
136
+
137
+ export type DataSourcesQueryResult =
138
+ | ({ status: "success" } & DataSourceSnapshot)
139
+ | { status: "error"; error: CustomBlockQueryDataSourceErrorInfo }
140
+
136
141
  export type NotionDataSourceTextFilterOperator =
137
142
  ExclusiveUnion<CustomBlockTextFilterOperator>
138
143
  export type NotionDataSourceNumberFilterOperator =
@@ -186,42 +191,12 @@ export type NotionDataSourceFilter = ExclusiveUnion<
186
191
  NotionDataSourcePropertyFilter | { and: NotionDataSourcePropertyFilter[] }
187
192
  >
188
193
 
189
- /**
190
- * Latest live snapshot of a data source query.
191
- *
192
- * - `items` — the rows the host has returned so far. Empty until the first response arrives.
193
- * - `isLoading` — `true` while a query is in flight.
194
- * - `hasMore` — `true` if the host indicated more rows are available beyond the requested prefix.
195
- * - `error` — structured error information if the most recent query failed.
196
- */
194
+ /** Latest live snapshot of a data source query. */
197
195
  export type DataSourceSnapshot = {
198
- items: NotionDataSourcePage[]
199
- /**
200
- * Contains only properties in the database schema.
201
- * Use this map to resolve property bindings and validate filter and sort references.
202
- * Undefined when the host does not provide a schema.
203
- */
204
- collectionSchema?: NotionCollectionSchema
205
- /**
206
- * Contains database properties and four synthetic metadata fields.
207
- * Use this map to describe values in query snapshots.
208
- * Synthetic entries do not establish filter or sort support.
209
- */
210
- propertySchemasById: { [propertyId: string]: NotionPropertySchema }
211
- /**
212
- * Maps user-defined property keys from the manifest to raw Notion property IDs.
213
- * Keys mapped to `undefined` indicate a declared-but-unbound slot.
214
- */
196
+ items: NotionPage[]
215
197
  propertyIdsByKey: { [key: string]: string | undefined }
216
- /**
217
- * Per-property schemas keyed by user-defined keys from the data source's `propertyIdsByKey`.
218
- * Built-ins are NOT included here. Keys mapped to `undefined` indicate a declared-but-unbound
219
- * slot.
220
- */
221
- propertySchemasByKey: { [key: string]: NotionPropertySchema | undefined }
222
198
  isLoading: boolean
223
199
  hasMore: boolean
224
- error?: CustomBlockQueryDataSourceErrorInfo
225
200
  }
226
201
 
227
202
  export type DataSourceQueryOptions = {
@@ -269,30 +244,6 @@ export type CreatePageArgs = {
269
244
  position?: NotionCreatePagePosition
270
245
  }
271
246
 
272
- /**
273
- * The result of a `sdk.pages.create` API call.
274
- */
275
- export type CreatePageResult = BridgeMessagePayload<
276
- CreatePageResultMessage,
277
- CustomBlockCreatePageErrorInfo
278
- >
279
-
280
- export type UpdatePageArgs = Omit<
281
- UpdatePageMessage,
282
- "type" | "requestId" | "archived"
283
- > & {
284
- /** Whether to archive this page. */
285
- is_archived?: boolean
286
- }
287
-
288
- /**
289
- * Result of `sdk.pages.get`.
290
- */
291
- export type GetPageResult = BridgeMessagePayload<
292
- GetPageResultMessage,
293
- CustomBlockGetPageErrorInfo
294
- >
295
-
296
247
  export type OpenPageOptions = {
297
248
  mode: OpenPageMode
298
249
  }
@@ -302,14 +253,15 @@ export type OpenPageResult = BridgeMessagePayload<
302
253
  CustomBlockOpenPageErrorInfo
303
254
  >
304
255
 
305
- /**
306
- * Result of `sdk.pages.update`, `sdk.pages.archive`, and `sdk.pages.unarchive`.
307
- */
308
- export type UpdatePageResult = BridgeMessagePayload<
309
- UpdatePageResultMessage,
310
- CustomBlockUpdatePageErrorInfo
256
+ export type OpenExternalUrlResult = BridgeMessagePayload<
257
+ OpenExternalUrlResultMessage,
258
+ CustomBlockOpenExternalUrlErrorInfo
311
259
  >
312
260
 
261
+ export type UpdatePageResult =
262
+ | { status: "success"; page: NotionPage }
263
+ | { status: "error"; error: CustomBlockUpdatePageErrorInfo }
264
+
313
265
  export type ListUsersArgs = Omit<ListUsersMessage, "type" | "requestId">
314
266
 
315
267
  export type ListUsersResult = BridgeMessagePayload<