@notionhq/custom-blocks 0.2.15 → 0.2.16

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.
Files changed (34) hide show
  1. package/dist/bridge/SandboxBridge.d.ts +7 -8
  2. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  3. package/dist/bridge/SandboxBridge.js +49 -44
  4. package/dist/bridge/dataSources/query.d.ts +2 -1
  5. package/dist/bridge/dataSources/query.d.ts.map +1 -1
  6. package/dist/bridge/dataSources/query.js +39 -8
  7. package/dist/bridge/dataSources/resolve.d.ts +10 -10
  8. package/dist/bridge/dataSources/resolve.d.ts.map +1 -1
  9. package/dist/bridge/dataSources/resolve.js +17 -5
  10. package/dist/protocol/dataSources/dataSource.d.ts +61 -6
  11. package/dist/protocol/dataSources/dataSource.js +35 -6
  12. package/dist/protocol/dataSources/propertySchema.d.ts +2 -0
  13. package/dist/protocol/dataSources/propertySchema.js +7 -0
  14. package/dist/protocol/messages/queryDataSource.d.ts +485 -485
  15. package/dist/protocol/messages/queryDataSource.js +43 -43
  16. package/dist/protocol/messages/queryDataSourceResult.d.ts +1 -1
  17. package/dist/protocol/messages/sandboxToHost.d.ts +178 -178
  18. package/dist/protocol/protocolVersion.d.ts +1 -1
  19. package/dist/protocol/protocolVersion.js +1 -1
  20. package/dist/typeUtils.d.ts +10 -0
  21. package/dist/typeUtils.d.ts.map +1 -0
  22. package/dist/typeUtils.js +1 -0
  23. package/dist/types.d.ts +18 -15
  24. package/dist/types.d.ts.map +1 -1
  25. package/dist/version.js +1 -1
  26. package/docs/block-location.md +4 -4
  27. package/docs/data-sources.md +60 -35
  28. package/docs/errors.md +17 -1
  29. package/package.json +1 -1
  30. package/src/bridge/SandboxBridge.ts +62 -50
  31. package/src/bridge/dataSources/query.ts +47 -9
  32. package/src/bridge/dataSources/resolve.ts +30 -13
  33. package/src/typeUtils.ts +10 -0
  34. package/src/types.ts +22 -16
@@ -14,6 +14,7 @@ import {
14
14
  type ResolvedCustomBlockDataSourcePropertyFilter,
15
15
  type ResolvedCustomBlockDataSourceSort,
16
16
  } from "@notionhq/custom-blocks-protocol/messages/queryDataSource.js"
17
+ import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
17
18
  import * as v from "valibot"
18
19
  import type { UseDataSourceOptions } from "../../types.js"
19
20
  import { unreachable } from "../../utils.js"
@@ -34,7 +35,7 @@ export type ResolvedDataSourceQuery = {
34
35
  }
35
36
 
36
37
  export type ResolveDataSourceQueryResult =
