@notionhq/custom-blocks 0.3.1 β†’ 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 (36) 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/page.d.ts +6 -8
  5. package/dist/bridge/page.d.ts.map +1 -1
  6. package/dist/bridge/page.js +9 -56
  7. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  8. package/dist/bridge/sandboxClient.js +29 -6
  9. package/dist/dataSources.d.ts.map +1 -1
  10. package/dist/dataSources.js +4 -3
  11. package/dist/index.d.ts +1 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/protocol/dataSources/dateValue.d.ts +2 -2
  14. package/dist/protocol/dataSources/dateValue.js +9 -7
  15. package/dist/protocol/messages/createPage.d.ts +64 -2
  16. package/dist/protocol/messages/createPageResult.d.ts +192 -6
  17. package/dist/protocol/messages/getPage.d.ts +192 -6
  18. package/dist/protocol/messages/hostToSandbox.d.ts +704 -22
  19. package/dist/protocol/messages/queryDataSourceResult.d.ts +128 -4
  20. package/dist/protocol/messages/sandboxToHost.d.ts +128 -4
  21. package/dist/protocol/messages/updatePage.d.ts +64 -2
  22. package/dist/protocol/messages/updatePageResult.d.ts +192 -6
  23. package/dist/protocol/pages/page.d.ts +360 -12
  24. package/dist/protocol/pages/page.js +48 -2
  25. package/dist/types.d.ts +1 -1
  26. package/dist/types.d.ts.map +1 -1
  27. package/dist/version.js +1 -1
  28. package/docs/data-sources.md +9 -8
  29. package/docs/pages.md +10 -7
  30. package/package.json +1 -1
  31. package/src/bridge/SandboxBridge.ts +181 -117
  32. package/src/bridge/page.ts +15 -82
  33. package/src/bridge/sandboxClient.ts +37 -7
  34. package/src/dataSources.ts +7 -9
  35. package/src/index.ts +6 -0
  36. package/src/types.ts +6 -0
package/docs/pages.md CHANGED
@@ -138,15 +138,15 @@ and a property in its schema.
138
138
  | `date` | 🟒 `start` and optional `end` strings, or `null` | 🟑 Minute precision | 🟑 Minute precision |
139
139
  | `people` | 🟒 Array of user/group ID objects | 🟒 Supported | 🟒 Supported |
140
140
  | `files` | 🟒 External links and hosted files | 🟑 File URLs only | 🟑 File URLs only |
141
- | `unique_id` | πŸ”΄ Unavailable | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
141
+ | `unique_id` | 🟑 Text fallback | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
142
142
  | `relation` | 🟒 Array of page ID objects | 🟑 Replaces value. Rejects `has_more: true`. | 🟑 Replaces value. Rejects `has_more: true`. |
143
143
  | `place` | 🟒 Coordinates and optional place details, or `null` | 🟒 Supported | 🟒 Supported |
144
- | `formula` | πŸ”΄ Unavailable | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
145
- | `rollup` | πŸ”΄ Unavailable | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
146
- | `button` | πŸ”΄ Unavailable | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
147
- | `verification` | πŸ”΄ Unavailable | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
144
+ | `formula` | 🟑 Text fallback | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
145
+ | `rollup` | 🟑 Text fallback | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
146
+ | `button` | 🟑 Text fallback | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
147
+ | `verification` | 🟑 Text fallback | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
148
148
  | `last_visited_time` | 🟑 Current viewer’s ISO timestamp | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
149
- | `location` | πŸ”΄ Unavailable | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
149
+ | `location` | 🟑 Text fallback | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
150
150
  | `created_time` | 🟒 ISO timestamp in UTC | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
151
151
  | `last_edited_time` | 🟒 ISO timestamp in UTC | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
152
152
  | `created_by` | 🟒 Actor record pointer | πŸ”΄ Unavailable | πŸ”΄ Unavailable |
@@ -167,7 +167,10 @@ and a property in its schema.
167
167
  - Relation writes replace the value and reject `has_more: true`.
168
168
  - Top-level `created_time` and `last_edited_time` remain available independently of schema
169
169
  properties.
