@notionhq/custom-blocks 0.3.3 → 0.3.4
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/LICENSE +21 -0
- package/README.md +11 -11
- package/dist/bridge/SandboxBridge.d.ts +0 -2
- package/dist/bridge/SandboxBridge.d.ts.map +1 -1
- package/dist/bridge/SandboxBridge.js +5 -8
- package/dist/bridge/SandboxInitialization.d.ts +1 -3
- package/dist/bridge/SandboxInitialization.d.ts.map +1 -1
- package/dist/bridge/SandboxInitialization.js +13 -15
- package/dist/bridge/appearance.d.ts +2 -10
- package/dist/bridge/appearance.d.ts.map +1 -1
- package/dist/bridge/appearance.js +2 -10
- package/dist/bridge/dataSources/query.d.ts +1 -2
- package/dist/bridge/dataSources/query.d.ts.map +1 -1
- package/dist/bridge/dataSources/query.js +15 -398
- package/dist/bridge/dataSources/queryFilter.d.ts +10 -0
- package/dist/bridge/dataSources/queryFilter.d.ts.map +1 -0
- package/dist/bridge/dataSources/queryFilter.js +123 -0
- package/dist/bridge/dataSources/resolvePropertyAddress.d.ts +10 -0
- package/dist/bridge/dataSources/resolvePropertyAddress.d.ts.map +1 -0
- package/dist/bridge/dataSources/resolvePropertyAddress.js +55 -0
- package/dist/bridge/dataSources/resolveSorts.d.ts +10 -0
- package/dist/bridge/dataSources/resolveSorts.d.ts.map +1 -0
- package/dist/bridge/dataSources/resolveSorts.js +80 -0
- package/dist/bridge/hostState.d.ts +3 -7
- package/dist/bridge/hostState.d.ts.map +1 -1
- package/dist/bridge/hostState.js +0 -24
- package/dist/bridge/page.d.ts +3 -3
- package/dist/bridge/page.d.ts.map +1 -1
- package/dist/bridge/page.js +2 -2
- package/dist/bridge/readLocalManifest.d.ts +23 -0
- package/dist/bridge/readLocalManifest.d.ts.map +1 -0
- package/dist/bridge/readLocalManifest.js +46 -0
- package/dist/bridge/sandboxClient.d.ts +0 -2
- package/dist/bridge/sandboxClient.d.ts.map +1 -1
- package/dist/bridge/sandboxClient.js +50 -27
- package/dist/protocol/dataSources/dataSourcePage.d.ts +3 -4
- package/dist/protocol/dataSources/dataSourcePage.js +2 -3
- package/dist/protocol/index.d.ts +1 -0
- package/dist/protocol/index.js +1 -0
- package/dist/protocol/isLocalBlockServerUrl.d.ts +2 -0
- package/dist/protocol/isLocalBlockServerUrl.js +9 -0
- package/dist/protocol/messages/connect.d.ts +2 -2
- package/dist/protocol/messages/connect.js +2 -2
- package/dist/protocol/messages/createPage.d.ts +1 -1
- package/dist/protocol/messages/createPage.js +1 -1
- package/dist/protocol/messages/createPageResult.d.ts +3 -3
- package/dist/protocol/messages/createPageResult.js +4 -4
- package/dist/protocol/messages/getPage.d.ts +2 -2
- package/dist/protocol/messages/getPage.js +4 -4
- package/dist/protocol/messages/hostToSandbox.d.ts +3 -3
- package/dist/protocol/messages/hostToSandbox.js +10 -10
- package/dist/protocol/messages/init.d.ts +1 -1
- package/dist/protocol/messages/queryDataSource.d.ts +24 -0
- package/dist/protocol/messages/queryDataSource.js +2 -0
- package/dist/protocol/messages/queryDataSourceResult.d.ts +3 -3
- package/dist/protocol/messages/queryDataSourceResult.js +5 -5
- package/dist/protocol/messages/sandboxToHost.d.ts +8 -0
- package/dist/protocol/messages/updatePageResult.d.ts +3 -3
- package/dist/protocol/messages/updatePageResult.js +4 -4
- package/dist/protocol/pages/page.d.ts +4 -4
- package/dist/protocol/pages/page.js +2 -2
- package/dist/protocol/parent.d.ts +1 -1
- package/dist/protocol/protocolVersion.d.ts +2 -2
- package/dist/protocol/protocolVersion.js +2 -2
- package/dist/react/useDataSource.d.ts +2 -2
- package/dist/react/useDataSource.js +2 -2
- package/dist/types.d.ts +5 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/docs/block-location.md +6 -4
- package/docs/bridge-interceptors.md +4 -0
- package/docs/build-tools.md +5 -2
- package/docs/data-sources.md +21 -17
- package/docs/deployment.md +16 -11
- package/docs/errors.md +11 -1
- package/docs/lifecycle.md +10 -3
- package/docs/pages.md +7 -3
- package/docs/users.md +4 -4
- package/package.json +3 -2
- package/src/bridge/SandboxBridge.ts +5 -10
- package/src/bridge/SandboxInitialization.ts +14 -20
- package/src/bridge/appearance.ts +2 -10
- package/src/bridge/dataSources/query.ts +17 -530
- package/src/bridge/dataSources/queryFilter.ts +181 -0
- package/src/bridge/dataSources/resolvePropertyAddress.ts +62 -0
- package/src/bridge/dataSources/resolveSorts.ts +107 -0
- package/src/bridge/hostState.ts +3 -43
- package/src/bridge/page.ts +4 -4
- package/src/bridge/readLocalManifest.ts +60 -0
- package/src/bridge/sandboxClient.ts +63 -41
- package/src/react/useDataSource.ts +2 -2
- package/src/types.ts +5 -5
- package/dist/bridge/loadManifest.d.ts +0 -21
- package/dist/bridge/loadManifest.d.ts.map +0 -1
- package/dist/bridge/loadManifest.js +0 -85
- package/src/bridge/loadManifest.ts +0 -114
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
2
|
+
import {
|
|
3
|
+
customBlockRelativeDateValueSchema,
|
|
4
|
+
type ResolvedCustomBlockDataSourceFilter,
|
|
5
|
+
type ResolvedCustomBlockDataSourcePropertyFilter,
|
|
6
|
+
resolvedCustomBlockDataSourcePropertyFilterSchema,
|
|
7
|
+
} from "@notionhq/custom-blocks-protocol/messages/queryDataSource.js"
|
|
8
|
+
import * as v from "valibot"
|
|
9
|
+
import { resolvePropertyAddress } from "./resolvePropertyAddress.js"
|
|
10
|
+
|
|
11
|
+
const MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN = 25
|
|
12
|
+
|
|
13
|
+
type ResolvePropertyFilterResult =
|
|
14
|
+
| { status: "ok"; filter: ResolvedCustomBlockDataSourcePropertyFilter }
|
|
15
|
+
| { status: "error"; error: string }
|
|
16
|
+
|
|
17
|
+
export function resolveFilter(
|
|
18
|
+
filter: unknown,
|
|
19
|
+
dataSource: NotionDataSource,
|
|
20
|
+
):
|
|
21
|
+
| { status: "ok"; filter?: ResolvedCustomBlockDataSourceFilter }
|
|
22
|
+
| { status: "error"; error: string } {
|
|
23
|
+
if (filter === undefined) {
|
|
24
|
+
return { status: "ok" }
|
|
25
|
+
}
|
|
26
|
+
if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
|
|
27
|
+
return {
|
|
28
|
+
status: "error",
|
|
29
|
+
error: "Data source query filter must be an object.",
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const filterObject = filter as Record<string, unknown>
|
|
33
|
+
if (Object.hasOwn(filterObject, "and")) {
|
|
34
|
+
if (
|
|
35
|
+
Object.keys(filterObject).length !== 1 ||
|
|
36
|
+
!Array.isArray(filterObject.and)
|
|
37
|
+
) {
|
|
38
|
+
return {
|
|
39
|
+
status: "error",
|
|
40
|
+
error: 'Data source query filter "and" must be an array.',
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (filterObject.and.length > MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN) {
|
|
44
|
+
return {
|
|
45
|
+
status: "error",
|
|
46
|
+
error: `Data source query filter "and" may contain at most ${MAX_DATA_SOURCE_QUERY_FILTER_CHILDREN} children.`,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const and: ResolvedCustomBlockDataSourcePropertyFilter[] = []
|
|
50
|
+
for (const child of filterObject.and) {
|
|
51
|
+
const resolved = resolvePropertyFilter(child, dataSource)
|
|
52
|
+
if (resolved.status === "error") {
|
|
53
|
+
return resolved
|
|
54
|
+
}
|
|
55
|
+
and.push(resolved.filter)
|
|
56
|
+
}
|
|
57
|
+
return { status: "ok", filter: { and } }
|
|
58
|
+
}
|
|
59
|
+
return resolvePropertyFilter(filterObject, dataSource)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function resolvePropertyFilter(
|
|
63
|
+
filter: unknown,
|
|
64
|
+
dataSource: NotionDataSource,
|
|
65
|
+
): ResolvePropertyFilterResult {
|
|
66
|
+
if (typeof filter !== "object" || filter === null || Array.isArray(filter)) {
|
|
67
|
+
return {
|
|
68
|
+
status: "error",
|
|
69
|
+
error: "Data source query property filter must be an object.",
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const filterObject = filter as Record<string, unknown>
|
|
73
|
+
const property = resolvePropertyAddress(filterObject, dataSource)
|
|
74
|
+
if (property.status === "error") {
|
|
75
|
+
return property
|
|
76
|
+
}
|
|
77
|
+
const branchKeys = Object.keys(filterObject).filter(
|
|
78
|
+
key => key !== "key" && key !== "propertyId",
|
|
79
|
+
)
|
|
80
|
+
if (branchKeys.length !== 1) {
|
|
81
|
+
return {
|
|
82
|
+
status: "error",
|
|
83
|
+
error:
|
|
84
|
+
"Data source query property filter must contain exactly one property branch.",
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const branch = branchKeys[0]
|
|
88
|
+
const value = filterObject[branch]
|
|
89
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
90
|
+
return invalidOperator(branch)
|
|
91
|
+
}
|
|
92
|
+
const operatorKeys = Object.keys(value).filter(
|
|
93
|
+
key => branch !== "date" || key !== "target",
|
|
94
|
+
)
|
|
95
|
+
if (operatorKeys.length !== 1) {
|
|
96
|
+
return invalidOperator(branch)
|
|
97
|
+
}
|
|
98
|
+
if (property.propertyType !== branch) {
|
|
99
|
+
return {
|
|
100
|
+
status: "error",
|
|
101
|
+
error: `Data source query filter branch "${branch}" does not match property type "${property.propertyType}".`,
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return parsePropertyFilter({
|
|
106
|
+
propertyId: property.propertyId,
|
|
107
|
+
branch,
|
|
108
|
+
value,
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function parsePropertyFilter(args: {
|
|
113
|
+
propertyId: string
|
|
114
|
+
branch: string
|
|
115
|
+
value: unknown
|
|
116
|
+
}): ResolvePropertyFilterResult {
|
|
117
|
+
const { propertyId, branch, value } = args
|
|
118
|
+
const parsed = v.safeParse(
|
|
119
|
+
resolvedCustomBlockDataSourcePropertyFilterSchema,
|
|
120
|
+
{ propertyId, [branch]: value },
|
|
121
|
+
)
|
|
122
|
+
if (!parsed.success) {
|
|
123
|
+
if (
|
|
124
|
+
resolvedCustomBlockDataSourcePropertyFilterSchema.options.every(
|
|
125
|
+
schema => !Object.hasOwn(schema.entries, branch),
|
|
126
|
+
)
|
|
127
|
+
) {
|
|
128
|
+
return {
|
|
129
|
+
status: "error",
|
|
130
|
+
error: `Data source query filter branch "${branch}" is not supported.`,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return invalidOperator(branch)
|
|
134
|
+
}
|
|
135
|
+
const filter = parsed.output
|
|
136
|
+
if (
|
|
137
|
+
"number" in filter &&
|
|
138
|
+
!Object.values(filter.number).every(
|
|
139
|
+
entry => typeof entry !== "number" || Number.isFinite(entry),
|
|
140
|
+
)
|
|
141
|
+
) {
|
|
142
|
+
return invalidOperator("number")
|
|
143
|
+
}
|
|
144
|
+
if (
|
|
145
|
+
"date" in filter &&
|
|
146
|
+
!Object.entries(filter.date).every(
|
|
147
|
+
([key, entry]) =>
|
|
148
|
+
key === "target" ||
|
|
149
|
+
entry === true ||
|
|
150
|
+
typeof entry === "object" ||
|
|
151
|
+
v.is(customBlockRelativeDateValueSchema, entry) ||
|
|
152
|
+
isValidIsoDate(entry),
|
|
153
|
+
)
|
|
154
|
+
) {
|
|
155
|
+
return invalidOperator("date")
|
|
156
|
+
}
|
|
157
|
+
return { status: "ok", filter }
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function invalidOperator(branch: string): { status: "error"; error: string } {
|
|
161
|
+
return {
|
|
162
|
+
status: "error",
|
|
163
|
+
error: `Data source query filter branch "${branch}" has an invalid operator or value.`,
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isValidIsoDate(value: string): boolean {
|
|
168
|
+
const dateOnly = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value)
|
|
169
|
+
if (dateOnly !== null) {
|
|
170
|
+
const [, year, month, day] = dateOnly
|
|
171
|
+
const date = new Date(
|
|
172
|
+
Date.UTC(Number(year), Number(month) - 1, Number(day)),
|
|
173
|
+
)
|
|
174
|
+
return date.toISOString().slice(0, 10) === value
|
|
175
|
+
}
|
|
176
|
+
return (
|
|
177
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?(?:Z|[+-]\d{2}:\d{2})$/.test(
|
|
178
|
+
value,
|
|
179
|
+
) && Number.isFinite(Date.parse(value))
|
|
180
|
+
)
|
|
181
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
2
|
+
|
|
3
|
+
export function resolvePropertyAddress(
|
|
4
|
+
value: Record<string, unknown>,
|
|
5
|
+
dataSource: NotionDataSource,
|
|
6
|
+
):
|
|
7
|
+
| { status: "ok"; propertyId: string; propertyType: string }
|
|
8
|
+
| { status: "error"; error: string } {
|
|
9
|
+
const hasKey = Object.hasOwn(value, "key")
|
|
10
|
+
const hasPropertyId = Object.hasOwn(value, "propertyId")
|
|
11
|
+
if (hasKey === hasPropertyId) {
|
|
12
|
+
return {
|
|
13
|
+
status: "error",
|
|
14
|
+
error:
|
|
15
|
+
"Data source query filters and sorts must use exactly one of key or propertyId.",
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
let propertyId: string
|
|
19
|
+
if (hasKey) {
|
|
20
|
+
if (typeof value.key !== "string") {
|
|
21
|
+
return {
|
|
22
|
+
status: "error",
|
|
23
|
+
error: "Data source query property key must be a string.",
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!Object.hasOwn(dataSource.propertyIdsByKey, value.key)) {
|
|
27
|
+
return {
|
|
28
|
+
status: "error",
|
|
29
|
+
error: `Unknown property key "${value.key}" for data source "${dataSource.key}".`,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const resolvedPropertyId = dataSource.propertyIdsByKey[value.key]
|
|
33
|
+
if (resolvedPropertyId === undefined) {
|
|
34
|
+
return {
|
|
35
|
+
status: "error",
|
|
36
|
+
error: `Property key "${value.key}" for data source "${dataSource.key}" is not bound.`,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
propertyId = resolvedPropertyId
|
|
40
|
+
} else {
|
|
41
|
+
if (typeof value.propertyId !== "string") {
|
|
42
|
+
return {
|
|
43
|
+
status: "error",
|
|
44
|
+
error: "Data source query propertyId must be a string.",
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
propertyId = value.propertyId
|
|
48
|
+
}
|
|
49
|
+
const properties = dataSource.collectionSchema?.propertiesById ?? {}
|
|
50
|
+
if (!Object.hasOwn(properties, propertyId)) {
|
|
51
|
+
return {
|
|
52
|
+
status: "error",
|
|
53
|
+
error: `Unknown property ID "${propertyId}" for data source "${dataSource.key}".`,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const propertySchema = properties[propertyId]
|
|
57
|
+
return {
|
|
58
|
+
status: "ok",
|
|
59
|
+
propertyId,
|
|
60
|
+
propertyType: propertySchema.type,
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
2
|
+
import { CUSTOM_BLOCK_DATA_SOURCE_SORTABLE_PROPERTY_TYPES } from "@notionhq/custom-blocks-protocol/dataSources/propertySchema.js"
|
|
3
|
+
import { notionSortDirectionSchema } from "@notionhq/custom-blocks-protocol/dataSources/sortDirection.js"
|
|
4
|
+
import {
|
|
5
|
+
CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS,
|
|
6
|
+
type ResolvedCustomBlockDataSourceSort,
|
|
7
|
+
} from "@notionhq/custom-blocks-protocol/messages/queryDataSource.js"
|
|
8
|
+
import * as v from "valibot"
|
|
9
|
+
import { resolvePropertyAddress } from "./resolvePropertyAddress.js"
|
|
10
|
+
|
|
11
|
+
const supportedDataSourceSortPropertyTypes = new Set<string>(
|
|
12
|
+
CUSTOM_BLOCK_DATA_SOURCE_SORTABLE_PROPERTY_TYPES,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
export function resolveSorts(
|
|
16
|
+
sorts: unknown,
|
|
17
|
+
dataSource: NotionDataSource,
|
|
18
|
+
):
|
|
19
|
+
| { status: "ok"; sorts?: ResolvedCustomBlockDataSourceSort[] }
|
|
20
|
+
| { status: "error"; error: string } {
|
|
21
|
+
if (sorts === undefined) {
|
|
22
|
+
return { status: "ok" }
|
|
23
|
+
}
|
|
24
|
+
if (!Array.isArray(sorts)) {
|
|
25
|
+
return {
|
|
26
|
+
status: "error",
|
|
27
|
+
error: "Data source query sorts must be an array.",
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (sorts.length === 0) {
|
|
31
|
+
return { status: "ok" }
|
|
32
|
+
}
|
|
33
|
+
if (sorts.length > CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS) {
|
|
34
|
+
return {
|
|
35
|
+
status: "error",
|
|
36
|
+
error: `Data source query sorts may contain at most ${CUSTOM_BLOCK_DATA_SOURCE_QUERY_MAX_SORTS} entries.`,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const resolvedSorts: ResolvedCustomBlockDataSourceSort[] = []
|
|
41
|
+
const seenPropertyIds = new Set<string>()
|
|
42
|
+
for (const sort of sorts) {
|
|
43
|
+
const resolved = resolveSort(sort, dataSource)
|
|
44
|
+
if (resolved.status === "error") {
|
|
45
|
+
return resolved
|
|
46
|
+
}
|
|
47
|
+
if (seenPropertyIds.has(resolved.sort.propertyId)) {
|
|
48
|
+
return {
|
|
49
|
+
status: "error",
|
|
50
|
+
error: `Data source query sorts cannot contain duplicate property ID "${resolved.sort.propertyId}".`,
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
seenPropertyIds.add(resolved.sort.propertyId)
|
|
54
|
+
resolvedSorts.push(resolved.sort)
|
|
55
|
+
}
|
|
56
|
+
return { status: "ok", sorts: resolvedSorts }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function resolveSort(
|
|
60
|
+
sort: unknown,
|
|
61
|
+
dataSource: NotionDataSource,
|
|
62
|
+
):
|
|
63
|
+
| { status: "ok"; sort: ResolvedCustomBlockDataSourceSort }
|
|
64
|
+
| { status: "error"; error: string } {
|
|
65
|
+
if (typeof sort !== "object" || sort === null || Array.isArray(sort)) {
|
|
66
|
+
return {
|
|
67
|
+
status: "error",
|
|
68
|
+
error: "Data source query sort must be an object.",
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const sortObject = sort as Record<string, unknown>
|
|
72
|
+
if (
|
|
73
|
+
Object.keys(sortObject).some(
|
|
74
|
+
key => !["key", "propertyId", "direction"].includes(key),
|
|
75
|
+
)
|
|
76
|
+
) {
|
|
77
|
+
return {
|
|
78
|
+
status: "error",
|
|
79
|
+
error: "Data source query sort contains unsupported fields.",
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const property = resolvePropertyAddress(sortObject, dataSource)
|
|
83
|
+
if (property.status === "error") {
|
|
84
|
+
return property
|
|
85
|
+
}
|
|
86
|
+
if (!supportedDataSourceSortPropertyTypes.has(property.propertyType)) {
|
|
87
|
+
return {
|
|
88
|
+
status: "error",
|
|
89
|
+
error: `Data source query sorts do not yet support property type "${property.propertyType}".`,
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const direction = sortObject.direction
|
|
93
|
+
if (
|
|
94
|
+
!Object.hasOwn(sortObject, "direction") ||
|
|
95
|
+
!v.is(notionSortDirectionSchema, direction)
|
|
96
|
+
) {
|
|
97
|
+
return {
|
|
98
|
+
status: "error",
|
|
99
|
+
error:
|
|
100
|
+
'Data source query sort direction must be "ascending" or "descending".',
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
status: "ok",
|
|
105
|
+
sort: { propertyId: property.propertyId, direction },
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/bridge/hostState.ts
CHANGED
|
@@ -4,14 +4,14 @@ import type { NotionBlockId } from "@notionhq/custom-blocks-protocol/ids.js"
|
|
|
4
4
|
import type { CustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest.js"
|
|
5
5
|
import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
|
|
6
6
|
import type {
|
|
7
|
+
bridgePageSchema,
|
|
7
8
|
CustomBlockPage,
|
|
8
|
-
notionPageDataSchema,
|
|
9
9
|
} from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
10
10
|
import type { NotionParent } from "@notionhq/custom-blocks-protocol/parent.js"
|
|
11
11
|
import type { NotionTheme } from "@notionhq/custom-blocks-protocol/theme.js"
|
|
12
12
|
import type { NotionUser } from "@notionhq/custom-blocks-protocol/users/user.js"
|
|
13
13
|
import type { InferOutput } from "valibot"
|
|
14
|
-
import type { DataSourceSnapshot
|
|
14
|
+
import type { DataSourceSnapshot } from "../types.js"
|
|
15
15
|
|
|
16
16
|
export type CustomBlockHostState = UninitializedHostState | InitializedHostState
|
|
17
17
|
|
|
@@ -37,7 +37,7 @@ export type InitializedHostState = {
|
|
|
37
37
|
export type DataSourceQueryState = {
|
|
38
38
|
dataSourceKey: string
|
|
39
39
|
/** Latest pages from the host as parsed from the bridge. */
|
|
40
|
-
items: InferOutput<typeof
|
|
40
|
+
items: InferOutput<typeof bridgePageSchema>[]
|
|
41
41
|
isLoading: boolean
|
|
42
42
|
hasMore: boolean
|
|
43
43
|
error?: CustomBlockQueryDataSourceErrorInfo
|
|
@@ -57,43 +57,3 @@ export function createEmptyDataSourceQueryState(
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
export type DataSourceQueryView = DataSourceSnapshot
|
|
60
|
-
|
|
61
|
-
const EMPTY_QUERY_VIEW: DataSourceQueryView = {
|
|
62
|
-
items: [],
|
|
63
|
-
propertyIdsByKey: {},
|
|
64
|
-
isLoading: false,
|
|
65
|
-
hasMore: false,
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function getDataSourceQueryView(
|
|
69
|
-
hostState: CustomBlockHostState,
|
|
70
|
-
key: string,
|
|
71
|
-
subscriptionId: string,
|
|
72
|
-
createNotionPage: (args: {
|
|
73
|
-
page: InferOutput<typeof notionPageDataSchema>
|
|
74
|
-
dataSource: NotionDataSource | undefined
|
|
75
|
-
}) => NotionPage,
|
|
76
|
-
): DataSourceQueryView {
|
|
77
|
-
if (hostState.status !== "initialized") {
|
|
78
|
-
return EMPTY_QUERY_VIEW
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const dataSource = hostState.dataSources.find(entry => entry.key === key)
|
|
82
|
-
const subscriptionState = hostState.dataSourceState[subscriptionId]
|
|
83
|
-
const queryState =
|
|
84
|
-
subscriptionState?.dataSourceKey === key
|
|
85
|
-
? subscriptionState
|
|
86
|
-
: createEmptyDataSourceQueryState(key)
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
items:
|
|
90
|
-
dataSource === undefined
|
|
91
|
-
? []
|
|
92
|
-
: queryState.items.map(entry =>
|
|
93
|
-
createNotionPage({ page: entry, dataSource }),
|
|
94
|
-
),
|
|
95
|
-
propertyIdsByKey: dataSource?.propertyIdsByKey ?? {},
|
|
96
|
-
isLoading: queryState.isLoading,
|
|
97
|
-
hasMore: queryState.hasMore,
|
|
98
|
-
}
|
|
99
|
-
}
|
package/src/bridge/page.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
2
|
-
import type {
|
|
2
|
+
import type { bridgePageSchema } from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
3
3
|
import type { InferOutput } from "valibot"
|
|
4
4
|
import type { NotionPage } from "../types.js"
|
|
5
5
|
|
|
6
|
-
export function
|
|
7
|
-
|
|
6
|
+
export function toNotionPageFromBridgePage(args: {
|
|
7
|
+
bridgePage: InferOutput<typeof bridgePageSchema>
|
|
8
8
|
dataSourceKey: string | undefined
|
|
9
9
|
getPropertyById: NotionPage["getPropertyById"]
|
|
10
10
|
getPropertyByKey: NotionPage["getPropertyByKey"]
|
|
@@ -12,7 +12,7 @@ export function toNotionPageFromNotionPageData(args: {
|
|
|
12
12
|
updateByKey: NotionPage["updateByKey"]
|
|
13
13
|
}): NotionPage {
|
|
14
14
|
return {
|
|
15
|
-
...args.
|
|
15
|
+
...args.bridgePage,
|
|
16
16
|
dataSourceKey: args.dataSourceKey ?? null,
|
|
17
17
|
getPropertyById: args.getPropertyById,
|
|
18
18
|
getPropertyByKey: args.getPropertyByKey,
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/errors.js"
|
|
2
|
+
import { isLocalBlockServerUrl } from "@notionhq/custom-blocks-protocol/isLocalBlockServerUrl.js"
|
|
3
|
+
import {
|
|
4
|
+
type CustomBlockManifest,
|
|
5
|
+
manifestSchema,
|
|
6
|
+
} from "@notionhq/custom-blocks-protocol/manifest.js"
|
|
7
|
+
import * as v from "valibot"
|
|
8
|
+
|
|
9
|
+
export type LocalManifestReadResult =
|
|
10
|
+
| { status: "success"; manifest: CustomBlockManifest | undefined }
|
|
11
|
+
| { status: "error"; error: CustomBlockErrorInfo<"manifest_invalid"> }
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Reads and validates JSON from the script with ID `notion-custom-block-manifest`
|
|
15
|
+
* and type `application/json`, injected into the block HTML by dev tooling.
|
|
16
|
+
* The SDK sends this declaration in `connect` so the host can prepare bindings.
|
|
17
|
+
*
|
|
18
|
+
* The URL check limits this development metadata to supported local block servers.
|
|
19
|
+
* Deployed blocks receive their manifest from the host in `init`. Reading this
|
|
20
|
+
* metadata uses the current document and makes no network request.
|
|
21
|
+
*/
|
|
22
|
+
export function readLocalManifest(args: {
|
|
23
|
+
document: Document
|
|
24
|
+
url: URL
|
|
25
|
+
}): LocalManifestReadResult {
|
|
26
|
+
if (!isLocalBlockServerUrl(args.url)) {
|
|
27
|
+
return { status: "success", manifest: undefined }
|
|
28
|
+
}
|
|
29
|
+
const metadata = args.document.getElementById("notion-custom-block-manifest")
|
|
30
|
+
if (metadata === null) {
|
|
31
|
+
return { status: "success", manifest: undefined }
|
|
32
|
+
}
|
|
33
|
+
if (
|
|
34
|
+
metadata.tagName !== "SCRIPT" ||
|
|
35
|
+
metadata.getAttribute("type") !== "application/json"
|
|
36
|
+
) {
|
|
37
|
+
return invalidManifest()
|
|
38
|
+
}
|
|
39
|
+
let input: unknown
|
|
40
|
+
try {
|
|
41
|
+
input = JSON.parse(metadata.textContent ?? "")
|
|
42
|
+
} catch {
|
|
43
|
+
return invalidManifest()
|
|
44
|
+
}
|
|
45
|
+
const parsed = v.safeParse(manifestSchema, input)
|
|
46
|
+
return parsed.success
|
|
47
|
+
? { status: "success", manifest: parsed.output }
|
|
48
|
+
: invalidManifest()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function invalidManifest(): LocalManifestReadResult {
|
|
52
|
+
return {
|
|
53
|
+
status: "error",
|
|
54
|
+
error: {
|
|
55
|
+
code: "manifest_invalid",
|
|
56
|
+
message: "The local custom block metadata is not a valid manifest.",
|
|
57
|
+
isRetryable: false,
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -16,12 +16,11 @@ import type {
|
|
|
16
16
|
} from "../types.js"
|
|
17
17
|
import {
|
|
18
18
|
type CustomBlockHostState,
|
|
19
|
+
createEmptyDataSourceQueryState,
|
|
19
20
|
type DataSourceQueryView,
|
|
20
|
-
getDataSourceQueryView as getDataSourceQueryViewWithBridge,
|
|
21
21
|
} from "./hostState.js"
|
|
22
|
-
import type { ManifestLoadResult } from "./loadManifest.js"
|
|
23
22
|
import type { MessageInterceptor } from "./messageInterceptor.js"
|
|
24
|
-
import { makeUpdatePageByKey,
|
|
23
|
+
import { makeUpdatePageByKey, toNotionPageFromBridgePage } from "./page.js"
|
|
25
24
|
import {
|
|
26
25
|
type MessageLogEntry,
|
|
27
26
|
type QueryDataSourceArgs,
|
|
@@ -44,10 +43,6 @@ export const customBlockHost = {
|
|
|
44
43
|
return getBridge().registerMessageInterceptor(interceptor)
|
|
45
44
|
},
|
|
46
45
|
|
|
47
|
-
sendConnect: (manifestResult: ManifestLoadResult) => {
|
|
48
|
-
getBridge().sendConnect(manifestResult)
|
|
49
|
-
},
|
|
50
|
-
|
|
51
46
|
initialize: () => getBridge().initialize(),
|
|
52
47
|
|
|
53
48
|
subscribe: (listener: () => void) => {
|
|
@@ -75,6 +70,13 @@ export const customBlockHost = {
|
|
|
75
70
|
},
|
|
76
71
|
}
|
|
77
72
|
|
|
73
|
+
const EMPTY_QUERY_VIEW: DataSourceQueryView = {
|
|
74
|
+
items: [],
|
|
75
|
+
propertyIdsByKey: {},
|
|
76
|
+
isLoading: false,
|
|
77
|
+
hasMore: false,
|
|
78
|
+
}
|
|
79
|
+
|
|
78
80
|
export const customBlockDataSources = {
|
|
79
81
|
createSubscriptionId: () => {
|
|
80
82
|
return getBridge().createDataSourceSubscriptionId()
|
|
@@ -93,40 +95,60 @@ export const customBlockDataSources = {
|
|
|
93
95
|
key: string,
|
|
94
96
|
subscriptionId: string,
|
|
95
97
|
): DataSourceQueryView => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
98
|
+
if (hostState.status !== "initialized") {
|
|
99
|
+
return EMPTY_QUERY_VIEW
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const dataSource = hostState.dataSources.find(entry => entry.key === key)
|
|
103
|
+
const subscriptionState = hostState.dataSourceState[subscriptionId]
|
|
104
|
+
const queryState =
|
|
105
|
+
subscriptionState?.dataSourceKey === key
|
|
106
|
+
? subscriptionState
|
|
107
|
+
: createEmptyDataSourceQueryState(key)
|
|
108
|
+
|
|
109
|
+
const items =
|
|
110
|
+
dataSource === undefined
|
|
111
|
+
? []
|
|
112
|
+
: queryState.items.map(bridgePage => {
|
|
113
|
+
const bridge = getBridge()
|
|
114
|
+
const update = async (
|
|
115
|
+
args: Parameters<SandboxBridge["updatePage"]>[0],
|
|
116
|
+
): Promise<UpdatePageResult> => {
|
|
117
|
+
const result = await bridge.updatePage(args)
|
|
118
|
+
if (result.status === "error") {
|
|
119
|
+
return result
|
|
120
|
+
}
|
|
121
|
+
Object.assign(page, result.page)
|
|
122
|
+
return { status: "success", page }
|
|
123
|
+
}
|
|
124
|
+
const page: NotionPage = toNotionPageFromBridgePage({
|
|
125
|
+
bridgePage,
|
|
126
|
+
dataSourceKey: key,
|
|
127
|
+
getPropertyById: propertyId =>
|
|
128
|
+
bridge.getPropertyById({ page, propertyId }),
|
|
129
|
+
getPropertyByKey: propertyKey =>
|
|
130
|
+
bridge.getPropertyByKey({ page, dataSource, propertyKey }),
|
|
131
|
+
updateById: payload =>
|
|
132
|
+
update({
|
|
133
|
+
...payload,
|
|
134
|
+
pageId: page.id,
|
|
135
|
+
dataSource: undefined,
|
|
136
|
+
}),
|
|
137
|
+
updateByKey: makeUpdatePageByKey({
|
|
138
|
+
dataSource,
|
|
139
|
+
update: payload =>
|
|
140
|
+
update({ ...payload, pageId: page.id, dataSource }),
|
|
141
|
+
}),
|
|
142
|
+
})
|
|
143
|
+
return page
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
items,
|
|
148
|
+
propertyIdsByKey: dataSource?.propertyIdsByKey ?? {},
|
|
149
|
+
isLoading: queryState.isLoading,
|
|
150
|
+
hasMore: queryState.hasMore,
|
|
151
|
+
}
|
|
130
152
|
},
|
|
131
153
|
}
|
|
132
154
|
|
|
@@ -21,8 +21,8 @@ type DataSourceHookState = {
|
|
|
21
21
|
* Reads from the data source mapped to the given semantic `key`.
|
|
22
22
|
*
|
|
23
23
|
* @param key - The semantic data source key the block is wired to (e.g. `"people"`).
|
|
24
|
-
* @param options - Optional live snapshot options.
|
|
25
|
-
*
|
|
24
|
+
* @param options - Optional live snapshot options. The host enforces the maximum row
|
|
25
|
+
* limit. `sorts` are applied in array order.
|
|
26
26
|
*
|
|
27
27
|
* @example
|
|
28
28
|
* const { items, propertyIdsByKey, isLoading, hasMore, error } = useDataSource("default", { limit: 25 })
|
package/src/types.ts
CHANGED
|
@@ -41,11 +41,11 @@ import type {
|
|
|
41
41
|
import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
|
|
42
42
|
import type { CustomBlockUpdatePageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
|
|
43
43
|
import type {
|
|
44
|
+
bridgePageSchema,
|
|
44
45
|
NotionPageCover,
|
|
45
46
|
NotionPageIcon,
|
|
46
47
|
NotionPagePropertyValue,
|
|
47
48
|
NotionPagePropertyWriteMap,
|
|
48
|
-
notionPageDataSchema,
|
|
49
49
|
} from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
50
50
|
import type { InferOutput } from "valibot"
|
|
51
51
|
|
|
@@ -104,7 +104,7 @@ export type NotionPagePropertyInputMap = {
|
|
|
104
104
|
[propertyIdOrKey: string]: NotionPagePropertyInputValue
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
export type NotionPage = InferOutput<typeof
|
|
107
|
+
export type NotionPage = InferOutput<typeof bridgePageSchema> & {
|
|
108
108
|
dataSourceKey: string | null
|
|
109
109
|
getPropertyById: (propertyId: string) => NotionPagePropertyValue | null
|
|
110
110
|
getPropertyByKey: (propertyKey: string) => NotionPagePropertyValue | null
|
|
@@ -232,9 +232,9 @@ export type UseDataSourceOptions = DataSourceQueryOptions
|
|
|
232
232
|
* parent shape; see https://developers.notion.com/reference/data-source.
|
|
233
233
|
*
|
|
234
234
|
* - `page_id`: Create a child page under the given Notion page.
|
|
235
|
-
* - `data_source_id`: Create a row inside the given Notion data source
|
|
236
|
-
*
|
|
237
|
-
*
|
|
235
|
+
* - `data_source_id`: Create a row inside the given Notion data source. The public API's
|
|
236
|
+
* `data_source_id` is the same UUID as the `collectionPointer.id` exposed in `dataSources`,
|
|
237
|
+
* so either value works here.
|
|
238
238
|
* - `data_source_key`: Create a row inside the data source that the custom block was configured with
|
|
239
239
|
* under this semantic key. The SDK resolves the key to a `data_source_id` locally before
|
|
240
240
|
* sending the request to the host.
|