@notionhq/custom-blocks 0.2.24 → 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.
- 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 +2 -1
- package/dist/bridge/dataSources/subscribe.d.ts.map +1 -1
- package/dist/bridge/dataSources/subscribe.js +6 -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 +103 -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 +1 -3
- package/dist/types.d.ts +55 -96
- 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 +167 -133
- package/docs/errors.md +9 -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 +10 -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 +140 -0
- package/src/index.ts +8 -19
- package/src/react/useDataSource.ts +1 -3
- package/src/types.ts +61 -109
|
@@ -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,10 @@ import type {
|
|
|
6
7
|
import { notifyListener } from "../notifyListener.js"
|
|
7
8
|
import { customBlockDataSources, customBlockHost } from "../sandboxClient.js"
|
|
8
9
|
|
|
9
|
-
export function subscribeToDataSource(
|
|
10
|
-
key,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}: SubscribeToDataSourceArgs): () => void {
|
|
10
|
+
export function subscribeToDataSource(
|
|
11
|
+
{ key, onSnapshot, options: providedOptions = {} }: SubscribeToDataSourceArgs,
|
|
12
|
+
onError?: (error: CustomBlockQueryDataSourceErrorInfo) => void,
|
|
13
|
+
): () => void {
|
|
14
14
|
const options = structuredClone(providedOptions)
|
|
15
15
|
const subscriptionId = customBlockDataSources.createSubscriptionId()
|
|
16
16
|
let active = true
|
|
@@ -43,6 +43,11 @@ export function subscribeToDataSource({
|
|
|
43
43
|
)
|
|
44
44
|
// Record inputs before calling consumer code, which can trigger another notification.
|
|
45
45
|
lastSnapshotInputs = { status: hostState.status, dataSource, queryState }
|
|
46
|
+
const error = queryState?.error
|
|
47
|
+
if (error && onError) {
|
|
48
|
+
notifyListener(() => onError(error))
|
|
49
|
+
return
|
|
50
|
+
}
|
|
46
51
|
notifyListener(() => onSnapshot(snapshot))
|
|
47
52
|
}
|
|
48
53
|
|
package/src/bridge/hostState.ts
CHANGED
|
@@ -1,23 +1,17 @@
|
|
|
1
1
|
import type { NotionContrastMode } from "@notionhq/custom-blocks-protocol/contrast.js"
|
|
2
2
|
import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
3
|
-
import type {
|
|
4
|
-
import type { NotionDataSourceValue } from "@notionhq/custom-blocks-protocol/dataSources/dataSourceValue.js"
|
|
5
|
-
import type { NotionPropertySchema } from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
|
|
6
|
-
import type {
|
|
7
|
-
NotionBlockId,
|
|
8
|
-
NotionPageId,
|
|
9
|
-
} from "@notionhq/custom-blocks-protocol/ids.js"
|
|
3
|
+
import type { NotionBlockId } from "@notionhq/custom-blocks-protocol/ids.js"
|
|
10
4
|
import type { CustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest.js"
|
|
11
5
|
import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
|
|
12
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
CustomBlockPage,
|
|
8
|
+
notionPageDataSchema,
|
|
9
|
+
} from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
13
10
|
import type { NotionParent } from "@notionhq/custom-blocks-protocol/parent.js"
|
|
14
11
|
import type { NotionTheme } from "@notionhq/custom-blocks-protocol/theme.js"
|
|
15
12
|
import type { NotionUser } from "@notionhq/custom-blocks-protocol/users/user.js"
|
|
16
|
-
import type {
|
|
17
|
-
|
|
18
|
-
NotionDataSourcePageUpdateArgs,
|
|
19
|
-
UpdatePageResult,
|
|
20
|
-
} from "../types.js"
|
|
13
|
+
import type { InferOutput } from "valibot"
|
|
14
|
+
import type { DataSourceSnapshot, NotionPage } from "../types.js"
|
|
21
15
|
|
|
22
16
|
export type CustomBlockHostState = UninitializedHostState | InitializedHostState
|
|
23
17
|
|
|
@@ -42,8 +36,8 @@ export type InitializedHostState = {
|
|
|
42
36
|
|
|
43
37
|
export type DataSourceQueryState = {
|
|
44
38
|
dataSourceKey: string
|
|
45
|
-
/** Latest pages from the host as parsed from the bridge.
|
|
46
|
-
items:
|
|
39
|
+
/** Latest pages from the host as parsed from the bridge. */
|
|
40
|
+
items: InferOutput<typeof notionPageDataSchema>[]
|
|
47
41
|
isLoading: boolean
|
|
48
42
|
hasMore: boolean
|
|
49
43
|
error?: CustomBlockQueryDataSourceErrorInfo
|
|
@@ -62,47 +56,23 @@ export function createEmptyDataSourceQueryState(
|
|
|
62
56
|
}
|
|
63
57
|
}
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
* Resolved page + schema views for a single data source, keyed by raw property
|
|
67
|
-
* ID and by user-defined key. Re-derived from `propertyIdsByKey` on every read so
|
|
68
|
-
* renames don't require a re-query.
|
|
69
|
-
*/
|
|
70
|
-
export type DataSourceQueryView = {
|
|
71
|
-
items: NotionDataSourcePage[]
|
|
72
|
-
collectionSchema?: NotionDataSource["collectionSchema"]
|
|
73
|
-
propertySchemasById: { [propertyId: string]: NotionPropertySchema }
|
|
74
|
-
propertyIdsByKey: { [key: string]: string | undefined }
|
|
75
|
-
propertySchemasByKey: { [key: string]: NotionPropertySchema | undefined }
|
|
76
|
-
isLoading: boolean
|
|
77
|
-
hasMore: boolean
|
|
78
|
-
error?: CustomBlockQueryDataSourceErrorInfo
|
|
79
|
-
}
|
|
59
|
+
export type DataSourceQueryView = DataSourceSnapshot
|
|
80
60
|
|
|
81
61
|
const EMPTY_QUERY_VIEW: DataSourceQueryView = {
|
|
82
62
|
items: [],
|
|
83
|
-
propertySchemasById: {},
|
|
84
63
|
propertyIdsByKey: {},
|
|
85
|
-
propertySchemasByKey: {},
|
|
86
64
|
isLoading: false,
|
|
87
65
|
hasMore: false,
|
|
88
66
|
}
|
|
89
67
|
|
|
90
|
-
/**
|
|
91
|
-
* Per-row callback that resolves property keys against the data source and forwards the
|
|
92
|
-
* resulting write to the bridge. Injected by callers (typically the SDK singleton in `init.ts`)
|
|
93
|
-
* so this module stays free of bridge-instance dependencies.
|
|
94
|
-
*/
|
|
95
|
-
export type UpdateDataSourcePageFn = (args: {
|
|
96
|
-
dataSource: NotionDataSource
|
|
97
|
-
pageId: NotionPageId
|
|
98
|
-
pageUpdateArgs: NotionDataSourcePageUpdateArgs
|
|
99
|
-
}) => Promise<UpdatePageResult>
|
|
100
|
-
|
|
101
68
|
export function getDataSourceQueryView(
|
|
102
69
|
hostState: CustomBlockHostState,
|
|
103
70
|
key: string,
|
|
104
71
|
subscriptionId: string,
|
|
105
|
-
|
|
72
|
+
createNotionPage: (args: {
|
|
73
|
+
page: InferOutput<typeof notionPageDataSchema>
|
|
74
|
+
dataSource: NotionDataSource | undefined
|
|
75
|
+
}) => NotionPage,
|
|
106
76
|
): DataSourceQueryView {
|
|
107
77
|
if (hostState.status !== "initialized") {
|
|
108
78
|
return EMPTY_QUERY_VIEW
|
|
@@ -115,64 +85,15 @@ export function getDataSourceQueryView(
|
|
|
115
85
|
? subscriptionState
|
|
116
86
|
: createEmptyDataSourceQueryState(key)
|
|
117
87
|
|
|
118
|
-
if (dataSource === undefined) {
|
|
119
|
-
return {
|
|
120
|
-
items: [],
|
|
121
|
-
collectionSchema: undefined,
|
|
122
|
-
propertySchemasById: {},
|
|
123
|
-
propertyIdsByKey: {},
|
|
124
|
-
propertySchemasByKey: {},
|
|
125
|
-
isLoading: queryState.isLoading,
|
|
126
|
-
hasMore: queryState.hasMore,
|
|
127
|
-
error: queryState.error,
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const propertyIdsByKey = dataSource.propertyIdsByKey
|
|
132
|
-
const propertySchemasById = dataSource.propertySchemasById
|
|
133
|
-
|
|
134
|
-
const resolvedItems: NotionDataSourcePage[] = queryState.items.map(entry => {
|
|
135
|
-
const propertiesByKey: {
|
|
136
|
-
[key: string]: NotionDataSourceValue | undefined
|
|
137
|
-
} = {}
|
|
138
|
-
for (const [key, propertyId] of Object.entries(propertyIdsByKey)) {
|
|
139
|
-
propertiesByKey[key] =
|
|
140
|
-
propertyId === undefined ? undefined : entry.propertiesById[propertyId]
|
|
141
|
-
}
|
|
142
|
-
const update = (args: NotionDataSourcePageUpdateArgs) =>
|
|
143
|
-
updateDataSourcePage({
|
|
144
|
-
dataSource,
|
|
145
|
-
pageId: entry.id,
|
|
146
|
-
pageUpdateArgs: args,
|
|
147
|
-
})
|
|
148
|
-
return {
|
|
149
|
-
id: entry.id,
|
|
150
|
-
is_archived: entry.is_archived,
|
|
151
|
-
in_trash: entry.in_trash,
|
|
152
|
-
propertiesById: entry.propertiesById,
|
|
153
|
-
propertiesByKey,
|
|
154
|
-
update,
|
|
155
|
-
archive: () => update({ is_archived: true }),
|
|
156
|
-
unarchive: () => update({ is_archived: false }),
|
|
157
|
-
}
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
const propertySchemasByKey: {
|
|
161
|
-
[key: string]: NotionPropertySchema | undefined
|
|
162
|
-
} = {}
|
|
163
|
-
for (const [key, propertyId] of Object.entries(propertyIdsByKey)) {
|
|
164
|
-
propertySchemasByKey[key] =
|
|
165
|
-
propertyId === undefined ? undefined : propertySchemasById[propertyId]
|
|
166
|
-
}
|
|
167
|
-
|
|
168
88
|
return {
|
|
169
|
-
items:
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
89
|
+
items:
|
|
90
|
+
dataSource === undefined
|
|
91
|
+
? []
|
|
92
|
+
: queryState.items.map(entry =>
|
|
93
|
+
createNotionPage({ page: entry, dataSource }),
|
|
94
|
+
),
|
|
95
|
+
propertyIdsByKey: dataSource?.propertyIdsByKey ?? {},
|
|
174
96
|
isLoading: queryState.isLoading,
|
|
175
97
|
hasMore: queryState.hasMore,
|
|
176
|
-
error: queryState.error,
|
|
177
98
|
}
|
|
178
99
|
}
|