@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.
@@ -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, operationName } = args
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
- for (const [identifier, value] of Object.entries(properties)) {
38
- const propertyIdResult = resolvePropertyIdentifierForDataSource({
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
- value.id !== undefined &&
50
- value.id !== identifier &&
51
- value.id !== propertyId
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[propertyId] !== undefined) {
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
- * Treats bound data source keys as aliases for property IDs. An unbound key may still be used as
93
- * a raw ID when it exactly matches a property in the data source schema.
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
- export function resolvePropertyIdentifierForDataSource(args: {
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, operationName } = args
103
- if (dataSource !== undefined && identifier in dataSource.propertyIdsByKey) {
104
- const propertyId = dataSource.propertyIdsByKey[identifier]
105
- if (propertyId === undefined) {
106
- // If the property is not bound to a key, check if it is a valid property ID. If so, we
107
- // can use it directly.
108
- if (identifier in dataSource.propertySchemasById) {
109
- return { status: "success", propertyId: identifier }
110
- }
111
- return {
112
- status: "error",
113
- error: makePropertyError(
114
- "unmapped_property",
115
- `${operationName} cannot resolve property key "${identifier}" because it is not bound to a Notion property.`,
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
  }
@@ -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: payload =>
122
- update({ ...payload, pageId: page.id, dataSource }),
121
+ updateByKey: makeUpdatePageByKey({
122
+ dataSource,
123
+ update: payload =>
124
+ update({ ...payload, pageId: page.id, dataSource }),
125
+ }),
123
126
  })
124
127
  return page
125
128
  },