@notionhq/custom-blocks 0.3.0 → 0.3.2

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 (45) hide show
  1. package/dist/bridge/SandboxBridge.d.ts +469 -15
  2. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  3. package/dist/bridge/SandboxBridge.js +131 -80
  4. package/dist/bridge/dataSources/subscribe.d.ts +3 -1
  5. package/dist/bridge/dataSources/subscribe.d.ts.map +1 -1
  6. package/dist/bridge/dataSources/subscribe.js +9 -1
  7. package/dist/bridge/page.d.ts +6 -8
  8. package/dist/bridge/page.d.ts.map +1 -1
  9. package/dist/bridge/page.js +9 -56
  10. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  11. package/dist/bridge/sandboxClient.js +29 -6
  12. package/dist/dataSources.d.ts.map +1 -1
  13. package/dist/dataSources.js +12 -10
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/protocol/dataSources/dateValue.d.ts +2 -2
  17. package/dist/protocol/dataSources/dateValue.js +9 -7
  18. package/dist/protocol/messages/createPage.d.ts +64 -2
  19. package/dist/protocol/messages/createPageResult.d.ts +192 -6
  20. package/dist/protocol/messages/getPage.d.ts +192 -6
  21. package/dist/protocol/messages/hostToSandbox.d.ts +704 -22
  22. package/dist/protocol/messages/queryDataSourceResult.d.ts +128 -4
  23. package/dist/protocol/messages/sandboxToHost.d.ts +128 -4
  24. package/dist/protocol/messages/updatePage.d.ts +64 -2
  25. package/dist/protocol/messages/updatePageResult.d.ts +192 -6
  26. package/dist/protocol/pages/page.d.ts +360 -12
  27. package/dist/protocol/pages/page.js +48 -2
  28. package/dist/react/useDataSource.d.ts +1 -1
  29. package/dist/react/useDataSource.d.ts.map +1 -1
  30. package/dist/react/useDataSource.js +19 -2
  31. package/dist/types.d.ts +5 -2
  32. package/dist/types.d.ts.map +1 -1
  33. package/dist/version.js +1 -1
  34. package/docs/data-sources.md +16 -12
  35. package/docs/errors.md +4 -0
  36. package/docs/pages.md +10 -7
  37. package/package.json +1 -1
  38. package/src/bridge/SandboxBridge.ts +181 -117
  39. package/src/bridge/dataSources/subscribe.ts +23 -1
  40. package/src/bridge/page.ts +15 -82
  41. package/src/bridge/sandboxClient.ts +37 -7
  42. package/src/dataSources.ts +19 -23
  43. package/src/index.ts +6 -0
  44. package/src/react/useDataSource.ts +20 -2
  45. package/src/types.ts +10 -1
@@ -1,90 +1,23 @@
1
- import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
2
- import type { NotionDataSourceId } from "@notionhq/custom-blocks-protocol/ids.js"
3
- import type { BridgeMessagePayload } from "@notionhq/custom-blocks-protocol/messages/messagePayload.js"
4
- import type { UpdatePageResultMessage } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
5
1
  import type { notionPageDataSchema } from "@notionhq/custom-blocks-protocol/pages/page.js"
6
2
  import type { InferOutput } from "valibot"
7
- import type {
8
- NotionPage,
9
- PageUpdatePayload,
10
- UpdatePageResult,
11
- } from "../types.js"
3
+ import type { NotionPage } from "../types.js"
12
4
 