37
- | { status: "error"; error: string }
38
+ | { status: "error"; error: CustomBlockQueryDataSourceErrorInfo }
38
39
  | {
39
40
  status: "ok"
40
41
  dataSource: NotionDataSource
@@ -60,34 +61,63 @@ export function resolveDataSourceQuery(args: {
60
61
  const { dataSources, key, options, warn } = args
61
62
  const resolvedOptions = resolveDataSourceQueryOptions(options)
62
63
  if (resolvedOptions.status === "error") {
63
- return resolvedOptions
64
+ return {
65
+ status: "error",
66
+ error: getInvalidQueryError(resolvedOptions.error),
67
+ }
64
68
  }
65
69
  const queryOptions = resolvedOptions.options
66
70
  const dataSource = dataSources.find(entry => entry.key === key)
67
71
  if (dataSource === undefined) {
68
72
  return {
69
73
  status: "error",
70
- error: `Unknown data source key "${key}". Known keys: [${dataSources.map(entry => entry.key).join(", ")}].`,
74
+ error: {
75
+ code: "unknown_data_source_key",
76
+ message: `Unknown data source key "${key}". Known keys: [${dataSources.map(entry => entry.key).join(", ")}].`,
77
+ // The query key or manifest must change before the query can succeed.
78
+ isRetryable: false,
79
+ },
71
80
  }
72
81
  }
73
82
  if (dataSource.collectionPointer === undefined) {
74
83
  return {
75
84
  status: "error",
76
- error: `Data source "${key}" has not been mapped to a database yet.`,
85
+ error: {
86
+ code: "unmapped_data_source",
87
+ message: `Data source "${key}" has not been mapped to a database yet.`,
88
+ // The host must supply a database binding before the query can succeed.
89
+ isRetryable: false,
90
+ },
91
+ }
92
+ }
93
+
94
+ if (
95
+ (queryOptions?.filter !== undefined ||
96
+ (Array.isArray(queryOptions?.sorts) && queryOptions.sorts.length > 0)) &&
97
+ dataSource.collectionSchema === undefined
98
+ ) {
99
+ return {
100
+ status: "error",
101
+ error: {
102
+ code: "data_source_schema_unavailable",
103
+ message: `You cannot query data source "${key}" with filters or sorts because its schema is unavailable.`,
104
+ // Retrying cannot load the schema. A host binding update must supply it.
105
+ isRetryable: false,
106
+ },
77
107
  }
78
108
  }
79
109
 
80
110
  const limit = resolveDataSourceQueryLimit(queryOptions?.limit, warn)
81
111
  if (limit.status === "error") {
82
- return limit
112
+ return { status: "error", error: getInvalidQueryError(limit.error) }
83
113
  }
84
114
  const filter = resolveFilter(queryOptions?.filter, dataSource)
85
115
  if (filter.status === "error") {
86
- return filter
116
+ return { status: "error", error: getInvalidQueryError(filter.error) }
87
117
  }
88
118
  const sorts = resolveSorts(queryOptions?.sorts, dataSource)
89
119
  if (sorts.status === "error") {
90
- return sorts
120
+ return { status: "error", error: getInvalidQueryError(sorts.error) }
91
121
  }
92
122
 
93
123
  const identity = stableJsonStringify({
@@ -109,6 +139,13 @@ export function resolveDataSourceQuery(args: {
109
139
  }
110
140
  }
111
141
 
142
+ function getInvalidQueryError(
143
+ message: string,
144
+ ): CustomBlockQueryDataSourceErrorInfo {
145
+ // The query, schema, or bindings must change before the query can succeed.
146
+ return { code: "invalid_data_source_query", message, isRetryable: false }
147
+ }
148
+
112
149
  function resolveDataSourceQueryOptions(
113
150
  options: unknown,
114
151
  ):
@@ -610,13 +647,14 @@ function resolvePropertyAddress(
610
647
  }
611
648
  propertyId = value.propertyId
612
649
  }
613
- if (!Object.hasOwn(dataSource.propertySchemasById, propertyId)) {
650
+ const properties = dataSource.collectionSchema?.propertiesById ?? {}
651
+ if (!Object.hasOwn(properties, propertyId)) {
614
652
  return {
615
653
  status: "error",
616
654
  error: `Unknown property ID "${propertyId}" for data source "${dataSource.key}".`,
617
655
  }
618
656
  }
619
- const propertySchema = dataSource.propertySchemasById[propertyId]
657
+ const propertySchema = properties[propertyId]
620
658
  return {
621
659
  status: "ok",
622
660
  propertyId,
@@ -1,29 +1,33 @@
1
1
  import type {
2
+ NotionCollectionSchema,
2
3
  NotionDataSource,
3
4
  NotionDataSourceBindings,
4
5
  } 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"
5
10
  import type { CustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest.js"
6
11
 
7
- type ResolveDataSourcesArgs = {
8
- manifest: CustomBlockManifest
9
- dataSourceBindings: NotionDataSourceBindings
10
- }
11
-
12
12
  /**
13
13
  * Builds the public {@link NotionDataSource} list the SDK exposes to consumers.
14
14
  *
15
- * Combines the host-supplied bindings (collection pointers + schemas) with the
16
- * manifest's declared data source keys. The host owns every mapping; the SDK
17
- * validates supplied IDs but never infers omitted bindings.
15
+ * Combines the host-supplied bindings (database pointers + schemas) with the manifest's declared
16
+ * data source keys. The host owns every mapping; the SDK validates supplied IDs but never infers
17
+ * omitted bindings.
18
18
  */
19
- export function resolveDataSources(
20
- args: ResolveDataSourcesArgs,
21
- ): NotionDataSource[] {
19
+ export function resolveDataSources(args: {
20
+ manifest: CustomBlockManifest
21
+ dataSourceBindings: NotionDataSourceBindings
22
+ }): NotionDataSource[] {
22
23
  return Object.entries(args.manifest.dataSources).map(
23
24
  ([key, manifestDataSource]) => {
24
25
  const binding = args.dataSourceBindings[key]
25
- const propertySchemasById =
26
+ const collectionProperties =
26
27
  binding?.collectionSchema?.propertiesById ?? {}
28
+ const propertySchemasById = getDataSourceReadSchemas(
29
+ binding?.collectionSchema,
30
+ )
27
31
 
28
32
  const bindingPropertyIdsByKey = binding?.propertyIdsByKey ?? {}
29
33
  const propertyIdsByKey: Record<string, string | undefined> = {}
@@ -33,7 +37,9 @@ export function resolveDataSources(
33
37
  for (const [propertyKey, manifestProperty] of manifestProperties) {
34
38
  const propertyId = bindingPropertyIdsByKey[propertyKey]
35
39
  const propertySchema =
36
- propertyId === undefined ? undefined : propertySchemasById[propertyId]
40
+ propertyId === undefined
41
+ ? undefined
42
+ : collectionProperties[propertyId]
37
43
  propertyIdsByKey[propertyKey] =
38
44
  propertySchema?.type === manifestProperty.type
39
45
  ? propertyId
@@ -50,3 +56,14 @@ export function resolveDataSources(
50
56
  },
51
57
  )
52
58
  }
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
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Returns a union of the keys from every member of `T`. Includes keys that only one union member
3
+ * defines.
4
+ */
5
+ export type UnionKeys<T> = T extends unknown ? keyof T : never
6
+
7
+ /** Allows one union member and prohibits fields from the other members. */
8
+ export type ExclusiveUnion<T, Whole = T> = T extends unknown
9
+ ? T & { [K in Exclude<UnionKeys<Whole>, keyof T>]?: never }
10
+ : never
package/src/types.ts CHANGED
@@ -51,6 +51,8 @@ import type {
51
51
  NotionPagePropertyValue,
52
52
  } from "@notionhq/custom-blocks-protocol/pages/page.js"
53
53
 
54
+ import type { ExclusiveUnion } from "./typeUtils.js"
55
+
54
56
  export type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/errors.js"
55
57
  export type {
56
58
  NotionDataSourceId,
@@ -127,26 +129,28 @@ export type NotionDataSourcePageUpdateArgs = {
127
129
  is_archived?: boolean
128
130
  }
129
131
 
130
- export type NotionDataSourceTextFilterOperator = CustomBlockTextFilterOperator
132
+ export type NotionDataSourceTextFilterOperator =
133
+ ExclusiveUnion<CustomBlockTextFilterOperator>
131
134
  export type NotionDataSourceNumberFilterOperator =
132
- CustomBlockNumberFilterOperator
135
+ ExclusiveUnion<CustomBlockNumberFilterOperator>
133
136
  export type NotionDataSourceCheckboxFilterOperator =
134
- CustomBlockCheckboxFilterOperator
137
+ ExclusiveUnion<CustomBlockCheckboxFilterOperator>
135
138
  export type NotionDataSourceOptionFilterOperator =
136
- CustomBlockOptionFilterOperator
139
+ ExclusiveUnion<CustomBlockOptionFilterOperator>
137
140
  export type NotionDataSourceContainsFilterOperator =
138
- CustomBlockContainsFilterOperator
139
- export type NotionDataSourceDateFilterOperator = CustomBlockDateFilterOperator
141
+ ExclusiveUnion<CustomBlockContainsFilterOperator>
142
+ export type NotionDataSourceDateFilterOperator =
143
+ ExclusiveUnion<CustomBlockDateFilterOperator>
140
144
  export type NotionDataSourcePersonFilterValue = CustomBlockPersonFilterValue
141
145
  export type NotionDataSourcePeopleFilterOperator =
142
- CustomBlockPeopleFilterOperator
146
+ ExclusiveUnion<CustomBlockPeopleFilterOperator>
143
147
 
144
148
  export type NotionDataSourcePropertyAddress =
145
149
  | { key: string; propertyId?: never }
146
150
  | { propertyId: string; key?: never }
147
151
 
148
152
  export type NotionDataSourcePropertyFilter = NotionDataSourcePropertyAddress &
149
- (
153
+ ExclusiveUnion<
150
154
  | { title: NotionDataSourceTextFilterOperator }
151
155
  | { rich_text: NotionDataSourceTextFilterOperator }
152
156
  | { url: NotionDataSourceTextFilterOperator }
@@ -159,15 +163,15 @@ export type NotionDataSourcePropertyFilter = NotionDataSourcePropertyAddress &
159
163
  | { status: NotionDataSourceOptionFilterOperator }
160
164
  | { date: NotionDataSourceDateFilterOperator }
161
165
  | { people: NotionDataSourcePeopleFilterOperator }
162
- )
166
+ >
163
167
 
164
168
  export type NotionDataSourceSort = NotionDataSourcePropertyAddress & {
165
169
  direction: "ascending" | "descending"
166
170
  }
167
171
 
168
- export type NotionDataSourceFilter =
169
- | NotionDataSourcePropertyFilter
170
- | { and: NotionDataSourcePropertyFilter[] }
172
+ export type NotionDataSourceFilter = ExclusiveUnion<
173
+ NotionDataSourcePropertyFilter | { and: NotionDataSourcePropertyFilter[] }
174
+ >
171
175
 
172
176
  /**
173
177
  * Latest live snapshot of a data source query.
@@ -180,13 +184,15 @@ export type NotionDataSourceFilter =
180
184
  export type DataSourceSnapshot = {
181
185
  items: NotionDataSourcePage[]
182
186
  /**
183
- * Collection/data source schema for the bound Notion data source, including raw property
184
- * schemas. Undefined when the semantic data source key has not been bound.
187
+ * Contains only properties in the database schema.
188
+ * Use this map to resolve property bindings and validate filter and sort references.
189
+ * Undefined when the host does not provide a schema.
185
190
  */
186
191
  collectionSchema?: NotionCollectionSchema
187
192
  /**
188
- * Per-property schemas keyed by raw property ID, including the four synthetic built-ins
189
- * (`created_time`, `last_edited_time`, `created_by`, `last_edited_by`).
193
+ * Contains database properties and four synthetic metadata fields.
194
+ * Use this map to describe values in query snapshots.
195
+ * Synthetic entries do not establish filter or sort support.
190
196
  */
191
197
  propertySchemasById: { [propertyId: string]: NotionPropertySchema }
192
198
  /**