@notionhq/custom-blocks 0.2.24 → 0.3.1
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 +8 -5
- package/dist/bridge/SandboxBridge.d.ts +15 -14
- package/dist/bridge/SandboxBridge.d.ts.map +1 -1
- package/dist/bridge/SandboxBridge.js +117 -35
- package/dist/bridge/dataSources/resolve.d.ts +1 -3
- package/dist/bridge/dataSources/resolve.d.ts.map +1 -1
- package/dist/bridge/dataSources/resolve.js +1 -11
- package/dist/bridge/dataSources/subscribe.d.ts +4 -1
- package/dist/bridge/dataSources/subscribe.d.ts.map +1 -1
- package/dist/bridge/dataSources/subscribe.js +14 -1
- package/dist/bridge/hostState.d.ts +11 -39
- package/dist/bridge/hostState.d.ts.map +1 -1
- package/dist/bridge/hostState.js +5 -50
- package/dist/bridge/page.d.ts +14 -0
- package/dist/bridge/page.d.ts.map +1 -0
- package/dist/bridge/page.js +59 -0
- package/dist/bridge/sandboxClient.d.ts +8 -14
- package/dist/bridge/sandboxClient.d.ts.map +1 -1
- package/dist/bridge/sandboxClient.js +13 -21
- package/dist/dataSources.d.ts +8 -0
- package/dist/dataSources.d.ts.map +1 -0
- package/dist/dataSources.js +104 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/protocol/dataSources/dataSourcePage.d.ts +3 -5
- package/dist/protocol/dataSources/dataSourcePage.js +2 -3
- package/dist/protocol/dataSources/propertySchema.d.ts +1 -0
- package/dist/protocol/index.d.ts +1 -0
- package/dist/protocol/index.js +1 -0
- package/dist/protocol/messages/createPage.js +2 -2
- package/dist/protocol/messages/createPageResult.d.ts +468 -1
- package/dist/protocol/messages/createPageResult.js +17 -2
- package/dist/protocol/messages/getPage.d.ts +468 -1
- package/dist/protocol/messages/getPage.js +18 -3
- package/dist/protocol/messages/hostToSandbox.d.ts +2587 -178
- package/dist/protocol/messages/hostToSandbox.js +24 -12
- package/dist/protocol/messages/openExternalUrl.d.ts +32 -0
- package/dist/protocol/messages/openExternalUrl.js +26 -0
- package/dist/protocol/messages/queryDataSourceResult.d.ts +468 -1
- package/dist/protocol/messages/queryDataSourceResult.js +18 -2
- package/dist/protocol/messages/sandboxToHost.d.ts +6 -0
- package/dist/protocol/messages/sandboxToHost.js +2 -0
- package/dist/protocol/messages/updatePage.d.ts +1 -0
- package/dist/protocol/messages/updatePage.js +4 -3
- package/dist/protocol/messages/updatePageResult.d.ts +467 -1
- package/dist/protocol/messages/updatePageResult.js +16 -2
- package/dist/protocol/pages/page.d.ts +386 -6
- package/dist/protocol/pages/page.js +23 -2
- package/dist/protocol/protocolVersion.d.ts +2 -1
- package/dist/protocol/protocolVersion.js +4 -1
- package/dist/react/useDataSource.d.ts +1 -1
- package/dist/react/useDataSource.d.ts.map +1 -1
- package/dist/react/useDataSource.js +19 -4
- package/dist/types.d.ts +59 -97
- package/dist/types.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/docs/block-location.md +3 -3
- package/docs/data-sources.md +170 -133
- package/docs/errors.md +13 -9
- package/docs/links.md +44 -0
- package/docs/pages.md +113 -182
- package/package.json +1 -1
- package/src/bridge/SandboxBridge.ts +189 -62
- package/src/bridge/dataSources/resolve.ts +1 -20
- package/src/bridge/dataSources/subscribe.ts +32 -5
- package/src/bridge/hostState.ts +21 -100
- package/src/bridge/page.ts +90 -0
- package/src/bridge/sandboxClient.ts +15 -25
- package/src/dataSources.ts +138 -0
- package/src/index.ts +8 -19
- package/src/react/useDataSource.ts +20 -4
- package/src/types.ts +65 -110
|
@@ -4,14 +4,23 @@ import type {
|
|
|
4
4
|
NotionDataSource,
|
|
5
5
|
NotionDataSourceBindings,
|
|
6
6
|
} from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
7
|
-
import type {
|
|
7
|
+
import type {
|
|
8
|
+
NotionDataSourceId,
|
|
9
|
+
NotionPageId,
|
|
10
|
+
} from "@notionhq/custom-blocks-protocol/ids.js"
|
|
8
11
|
import type { CustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest.js"
|
|
9
12
|
import type {
|
|
10
13
|
CreatePageMessage,
|
|
11
14
|
CreatePageMessageParent,
|
|
12
15
|
} from "@notionhq/custom-blocks-protocol/messages/createPage.js"
|
|
13
|
-
import type {
|
|
14
|
-
|
|
16
|
+
import type {
|
|
17
|
+
CreatePageResultMessage,
|
|
18
|
+
CustomBlockCreatePageErrorInfo,
|
|
19
|
+
} from "@notionhq/custom-blocks-protocol/messages/createPageResult.js"
|
|
20
|
+
import type {
|
|
21
|
+
GetPageMessage,
|
|
22
|
+
GetPageResultMessage,
|
|
23
|
+
} from "@notionhq/custom-blocks-protocol/messages/getPage.js"
|
|
15
24
|
import type { GetUserMessage } from "@notionhq/custom-blocks-protocol/messages/getUser.js"
|
|
16
25
|
import { hostToSandboxMessageSchema } from "@notionhq/custom-blocks-protocol/messages/hostToSandbox.js"
|
|
17
26
|
import { readIncomingType } from "@notionhq/custom-blocks-protocol/messages/incomingType.js"
|
|
@@ -22,6 +31,8 @@ import {
|
|
|
22
31
|
import type { CustomBlockInitResultErrorInfo } from "@notionhq/custom-blocks-protocol/messages/initResult.js"
|
|
23
32
|
import type { InvalidHostMessage } from "@notionhq/custom-blocks-protocol/messages/invalidHostMessage.js"
|
|
24
33
|
import type { ListUsersMessage } from "@notionhq/custom-blocks-protocol/messages/listUsers.js"
|
|
34
|
+
import type { BridgeMessagePayload } from "@notionhq/custom-blocks-protocol/messages/messagePayload.js"
|
|
35
|
+
import type { OpenExternalUrlMessage } from "@notionhq/custom-blocks-protocol/messages/openExternalUrl.js"
|
|
25
36
|
import type { OpenPageMessage } from "@notionhq/custom-blocks-protocol/messages/openPage.js"
|
|
26
37
|
import type { QueryDataSourceMessage } from "@notionhq/custom-blocks-protocol/messages/queryDataSource.js"
|
|
27
38
|
import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
|
|
@@ -29,7 +40,10 @@ import type { ResizeMessage } from "@notionhq/custom-blocks-protocol/messages/re
|
|
|
29
40
|
import type { SandboxToHostMessage } from "@notionhq/custom-blocks-protocol/messages/sandboxToHost.js"
|
|
30
41
|
import type { UnsubscribeDataSourceQueryMessage } from "@notionhq/custom-blocks-protocol/messages/unsubscribeDataSourceQuery.js"
|
|
31
42
|
import type { UpdatePageMessage } from "@notionhq/custom-blocks-protocol/messages/updatePage.js"
|
|
43
|
+
import type { UpdatePageResultMessage } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
|
|
44
|
+
import type { notionPageDataSchema } from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
32
45
|
import type { NotionTheme } from "@notionhq/custom-blocks-protocol/theme.js"
|
|
46
|
+
import type { InferOutput } from "valibot"
|
|
33
47
|
import * as v from "valibot"
|
|
34
48
|
import type {
|
|
35
49
|
CreatePageArgs,
|
|
@@ -39,12 +53,12 @@ import type {
|
|
|
39
53
|
GetUserResult,
|
|
40
54
|
ListUsersArgs,
|
|
41
55
|
ListUsersResult,
|
|
42
|
-
|
|
56
|
+
NotionPage,
|
|
43
57
|
NotionUserId,
|
|
58
|
+
OpenExternalUrlResult,
|
|
44
59
|
OpenPageOptions,
|
|
45
60
|
OpenPageResult,
|
|
46
|
-
|
|
47
|
-
UpdatePageResult,
|
|
61
|
+
PageUpdatePayload,
|
|
48
62
|
} from "../types.js"
|
|
49
63
|
import { unreachable } from "../utils.js"
|
|
50
64
|
import {
|
|
@@ -52,10 +66,7 @@ import {
|
|
|
52
66
|
NOTION_LIGHT_BACKGROUND_BASE,
|
|
53
67
|
} from "./appearance.js"
|
|
54
68
|
import { resolveDataSourceQuery } from "./dataSources/query.js"
|
|
55
|
-
import {
|
|
56
|
-
getDataSourceReadSchemas,
|
|
57
|
-
resolveDataSources,
|
|
58
|
-
} from "./dataSources/resolve.js"
|
|
69
|
+
import { resolveDataSources } from "./dataSources/resolve.js"
|
|
59
70
|
import { resolvePropertyWriteMapForDataSource } from "./dataSources/resolveProperty.js"
|
|
60
71
|
import {
|
|
61
72
|
type CustomBlockHostState,
|
|
@@ -65,6 +76,7 @@ import {
|
|
|
65
76
|
import type { ManifestLoadResult } from "./loadManifest.js"
|
|
66
77
|
import type { MessageInterceptor } from "./messageInterceptor.js"
|
|
67
78
|
import { notifyListener } from "./notifyListener.js"
|
|
79
|
+
import { toNotionPageFromNotionPageData } from "./page.js"
|
|
68
80
|
import { PendingRequests } from "./pendingRequests.js"
|
|
69
81
|
import { SandboxInitialization } from "./SandboxInitialization.js"
|
|
70
82
|
|
|
@@ -95,12 +107,12 @@ export class SandboxBridge {
|
|
|
95
107
|
private messageInterceptors = new Set<MessageInterceptor>()
|
|
96
108
|
private messageLog: MessageLogEntry[] = []
|
|
97
109
|
private messageLogListeners = new Set<() => void>()
|
|
98
|
-
private readonly pendingCreatePage = new PendingRequests<
|
|
99
|
-
|
|
100
|
-
)
|
|
101
|
-
private readonly pendingGetPage = new PendingRequests<
|
|
102
|
-
|
|
103
|
-
)
|
|
110
|
+
private readonly pendingCreatePage = new PendingRequests<
|
|
111
|
+
BridgeMessagePayload<CreatePageResultMessage>
|
|
112
|
+
>("custom-block-create-page")
|
|
113
|
+
private readonly pendingGetPage = new PendingRequests<
|
|
114
|
+
BridgeMessagePayload<GetPageResultMessage>
|
|
115
|
+
>("custom-block-get-page")
|
|
104
116
|
private readonly pendingGetUser = new PendingRequests<GetUserResult>(
|
|
105
117
|
"custom-block-get-user",
|
|
106
118
|
)
|
|
@@ -110,9 +122,11 @@ export class SandboxBridge {
|
|
|
110
122
|
private readonly pendingOpenPage = new PendingRequests<OpenPageResult>(
|
|
111
123
|
"custom-block-open-page",
|
|
112
124
|
)
|
|
113
|
-
private readonly
|
|
114
|
-
"custom-block-
|
|
115
|
-
|
|
125
|
+
private readonly pendingOpenExternalUrl =
|
|
126
|
+
new PendingRequests<OpenExternalUrlResult>("custom-block-open-url")
|
|
127
|
+
private readonly pendingUpdatePage = new PendingRequests<
|
|
128
|
+
BridgeMessagePayload<UpdatePageResultMessage>
|
|
129
|
+
>("custom-block-update-page")
|
|
116
130
|
private latestDataSourceBindings: NotionDataSourceBindings = {}
|
|
117
131
|
private isMockState = false
|
|
118
132
|
private isListening = false
|
|
@@ -164,7 +178,7 @@ export class SandboxBridge {
|
|
|
164
178
|
this.messageLog.push({
|
|
165
179
|
timestamp: new Date().toISOString(),
|
|
166
180
|
direction,
|
|
167
|
-
data,
|
|
181
|
+
data: redactExternalUrl(data),
|
|
168
182
|
})
|
|
169
183
|
for (const listener of this.messageLogListeners) {
|
|
170
184
|
listener()
|
|
@@ -386,7 +400,7 @@ export class SandboxBridge {
|
|
|
386
400
|
}
|
|
387
401
|
|
|
388
402
|
case "createPageResult": {
|
|
389
|
-
const result:
|
|
403
|
+
const result: BridgeMessagePayload<CreatePageResultMessage> =
|
|
390
404
|
message.status === "success"
|
|
391
405
|
? { status: "success", page: message.page }
|
|
392
406
|
: { status: "error", error: message.error }
|
|
@@ -399,7 +413,7 @@ export class SandboxBridge {
|
|
|
399
413
|
}
|
|
400
414
|
|
|
401
415
|
case "getPageResult": {
|
|
402
|
-
const result:
|
|
416
|
+
const result: BridgeMessagePayload<GetPageResultMessage> =
|
|
403
417
|
message.status === "success"
|
|
404
418
|
? { status: "success", page: message.page }
|
|
405
419
|
: { status: "error", error: message.error }
|
|
@@ -450,8 +464,21 @@ export class SandboxBridge {
|
|
|
450
464
|
return
|
|
451
465
|
}
|
|
452
466
|
|
|
467
|
+
case "openExternalUrlResult": {
|
|
468
|
+
const result: OpenExternalUrlResult =
|
|
469
|
+
message.status === "success"
|
|
470
|
+
? { status: "success" }
|
|
471
|
+
: { status: "error", error: message.error }
|
|
472
|
+
if (!this.pendingOpenExternalUrl.resolve(message.requestId, result)) {
|
|
473
|
+
console.warn(
|
|
474
|
+
`[custom-blocks-sdk] openExternalUrlResult for unknown requestId ${message.requestId}`,
|
|
475
|
+
)
|
|
476
|
+
}
|
|
477
|
+
return
|
|
478
|
+
}
|
|
479
|
+
|
|
453
480
|
case "updatePageResult": {
|
|
454
|
-
const result:
|
|
481
|
+
const result: BridgeMessagePayload<UpdatePageResultMessage> =
|
|
455
482
|
message.status === "success"
|
|
456
483
|
? { status: "success", page: message.page }
|
|
457
484
|
: { status: "error", error: message.error }
|
|
@@ -468,6 +495,7 @@ export class SandboxBridge {
|
|
|
468
495
|
if (currentState === undefined) {
|
|
469
496
|
return
|
|
470
497
|
}
|
|
498
|
+
const items = message.status === "success" ? message.items : []
|
|
471
499
|
const queryResult =
|
|
472
500
|
message.status === "error"
|
|
473
501
|
? {
|
|
@@ -476,7 +504,7 @@ export class SandboxBridge {
|
|
|
476
504
|
error: message.error,
|
|
477
505
|
}
|
|
478
506
|
: {
|
|
479
|
-
items
|
|
507
|
+
items,
|
|
480
508
|
hasMore: message.hasMore,
|
|
481
509
|
error: undefined,
|
|
482
510
|
}
|
|
@@ -730,42 +758,67 @@ export class SandboxBridge {
|
|
|
730
758
|
this.postToHost(outbound)
|
|
731
759
|
}
|
|
732
760
|
|
|
733
|
-
createPage(
|
|
761
|
+
createPage(
|
|
762
|
+
createPageArgs: CreatePageArgs,
|
|
763
|
+
dataSourceId: NotionDataSourceId | undefined,
|
|
764
|
+
): Promise<CreatePageResult> {
|
|
734
765
|
return new Promise(resolve => {
|
|
735
|
-
const resolvedParent = this.resolveCreatePageParent(
|
|
766
|
+
const resolvedParent = this.resolveCreatePageParent(createPageArgs.parent)
|
|
736
767
|
if (resolvedParent.status === "error") {
|
|
737
768
|
resolve(resolvedParent)
|
|
738
769
|
return
|
|
739
770
|
}
|
|
740
771
|
const resolvedProperties = resolvePropertyWriteMapForDataSource({
|
|
741
772
|
dataSource: resolvedParent.dataSource,
|
|
742
|
-
properties:
|
|
773
|
+
properties: createPageArgs.properties,
|
|
743
774
|
operationName: "createPage",
|
|
744
775
|
})
|
|
745
776
|
if (resolvedProperties.status === "error") {
|
|
746
777
|
resolve(resolvedProperties)
|
|
747
778
|
return
|
|
748
779
|
}
|
|
749
|
-
const requestId = this.pendingCreatePage.allocate(
|
|
780
|
+
const requestId = this.pendingCreatePage.allocate(result => {
|
|
781
|
+
if (result.status === "error") {
|
|
782
|
+
resolve(result)
|
|
783
|
+
return
|
|
784
|
+
}
|
|
785
|
+
resolve({
|
|
786
|
+
status: "success",
|
|
787
|
+
page: this.buildNotionPage({ page: result.page, dataSourceId }),
|
|
788
|
+
})
|
|
789
|
+
})
|
|
750
790
|
const outbound: CreatePageMessage = {
|
|
751
791
|
type: "createPage",
|
|
752
792
|
requestId,
|
|
753
793
|
parent: resolvedParent.parent,
|
|
754
794
|
properties: resolvedProperties.properties,
|
|
755
795
|
}
|
|
756
|
-
if (
|
|
757
|
-
outbound.position =
|
|
796
|
+
if (createPageArgs.position !== undefined) {
|
|
797
|
+
outbound.position = createPageArgs.position
|
|
758
798
|
}
|
|
759
799
|
this.postToHost(outbound)
|
|
760
800
|
})
|
|
761
801
|
}
|
|
762
802
|
|
|
763
|
-
getPage(
|
|
803
|
+
getPage(
|
|
804
|
+
pageId: NotionPageId,
|
|
805
|
+
dataSourceId: NotionDataSourceId | undefined,
|
|
806
|
+
): Promise<GetPageResult> {
|
|
764
807
|
return new Promise(resolve => {
|
|
765
|
-
const requestId = this.pendingGetPage.allocate(
|
|
808
|
+
const requestId = this.pendingGetPage.allocate(result => {
|
|
809
|
+
if (result.status === "error") {
|
|
810
|
+
resolve(result)
|
|
811
|
+
return
|
|
812
|
+
}
|
|
813
|
+
resolve({
|
|
814
|
+
status: "success",
|
|
815
|
+
page: this.buildNotionPage({ page: result.page, dataSourceId }),
|
|
816
|
+
})
|
|
817
|
+
})
|
|
766
818
|
const outbound: GetPageMessage = {
|
|
767
819
|
type: "getPage",
|
|
768
820
|
requestId,
|
|
821
|
+
dataSourceId,
|
|
769
822
|
pageId,
|
|
770
823
|
}
|
|
771
824
|
this.postToHost(outbound)
|
|
@@ -788,6 +841,18 @@ export class SandboxBridge {
|
|
|
788
841
|
})
|
|
789
842
|
}
|
|
790
843
|
|
|
844
|
+
openExternalUrl(url: string): Promise<OpenExternalUrlResult> {
|
|
845
|
+
return new Promise(resolve => {
|
|
846
|
+
const requestId = this.pendingOpenExternalUrl.allocate(resolve)
|
|
847
|
+
const outbound: OpenExternalUrlMessage = {
|
|
848
|
+
type: "openExternalUrl",
|
|
849
|
+
requestId,
|
|
850
|
+
url,
|
|
851
|
+
}
|
|
852
|
+
this.postToHost(outbound)
|
|
853
|
+
})
|
|
854
|
+
}
|
|
855
|
+
|
|
791
856
|
getUser(userId: NotionUserId): Promise<GetUserResult> {
|
|
792
857
|
return new Promise(resolve => {
|
|
793
858
|
const requestId = this.pendingGetUser.allocate(resolve)
|
|
@@ -813,7 +878,43 @@ export class SandboxBridge {
|
|
|
813
878
|
})
|
|
814
879
|
}
|
|
815
880
|
|
|
816
|
-
updatePage(
|
|
881
|
+
private updatePage(
|
|
882
|
+
args: UpdatePageArgs,
|
|
883
|
+
dataSourceId: NotionDataSourceId | undefined,
|
|
884
|
+
): Promise<BridgeMessagePayload<UpdatePageResultMessage>> {
|
|
885
|
+
const dataSource =
|
|
886
|
+
this.hostState.status === "initialized" && dataSourceId !== undefined
|
|
887
|
+
? this.hostState.dataSources.find(
|
|
888
|
+
source => source.collectionPointer?.id === dataSourceId,
|
|
889
|
+
)
|
|
890
|
+
: 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]
|
|
896
|
+
: undefined
|
|
897
|
+
return !id || !dataSource?.collectionSchema?.propertiesById[id]
|
|
898
|
+
})
|
|
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
|
+
})
|
|
908
|
+
}
|
|
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
|
+
}
|
|
817
918
|
const isArchived = args.is_archived
|
|
818
919
|
return new Promise(resolve => {
|
|
819
920
|
if (
|
|
@@ -838,10 +939,11 @@ export class SandboxBridge {
|
|
|
838
939
|
const outbound: UpdatePageMessage = {
|
|
839
940
|
type: "updatePage",
|
|
840
941
|
requestId,
|
|
942
|
+
dataSourceId,
|
|
841
943
|
pageId: args.pageId,
|
|
842
944
|
}
|
|
843
945
|
if (args.properties !== undefined) {
|
|
844
|
-
outbound.properties =
|
|
946
|
+
outbound.properties = resolved.properties
|
|
845
947
|
}
|
|
846
948
|
if (args.icon !== undefined) {
|
|
847
949
|
outbound.icon = args.icon
|
|
@@ -857,34 +959,38 @@ export class SandboxBridge {
|
|
|
857
959
|
}
|
|
858
960
|
|
|
859
961
|
/**
|
|
860
|
-
*
|
|
861
|
-
* `
|
|
862
|
-
* returned from {@link getDataSourceQueryView}.
|
|
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.
|
|
863
964
|
*/
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
const
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
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),
|
|
888
994
|
})
|
|
889
995
|
}
|
|
890
996
|
|
|
@@ -1049,6 +1155,21 @@ function readInitializationId(data: unknown): string | undefined {
|
|
|
1049
1155
|
return undefined
|
|
1050
1156
|
}
|
|
1051
1157
|
|
|
1158
|
+
function redactExternalUrl(data: unknown): unknown {
|
|
1159
|
+
if (
|
|
1160
|
+
typeof data === "object" &&
|
|
1161
|
+
data !== null &&
|
|
1162
|
+
"type" in data &&
|
|
1163
|
+
data.type === "openExternalUrl"
|
|
1164
|
+
) {
|
|
1165
|
+
return {
|
|
1166
|
+
type: "openExternalUrl",
|
|
1167
|
+
...("requestId" in data ? { requestId: data.requestId } : {}),
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
return data
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1052
1173
|
function getInitBindingError(
|
|
1053
1174
|
manifest: CustomBlockManifest,
|
|
1054
1175
|
dataSources: NotionDataSource[],
|
|
@@ -1090,7 +1211,7 @@ function resolveMockDataSources(
|
|
|
1090
1211
|
collectionPointer: binding.collectionPointer,
|
|
1091
1212
|
collectionSchema: binding.collectionSchema,
|
|
1092
1213
|
propertyIdsByKey: { ...(binding.propertyIdsByKey ?? {}) },
|
|
1093
|
-
propertySchemasById:
|
|
1214
|
+
propertySchemasById: binding.collectionSchema?.propertiesById ?? {},
|
|
1094
1215
|
}))
|
|
1095
1216
|
}
|
|
1096
1217
|
|
|
@@ -1114,3 +1235,9 @@ function formatInvalidHostReason(
|
|
|
1114
1235
|
const extra = issues.length > 1 ? ` (+${issues.length - 1} more)` : ""
|
|
1115
1236
|
return `Could not parse ${labelled}: ${detail}${extra}`
|
|
1116
1237
|
}
|
|
1238
|
+
|
|
1239
|
+
type UpdatePageArgs = Omit<
|
|
1240
|
+
UpdatePageMessage,
|
|
1241
|
+
"type" | "requestId" | "archived" | "properties" | "dataSourceId"
|
|
1242
|
+
> &
|
|
1243
|
+
PageUpdatePayload
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
NotionCollectionSchema,
|
|
3
2
|
NotionDataSource,
|
|
4
3
|
NotionDataSourceBindings,
|
|
5
4
|
} from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
6
|
-
import {
|
|
7
|
-
NOTION_BUILTIN_PROPERTY_SCHEMAS,
|
|
8
|
-
type NotionPropertySchema,
|
|
9
|
-
} from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
|
|
10
5
|
import type { CustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest.js"
|
|
11
6
|
|
|
12
7
|
/**
|
|
@@ -25,9 +20,6 @@ export function resolveDataSources(args: {
|
|
|
25
20
|
const binding = args.dataSourceBindings[key]
|
|
26
21
|
const collectionProperties =
|
|
27
22
|
binding?.collectionSchema?.propertiesById ?? {}
|
|
28
|
-
const propertySchemasById = getDataSourceReadSchemas(
|
|
29
|
-
binding?.collectionSchema,
|
|
30
|
-
)
|
|
31
23
|
|
|
32
24
|
const bindingPropertyIdsByKey = binding?.propertyIdsByKey ?? {}
|
|
33
25
|
const propertyIdsByKey: Record<string, string | undefined> = {}
|
|
@@ -51,19 +43,8 @@ export function resolveDataSources(args: {
|
|
|
51
43
|
collectionPointer: binding?.collectionPointer,
|
|
52
44
|
collectionSchema: binding?.collectionSchema,
|
|
53
45
|
propertyIdsByKey,
|
|
54
|
-
propertySchemasById,
|
|
46
|
+
propertySchemasById: collectionProperties,
|
|
55
47
|
}
|
|
56
48
|
},
|
|
57
49
|
)
|
|
58
50
|
}
|
|
59
|
-
|
|
60
|
-
export function getDataSourceReadSchemas(
|
|
61
|
-
collectionSchema: NotionCollectionSchema | undefined,
|
|
62
|
-
): Record<string, NotionPropertySchema> {
|
|
63
|
-
// Queries without filters or sorts can return metadata without a database schema. Keep
|
|
64
|
-
// metadata schemas available for reading those values.
|
|
65
|
-
return {
|
|
66
|
-
...collectionSchema?.propertiesById,
|
|
67
|
-
...NOTION_BUILTIN_PROPERTY_SCHEMAS,
|
|
68
|
-
}
|
|
69
|
-
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
|
|
1
2
|
import type { SubscribeToDataSourceArgs } from "../../types.js"
|
|
2
3
|
import type {
|
|
3
4
|
CustomBlockHostState,
|
|
@@ -6,11 +7,24 @@ import type {
|
|
|
6
7
|
import { notifyListener } from "../notifyListener.js"
|
|
7
8
|
import { customBlockDataSources, customBlockHost } from "../sandboxClient.js"
|
|
8
9
|
|
|
9
|
-
export function subscribeToDataSource(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
export function subscribeToDataSource(
|
|
11
|
+
args: SubscribeToDataSourceArgs,
|
|
12
|
+
): () => void
|
|
13
|
+
/** @deprecated Pass onError in the first argument instead. */
|
|
14
|
+
export function subscribeToDataSource(
|
|
15
|
+
args: SubscribeToDataSourceArgs,
|
|
16
|
+
onError?: (error: CustomBlockQueryDataSourceErrorInfo) => void,
|
|
17
|
+
): () => void
|
|
18
|
+
export function subscribeToDataSource(
|
|
19
|
+
{
|
|
20
|
+
key,
|
|
21
|
+
onSnapshot,
|
|
22
|
+
onError: providedOnError,
|
|
23
|
+
options: providedOptions = {},
|
|
24
|
+
}: SubscribeToDataSourceArgs,
|
|
25
|
+
legacyOnError?: (error: CustomBlockQueryDataSourceErrorInfo) => void,
|
|
26
|
+
): () => void {
|
|
27
|
+
const onError = providedOnError ?? legacyOnError
|
|
14
28
|
const options = structuredClone(providedOptions)
|
|
15
29
|
const subscriptionId = customBlockDataSources.createSubscriptionId()
|
|
16
30
|
let active = true
|
|
@@ -41,8 +55,21 @@ export function subscribeToDataSource({
|
|
|
41
55
|
key,
|
|
42
56
|
subscriptionId,
|
|
43
57
|
)
|
|
58
|
+
const backingSourceChanged =
|
|
59
|
+
lastSnapshotInputs !== undefined &&
|
|
60
|
+
lastSnapshotInputs.dataSource?.collectionPointer?.id !==
|
|
61
|
+
dataSource?.collectionPointer?.id
|
|
44
62
|
// Record inputs before calling consumer code, which can trigger another notification.
|
|
45
63
|
lastSnapshotInputs = { status: hostState.status, dataSource, queryState }
|
|
64
|
+
const error = queryState?.error
|
|
65
|
+
if (error && onError) {
|
|
66
|
+
if (backingSourceChanged) {
|
|
67
|
+
// Sync failures skip loading, so deliver the bridge's reset before the error.
|
|
68
|
+
notifyListener(() => onSnapshot(snapshot))
|
|
69
|
+
}
|
|
70
|
+
notifyListener(() => onError(error))
|
|
71
|
+
return
|
|
72
|
+
}
|
|
46
73
|
notifyListener(() => onSnapshot(snapshot))
|
|
47
74
|
}
|
|
48
75
|
|