170
- - Formula, rollup, unique ID, and other omitted types do not return structured page values.
170
+ - Formula, rollup, unique ID, button, verification, and location reads retain their type with a
171
+ string value.
172
+ - These strings contain stored text only. Formulas and rollups are not evaluated. Missing values
173
+ return `""`.
171
174
 
172
175
  For query properties, filters, and sorts, see
173
176
  [Data source property support](./data-sources.md#property-support).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notionhq/custom-blocks",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -4,10 +4,7 @@ import type {
4
4
  NotionDataSource,
5
5
  NotionDataSourceBindings,
6
6
  } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
7
- import type {
8
- NotionDataSourceId,
9
- NotionPageId,
10
- } from "@notionhq/custom-blocks-protocol/ids.js"
7
+ import type { NotionPageId } from "@notionhq/custom-blocks-protocol/ids.js"
11
8
  import type { CustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest.js"
12
9
  import type {
13
10
  CreatePageMessage,
@@ -41,9 +38,7 @@ import type { SandboxToHostMessage } from "@notionhq/custom-blocks-protocol/mess
41
38
  import type { UnsubscribeDataSourceQueryMessage } from "@notionhq/custom-blocks-protocol/messages/unsubscribeDataSourceQuery.js"
42
39
  import type { UpdatePageMessage } from "@notionhq/custom-blocks-protocol/messages/updatePage.js"
43
40
  import type { UpdatePageResultMessage } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
44
- import type { notionPageDataSchema } from "@notionhq/custom-blocks-protocol/pages/page.js"
45
41
  import type { NotionTheme } from "@notionhq/custom-blocks-protocol/theme.js"
46
- import type { InferOutput } from "valibot"
47
42
  import * as v from "valibot"
48
43
  import type {
49
44
  CreatePageArgs,
@@ -59,6 +54,7 @@ import type {
59
54
  OpenPageOptions,
60
55
  OpenPageResult,
61
56
  PageUpdatePayload,
57
+ UpdatePageResult,
62
58
  } from "../types.js"
63
59
  import { unreachable } from "../utils.js"
64
60
  import {
@@ -103,6 +99,7 @@ export class SandboxBridge {
103
99
  theme: "light",
104
100
  contrastMode: DEFAULT_CONTRAST_MODE,
105
101
  }
102
+ private pendingPageUpdates = new Map<NotionPageId, Promise<void>>()
106
103
  private listeners = new Set<() => void>()
107
104
  private messageInterceptors = new Set<MessageInterceptor>()
108
105
  private messageLog: MessageLogEntry[] = []
@@ -758,19 +755,34 @@ export class SandboxBridge {
758
755
  this.postToHost(outbound)
759
756
  }
760
757
 
761
- createPage(
762
- createPageArgs: CreatePageArgs,
763
- dataSourceId: NotionDataSourceId | undefined,
764
- ): Promise<CreatePageResult> {
758
+ createPage(args: {
759
+ parent: CreatePageArgs["parent"]
760
+ properties: CreatePageArgs["properties"]
761
+ position?: CreatePageArgs["position"]
762
+ dataSource: NotionDataSource | undefined
763
+ }): Promise<CreatePageResult> {
764
+ const { dataSource } = args
765
+ const dataSourceKey = dataSource?.key
765
766
  return new Promise(resolve => {
766
- const resolvedParent = this.resolveCreatePageParent(createPageArgs.parent)
767
- if (resolvedParent.status === "error") {
768
- resolve(resolvedParent)
769
- return
767
+ let parent: CreatePageMessageParent
768
+ let propertyDataSource = dataSource
769
+ if (dataSource?.collectionPointer) {
770
+ parent = {
771
+ type: "data_source_id",
772
+ data_source_id: dataSource.collectionPointer.id,
773
+ }
774
+ } else {
775
+ const resolvedParent = this.resolveCreatePageParent(args.parent)
776
+ if (resolvedParent.status === "error") {
777
+ resolve(resolvedParent)
778
+ return
779
+ }
780
+ parent = resolvedParent.parent
781
+ propertyDataSource = resolvedParent.dataSource
770
782
  }
771
783
  const resolvedProperties = resolvePropertyWriteMapForDataSource({
772
- dataSource: resolvedParent.dataSource,
773
- properties: createPageArgs.properties,
784
+ dataSource: propertyDataSource,
785
+ properties: args.properties,
774
786
  operationName: "createPage",
775
787
  })
776
788
  if (resolvedProperties.status === "error") {
@@ -782,38 +794,79 @@ export class SandboxBridge {
782
794
  resolve(result)
783
795
  return
784
796
  }
785
- resolve({
786
- status: "success",
787
- page: this.buildNotionPage({ page: result.page, dataSourceId }),
797
+ const update = async (
798
+ args: Parameters<SandboxBridge["updatePage"]>[0],
799
+ ): Promise<UpdatePageResult> => {
800
+ const result = await this.updatePage(args)
801
+ if (result.status === "error") {
802
+ return result
803
+ }
804
+ Object.assign(page, result.page)
805
+ return { status: "success", page }
806
+ }
807
+ const page: NotionPage = toNotionPageFromNotionPageData({
808
+ page: result.page,
809
+ dataSourceKey,
810
+ getPropertyById: propertyId =>
811
+ this.getPropertyById({ page, propertyId }),
812
+ getPropertyByKey: propertyKey =>
813
+ this.getPropertyByKey({ page, dataSource, propertyKey }),
814
+ updateById: payload =>
815
+ update({ ...payload, pageId: page.id, dataSource: undefined }),
816
+ updateByKey: payload =>
817
+ update({ ...payload, pageId: page.id, dataSource }),
788
818
  })
819
+ resolve({ status: "success", page })
789
820
  })
790
821
  const outbound: CreatePageMessage = {
791
822
  type: "createPage",
792
823
  requestId,
793
- parent: resolvedParent.parent,
824
+ parent,
794
825
  properties: resolvedProperties.properties,
795
826
  }
796
- if (createPageArgs.position !== undefined) {
797
- outbound.position = createPageArgs.position
827
+ if (args.position !== undefined) {
828
+ outbound.position = args.position
798
829
  }
799
830
  this.postToHost(outbound)
800
831
  })
801
832
  }
802
833
 
803
- getPage(
804
- pageId: NotionPageId,
805
- dataSourceId: NotionDataSourceId | undefined,
806
- ): Promise<GetPageResult> {
834
+ getPage(args: {
835
+ pageId: NotionPageId
836
+ dataSource: NotionDataSource | undefined
837
+ }): Promise<GetPageResult> {
838
+ const { pageId, dataSource } = args
839
+ const dataSourceId = dataSource?.collectionPointer?.id
840
+ const dataSourceKey = dataSource?.key
807
841
  return new Promise(resolve => {
808
842
  const requestId = this.pendingGetPage.allocate(result => {
809
843
  if (result.status === "error") {
810
844
  resolve(result)
811
845
  return
812
846
  }
813
- resolve({
814
- status: "success",
815
- page: this.buildNotionPage({ page: result.page, dataSourceId }),
847
+ const update = async (
848
+ args: Parameters<SandboxBridge["updatePage"]>[0],
849
+ ): Promise<UpdatePageResult> => {
850
+ const result = await this.updatePage(args)
851
+ if (result.status === "error") {
852
+ return result
853
+ }
854
+ Object.assign(page, result.page)
855
+ return { status: "success", page }
856
+ }
857
+ const page: NotionPage = toNotionPageFromNotionPageData({
858
+ page: result.page,
859
+ dataSourceKey,
860
+ getPropertyById: propertyId =>
861
+ this.getPropertyById({ page, propertyId }),
862
+ getPropertyByKey: propertyKey =>
863
+ this.getPropertyByKey({ page, dataSource, propertyKey }),
864
+ updateById: payload =>
865
+ update({ ...payload, pageId: page.id, dataSource: undefined }),
866
+ updateByKey: payload =>
867
+ update({ ...payload, pageId: page.id, dataSource }),
816
868
  })
869
+ resolve({ status: "success", page })
817
870
  })
818
871
  const outbound: GetPageMessage = {
819
872
  type: "getPage",
@@ -878,67 +931,102 @@ export class SandboxBridge {
878
931
  })
879
932
  }
880
933
 
881
- private updatePage(
882
- args: UpdatePageArgs,
883
- dataSourceId: NotionDataSourceId | undefined,
884
- ): Promise<BridgeMessagePayload<UpdatePageResultMessage>> {
885
- const dataSource =
886
- this.hostState.status === "initialized" && dataSourceId !== undefined
934
+ getPropertyById(args: { page: NotionPage; propertyId: string }) {
935
+ return (
936
+ Object.values(args.page.properties).find(
937
+ value => value?.id === args.propertyId,
938
+ ) ?? null
939
+ )
940
+ }
941
+
942
+ getPropertyByKey(args: {
943
+ page: NotionPage
944
+ dataSource: NotionDataSource | undefined
945
+ propertyKey: string
946
+ }) {
947
+ const { page, dataSource, propertyKey } = args
948
+ const currentDataSource =
949
+ this.hostState.status === "initialized"
887
950
  ? this.hostState.dataSources.find(
888
- source => source.collectionPointer?.id === dataSourceId,
951
+ source => source.key === dataSource?.key,
889
952
  )
890
953
  : undefined
891
- if (dataSourceId !== undefined) {
892
- const unmappedKey = Object.keys(args.properties ?? {}).find(key => {
893
- const id =
894
- dataSource && Object.hasOwn(dataSource.propertyIdsByKey, key)
895
- ? dataSource.propertyIdsByKey[key]
954
+ // Keep the captured mapping valid only while its key remains bound to the original database.
955
+ if (
956
+ !dataSource?.collectionPointer ||
957
+ currentDataSource?.collectionPointer?.id !==
958
+ dataSource.collectionPointer.id
959
+ ) {
960
+ return null
961
+ }
962
+ const propertyId = Object.hasOwn(dataSource.propertyIdsByKey, propertyKey)
963
+ ? dataSource.propertyIdsByKey[propertyKey]
964
+ : undefined
965
+ return propertyId === undefined
966
+ ? null
967
+ : this.getPropertyById({ page, propertyId })
968
+ }
969
+
970
+ updatePage(args: {
971
+ pageId: NotionPageId
972
+ properties?: PageUpdatePayload["properties"]
973
+ icon?: PageUpdatePayload["icon"]
974
+ cover?: PageUpdatePayload["cover"]
975
+ is_archived?: PageUpdatePayload["is_archived"]
976
+ dataSource: NotionDataSource | undefined
977
+ }): Promise<BridgeMessagePayload<UpdatePageResultMessage>> {
978
+ const operation = async (): Promise<
979
+ BridgeMessagePayload<UpdatePageResultMessage>
980
+ > => {
981
+ const { dataSource } = args
982
+ 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]
896
988
  : undefined
897
- return !id || !dataSource?.collectionSchema?.propertiesById[id]
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
+ const resolved = resolvePropertyWriteMapForDataSource({
1006
+ dataSource,
1007
+ properties: args.properties ?? {},
1008
+ operationName: "page.update",
898
1009
  })
899
- if (unmappedKey !== undefined) {
900
- return Promise.resolve({
901
- status: "error",
902
- error: {
903
- code: "unmapped_property",
904
- message: `Cannot resolve property key "${unmappedKey}" on this page.`,
905
- isRetryable: false,
906
- },
907
- })
1010
+ if (resolved.status === "error") {
1011
+ return resolved
908
1012
  }
909
- }
910
- const resolved = resolvePropertyWriteMapForDataSource({
911
- dataSource,
912
- properties: args.properties ?? {},
913
- operationName: "page.update",
914
- })
915
- if (resolved.status === "error") {
916
- return Promise.resolve(resolved)
917
- }
918
- const isArchived = args.is_archived
919
- return new Promise(resolve => {
920
1013
  if (
921
- (args.properties === undefined ||
922
- Object.keys(args.properties).length === 0) &&
1014
+ Object.keys(args.properties ?? {}).length === 0 &&
923
1015
  args.icon === undefined &&
924
1016
  args.cover === undefined &&
925
- isArchived === undefined
1017
+ args.is_archived === undefined
926
1018
  ) {
927
- resolve({
1019
+ return {
928
1020
  status: "error",
929
1021
  error: {
930
1022
  code: "invalid_page_update",
931
1023
  message: "updatePage requires at least one property.",
932
1024
  isRetryable: false,
933
1025
  },
934
- })
935
- return
1026
+ }
936
1027
  }
937
-
938
- const requestId = this.pendingUpdatePage.allocate(resolve)
939
- const outbound: UpdatePageMessage = {
1028
+ const outbound: Omit<UpdatePageMessage, "requestId"> = {
940
1029
  type: "updatePage",
941
- requestId,
942
1030
  dataSourceId,
943
1031
  pageId: args.pageId,
944
1032
  }
@@ -951,47 +1039,29 @@ export class SandboxBridge {
951
1039
  if (args.cover !== undefined) {
952
1040
  outbound.cover = args.cover
953
1041
  }
954
- if (isArchived !== undefined) {
955
- outbound.archived = isArchived
1042
+ if (args.is_archived !== undefined) {
1043
+ outbound.archived = args.is_archived
1044
+ }
1045
+ return new Promise<BridgeMessagePayload<UpdatePageResultMessage>>(
1046
+ resolve => {
1047
+ const requestId = this.pendingUpdatePage.allocate(resolve)
1048
+ this.postToHost({ ...outbound, requestId })
1049
+ },
1050
+ )
1051
+ }
1052
+ const pending = this.pendingPageUpdates.get(args.pageId)
1053
+ const result = pending ? pending.then(operation) : operation()
1054
+ const finished = result.then(
1055
+ () => undefined,
1056
+ () => undefined,
1057
+ )
1058
+ this.pendingPageUpdates.set(args.pageId, finished)
1059
+ void finished.then(() => {
1060
+ if (this.pendingPageUpdates.get(args.pageId) === finished) {
1061
+ this.pendingPageUpdates.delete(args.pageId)
956
1062
  }
957
- this.postToHost(outbound)
958
- })
959
- }
960
-
961
- /**
962
- * Builds an SDK page object from loaded page data and wires its methods to the bridge.
963
- * Unlike `pages.create`, this does not send a create request or create a page in Notion.
964
- */
965
- buildNotionPage(args: {
966
- page: InferOutput<typeof notionPageDataSchema>
967
- dataSourceId: NotionDataSourceId | undefined
968
- }): NotionPage {
969
- const { page, dataSourceId } = args
970
- const dataSource =
971
- this.hostState.status === "initialized" && dataSourceId !== undefined
972
- ? this.hostState.dataSources.find(
973
- source => source.collectionPointer?.id === dataSourceId,
974
- )
975
- : undefined
976
- return toNotionPageFromNotionPageData({
977
- page,
978
- dataSource,
979
- getDataSource: () => {
980
- if (
981
- this.hostState.status !== "initialized" ||
982
- dataSourceId === undefined
983
- ) {
984
- return undefined
985
- }
986
- return this.hostState.dataSources.find(
987
- source =>
988
- source.key === dataSource?.key &&
989
- source.collectionPointer?.id === dataSourceId,
990
- )
991
- },
992
- update: (payload, updateDataSourceId) =>
993
- this.updatePage({ ...payload, pageId: page.id }, updateDataSourceId),
994
1063
  })
1064
+ return result
995
1065
  }
996
1066
 
997
1067
  /**
@@ -1235,9 +1305,3 @@ function formatInvalidHostReason(
1235
1305
  const extra = issues.length > 1 ? ` (+${issues.length - 1} more)` : ""
1236
1306
  return `Could not parse ${labelled}: ${detail}${extra}`
1237
1307
  }
1238
-
1239
- type UpdatePageArgs = Omit<
1240
- UpdatePageMessage,
1241
- "type" | "requestId" | "archived" | "properties" | "dataSourceId"
1242
- > &
1243
- PageUpdatePayload
@@ -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 }
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,