@notionhq/custom-blocks 0.3.2 → 0.3.3
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 +2 -8
- package/dist/bridge/SandboxBridge.d.ts.map +1 -1
- package/dist/bridge/SandboxBridge.js +12 -32
- package/dist/bridge/dataSources/resolveProperty.d.ts +1 -20
- package/dist/bridge/dataSources/resolveProperty.d.ts.map +1 -1
- package/dist/bridge/dataSources/resolveProperty.js +37 -32
- package/dist/bridge/page.d.ts +6 -0
- package/dist/bridge/page.d.ts.map +1 -1
- package/dist/bridge/page.js +17 -0
- package/dist/bridge/sandboxClient.d.ts.map +1 -1
- package/dist/bridge/sandboxClient.js +5 -2
- package/dist/protocol/protocolVersion.d.ts +2 -1
- package/dist/protocol/protocolVersion.js +3 -1
- package/dist/version.js +1 -1
- package/docs/build-tools.md +55 -0
- package/docs/data-sources.md +3 -1
- package/docs/deployment.md +16 -34
- package/docs/errors.md +65 -52
- package/docs/pages.md +79 -75
- package/package.json +2 -18
- package/src/bridge/SandboxBridge.ts +14 -39
- package/src/bridge/dataSources/resolveProperty.ts +48 -39
- package/src/bridge/page.ts +24 -0
- package/src/bridge/sandboxClient.ts +6 -3
|
@@ -21,35 +21,46 @@ function makePropertyError(
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Resolves a public SDK property write map into the ID-keyed bridge shape.
|
|
24
|
-
*
|
|
25
|
-
* Identifiers in `properties` may be raw property IDs or data source property keys; the latter
|
|
26
|
-
* are looked up in the `dataSource`'s `propertyIdsByKey`. Each value is re-parsed through
|
|
27
|
-
* the page property value schema with its `id` rewritten to the resolved property ID.
|
|
24
|
+
* A data source makes every identifier a property key; without one, identifiers are raw IDs.
|
|
28
25
|
*/
|
|
29
26
|
export function resolvePropertyWriteMapForDataSource(args: {
|
|
30
27
|
dataSource: NotionDataSource | undefined
|
|
31
28
|
properties: NotionPagePropertyInputMap
|
|
32
|
-
operationName: string
|
|
33
29
|
}): PropertyWriteMapResolutionResult {
|
|
34
|
-
const { dataSource, properties
|
|
30
|
+
const { dataSource, properties } = args
|
|
31
|
+
const parsedMap = v.safeParse(v.record(v.string(), v.unknown()), properties)
|
|
32
|
+
if (Array.isArray(properties) || !parsedMap.success) {
|
|
33
|
+
return {
|
|
34
|
+
status: "error",
|
|
35
|
+
error: makePropertyError(
|
|
36
|
+
"invalid_property_value",
|
|
37
|
+
"Property maps must be objects.",
|
|
38
|
+
),
|
|
39
|
+
}
|
|
40
|
+
}
|
|
35
41
|
|
|
36
|
-
const resolvedProperties: CreatePageMessage["properties"] =
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
const resolvedProperties: CreatePageMessage["properties"] =
|
|
43
|
+
Object.create(null)
|
|
44
|
+
for (const [identifier, value] of Object.entries(parsedMap.output)) {
|
|
45
|
+
const propertyIdResult = resolveWriteIdentifier({
|
|
39
46
|
dataSource,
|
|
40
47
|
identifier,
|
|
41
|
-
operationName,
|
|
42
48
|
})
|
|
43
49
|
if (propertyIdResult.status === "error") {
|
|
44
50
|
return propertyIdResult
|
|
45
51
|
}
|
|
46
52
|
|
|
47
53
|
const propertyId = propertyIdResult.propertyId
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
55
|
+
return {
|
|
56
|
+
status: "error",
|
|
57
|
+
error: makePropertyError(
|
|
58
|
+
"invalid_property_value",
|
|
59
|
+
`Invalid value for property ${identifier}.`,
|
|
60
|
+
),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if ("id" in value && value.id !== undefined && value.id !== propertyId) {
|
|
53
64
|
return {
|
|
54
65
|
status: "error",
|
|
55
66
|
error: makePropertyError(
|
|
@@ -58,7 +69,7 @@ export function resolvePropertyWriteMapForDataSource(args: {
|
|
|
58
69
|
),
|
|
59
70
|
}
|
|
60
71
|
}
|
|
61
|
-
if (resolvedProperties
|
|
72
|
+
if (Object.hasOwn(resolvedProperties, propertyId)) {
|
|
62
73
|
return {
|
|
63
74
|
status: "error",
|
|
64
75
|
error: makePropertyError(
|
|
@@ -89,35 +100,33 @@ export function resolvePropertyWriteMapForDataSource(args: {
|
|
|
89
100
|
}
|
|
90
101
|
|
|
91
102
|
/**
|
|
92
|
-
*
|
|
93
|
-
* a
|
|
103
|
+
* Resolves property keys when a data source is present and raw property IDs otherwise.
|
|
104
|
+
* Property keys require a bound, loaded property so an unresolved key cannot become a raw ID.
|
|
94
105
|
*/
|
|
95
|
-
|
|
106
|
+
function resolveWriteIdentifier(args: {
|
|
96
107
|
dataSource: NotionDataSource | undefined
|
|
97
108
|
identifier: string
|
|
98
|
-
operationName: string
|
|
99
109
|
}):
|
|
100
110
|
| { status: "success"; propertyId: string }
|
|
101
111
|
| { status: "error"; error: CustomBlockPropertyErrorInfo } {
|
|
102
|
-
const { dataSource, identifier
|
|
103
|
-
if (dataSource
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
const { dataSource, identifier } = args
|
|
113
|
+
if (dataSource === undefined) {
|
|
114
|
+
return { status: "success", propertyId: identifier }
|
|
115
|
+
}
|
|
116
|
+
const propertyId = Object.hasOwn(dataSource.propertyIdsByKey, identifier)
|
|
117
|
+
? dataSource.propertyIdsByKey[identifier]
|
|
118
|
+
: undefined
|
|
119
|
+
if (
|
|
120
|
+
propertyId === undefined ||
|
|
121
|
+
!dataSource.collectionSchema?.propertiesById[propertyId]
|
|
122
|
+
) {
|
|
123
|
+
return {
|
|
124
|
+
status: "error",
|
|
125
|
+
error: makePropertyError(
|
|
126
|
+
"unmapped_property",
|
|
127
|
+
`Cannot resolve property key "${identifier}" on this page.`,
|
|
128
|
+
),
|
|
118
129
|
}
|
|
119
|
-
return { status: "success", propertyId }
|
|
120
130
|
}
|
|
121
|
-
|
|
122
|
-
return { status: "success", propertyId: identifier }
|
|
131
|
+
return { status: "success", propertyId }
|
|
123
132
|
}
|
package/src/bridge/page.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/dataSource.js"
|
|
1
2
|
import type { notionPageDataSchema } from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
2
3
|
import type { InferOutput } from "valibot"
|
|
3
4
|
import type { NotionPage } from "../types.js"
|
|
@@ -21,3 +22,26 @@ export function toNotionPageFromNotionPageData(args: {
|
|
|
21
22
|
unarchive: () => args.updateById({ is_archived: false }),
|
|
22
23
|
}
|
|
23
24
|
}
|
|
25
|
+
|
|
26
|
+
/** Prevents key-based property updates from falling back to raw IDs without data source context. */
|
|
27
|
+
export function makeUpdatePageByKey(args: {
|
|
28
|
+
dataSource: NotionDataSource | undefined
|
|
29
|
+
update: NotionPage["updateByKey"]
|
|
30
|
+
}): NotionPage["updateByKey"] {
|
|
31
|
+
return async payload => {
|
|
32
|
+
if (
|
|
33
|
+
args.dataSource === undefined &&
|
|
34
|
+
Object.keys(payload.properties ?? {}).length > 0
|
|
35
|
+
) {
|
|
36
|
+
return {
|
|
37
|
+
status: "error",
|
|
38
|
+
error: {
|
|
39
|
+
code: "unmapped_property",
|
|
40
|
+
message: "Key-based property updates require data source context.",
|
|
41
|
+
isRetryable: false,
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return args.update(payload)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
} from "./hostState.js"
|
|
22
22
|
import type { ManifestLoadResult } from "./loadManifest.js"
|
|
23
23
|
import type { MessageInterceptor } from "./messageInterceptor.js"
|
|
24
|
-
import { toNotionPageFromNotionPageData } from "./page.js"
|
|
24
|
+
import { makeUpdatePageByKey, toNotionPageFromNotionPageData } from "./page.js"
|
|
25
25
|
import {
|
|
26
26
|
type MessageLogEntry,
|
|
27
27
|
type QueryDataSourceArgs,
|
|
@@ -118,8 +118,11 @@ export const customBlockDataSources = {
|
|
|
118
118
|
bridge.getPropertyByKey({ page, dataSource, propertyKey }),
|
|
119
119
|
updateById: payload =>
|
|
120
120
|
update({ ...payload, pageId: page.id, dataSource: undefined }),
|
|
121
|
-
updateByKey:
|
|
122
|
-
|
|
121
|
+
updateByKey: makeUpdatePageByKey({
|
|
122
|
+
dataSource,
|
|
123
|
+
update: payload =>
|
|
124
|
+
update({ ...payload, pageId: page.id, dataSource }),
|
|
125
|
+
}),
|
|
123
126
|
})
|
|
124
127
|
return page
|
|
125
128
|
},
|