@notionhq/custom-blocks 0.1.16 → 0.1.18

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 (58) hide show
  1. package/dist/bridge/SandboxBridge.d.ts +3 -3
  2. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  3. package/dist/bridge/SandboxBridge.js +7 -5
  4. package/dist/bridge/dataSources/resolveProperty.d.ts +2 -1
  5. package/dist/bridge/dataSources/resolveProperty.d.ts.map +1 -1
  6. package/dist/bridge/hostState.d.ts +2 -1
  7. package/dist/bridge/hostState.d.ts.map +1 -1
  8. package/dist/bridge/sandboxClient.d.ts +1 -0
  9. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  10. package/dist/bridge/sandboxClient.js +26 -17
  11. package/dist/index.d.ts +1 -2
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/init.d.ts.map +1 -1
  14. package/dist/init.js +1 -0
  15. package/dist/protocol/dataSources/dataSource.d.ts +12 -12
  16. package/dist/protocol/dataSources/dataSourcePage.d.ts +1 -52
  17. package/dist/protocol/dataSources/dataSourcePage.d.ts.map +1 -1
  18. package/dist/protocol/dataSources/dataSourcePage.js +1 -3
  19. package/dist/protocol/ids.d.ts +10 -26
  20. package/dist/protocol/ids.d.ts.map +1 -1
  21. package/dist/protocol/ids.js +25 -5
  22. package/dist/protocol/messages/createPage.d.ts +8 -8
  23. package/dist/protocol/messages/createPageResult.d.ts +5 -5
  24. package/dist/protocol/messages/currentUserChanged.d.ts +1 -1
  25. package/dist/protocol/messages/dataSourcesChanged.d.ts +3 -3
  26. package/dist/protocol/messages/getPage.d.ts +6 -6
  27. package/dist/protocol/messages/getUser.d.ts +2 -2
  28. package/dist/protocol/messages/getUser.js +2 -2
  29. package/dist/protocol/messages/hostToSandbox.d.ts +45 -45
  30. package/dist/protocol/messages/init.d.ts +14 -14
  31. package/dist/protocol/messages/listUsers.d.ts +1 -1
  32. package/dist/protocol/messages/pageChanged.d.ts +5 -5
  33. package/dist/protocol/messages/parentChanged.d.ts +4 -4
  34. package/dist/protocol/messages/queryDataSource.d.ts +1 -1
  35. package/dist/protocol/messages/queryDataSourceResult.d.ts +1 -1
  36. package/dist/protocol/messages/sandboxToHost.d.ts +8 -8
  37. package/dist/protocol/messages/updatePage.d.ts +1 -1
  38. package/dist/protocol/messages/updatePageResult.d.ts +5 -5
  39. package/dist/protocol/pages/page.d.ts +10 -24
  40. package/dist/protocol/pages/page.d.ts.map +1 -1
  41. package/dist/protocol/parent.d.ts +4 -4
  42. package/dist/protocol/users/user.d.ts +4 -7
  43. package/dist/protocol/users/user.d.ts.map +1 -1
  44. package/dist/protocol/users/user.js +2 -1
  45. package/dist/react/standalonePreview.d.ts.map +1 -1
  46. package/dist/react/standalonePreview.js +3 -1
  47. package/dist/types.d.ts +64 -6
  48. package/dist/types.d.ts.map +1 -1
  49. package/dist/version.js +1 -1
  50. package/package.json +1 -1
  51. package/src/bridge/SandboxBridge.ts +8 -10
  52. package/src/bridge/dataSources/resolveProperty.ts +2 -4
  53. package/src/bridge/hostState.ts +6 -6
  54. package/src/bridge/sandboxClient.ts +28 -17
  55. package/src/index.ts +0 -8
  56. package/src/init.ts +1 -0
  57. package/src/react/standalonePreview.ts +3 -1
  58. package/src/types.ts +72 -10
@@ -22,23 +22,34 @@ import { type MessageLogEntry, SandboxBridge } from "./SandboxBridge.js"
22
22
 
23
23
  export type { MessageLogEntry }
24
24
 
