@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
|
@@ -0,0 +1,90 @@
|
|
|
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
|
+
import type { notionPageDataSchema } from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
6
|
+
import type { InferOutput } from "valibot"
|
|
7
|
+
import type {
|
|
8
|
+
NotionPage,
|
|
9
|
+
PageUpdatePayload,
|
|
10
|
+
UpdatePageResult,
|
|
11
|
+
} from "../types.js"
|
|
12
|
+
|
|
13
|
+
export function toNotionPageFromNotionPageData(args: {
|
|
14
|
+
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>>
|
|
21
|
+
}): 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
|
|
50
|
+
}
|
|
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
|
+
}
|
|
@@ -8,10 +8,9 @@ import type {
|
|
|
8
8
|
ListUsersArgs,
|
|
9
9
|
ListUsersResult,
|
|
10
10
|
NotionUserId,
|
|
11
|
+
OpenExternalUrlResult,
|
|
11
12
|
OpenPageOptions,
|
|
12
13
|
OpenPageResult,
|
|
13
|
-
UpdatePageArgs,
|
|
14
|
-
UpdatePageResult,
|
|
15
14
|
} from "../types.js"
|
|
16
15
|
import {
|
|
17
16
|
type CustomBlockHostState,
|
|
@@ -28,7 +27,7 @@ import {
|
|
|
28
27
|
|
|
29
28
|
let bridge: SandboxBridge | undefined
|
|
30
29
|
|
|
31
|
-
function getBridge(): SandboxBridge {
|
|
30
|
+
export function getBridge(): SandboxBridge {
|
|
32
31
|
if (!bridge) {
|
|
33
32
|
bridge = new SandboxBridge()
|
|
34
33
|
}
|
|
@@ -95,7 +94,11 @@ export const customBlockDataSources = {
|
|
|
95
94
|
hostState,
|
|
96
95
|
key,
|
|
97
96
|
subscriptionId,
|
|
98
|
-
|
|
97
|
+
({ page, dataSource }) =>
|
|
98
|
+
getBridge().buildNotionPage({
|
|
99
|
+
page,
|
|
100
|
+
dataSourceId: dataSource?.collectionPointer?.id,
|
|
101
|
+
}),
|
|
99
102
|
)
|
|
100
103
|
},
|
|
101
104
|
}
|
|
@@ -118,14 +121,14 @@ export const pages = {
|
|
|
118
121
|
* Creates a new Notion page.
|
|
119
122
|
*/
|
|
120
123
|
create: (input: CreatePageArgs): Promise<CreatePageResult> => {
|
|
121
|
-
return getBridge().createPage(input)
|
|
124
|
+
return getBridge().createPage(input, undefined)
|
|
122
125
|
},
|
|
123
126
|
|
|
124
127
|
/**
|
|
125
128
|
* Fetches a page by id.
|
|
126
129
|
*/
|
|
127
130
|
get: (pageId: NotionPageId): Promise<GetPageResult> => {
|
|
128
|
-
return getBridge().getPage(pageId)
|
|
131
|
+
return getBridge().getPage(pageId, undefined)
|
|
129
132
|
},
|
|
130
133
|
|
|
131
134
|
/**
|
|
@@ -137,26 +140,13 @@ export const pages = {
|
|
|
137
140
|
): Promise<OpenPageResult> => {
|
|
138
141
|
return getBridge().openPage(pageId, options)
|
|
139
142
|
},
|
|
143
|
+
}
|
|
140
144
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return getBridge().
|
|
146
|
-
},
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Archives a page.
|
|
150
|
-
*/
|
|
151
|
-
archive: (pageId: NotionPageId): Promise<UpdatePageResult> => {
|
|
152
|
-
return getBridge().updatePage({ pageId, is_archived: true })
|
|
153
|
-
},
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Unarchives a page.
|
|
157
|
-
*/
|
|
158
|
-
unarchive: (pageId: NotionPageId): Promise<UpdatePageResult> => {
|
|
159
|
-
return getBridge().updatePage({ pageId, is_archived: false })
|
|
145
|
+
/** External-link APIs exposed under `sdk.links.*`. */
|
|
146
|
+
export const links = {
|
|
147
|
+
/** Requests host-owned approval before opening an HTTPS URL. */
|
|
148
|
+
open: (url: string): Promise<OpenExternalUrlResult> => {
|
|
149
|
+
return getBridge().openExternalUrl(url)
|
|
160
150
|
},
|
|
161
151
|
}
|
|
162
152
|
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
2
|
+
import type {
|
|
3
|
+
NotionDataSourceId,
|
|
4
|
+
NotionPageId,
|
|
5
|
+
} from "@notionhq/custom-blocks-protocol/ids.js"
|
|
6
|
+
import { CustomBlockInitializationError } from "@notionhq/custom-blocks-protocol/messages/init.js"
|
|
7
|
+
import { subscribeToDataSource } from "./bridge/dataSources/subscribe.js"
|
|
8
|
+
import { getBridge } from "./bridge/sandboxClient.js"
|
|
9
|
+
import type {
|
|
10
|
+
CreatePageArgs,
|
|
11
|
+
CustomBlockGetPageErrorInfo,
|
|
12
|
+
DataSourceQueryOptions,
|
|
13
|
+
DataSourcesPageResult,
|
|
14
|
+
DataSourcesQueryResult,
|
|
15
|
+
} from "./types.js"
|
|
16
|
+
|
|
17
|
+
export const dataSources = {
|
|
18
|
+
async getPage(
|
|
19
|
+
key: string,
|
|
20
|
+
pageId: NotionPageId,
|
|
21
|
+
): Promise<DataSourcesPageResult> {
|
|
22
|
+
const resolved = resolvePageDataSource(key)
|
|
23
|
+
if (resolved.status === "error") {
|
|
24
|
+
return resolved
|
|
25
|
+
}
|
|
26
|
+
const { dataSource, dataSourceId } = resolved
|
|
27
|
+
const result = await getBridge().getPage(pageId, dataSourceId)
|
|
28
|
+
return result.status === "error"
|
|
29
|
+
? result
|
|
30
|
+
: { ...result, propertyIdsByKey: dataSource.propertyIdsByKey }
|
|
31
|
+
},
|
|
32
|
+
async create(
|
|
33
|
+
key: string,
|
|
34
|
+
payload: Omit<CreatePageArgs, "parent" | "position">,
|
|
35
|
+
): Promise<DataSourcesPageResult> {
|
|
36
|
+
const resolved = resolvePageDataSource(key)
|
|
37
|
+
if (resolved.status === "error") {
|
|
38
|
+
return resolved
|
|
39
|
+
}
|
|
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
|
+
)
|
|
48
|
+
return result.status === "error"
|
|
49
|
+
? result
|
|
50
|
+
: { ...result, propertyIdsByKey: dataSource.propertyIdsByKey }
|
|
51
|
+
},
|
|
52
|
+
async query(
|
|
53
|
+
key: string,
|
|
54
|
+
options?: DataSourceQueryOptions,
|
|
55
|
+
): Promise<DataSourcesQueryResult> {
|
|
56
|
+
const bridge = getBridge()
|
|
57
|
+
if (bridge.getHostState().status !== "initialized") {
|
|
58
|
+
try {
|
|
59
|
+
await bridge.initialize()
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (bridge.getHostState().status !== "initialized") {
|
|
62
|
+
return {
|
|
63
|
+
status: "error",
|
|
64
|
+
error:
|
|
65
|
+
error instanceof CustomBlockInitializationError
|
|
66
|
+
? {
|
|
67
|
+
code: error.code,
|
|
68
|
+
message: error.message,
|
|
69
|
+
isRetryable: error.isRetryable,
|
|
70
|
+
}
|
|
71
|
+
: {
|
|
72
|
+
code: "unknown_error",
|
|
73
|
+
message:
|
|
74
|
+
error instanceof Error ? error.message : String(error),
|
|
75
|
+
isRetryable: false,
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return new Promise(resolve => {
|
|
82
|
+
let completed = false
|
|
83
|
+
let unsubscribe = () => {}
|
|
84
|
+
unsubscribe = subscribeToDataSource(
|
|
85
|
+
{
|
|
86
|
+
key,
|
|
87
|
+
options,
|
|
88
|
+
onSnapshot: snapshot => {
|
|
89
|
+
if (snapshot.isLoading || completed) {
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
completed = true
|
|
93
|
+
unsubscribe()
|
|
94
|
+
resolve({ status: "success", ...snapshot })
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
error => {
|
|
98
|
+
if (completed) {
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
completed = true
|
|
102
|
+
unsubscribe()
|
|
103
|
+
resolve({ status: "error", error })
|
|
104
|
+
},
|
|
105
|
+
)
|
|
106
|
+
if (completed) {
|
|
107
|
+
unsubscribe()
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function resolvePageDataSource(key: string):
|
|
114
|
+
| {
|
|
115
|
+
status: "success"
|
|
116
|
+
dataSource: NotionDataSource
|
|
117
|
+
dataSourceId: NotionDataSourceId
|
|
118
|
+
}
|
|
119
|
+
| { status: "error"; error: CustomBlockGetPageErrorInfo } {
|
|
120
|
+
const hostState = getBridge().getHostState()
|
|
121
|
+
const dataSource =
|
|
122
|
+
hostState.status === "initialized"
|
|
123
|
+
? hostState.dataSources.find(source => source.key === key)
|
|
124
|
+
: undefined
|
|
125
|
+
if (!dataSource?.collectionPointer) {
|
|
126
|
+
return {
|
|
127
|
+
status: "error",
|
|
128
|
+
error: {
|
|
129
|
+
code: dataSource ? "unmapped_data_source" : "unknown_data_source_key",
|
|
130
|
+
message: `No bound data source for "${key}".`,
|
|
131
|
+
isRetryable: false,
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
status: "success",
|
|
137
|
+
dataSource,
|
|
138
|
+
dataSourceId: dataSource.collectionPointer.id,
|
|
139
|
+
}
|
|
140
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -12,18 +12,6 @@ export type {
|
|
|
12
12
|
NotionCollectionSchema,
|
|
13
13
|
NotionDataSource,
|
|
14
14
|
} from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
15
|
-
export type { NotionDataSourceValue } from "@notionhq/custom-blocks-protocol/dataSources/dataSourceValue.js"
|
|
16
|
-
export type {
|
|
17
|
-
NotionDate,
|
|
18
|
-
NotionDateRange,
|
|
19
|
-
NotionDateReminder,
|
|
20
|
-
NotionDateTime,
|
|
21
|
-
NotionDateTimeRange,
|
|
22
|
-
NotionDateTimeReminder,
|
|
23
|
-
NotionDateValue,
|
|
24
|
-
NotionNoReminder,
|
|
25
|
-
NotionTimeReminder,
|
|
26
|
-
} from "@notionhq/custom-blocks-protocol/dataSources/dateValue.js"
|
|
27
15
|
export type {
|
|
28
16
|
CustomBlockDataSourceResolutionErrorCode,
|
|
29
17
|
CustomBlockDataSourceResolutionErrorInfo,
|
|
@@ -31,7 +19,6 @@ export type {
|
|
|
31
19
|
CustomBlockPropertyErrorInfo,
|
|
32
20
|
} from "@notionhq/custom-blocks-protocol/dataSources/errors.js"
|
|
33
21
|
export type {
|
|
34
|
-
NotionBuiltinPropertyId,
|
|
35
22
|
NotionDualProperty,
|
|
36
23
|
NotionPropertyColor,
|
|
37
24
|
NotionPropertyOption,
|
|
@@ -39,10 +26,7 @@ export type {
|
|
|
39
26
|
NotionPropertyType,
|
|
40
27
|
NotionStatusGroup,
|
|
41
28
|
} from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
|
|
42
|
-
export {
|
|
43
|
-
NOTION_BUILTIN_PROPERTY_IDS,
|
|
44
|
-
NOTION_PROPERTY_TYPES,
|
|
45
|
-
} from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
|
|
29
|
+
export { NOTION_PROPERTY_TYPES } from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
|
|
46
30
|
export type { NotionRecordPointer } from "@notionhq/custom-blocks-protocol/dataSources/recordPointer.js"
|
|
47
31
|
export type { NotionSortDirection } from "@notionhq/custom-blocks-protocol/dataSources/sortDirection.js"
|
|
48
32
|
export type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/errors.js"
|
|
@@ -81,6 +65,10 @@ export type {
|
|
|
81
65
|
CustomBlockListUsersErrorCode,
|
|
82
66
|
CustomBlockListUsersErrorInfo,
|
|
83
67
|
} from "@notionhq/custom-blocks-protocol/messages/listUsers.js"
|
|
68
|
+
export type {
|
|
69
|
+
CustomBlockOpenExternalUrlErrorCode,
|
|
70
|
+
CustomBlockOpenExternalUrlErrorInfo,
|
|
71
|
+
} from "@notionhq/custom-blocks-protocol/messages/openExternalUrl.js"
|
|
84
72
|
export type {
|
|
85
73
|
CustomBlockOpenPageErrorCode,
|
|
86
74
|
CustomBlockOpenPageErrorInfo,
|
|
@@ -96,7 +84,7 @@ export type {
|
|
|
96
84
|
} from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
|
|
97
85
|
export type {
|
|
98
86
|
CustomBlockPage,
|
|
99
|
-
|
|
87
|
+
NotionDateInput,
|
|
100
88
|
NotionPageCover,
|
|
101
89
|
NotionPageIcon,
|
|
102
90
|
NotionPagePropertyReadMap,
|
|
@@ -105,8 +93,9 @@ export type {
|
|
|
105
93
|
} from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
106
94
|
export type { NotionParent } from "@notionhq/custom-blocks-protocol/parent.js"
|
|
107
95
|
export type { NotionTheme } from "@notionhq/custom-blocks-protocol/theme.js"
|
|
108
|
-
export { pages, users } from "./bridge/sandboxClient.js"
|
|
96
|
+
export { links, pages, users } from "./bridge/sandboxClient.js"
|
|
109
97
|
export { customBlock } from "./customBlock.js"
|
|
98
|
+
export { dataSources } from "./dataSources.js"
|
|
110
99
|
export {
|
|
111
100
|
type CustomBlockInitPayload,
|
|
112
101
|
initCustomBlock,
|
|
@@ -5,9 +5,7 @@ import type { UseDataSourceOptions, UseDataSourceResult } from "../types.js"
|
|
|
5
5
|
|
|
6
6
|
const EMPTY_DATA_SOURCE_RESULT: UseDataSourceResult = {
|
|
7
7
|
items: [],
|
|
8
|
-
propertySchemasById: {},
|
|
9
8
|
propertyIdsByKey: {},
|
|
10
|
-
propertySchemasByKey: {},
|
|
11
9
|
isLoading: false,
|
|
12
10
|
hasMore: false,
|
|
13
11
|
}
|
|
@@ -26,7 +24,7 @@ type DataSourceHookState = {
|
|
|
26
24
|
* is capped at 999. `sorts` are applied in array order.
|
|
27
25
|
*
|
|
28
26
|
* @example
|
|
29
|
-
* const { items, isLoading, hasMore
|
|
27
|
+
* const { items, propertyIdsByKey, isLoading, hasMore } = useDataSource("default", { limit: 25 })
|
|
30
28
|
*/
|
|
31
29
|
export function useDataSource(
|
|
32
30
|
key: string,
|
package/src/types.ts
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
|
-
import type { NotionCollectionSchema } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
2
|
-
import type { NotionDataSourceValue } from "@notionhq/custom-blocks-protocol/dataSources/dataSourceValue.js"
|
|
3
|
-
import type { NotionPropertySchema } from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
|
|
4
1
|
import type { NotionSortDirection } from "@notionhq/custom-blocks-protocol/dataSources/sortDirection.js"
|
|
5
2
|
import type {
|
|
6
3
|
NotionDataSourceId,
|
|
7
4
|
NotionPageId,
|
|
8
5
|
} from "@notionhq/custom-blocks-protocol/ids.js"
|
|
9
6
|
import type { NotionCreatePagePosition } from "@notionhq/custom-blocks-protocol/messages/createPage.js"
|
|
10
|
-
import type {
|
|
11
|
-
|
|
12
|
-
CustomBlockCreatePageErrorInfo,
|
|
13
|
-
} from "@notionhq/custom-blocks-protocol/messages/createPageResult.js"
|
|
14
|
-
import type {
|
|
15
|
-
CustomBlockGetPageErrorInfo,
|
|
16
|
-
GetPageResultMessage,
|
|
17
|
-
} from "@notionhq/custom-blocks-protocol/messages/getPage.js"
|
|
7
|
+
import type { CustomBlockCreatePageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/createPageResult.js"
|
|
8
|
+
import type { CustomBlockGetPageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/getPage.js"
|
|
18
9
|
import type {
|
|
19
10
|
CustomBlockGetUserErrorInfo,
|
|
20
11
|
GetUserResultMessage,
|
|
@@ -25,6 +16,10 @@ import type {
|
|
|
25
16
|
ListUsersResultMessage,
|
|
26
17
|
} from "@notionhq/custom-blocks-protocol/messages/listUsers.js"
|
|
27
18
|
import type { BridgeMessagePayload } from "@notionhq/custom-blocks-protocol/messages/messagePayload.js"
|
|
19
|
+
import type {
|
|
20
|
+
CustomBlockOpenExternalUrlErrorInfo,
|
|
21
|
+
OpenExternalUrlResultMessage,
|
|
22
|
+
} from "@notionhq/custom-blocks-protocol/messages/openExternalUrl.js"
|
|
28
23
|
import type {
|
|
29
24
|
CustomBlockOpenPageErrorInfo,
|
|
30
25
|
OpenPageMode,
|
|
@@ -44,16 +39,15 @@ import type {
|
|
|
44
39
|
CustomBlockTextFilterOperator,
|
|
45
40
|
} from "@notionhq/custom-blocks-protocol/messages/queryDataSource.js"
|
|
46
41
|
import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
|
|
47
|
-
import type {
|
|
48
|
-
import type {
|
|
49
|
-
CustomBlockUpdatePageErrorInfo,
|
|
50
|
-
UpdatePageResultMessage,
|
|
51
|
-
} from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
|
|
42
|
+
import type { CustomBlockUpdatePageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
|
|
52
43
|
import type {
|
|
53
44
|
NotionPageCover,
|
|
54
45
|
NotionPageIcon,
|
|
46
|
+
NotionPagePropertyValue,
|
|
55
47
|
NotionPagePropertyWriteMap,
|
|
48
|
+
notionPageDataSchema,
|
|
56
49
|
} from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
50
|
+
import type { InferOutput } from "valibot"
|
|
57
51
|
|
|
58
52
|
import type { ExclusiveUnion } from "./typeUtils.js"
|
|
59
53
|
|
|
@@ -72,6 +66,7 @@ export type {
|
|
|
72
66
|
CustomBlockGetPageErrorInfo,
|
|
73
67
|
CustomBlockGetUserErrorInfo,
|
|
74
68
|
CustomBlockListUsersErrorInfo,
|
|
69
|
+
CustomBlockOpenExternalUrlErrorInfo,
|
|
75
70
|
CustomBlockOpenPageErrorInfo,
|
|
76
71
|
CustomBlockQueryDataSourceErrorInfo,
|
|
77
72
|
CustomBlockUpdatePageErrorInfo,
|
|
@@ -83,7 +78,17 @@ export type {
|
|
|
83
78
|
* from the map key before sending the bridge message.
|
|
84
79
|
*/
|
|
85
80
|
export type NotionPagePropertyInputValue =
|
|
86
|
-
|
|
81
|
+
Exclude<
|
|
82
|
+
NotionPagePropertyWriteMap[string],
|
|
83
|
+
{
|
|
84
|
+
type:
|
|
85
|
+
| "created_time"
|
|
86
|
+
| "last_edited_time"
|
|
87
|
+
| "created_by"
|
|
88
|
+
| "last_edited_by"
|
|
89
|
+
| "last_visited_time"
|
|
90
|
+
}
|
|
91
|
+
> extends infer PropertyValue
|
|
87
92
|
? PropertyValue extends { id: string }
|
|
88
93
|
? Omit<PropertyValue, "id"> & { id?: string }
|
|
89
94
|
: never
|
|
@@ -93,46 +98,46 @@ export type NotionPagePropertyInputMap = {
|
|
|
93
98
|
[propertyIdOrKey: string]: NotionPagePropertyInputValue
|
|
94
99
|
}
|
|
95
100
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
/** Whether the page or an ancestor is archived. */
|
|
103
|
-
is_archived: boolean
|
|
104
|
-
/** Whether the page or an ancestor is in Trash. */
|
|
105
|
-
in_trash: boolean
|
|
106
|
-
/**
|
|
107
|
-
* Keyed mapping of raw property IDs to their value. Includes all properties on the data source,
|
|
108
|
-
* including the four built-ins (`created_time`, `last_edited_time`, `created_by`,
|
|
109
|
-
* `last_edited_by`).
|
|
110
|
-
*/
|
|
111
|
-
propertiesById: { [propertyId: string]: NotionDataSourceValue | undefined }
|
|
112
|
-
/**
|
|
113
|
-
* Keyed mapping of user-defined keys to their value. Only includes properties that are mapped
|
|
114
|
-
* to a raw property ID in the data source's `propertyIdsByKey`.
|
|
115
|
-
*/
|
|
116
|
-
propertiesByKey: { [key: string]: NotionDataSourceValue | undefined }
|
|
117
|
-
/**
|
|
118
|
-
* Updates this page using the data source's property-key bindings. The SDK resolves property
|
|
119
|
-
* keys to raw property IDs before sending the bridge message to the host.
|
|
120
|
-
*/
|
|
121
|
-
update: (args: NotionDataSourcePageUpdateArgs) => Promise<UpdatePageResult>
|
|
122
|
-
/** Archives this page. Shorthand for update({ is_archived: true }). */
|
|
101
|
+
export type NotionPage = InferOutput<typeof notionPageDataSchema> & {
|
|
102
|
+
dataSourceKey: string | null
|
|
103
|
+
getPropertyById: (propertyId: string) => NotionPagePropertyValue | null
|
|
104
|
+
getPropertyByKey: (propertyKey: string) => NotionPagePropertyValue | null
|
|
105
|
+
updateById: (payload: PageUpdatePayload) => Promise<UpdatePageResult>
|
|
106
|
+
updateByKey: (payload: PageUpdatePayload) => Promise<UpdatePageResult>
|
|
123
107
|
archive: () => Promise<UpdatePageResult>
|
|
124
|
-
/** Unarchives this page. Shorthand for update({ is_archived: false }). */
|
|
125
108
|
unarchive: () => Promise<UpdatePageResult>
|
|
126
109
|
}
|
|
127
110
|
|
|
128
|
-
export type
|
|
111
|
+
export type PageUpdatePayload = {
|
|
129
112
|
properties?: NotionPagePropertyInputMap
|
|
130
113
|
icon?: NotionPageIcon
|
|
131
114
|
cover?: NotionPageCover
|
|
132
|
-
/** Whether to archive this page. */
|
|
133
115
|
is_archived?: boolean
|
|
134
116
|
}
|
|
135
117
|
|
|
118
|
+
export type GetPageResult =
|
|
119
|
+
| { status: "success"; page: NotionPage }
|
|
120
|
+
| { status: "error"; error: CustomBlockGetPageErrorInfo }
|
|
121
|
+
|
|
122
|
+
export type CreatePageResult =
|
|
123
|
+
| { status: "success"; page: NotionPage }
|
|
124
|
+
| { status: "error"; error: CustomBlockCreatePageErrorInfo }
|
|
125
|
+
|
|
126
|
+
export type DataSourcesPageResult =
|
|
127
|
+
| {
|
|
128
|
+
status: "success"
|
|
129
|
+
page: NotionPage
|
|
130
|
+
propertyIdsByKey: Record<string, string | undefined>
|
|
131
|
+
}
|
|
132
|
+
| {
|
|
133
|
+
status: "error"
|
|
134
|
+
error: CustomBlockGetPageErrorInfo | CustomBlockCreatePageErrorInfo
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export type DataSourcesQueryResult =
|
|
138
|
+
| ({ status: "success" } & DataSourceSnapshot)
|
|
139
|
+
| { status: "error"; error: CustomBlockQueryDataSourceErrorInfo }
|
|
140
|
+
|
|
136
141
|
export type NotionDataSourceTextFilterOperator =
|
|
137
142
|
ExclusiveUnion<CustomBlockTextFilterOperator>
|
|
138
143
|
export type NotionDataSourceNumberFilterOperator =
|
|
@@ -186,42 +191,12 @@ export type NotionDataSourceFilter = ExclusiveUnion<
|
|
|
186
191
|
NotionDataSourcePropertyFilter | { and: NotionDataSourcePropertyFilter[] }
|
|
187
192
|
>
|
|
188
193
|
|
|
189
|
-
/**
|
|
190
|
-
* Latest live snapshot of a data source query.
|
|
191
|
-
*
|
|
192
|
-
* - `items` — the rows the host has returned so far. Empty until the first response arrives.
|
|
193
|
-
* - `isLoading` — `true` while a query is in flight.
|
|
194
|
-
* - `hasMore` — `true` if the host indicated more rows are available beyond the requested prefix.
|
|
195
|
-
* - `error` — structured error information if the most recent query failed.
|
|
196
|
-
*/
|
|
194
|
+
/** Latest live snapshot of a data source query. */
|
|
197
195
|
export type DataSourceSnapshot = {
|
|
198
|
-
items:
|
|
199
|
-
/**
|
|
200
|
-
* Contains only properties in the database schema.
|
|
201
|
-
* Use this map to resolve property bindings and validate filter and sort references.
|
|
202
|
-
* Undefined when the host does not provide a schema.
|
|
203
|
-
*/
|
|
204
|
-
collectionSchema?: NotionCollectionSchema
|
|
205
|
-
/**
|
|
206
|
-
* Contains database properties and four synthetic metadata fields.
|
|
207
|
-
* Use this map to describe values in query snapshots.
|
|
208
|
-
* Synthetic entries do not establish filter or sort support.
|
|
209
|
-
*/
|
|
210
|
-
propertySchemasById: { [propertyId: string]: NotionPropertySchema }
|
|
211
|
-
/**
|
|
212
|
-
* Maps user-defined property keys from the manifest to raw Notion property IDs.
|
|
213
|
-
* Keys mapped to `undefined` indicate a declared-but-unbound slot.
|
|
214
|
-
*/
|
|
196
|
+
items: NotionPage[]
|
|
215
197
|
propertyIdsByKey: { [key: string]: string | undefined }
|
|
216
|
-
/**
|
|
217
|
-
* Per-property schemas keyed by user-defined keys from the data source's `propertyIdsByKey`.
|
|
218
|
-
* Built-ins are NOT included here. Keys mapped to `undefined` indicate a declared-but-unbound
|
|
219
|
-
* slot.
|
|
220
|
-
*/
|
|
221
|
-
propertySchemasByKey: { [key: string]: NotionPropertySchema | undefined }
|
|
222
198
|
isLoading: boolean
|
|
223
199
|
hasMore: boolean
|
|
224
|
-
error?: CustomBlockQueryDataSourceErrorInfo
|
|
225
200
|
}
|
|
226
201
|
|
|
227
202
|
export type DataSourceQueryOptions = {
|
|
@@ -269,30 +244,6 @@ export type CreatePageArgs = {
|
|
|
269
244
|
position?: NotionCreatePagePosition
|
|
270
245
|
}
|
|
271
246
|
|
|
272
|
-
/**
|
|
273
|
-
* The result of a `sdk.pages.create` API call.
|
|
274
|
-
*/
|
|
275
|
-
export type CreatePageResult = BridgeMessagePayload<
|
|
276
|
-
CreatePageResultMessage,
|
|
277
|
-
CustomBlockCreatePageErrorInfo
|
|
278
|
-
>
|
|
279
|
-
|
|
280
|
-
export type UpdatePageArgs = Omit<
|
|
281
|
-
UpdatePageMessage,
|
|
282
|
-
"type" | "requestId" | "archived"
|
|
283
|
-
> & {
|
|
284
|
-
/** Whether to archive this page. */
|
|
285
|
-
is_archived?: boolean
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Result of `sdk.pages.get`.
|
|
290
|
-
*/
|
|
291
|
-
export type GetPageResult = BridgeMessagePayload<
|
|
292
|
-
GetPageResultMessage,
|
|
293
|
-
CustomBlockGetPageErrorInfo
|
|
294
|
-
>
|
|
295
|
-
|
|
296
247
|
export type OpenPageOptions = {
|
|
297
248
|
mode: OpenPageMode
|
|
298
249
|
}
|
|
@@ -302,14 +253,15 @@ export type OpenPageResult = BridgeMessagePayload<
|
|
|
302
253
|
CustomBlockOpenPageErrorInfo
|
|
303
254
|
>
|
|
304
255
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
export type UpdatePageResult = BridgeMessagePayload<
|
|
309
|
-
UpdatePageResultMessage,
|
|
310
|
-
CustomBlockUpdatePageErrorInfo
|
|
256
|
+
export type OpenExternalUrlResult = BridgeMessagePayload<
|
|
257
|
+
OpenExternalUrlResultMessage,
|
|
258
|
+
CustomBlockOpenExternalUrlErrorInfo
|
|
311
259
|
>
|
|
312
260
|
|
|
261
|
+
export type UpdatePageResult =
|
|
262
|
+
| { status: "success"; page: NotionPage }
|
|
263
|
+
| { status: "error"; error: CustomBlockUpdatePageErrorInfo }
|
|
264
|
+
|
|
313
265
|
export type ListUsersArgs = Omit<ListUsersMessage, "type" | "requestId">
|
|
314
266
|
|
|
315
267
|
export type ListUsersResult = BridgeMessagePayload<
|