@notionhq/custom-blocks 0.5.0 → 0.5.2

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.
@@ -0,0 +1,54 @@
1
+ import { bridgeDataSourceBindingSchema } from "@notionhq/custom-blocks-protocol/dataSources/bridgeDataSourceBinding.js"
2
+ import { dataSourcePointerValidator } from "@notionhq/custom-blocks-protocol/dataSources/dataSourcePointer.js"
3
+ import { notionDataSourceSchemaValidator } from "@notionhq/custom-blocks-protocol/dataSources/dataSourceSchema.js"
4
+ import { initMessageSchema } from "@notionhq/custom-blocks-protocol/handshake/init.js"
5
+ import { hostToSandboxMessageSchema } from "@notionhq/custom-blocks-protocol/messages/hostToSandbox.js"
6
+ import { readIncomingType } from "@notionhq/custom-blocks-protocol/messages/incomingType.js"
7
+ import * as v from "valibot"
8
+
9
+ /**
10
+ * Older hosts such as 0.9.0 send `collectionPointer` and `collectionSchema` during
11
+ * initialization and live updates. Normalize these pre-protocol-11 names to
12
+ * `dataSourcePointer` and `dataSourceSchema` before exposing bindings to SDK consumers.
13
+ * Current fields take precedence when both names are present. Malformed fields and
14
+ * invalid property mappings still fail validation; this compatibility does not add
15
+ * host operations or repair bindings the Notion client cannot read from storage.
16
+ */
17
+ export function parseHostToSandboxMessage(data: unknown) {
18
+ const type = readIncomingType(data)
19
+ if (type === "init" || type === "dataSourcesChanged") {
20
+ return v.safeParse(bindingMessageSchema, data)
21
+ }
22
+ return v.safeParse(hostToSandboxMessageSchema, data)
23
+ }
24
+
25
+ const bindingSchema = v.pipe(
26
+ v.object({
27
+ ...bridgeDataSourceBindingSchema.entries,
28
+ collectionPointer: v.optional(dataSourcePointerValidator),
29
+ collectionSchema: v.optional(notionDataSourceSchemaValidator),
30
+ }),
31
+ v.transform(({ collectionPointer, collectionSchema, ...binding }) => ({
32
+ ...binding,
33
+ dataSourcePointer: binding.dataSourcePointer ?? collectionPointer,
34
+ dataSourceSchema: binding.dataSourceSchema ?? collectionSchema,
35
+ })),
36
+ )
37
+
38
+ const dataSourcesSchema = v.object({
39
+ bindings: v.record(v.string(), bindingSchema),
40
+ })
41
+
42
+ const bindingMessageSchema = v.variant("type", [
43
+ v.variant("status", [
44
+ v.object({
45
+ ...initMessageSchema.options[0].entries,
46
+ dataSources: dataSourcesSchema,
47
+ }),
48
+ initMessageSchema.options[1],
49
+ ]),
50
+ v.object({
51
+ type: v.literal("dataSourcesChanged"),
52
+ dataSources: dataSourcesSchema,
53
+ }),
54
+ ])
@@ -17,7 +17,7 @@ export function resolveDataSources(args: {
17
17
  return Object.entries(args.manifest.dataSources).map(
18
18
  ([key, manifestDataSource]) => {
19
19
  const binding = args.dataSourceBindings[key]
20
- const collectionProperties =
20
+ const dataSourceProperties =
21
21
  binding?.dataSourceSchema?.propertiesById ?? {}
22
22
 
23
23
  const resolvedBinding = resolveDataSourceBinding({
@@ -36,7 +36,7 @@ export function resolveDataSources(args: {
36
36
  dataSourcePointer: binding?.dataSourcePointer,
37
37
  dataSourceSchema: binding?.dataSourceSchema,
38
38
  propertyIdsByKey,
39
- propertySchemasById: collectionProperties,
39
+ propertySchemasById: dataSourceProperties,
40
40
  ...(binding?.inaccessible === true && { inaccessible: true }),
41
41
  }
42
42
  },
@@ -35,6 +35,9 @@ export function subscribeToDataSource({
35
35
  propertyIdsByKey: source?.propertyIdsByKey ?? {},
36
36
  isLoading,
37
37
  hasMore: result?.status === "success" && result.hasMore,
38
+ ...(result?.status === "success" && result.limitClamp !== undefined
39
+ ? { limitClamp: result.limitClamp }
40
+ : {}),
38
41
  }
39
42
  notifyListener(() => onSnapshot(snapshot))
40
43
  }
@@ -1,3 +1,4 @@
1
+ import type { BridgeDataSourceQueryLimitClamp } from "@notionhq/custom-blocks-protocol/dataSources/bridgeDataSourceQueryLimitClamp.js"
1
2
  import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/dataSources/messages/queryDataSource.js"
2
3
  import type { CustomBlockCreatePageErrorInfo } from "@notionhq/custom-blocks-protocol/pages/messages/createPage.js"
3
4
  import type { CustomBlockGetPageErrorInfo } from "@notionhq/custom-blocks-protocol/pages/messages/getPage.js"
@@ -22,12 +23,17 @@ export type DataSourcesQueryResult =
22
23
  | ({ status: "success" } & DataSourceSnapshot)
23
24
  | { status: "error"; error: CustomBlockQueryDataSourceErrorInfo }
24
25
 
26
+ /** A host-reported reduction of the requested row limit. */
27
+ export type DataSourceQueryLimitClamp = BridgeDataSourceQueryLimitClamp
28
+
25
29
  /** Latest live snapshot of a data source query. */
26
30
  export type DataSourceSnapshot = {
27
31
  items: NotionPage[]
28
32
  propertyIdsByKey: { [key: string]: string | undefined }
29
33
  isLoading: boolean
30
34
  hasMore: boolean
35
+ /** A reported host cap. Older hosts may cap queries without reporting it. */
36
+ limitClamp?: DataSourceQueryLimitClamp
31
37
  }
32
38
 
33
39
  export type NotionArchiveStatus = "active" | "archived" | "all"