25
- const bridge = new SandboxBridge()
25
+ let bridge: SandboxBridge | undefined
26
+
27
+ function getBridge(): SandboxBridge {
28
+ if (!bridge) {
29
+ bridge = new SandboxBridge()
30
+ }
31
+ return bridge
32
+ }
26
33
 
27
34
  export const customBlockHost = {
35
+ start: () => {
36
+ getBridge().startListening()
37
+ },
38
+
28
39
  sendConnect: (manifestResult: ManifestLoadResult) => {
29
- bridge.sendConnect(manifestResult)
40
+ getBridge().sendConnect(manifestResult)
30
41
  },
31
42
 
32
43
  awaitInit: (signal?: AbortSignal): Promise<void> => {
33
- return bridge.awaitInit(signal)
44
+ return getBridge().awaitInit(signal)
34
45
  },
35
46
 
36
47
  subscribe: (listener: () => void) => {
37
- return bridge.subscribe(listener)
48
+ return getBridge().subscribe(listener)
38
49
  },
39
50
 
40
51
  getState: (): CustomBlockHostState => {
41
- return bridge.getHostState()
52
+ return getBridge().getHostState()
42
53
  },
43
54
 
44
55
  /**
@@ -46,17 +57,17 @@ export const customBlockHost = {
46
57
  * standalone preview state.
47
58
  */
48
59
  setMockState: (message: InitMessage) => {
49
- bridge.setMockState(message)
60
+ getBridge().setMockState(message)
50
61
  },
51
62
 
52
63
  postResize: (height: number) => {
53
- bridge.postResize(height)
64
+ getBridge().postResize(height)
54
65
  },
55
66
  }
56
67
 