13
5
  export function toNotionPageFromNotionPageData(args: {
14
6
  page: InferOutput<typeof notionPageDataSchema>
15
- dataSource: NotionDataSource | undefined
16
- getDataSource: () => NotionDataSource | undefined
17
- update: (
18
- payload: PageUpdatePayload,
19
- dataSourceId: NotionDataSourceId | undefined,
20
- ) => Promise<BridgeMessagePayload<UpdatePageResultMessage>>
7
+ dataSourceKey: string | undefined
8
+ getPropertyById: NotionPage["getPropertyById"]
9
+ getPropertyByKey: NotionPage["getPropertyByKey"]
10
+ updateById: NotionPage["updateById"]
11
+ updateByKey: NotionPage["updateByKey"]
21
12
  }): NotionPage {
22
- const { page: bridgePage, dataSource } = args
23
- const getPropertyById = (id: string) =>
24
- Object.values(sdkPage.properties).find(value => value?.id === id) ?? null
25
- let pending: Promise<void> | undefined
26
- const updatePage = (
27
- payload: PageUpdatePayload,
28
- dataSourceId: NotionDataSourceId | undefined,
29
- ): Promise<UpdatePageResult> => {
30
- const operation = async (): Promise<UpdatePageResult> => {
31
- const result = await args.update(payload, dataSourceId)
32
- if (result.status === "error") {
33
- return result
34
- }
35
- Object.assign(sdkPage, result.page)
36
- return { status: "success", page: sdkPage }
37
- }
38
- const result = pending ? pending.then(operation) : operation()
39
- const finished = result.then(
40
- () => undefined,
41
- () => undefined,
42
- )
43
- pending = finished
44
- void finished.then(() => {
45
- if (pending === finished) {
46
- pending = undefined
47
- }
48
- })
49
- return result
13
+ return {
14
+ ...args.page,
15
+ dataSourceKey: args.dataSourceKey ?? null,
16
+ getPropertyById: args.getPropertyById,
17
+ getPropertyByKey: args.getPropertyByKey,
18
+ updateById: args.updateById,
19
+ updateByKey: args.updateByKey,
20
+ archive: () => args.updateById({ is_archived: true }),
21
+ unarchive: () => args.updateById({ is_archived: false }),
50
22
  }
51
- const updateById = (payload: PageUpdatePayload) =>
52
- updatePage(payload, undefined)
53
- const updateByKey = (
54
- payload: PageUpdatePayload,
55
- ): Promise<UpdatePageResult> => {
56
- const dataSourceId = dataSource?.collectionPointer?.id
57
- if (dataSourceId === undefined) {
58
- return Promise.resolve({
59
- status: "error",
60
- error: {
61
- code: "unmapped_data_source",
62
- message: "This page has no bound data source context.",
63
- isRetryable: false,
64
- },
65
- })
66
- }
67
- return updatePage(payload, dataSourceId)
68
- }
69
- const methods = {
70
- getPropertyById,
71
- getPropertyByKey: (key: string) => {
72
- const source = args.getDataSource()
73
- const id =
74
- source && Object.hasOwn(source.propertyIdsByKey, key)
75
- ? source.propertyIdsByKey[key]
76
- : undefined
77
- return id === undefined ? null : getPropertyById(id)
78
- },
79
- updateById,
80
- updateByKey,
81
- archive: () => updateById({ is_archived: true }),
82
- unarchive: () => updateById({ is_archived: false }),
83
- }
84
- const sdkPage: NotionPage = {
85
- ...bridgePage,
86
- dataSourceKey: dataSource?.key ?? null,
87
- ...methods,
88
- }
89
- return sdkPage
90
23
  }
@@ -7,10 +7,12 @@ import type {
7
7
  GetUserResult,
8
8
  ListUsersArgs,
9
9
  ListUsersResult,
10
+ NotionPage,
10
11
  NotionUserId,
11
12
  OpenExternalUrlResult,
12
13
  OpenPageOptions,
13
14
  OpenPageResult,
15
+ UpdatePageResult,
14
16
  } from "../types.js"
15
17
  import {
16
18
  type CustomBlockHostState,
@@ -19,6 +21,7 @@ import {
19
21
  } from "./hostState.js"
20
22
  import type { ManifestLoadResult } from "./loadManifest.js"
21
23
  import type { MessageInterceptor } from "./messageInterceptor.js"
24
+ import { toNotionPageFromNotionPageData } from "./page.js"
22
25
  import {
23
26
  type MessageLogEntry,
24
27
  type QueryDataSourceArgs,
@@ -94,11 +97,32 @@ export const customBlockDataSources = {
94
97
  hostState,
95
98
  key,
96
99
  subscriptionId,
97
- ({ page, dataSource }) =>
98
- getBridge().buildNotionPage({
99
- page,
100
- dataSourceId: dataSource?.collectionPointer?.id,
101
- }),
100
+ ({ page: pageData, dataSource }) => {
101
+ const bridge = getBridge()
102
+ const update = async (
103
+ args: Parameters<SandboxBridge["updatePage"]>[0],
104
+ ): Promise<UpdatePageResult> => {
105
+ const result = await bridge.updatePage(args)
106
+ if (result.status === "error") {
107
+ return result
108
+ }
109
+ Object.assign(page, result.page)
110
+ return { status: "success", page }
111
+ }
112
+ const page: NotionPage = toNotionPageFromNotionPageData({
113
+ page: pageData,
114
+ dataSourceKey: key,
115
+ getPropertyById: propertyId =>
116
+ bridge.getPropertyById({ page, propertyId }),
117
+ getPropertyByKey: propertyKey =>
118
+ bridge.getPropertyByKey({ page, dataSource, propertyKey }),
119
+ updateById: payload =>
120
+ update({ ...payload, pageId: page.id, dataSource: undefined }),
121
+ updateByKey: payload =>
122
+ update({ ...payload, pageId: page.id, dataSource }),
123
+ })
124
+ return page
125
+ },
102
126
  )
103
127
  },
104
128
  }
@@ -121,14 +145,20 @@ export const pages = {
121
145
  * Creates a new Notion page.
122
146
  */
123
147
  create: (input: CreatePageArgs): Promise<CreatePageResult> => {
124
- return getBridge().createPage(input, undefined)
148
+ return getBridge().createPage({
149
+ ...input,
150
+ dataSource: undefined,
151
+ })
125
152
  },
126
153
 
127
154
  /**
128
155
  * Fetches a page by id.
129
156
  */
130
157
  get: (pageId: NotionPageId): Promise<GetPageResult> => {
131
- return getBridge().getPage(pageId, undefined)
158
+ return getBridge().getPage({
159
+ pageId,
160
+ dataSource: undefined,
161
+ })
132
162
  },
133
163
 
134
164
  /**
@@ -23,8 +23,8 @@ export const dataSources = {
23
23
  if (resolved.status === "error") {
24
24
  return resolved
25
25
  }
26
- const { dataSource, dataSourceId } = resolved
27
- const result = await getBridge().getPage(pageId, dataSourceId)
26
+ const { dataSource } = resolved
27
+ const result = await getBridge().getPage({ pageId, dataSource })
28
28
  return result.status === "error"
29
29
  ? result
30
30
  : { ...result, propertyIdsByKey: dataSource.propertyIdsByKey }
@@ -38,13 +38,11 @@ export const dataSources = {
38
38
  return resolved
39
39
  }
40
40
  const { dataSource, dataSourceId } = resolved
41
- const result = await getBridge().createPage(
42
- {
43
- ...payload,
44
- parent: { type: "data_source_id", data_source_id: dataSourceId },
45
- },
46
- dataSourceId,
47
- )
41
+ const result = await getBridge().createPage({
42
+ ...payload,
43
+ parent: { type: "data_source_id", data_source_id: dataSourceId },
44
+ dataSource,
45
+ })
48
46
  return result.status === "error"
49
47
  ? result
50
48
  : { ...result, propertyIdsByKey: dataSource.propertyIdsByKey }
@@ -81,20 +79,18 @@ export const dataSources = {
81
79
  return new Promise(resolve => {
82
80
  let completed = false
83
81
  let unsubscribe = () => {}
84
- unsubscribe = subscribeToDataSource(
85
- {
86
- key,
87
- options,
88
- onSnapshot: snapshot => {
89
- if (snapshot.isLoading || completed) {
90
- return
91
- }
92
- completed = true
93
- unsubscribe()
94
- resolve({ status: "success", ...snapshot })
95
- },
82
+ unsubscribe = subscribeToDataSource({
83
+ key,
84
+ options,
85
+ onSnapshot: snapshot => {
86
+ if (snapshot.isLoading || completed) {
87
+ return
88
+ }
89
+ completed = true
90
+ unsubscribe()
91
+ resolve({ status: "success", ...snapshot })
96
92
  },
97
- error => {
93
+ onError: error => {
98
94
  if (completed) {
99
95
  return
100
96
  }
@@ -102,7 +98,7 @@ export const dataSources = {
102
98
  unsubscribe()
103
99
  resolve({ status: "error", error })
104
100
  },
105
- )
101
+ })
106
102
  if (completed) {
107
103
  unsubscribe()
108
104
  }
package/src/index.ts CHANGED
@@ -12,6 +12,12 @@ export type {
12
12
  NotionCollectionSchema,
13
13
  NotionDataSource,
14
14
  } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
15
+ export type {
16
+ NotionDateReminder,
17
+ NotionDateTimeReminder,
18
+ NotionDisabledReminder,
19
+ NotionTimeReminder,
20
+ } from "@notionhq/custom-blocks-protocol/dataSources/dateValue.js"
15
21
  export type {
16
22
  CustomBlockDataSourceResolutionErrorCode,
17
23
  CustomBlockDataSourceResolutionErrorInfo,
@@ -8,6 +8,7 @@ const EMPTY_DATA_SOURCE_RESULT: UseDataSourceResult = {
8
8
  propertyIdsByKey: {},
9
9
  isLoading: false,
10
10
  hasMore: false,
11
+ error: undefined,
11
12
  }
12
13
 
13
14
  type DataSourceHookState = {
@@ -24,7 +25,7 @@ type DataSourceHookState = {
24
25
  * is capped at 999. `sorts` are applied in array order.
25
26
  *
26
27
  * @example
27
- * const { items, propertyIdsByKey, isLoading, hasMore } = useDataSource("default", { limit: 25 })
28
+ * const { items, propertyIdsByKey, isLoading, hasMore, error } = useDataSource("default", { limit: 25 })
28
29
  */
29
30
  export function useDataSource(
30
31
  key: string,
@@ -41,9 +42,26 @@ export function useDataSource(
41
42
  return customBlock.subscribeToDataSource({
42
43
  key,
43
44
  onSnapshot: snapshot => {
44
- setState({ key, optionsIdentity, snapshot })
45
+ setState({
46
+ key,
47
+ optionsIdentity,
48
+ snapshot: { ...snapshot, error: undefined },
49
+ })
45
50
  },
46
51
  options,
52
+ onError: error => {
53
+ setState(previous => {
54
+ const snapshot =
55
+ previous.key === key && previous.optionsIdentity === optionsIdentity
56
+ ? previous.snapshot
57
+ : EMPTY_DATA_SOURCE_RESULT
58
+ return {
59
+ key,
60
+ optionsIdentity,
61
+ snapshot: { ...snapshot, isLoading: false, error },
62
+ }
63
+ })
64
+ },
47
65
  })
48
66
  }, [key, optionsIdentity])
49
67
  if (state.key !== key || state.optionsIdentity !== optionsIdentity) {
package/src/types.ts CHANGED
@@ -87,6 +87,12 @@ export type NotionPagePropertyInputValue =
87
87
  | "created_by"
88
88
  | "last_edited_by"
89
89
  | "last_visited_time"
90
+ | "unique_id"
91
+ | "button"
92
+ | "formula"
93
+ | "location"
94
+ | "rollup"
95
+ | "verification"
90
96
  }
91
97
  > extends infer PropertyValue
92
98
  ? PropertyValue extends { id: string }
@@ -211,10 +217,13 @@ export type DataSourceQueryOptions = {
211
217
  export type SubscribeToDataSourceArgs = {
212
218
  key: string
213
219
  onSnapshot: (snapshot: DataSourceSnapshot) => void
220
+ onError?: (error: CustomBlockQueryDataSourceErrorInfo) => void
214
221
  options?: DataSourceQueryOptions
215
222
  }
216
223
 
217
- export type UseDataSourceResult = DataSourceSnapshot
224
+ export type UseDataSourceResult = DataSourceSnapshot & {
225
+ error: CustomBlockQueryDataSourceErrorInfo | undefined
226
+ }
218
227
 
219
228
  export type UseDataSourceOptions = DataSourceQueryOptions
220
229