57
68
  export const customBlockDataSources = {
58
69
  query: (key: string, options?: UseDataSourceOptions) => {
59
- bridge.queryDataSource(key, options)
70
+ getBridge().queryDataSource(key, options)
60
71
  },
61
72
 
62
73
  getView: (
@@ -64,18 +75,18 @@ export const customBlockDataSources = {
64
75
  key: string,
65
76
  ): DataSourceQueryView => {
66
77
  return getDataSourceQueryViewWithBridge(hostState, key, args =>
67
- bridge.updateDataSourcePage(args),
78
+ getBridge().updateDataSourcePage(args),
68
79
  )
69
80
  },
70
81
  }
71
82
 
72
83
  export const messageLog = {
73
84
  getSnapshot: (): readonly MessageLogEntry[] => {
74
- return bridge.getMessageLog()
85
+ return getBridge().getMessageLog()
75
86
  },
76
87
 
77
88
  subscribe: (listener: () => void) => {
78
- return bridge.subscribeToMessageLog(listener)
89
+ return getBridge().subscribeToMessageLog(listener)
79
90
  },
80
91
  }
81
92
 
@@ -87,28 +98,28 @@ export const pages = {
87
98
  * Creates a new Notion page.
88
99
  */
89
100
  create: (input: CreatePageArgs): Promise<CreatePageResult> => {
90
- return bridge.createPage(input)
101
+ return getBridge().createPage(input)
91
102
  },
92
103
 
93
104
  /**
94
105
  * Fetches a page by id.
95
106
  */
96
107
  get: (pageId: NotionPageId): Promise<GetPageResult> => {
97
- return bridge.getPage(pageId)
108
+ return getBridge().getPage(pageId)
98
109
  },
99
110
 
100
111
  /**
101
112
  * Updates an existing page.
102
113
  */
103
114
  update: (input: UpdatePageArgs): Promise<UpdatePageResult> => {
104
- return bridge.updatePage(input)
115
+ return getBridge().updatePage(input)
105
116
  },
106
117
 
107
118
  /**
108
119
  * Soft-delete a page by archiving it.
109
120
  */
110
121
  delete: (pageId: NotionPageId): Promise<UpdatePageResult> => {
111
- return bridge.updatePage({ pageId, archived: true })
122
+ return getBridge().updatePage({ pageId, archived: true })
112
123
  },
113
124
  }
114
125
 
@@ -120,13 +131,13 @@ export const users = {
120
131
  * Lists users visible in the current custom block workspace.
121
132
  */
122
133
  list: (input?: ListUsersArgs): Promise<ListUsersResult> => {
123
- return bridge.listUsers(input)
134
+ return getBridge().listUsers(input)
124
135
  },
125
136
 
126
137
  /**
127
138
  * Fetches a user by id.
128
139
  */
129
140
  get: (userId: NotionUserId): Promise<GetUserResult> => {
130
- return bridge.getUser(userId)
141
+ return getBridge().getUser(userId)
131
142
  },
132
143
  }
package/src/index.ts CHANGED
@@ -12,12 +12,6 @@ export type {
12
12
  NotionCollectionSchema,
13
13
  NotionDataSource,
14
14
  } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
15
- export type {
16
- NotionDataSourcePage,
17
- NotionDataSourcePageUpdateArgs,
18
- NotionDataSourcePageUpdateInput,
19
- NotionDataSourcePageUpdateResult,
20
- } from "@notionhq/custom-blocks-protocol/dataSources/dataSourcePage.js"
21
15
  export type { NotionDataSourceValue } from "@notionhq/custom-blocks-protocol/dataSources/dataSourceValue.js"
22
16
  export type {
23
17
  NotionDate,
@@ -98,8 +92,6 @@ export type {
98
92
  NotionPageCover,
99
93
  NotionPageIcon,
100
94
  NotionPageId,
101
- NotionPagePropertyInputMap,
102
- NotionPagePropertyInputValue,
103
95
  NotionPagePropertyValue,
104
96
  NotionPagePropertyWriteMap,
105
97
  } from "@notionhq/custom-blocks-protocol/pages/page.js"
package/src/init.ts CHANGED
@@ -76,6 +76,7 @@ export function initCustomBlock(
76
76
  opts: InitCustomBlockOptions = {},
77
77
  ): Promise<CustomBlockInitPayload> {
78
78
  if (initPromise === undefined) {
79
+ customBlockHost.start()
79
80
  initPromise = performHandshake(opts)
80
81
  }
81
82
  return initPromise
@@ -3,11 +3,13 @@ import {
3
3
  notionBlockIdSchema,
4
4
  notionPageIdSchema,
5
5
  } from "@notionhq/custom-blocks-protocol/ids.js"
6
+ import { notionUserIdSchema } from "@notionhq/custom-blocks-protocol/users/user.js"
6
7
  import * as v from "valibot"
7
8
  import { customBlockHost } from "../bridge/sandboxClient.js"
8
9
 
9
10
  const previewBlockId = v.parse(notionBlockIdSchema, "")
10
11
  const previewPageId = v.parse(notionPageIdSchema, "")
12
+ const previewUserId = v.parse(notionUserIdSchema, "")
11
13
 
12
14
  export function seedStandalonePreviewState() {
13
15
  customBlockHost.setMockState({
@@ -23,7 +25,7 @@ export function seedStandalonePreviewState() {
23
25
  dataSources: { bindings: {} },
24
26
  currentUser: {
25
27
  object: "user",
26
- id: "",
28
+ id: previewUserId,
27
29
  name: "Preview User",
28
30
  avatar_url: null,
29
31
  type: "person",
package/src/types.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  import type { NotionCollectionSchema } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
2
- import type { NotionDataSourcePage } from "@notionhq/custom-blocks-protocol/dataSources/dataSourcePage.js"
2
+ import type { NotionDataSourceValue } from "@notionhq/custom-blocks-protocol/dataSources/dataSourceValue.js"
3
3
  import type { NotionPropertySchema } from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
4
- import type { NotionDataSourceId } from "@notionhq/custom-blocks-protocol/ids.js"
4
+ import type {
5
+ NotionDataSourceId,
6
+ NotionPageId,
7
+ } from "@notionhq/custom-blocks-protocol/ids.js"
5
8
  import type { NotionCreatePagePosition } from "@notionhq/custom-blocks-protocol/messages/createPage.js"
6
9
  import type { CustomBlockCreatePageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/createPageResult.js"
7
10
  import type { CustomBlockGetPageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/getPage.js"
@@ -15,25 +18,21 @@ import type { UpdatePageMessage } from "@notionhq/custom-blocks-protocol/message
15
18
  import type { CustomBlockUpdatePageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
16
19
  import type {
17
20
  NotionPage,
18
- NotionPageId,
19
- NotionPagePropertyInputMap,
21
+ NotionPageCover,
22
+ NotionPageIcon,
23
+ NotionPagePropertyValue,
20
24
  } from "@notionhq/custom-blocks-protocol/pages/page.js"
21
25
  import type {
22
26
  NotionUser,
23
27
  NotionUserList,
24
28
  } from "@notionhq/custom-blocks-protocol/users/user.js"
25
29
 
26
- export type {
27
- NotionDataSourcePageUpdateArgs,
28
- NotionDataSourcePageUpdateInput,
29
- NotionDataSourcePageUpdateResult,
30
- } from "@notionhq/custom-blocks-protocol/dataSources/dataSourcePage.js"
31
30
  export type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/errors.js"
32
31
  export type {
33
32
  NotionDataSourceId,
33
+ NotionPageId,
34
34
  NotionSpaceId,
35
35
  } from "@notionhq/custom-blocks-protocol/ids.js"
36
- export type { NotionPageId } from "@notionhq/custom-blocks-protocol/pages/page.js"
37
36
  export type {
38
37
  NotionUser,
39
38
  NotionUserId,
@@ -48,6 +47,69 @@ export type {
48
47
  CustomBlockUpdatePageErrorInfo,
49
48
  }
50
49
 
50
+ type WithOptionalPropertyId<T> = T extends { id: string }
51
+ ? Omit<T, "id"> & { id?: string }
52
+ : never
53
+
54
+ /**
55
+ * Public SDK page property write input. Keys may be raw property IDs or data source
56
+ * property keys. `id` is optional because the SDK resolves the final raw property ID
57
+ * from the map key before sending the bridge message.
58
+ */
59
+ export type NotionPagePropertyInputValue =
60
+ WithOptionalPropertyId<NotionPagePropertyValue>
61
+
62
+ export type NotionPagePropertyInputMap = {
63
+ [propertyIdOrKey: string]: NotionPagePropertyInputValue
64
+ }
65
+
66
+ /**
67
+ * Consumer-facing page shape returned from `useDataSource`. Derived from the bridge payload
68
+ * plus the data source's `propertyIdsByKey`.
69
+ */
70
+ export type NotionDataSourcePage = {
71
+ id: NotionPageId
72
+ /**
73
+ * Keyed mapping of raw property IDs to their value. Includes all properties on the data source,
74
+ * including the four built-ins (`created_time`, `last_edited_time`, `created_by`,
75
+ * `last_edited_by`).
76
+ */
77
+ propertiesById: { [propertyId: string]: NotionDataSourceValue | undefined }
78
+ /**
79
+ * Keyed mapping of user-defined keys to their value. Only includes properties that are mapped
80
+ * to a raw property ID in the data source's `propertyIdsByKey`.
81
+ */
82
+ propertiesByKey: { [key: string]: NotionDataSourceValue | undefined }
83
+ /**
84
+ * Updates this page using the data source's property-key bindings. The SDK resolves property
85
+ * keys to raw property IDs before sending the bridge message to the host.
86
+ */
87
+ update: (
88
+ args: NotionDataSourcePageUpdateArgs,
89
+ ) => Promise<NotionDataSourcePageUpdateResult>
90
+ }
91
+
92
+ export type NotionDataSourcePageUpdateArgs = {
93
+ properties?: NotionPagePropertyInputMap
94
+ icon?: NotionPageIcon
95
+ cover?: NotionPageCover
96
+ archived?: boolean
97
+ }
98
+
99
+ /**
100
+ * @deprecated Use `NotionDataSourcePageUpdateArgs` instead.
101
+ *
102
+ * TODO(custom-blocks): Remove this alias when bumping the SDK version to 0.2.0.
103
+ */
104
+ export type NotionDataSourcePageUpdateInput = NotionDataSourcePageUpdateArgs
105
+
106
+ export type NotionDataSourcePageUpdateResult =
107
+ | {
108
+ status: "success"
109
+ page: NotionPage
110
+ }
111
+ | { status: "error"; error: CustomBlockUpdatePageErrorInfo }
112
+
51
113
  /**
52
114
  * Return shape of `useDataSource`.
53
115
  *