@notionhq/custom-blocks 0.3.1 → 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.
Files changed (46) hide show
  1. package/README.md +2 -8
  2. package/dist/bridge/SandboxBridge.d.ts +469 -15
  3. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  4. package/dist/bridge/SandboxBridge.js +119 -88
  5. package/dist/bridge/dataSources/resolveProperty.d.ts +1 -20
  6. package/dist/bridge/dataSources/resolveProperty.d.ts.map +1 -1
  7. package/dist/bridge/dataSources/resolveProperty.js +37 -32
  8. package/dist/bridge/page.d.ts +11 -7
  9. package/dist/bridge/page.d.ts.map +1 -1
  10. package/dist/bridge/page.js +20 -50
  11. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  12. package/dist/bridge/sandboxClient.js +32 -6
  13. package/dist/dataSources.d.ts.map +1 -1
  14. package/dist/dataSources.js +4 -3
  15. package/dist/index.d.ts +1 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/protocol/dataSources/dateValue.d.ts +2 -2
  18. package/dist/protocol/dataSources/dateValue.js +9 -7
  19. package/dist/protocol/messages/createPage.d.ts +64 -2
  20. package/dist/protocol/messages/createPageResult.d.ts +192 -6
  21. package/dist/protocol/messages/getPage.d.ts +192 -6
  22. package/dist/protocol/messages/hostToSandbox.d.ts +704 -22
  23. package/dist/protocol/messages/queryDataSourceResult.d.ts +128 -4
  24. package/dist/protocol/messages/sandboxToHost.d.ts +128 -4
  25. package/dist/protocol/messages/updatePage.d.ts +64 -2
  26. package/dist/protocol/messages/updatePageResult.d.ts +192 -6
  27. package/dist/protocol/pages/page.d.ts +360 -12
  28. package/dist/protocol/pages/page.js +48 -2
  29. package/dist/protocol/protocolVersion.d.ts +2 -1
  30. package/dist/protocol/protocolVersion.js +3 -1
  31. package/dist/types.d.ts +1 -1
  32. package/dist/types.d.ts.map +1 -1
  33. package/dist/version.js +1 -1
  34. package/docs/build-tools.md +55 -0
  35. package/docs/data-sources.md +12 -9
  36. package/docs/deployment.md +16 -34
  37. package/docs/errors.md +65 -52
  38. package/docs/pages.md +85 -78
  39. package/package.json +2 -18
  40. package/src/bridge/SandboxBridge.ts +168 -129
  41. package/src/bridge/dataSources/resolveProperty.ts +48 -39
  42. package/src/bridge/page.ts +32 -75
  43. package/src/bridge/sandboxClient.ts +40 -7
  44. package/src/dataSources.ts +7 -9
  45. package/src/index.ts +6 -0
  46. package/src/types.ts +6 -0
@@ -10,7 +10,7 @@ import { resolveDataSources } from "./dataSources/resolve.js";
10
10
  import { resolvePropertyWriteMapForDataSource } from "./dataSources/resolveProperty.js";
11
11
  import { createEmptyDataSourceQueryState, } from "./hostState.js";
12
12
  import { notifyListener } from "./notifyListener.js";
13
- import { toNotionPageFromNotionPageData } from "./page.js";
13
+ import { makeUpdatePageByKey, toNotionPageFromNotionPageData } from "./page.js";
14
14
  import { PendingRequests } from "./pendingRequests.js";
15
15
  import { SandboxInitialization } from "./SandboxInitialization.js";
16
16
  export class SandboxBridge {
@@ -19,6 +19,7 @@ export class SandboxBridge {
19
19
  theme: "light",
20
20
  contrastMode: DEFAULT_CONTRAST_MODE,
21
21
  };
22
+ pendingPageUpdates = new Map();
22
23
  listeners = new Set();
23
24
  messageInterceptors = new Set();
24
25
  messageLog = [];
@@ -545,17 +546,30 @@ export class SandboxBridge {
545
546
  };
546
547
  this.postToHost(outbound);
547
548
  }
548
- createPage(createPageArgs, dataSourceId) {
549
+ createPage(args) {
550
+ const { dataSource } = args;
551
+ const dataSourceKey = dataSource?.key;
549
552
  return new Promise(resolve => {
550
- const resolvedParent = this.resolveCreatePageParent(createPageArgs.parent);
551
- if (resolvedParent.status === "error") {
552
- resolve(resolvedParent);
553
- return;
553
+ let parent;
554
+ let propertyDataSource = dataSource;
555
+ if (dataSource?.collectionPointer) {
556
+ parent = {
557
+ type: "data_source_id",
558
+ data_source_id: dataSource.collectionPointer.id,
559
+ };
560
+ }
561
+ else {
562
+ const resolvedParent = this.resolveCreatePageParent(args.parent);
563
+ if (resolvedParent.status === "error") {
564
+ resolve(resolvedParent);
565
+ return;
566
+ }
567
+ parent = resolvedParent.parent;
568
+ propertyDataSource = resolvedParent.dataSource;
554
569
  }
555
570
  const resolvedProperties = resolvePropertyWriteMapForDataSource({
556
- dataSource: resolvedParent.dataSource,
557
- properties: createPageArgs.properties,
558
- operationName: "createPage",
571
+ dataSource: propertyDataSource,
572
+ properties: args.properties,
559
573
  });
560
574
  if (resolvedProperties.status === "error") {
561
575
  resolve(resolvedProperties);
@@ -566,34 +580,69 @@ export class SandboxBridge {
566
580
  resolve(result);
567
581
  return;
568
582
  }
569
- resolve({
570
- status: "success",
571
- page: this.buildNotionPage({ page: result.page, dataSourceId }),
583
+ const update = async (args) => {
584
+ const result = await this.updatePage(args);
585
+ if (result.status === "error") {
586
+ return result;
587
+ }
588
+ Object.assign(page, result.page);
589
+ return { status: "success", page };
590
+ };
591
+ const page = toNotionPageFromNotionPageData({
592
+ page: result.page,
593
+ dataSourceKey,
594
+ getPropertyById: propertyId => this.getPropertyById({ page, propertyId }),
595
+ getPropertyByKey: propertyKey => this.getPropertyByKey({ page, dataSource, propertyKey }),
596
+ updateById: payload => update({ ...payload, pageId: page.id, dataSource: undefined }),
597
+ updateByKey: makeUpdatePageByKey({
598
+ dataSource,
599
+ update: payload => update({ ...payload, pageId: page.id, dataSource }),
600
+ }),
572
601
  });
602
+ resolve({ status: "success", page });
573
603
  });
574
604
  const outbound = {
575
605
  type: "createPage",
576
606
  requestId,
577
- parent: resolvedParent.parent,
607
+ parent,
578
608
  properties: resolvedProperties.properties,
579
609
  };
580
- if (createPageArgs.position !== undefined) {
581
- outbound.position = createPageArgs.position;
610
+ if (args.position !== undefined) {
611
+ outbound.position = args.position;
582
612
  }
583
613
  this.postToHost(outbound);
584
614
  });
585
615
  }
586
- getPage(pageId, dataSourceId) {
616
+ getPage(args) {
617
+ const { pageId, dataSource } = args;
618
+ const dataSourceId = dataSource?.collectionPointer?.id;
619
+ const dataSourceKey = dataSource?.key;
587
620
  return new Promise(resolve => {
588
621
  const requestId = this.pendingGetPage.allocate(result => {
589
622
  if (result.status === "error") {
590
623
  resolve(result);
591
624
  return;
592
625
  }
593
- resolve({
594
- status: "success",
595
- page: this.buildNotionPage({ page: result.page, dataSourceId }),
626
+ const update = async (args) => {
627
+ const result = await this.updatePage(args);
628
+ if (result.status === "error") {
629
+ return result;
630
+ }
631
+ Object.assign(page, result.page);
632
+ return { status: "success", page };
633
+ };
634
+ const page = toNotionPageFromNotionPageData({
635
+ page: result.page,
636
+ dataSourceKey,
637
+ getPropertyById: propertyId => this.getPropertyById({ page, propertyId }),
638
+ getPropertyByKey: propertyKey => this.getPropertyByKey({ page, dataSource, propertyKey }),
639
+ updateById: payload => update({ ...payload, pageId: page.id, dataSource: undefined }),
640
+ updateByKey: makeUpdatePageByKey({
641
+ dataSource,
642
+ update: payload => update({ ...payload, pageId: page.id, dataSource }),
643
+ }),
596
644
  });
645
+ resolve({ status: "success", page });
597
646
  });
598
647
  const outbound = {
599
648
  type: "getPage",
@@ -650,57 +699,53 @@ export class SandboxBridge {
650
699
  this.postToHost(outbound);
651
700
  });
652
701
  }
653
- updatePage(args, dataSourceId) {
654
- const dataSource = this.hostState.status === "initialized" && dataSourceId !== undefined
655
- ? this.hostState.dataSources.find(source => source.collectionPointer?.id === dataSourceId)
702
+ getPropertyById(args) {
703
+ return (Object.values(args.page.properties).find(value => value?.id === args.propertyId) ?? null);
704
+ }
705
+ getPropertyByKey(args) {
706
+ const { page, dataSource, propertyKey } = args;
707
+ const currentDataSource = this.hostState.status === "initialized"
708
+ ? this.hostState.dataSources.find(source => source.key === dataSource?.key)
656
709
  : undefined;
657
- if (dataSourceId !== undefined) {
658
- const unmappedKey = Object.keys(args.properties ?? {}).find(key => {
659
- const id = dataSource && Object.hasOwn(dataSource.propertyIdsByKey, key)
660
- ? dataSource.propertyIdsByKey[key]
661
- : undefined;
662
- return !id || !dataSource?.collectionSchema?.propertiesById[id];
710
+ // Keep the captured mapping valid only while its key remains bound to the original database.
711
+ if (!dataSource?.collectionPointer ||
712
+ currentDataSource?.collectionPointer?.id !==
713
+ dataSource.collectionPointer.id) {
714
+ return null;
715
+ }
716
+ const propertyId = Object.hasOwn(dataSource.propertyIdsByKey, propertyKey)
717
+ ? dataSource.propertyIdsByKey[propertyKey]
718
+ : undefined;
719
+ return propertyId === undefined
720
+ ? null
721
+ : this.getPropertyById({ page, propertyId });
722
+ }
723
+ updatePage(args) {
724
+ const operation = async () => {
725
+ const { dataSource } = args;
726
+ const dataSourceId = dataSource?.collectionPointer?.id;
727
+ const resolved = resolvePropertyWriteMapForDataSource({
728
+ dataSource,
729
+ properties: args.properties === undefined ? {} : args.properties,
663
730
  });
664
- if (unmappedKey !== undefined) {
665
- return Promise.resolve({
666
- status: "error",
667
- error: {
668
- code: "unmapped_property",
669
- message: `Cannot resolve property key "${unmappedKey}" on this page.`,
670
- isRetryable: false,
671
- },
672
- });
731
+ if (resolved.status === "error") {
732
+ return resolved;
673
733
  }
674
- }
675
- const resolved = resolvePropertyWriteMapForDataSource({
676
- dataSource,
677
- properties: args.properties ?? {},
678
- operationName: "page.update",
679
- });
680
- if (resolved.status === "error") {
681
- return Promise.resolve(resolved);
682
- }
683
- const isArchived = args.is_archived;
684
- return new Promise(resolve => {
685
- if ((args.properties === undefined ||
686
- Object.keys(args.properties).length === 0) &&
734
+ if (Object.keys(args.properties ?? {}).length === 0 &&
687
735
  args.icon === undefined &&
688
736
  args.cover === undefined &&
689
- isArchived === undefined) {
690
- resolve({
737
+ args.is_archived === undefined) {
738
+ return {
691
739
  status: "error",
692
740
  error: {
693
741
  code: "invalid_page_update",
694
742
  message: "updatePage requires at least one property.",
695
743
  isRetryable: false,
696
744
  },
697
- });
698
- return;
745
+ };
699
746
  }
700
- const requestId = this.pendingUpdatePage.allocate(resolve);
701
747
  const outbound = {
702
748
  type: "updatePage",
703
- requestId,
704
749
  dataSourceId,
705
750
  pageId: args.pageId,
706
751
  };
@@ -713,34 +758,24 @@ export class SandboxBridge {
713
758
  if (args.cover !== undefined) {
714
759
  outbound.cover = args.cover;
715
760
  }
716
- if (isArchived !== undefined) {
717
- outbound.archived = isArchived;
761
+ if (args.is_archived !== undefined) {
762
+ outbound.archived = args.is_archived;
763
+ }
764
+ return new Promise(resolve => {
765
+ const requestId = this.pendingUpdatePage.allocate(resolve);
766
+ this.postToHost({ ...outbound, requestId });
767
+ });
768
+ };
769
+ const pending = this.pendingPageUpdates.get(args.pageId);
770
+ const result = pending ? pending.then(operation) : operation();
771
+ const finished = result.then(() => undefined, () => undefined);
772
+ this.pendingPageUpdates.set(args.pageId, finished);
773
+ void finished.then(() => {
774
+ if (this.pendingPageUpdates.get(args.pageId) === finished) {
775
+ this.pendingPageUpdates.delete(args.pageId);
718
776
  }
719
- this.postToHost(outbound);
720
- });
721
- }
722
- /**
723
- * Builds an SDK page object from loaded page data and wires its methods to the bridge.
724
- * Unlike `pages.create`, this does not send a create request or create a page in Notion.
725
- */
726
- buildNotionPage(args) {
727
- const { page, dataSourceId } = args;
728
- const dataSource = this.hostState.status === "initialized" && dataSourceId !== undefined
729
- ? this.hostState.dataSources.find(source => source.collectionPointer?.id === dataSourceId)
730
- : undefined;
731
- return toNotionPageFromNotionPageData({
732
- page,
733
- dataSource,
734
- getDataSource: () => {
735
- if (this.hostState.status !== "initialized" ||
736
- dataSourceId === undefined) {
737
- return undefined;
738
- }
739
- return this.hostState.dataSources.find(source => source.key === dataSource?.key &&
740
- source.collectionPointer?.id === dataSourceId);
741
- },
742
- update: (payload, updateDataSourceId) => this.updatePage({ ...payload, pageId: page.id }, updateDataSourceId),
743
777
  });
778
+ return result;
744
779
  }
745
780
  /**
746
781
  * Translates the public `CreatePageArgs["parent"]` into the bridge-native
@@ -751,12 +786,8 @@ export class SandboxBridge {
751
786
  switch (parent.type) {
752
787
  case "page_id":
753
788
  return { status: "ok", parent, dataSource: undefined };
754
- case "data_source_id": {
755
- const dataSource = this.hostState.status === "initialized"
756
- ? this.hostState.dataSources.find(entry => entry.collectionPointer?.id === parent.data_source_id)
757
- : undefined;
758
- return { status: "ok", parent, dataSource };
759
- }
789
+ case "data_source_id":
790
+ return { status: "ok", parent, dataSource: undefined };
760
791
  case "data_source_key": {
761
792
  if (this.hostState.status !== "initialized") {
762
793
  return {
@@ -11,29 +11,10 @@ export type PropertyWriteMapResolutionResult = {
11
11
  };
12
12
  /**
13
13
  * Resolves a public SDK property write map into the ID-keyed bridge shape.
14
- *
15
- * Identifiers in `properties` may be raw property IDs or data source property keys; the latter
16
- * are looked up in the `dataSource`'s `propertyIdsByKey`. Each value is re-parsed through
17
- * the page property value schema with its `id` rewritten to the resolved property ID.
14
+ * A data source makes every identifier a property key; without one, identifiers are raw IDs.
18
15
  */
19
16
  export declare function resolvePropertyWriteMapForDataSource(args: {
20
17
  dataSource: NotionDataSource | undefined;
21
18
  properties: NotionPagePropertyInputMap;
22
- operationName: string;
23
19
  }): PropertyWriteMapResolutionResult;
24
- /**
25
- * Treats bound data source keys as aliases for property IDs. An unbound key may still be used as
26
- * a raw ID when it exactly matches a property in the data source schema.
27
- */
28
- export declare function resolvePropertyIdentifierForDataSource(args: {
29
- dataSource: NotionDataSource | undefined;
30
- identifier: string;
31
- operationName: string;
32
- }): {
33
- status: "success";
34
- propertyId: string;
35
- } | {
36
- status: "error";
37
- error: CustomBlockPropertyErrorInfo;
38
- };
39
20
  //# sourceMappingURL=resolveProperty.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolveProperty.d.ts","sourceRoot":"","sources":["../../../src/bridge/dataSources/resolveProperty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAA;AAClG,OAAO,KAAK,EAEX,4BAA4B,EAC5B,MAAM,wDAAwD,CAAA;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAGhG,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM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EAAE,4BAA4B,CAAA;CAAE,CAAA;AAS3D;;;GAGG;AACH,wBAAgB,oCAAoC,CAAC,IAAI,EAAE;IAC1D,UAAU,EAAE,gBAAgB,GAAG,SAAS,CAAA;IACxC,UAAU,EAAE,0BAA0B,CAAA;CACtC,GAAG,gCAAgC,CAuEnC"}
@@ -5,33 +5,40 @@ function makePropertyError(code, message) {
5
5
  }
6
6
  /**
7
7
  * Resolves a public SDK property write map into the ID-keyed bridge shape.
8
- *
9
- * Identifiers in `properties` may be raw property IDs or data source property keys; the latter
10
- * are looked up in the `dataSource`'s `propertyIdsByKey`. Each value is re-parsed through
11
- * the page property value schema with its `id` rewritten to the resolved property ID.
8
+ * A data source makes every identifier a property key; without one, identifiers are raw IDs.
12
9
  */
13
10
  export function resolvePropertyWriteMapForDataSource(args) {
14
- const { dataSource, properties, operationName } = args;
15
- const resolvedProperties = {};
16
- for (const [identifier, value] of Object.entries(properties)) {
17
- const propertyIdResult = resolvePropertyIdentifierForDataSource({
11
+ const { dataSource, properties } = args;
12
+ const parsedMap = v.safeParse(v.record(v.string(), v.unknown()), properties);
13
+ if (Array.isArray(properties) || !parsedMap.success) {
14
+ return {
15
+ status: "error",
16
+ error: makePropertyError("invalid_property_value", "Property maps must be objects."),
17
+ };
18
+ }
19
+ const resolvedProperties = Object.create(null);
20
+ for (const [identifier, value] of Object.entries(parsedMap.output)) {
21
+ const propertyIdResult = resolveWriteIdentifier({
18
22
  dataSource,
19
23
  identifier,
20
- operationName,
21
24
  });
22
25
  if (propertyIdResult.status === "error") {
23
26
  return propertyIdResult;
24
27
  }
25
28
  const propertyId = propertyIdResult.propertyId;
26
- if (value.id !== undefined &&
27
- value.id !== identifier &&
28
- value.id !== propertyId) {
29
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
30
+ return {
31
+ status: "error",
32
+ error: makePropertyError("invalid_property_value", `Invalid value for property ${identifier}.`),
33
+ };
34
+ }
35
+ if ("id" in value && value.id !== undefined && value.id !== propertyId) {
29
36
  return {
30
37
  status: "error",
31
38
  error: makePropertyError("property_id_mismatch", `Property ${identifier} resolved to ${propertyId} but value id was ${value.id}.`),
32
39
  };
33
40
  }
34
- if (resolvedProperties[propertyId] !== undefined) {
41
+ if (Object.hasOwn(resolvedProperties, propertyId)) {
35
42
  return {
36
43
  status: "error",
37
44
  error: makePropertyError("duplicate_property", `Cannot set property ${propertyId} more than once.`),
@@ -52,25 +59,23 @@ export function resolvePropertyWriteMapForDataSource(args) {
52
59
  return { status: "success", properties: resolvedProperties };
53
60
  }
54
61
  /**
55
- * Treats bound data source keys as aliases for property IDs. An unbound key may still be used as
56
- * a raw ID when it exactly matches a property in the data source schema.
62
+ * Resolves property keys when a data source is present and raw property IDs otherwise.
63
+ * Property keys require a bound, loaded property so an unresolved key cannot become a raw ID.
57
64
  */
58
- export function resolvePropertyIdentifierForDataSource(args) {
59
- const { dataSource, identifier, operationName } = args;
60
- if (dataSource !== undefined && identifier in dataSource.propertyIdsByKey) {
61
- const propertyId = dataSource.propertyIdsByKey[identifier];
62
- if (propertyId === undefined) {
63
- // If the property is not bound to a key, check if it is a valid property ID. If so, we
64
- // can use it directly.
65
- if (identifier in dataSource.propertySchemasById) {
66
- return { status: "success", propertyId: identifier };
67
- }
68
- return {
69
- status: "error",
70
- error: makePropertyError("unmapped_property", `${operationName} cannot resolve property key "${identifier}" because it is not bound to a Notion property.`),
71
- };
72
- }
73
- return { status: "success", propertyId };
65
+ function resolveWriteIdentifier(args) {
66
+ const { dataSource, identifier } = args;
67
+ if (dataSource === undefined) {
68
+ return { status: "success", propertyId: identifier };
69
+ }
70
+ const propertyId = Object.hasOwn(dataSource.propertyIdsByKey, identifier)
71
+ ? dataSource.propertyIdsByKey[identifier]
72
+ : undefined;
73
+ if (propertyId === undefined ||
74
+ !dataSource.collectionSchema?.propertiesById[propertyId]) {
75
+ return {
76
+ status: "error",
77
+ error: makePropertyError("unmapped_property", `Cannot resolve property key "${identifier}" on this page.`),
78
+ };
74
79
  }
75
- return { status: "success", propertyId: identifier };
80
+ return { status: "success", propertyId };
76
81
  }
@@ -1,14 +1,18 @@
1
1
  import type { NotionDataSource } from "../protocol/dataSources/dataSource.js";
2
- import type { NotionDataSourceId } from "../protocol/ids.js";
3
- import type { BridgeMessagePayload } from "../protocol/messages/messagePayload.js";
4
- import type { UpdatePageResultMessage } from "../protocol/messages/updatePageResult.js";
5
2
  import type { notionPageDataSchema } from "../protocol/pages/page.js";
6
3
  import type { InferOutput } from "valibot";
7
- import type { NotionPage, PageUpdatePayload } from "../types.js";
4
+ import type { NotionPage } from "../types.js";
8
5
  export declare function toNotionPageFromNotionPageData(args: {
9
6
  page: InferOutput<typeof notionPageDataSchema>;
10
- dataSource: NotionDataSource | undefined;
11
- getDataSource: () => NotionDataSource | undefined;
12
- update: (payload: PageUpdatePayload, dataSourceId: NotionDataSourceId | undefined) => Promise<BridgeMessagePayload<UpdatePageResultMessage>>;
7
+ dataSourceKey: string | undefined;
8
+ getPropertyById: NotionPage["getPropertyById"];
9
+ getPropertyByKey: NotionPage["getPropertyByKey"];
10
+ updateById: NotionPage["updateById"];
11
+ updateByKey: NotionPage["updateByKey"];
13
12
  }): NotionPage;
13
+ /** Prevents key-based property updates from falling back to raw IDs without data source context. */
14
+ export declare function makeUpdatePageByKey(args: {
15
+ dataSource: NotionDataSource | undefined;
16
+ update: NotionPage["updateByKey"];
17
+ }): NotionPage["updateByKey"];
14
18
  //# sourceMappingURL=page.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../../src/bridge/page.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAA;AAClG,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AACjF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,6DAA6D,CAAA;AACvG,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,+DAA+D,CAAA;AAC5G,OAAO,K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AAmB,CAAC,IAAI,EAAE;IACzC,UAAU,EAAE,gBAAgB,GAAG,SAAS,CAAA;IACxC,MAAM,EAAE,UAAU,CAAC,aAAa,CAAC,CAAA;CACjC,GAAG,UAAU,CAAC,aAAa,CAAC,CAiB5B"}
@@ -1,59 +1,29 @@
1
1
  export function toNotionPageFromNotionPageData(args) {
2
- const { page: bridgePage, dataSource } = args;
3
- const getPropertyById = (id) => Object.values(sdkPage.properties).find(value => value?.id === id) ?? null;
4
- let pending;
5
- const updatePage = (payload, dataSourceId) => {
6
- const operation = async () => {
7
- const result = await args.update(payload, dataSourceId);
8
- if (result.status === "error") {
9
- return result;
10
- }
11
- Object.assign(sdkPage, result.page);
12
- return { status: "success", page: sdkPage };
13
- };
14
- const result = pending ? pending.then(operation) : operation();
15
- const finished = result.then(() => undefined, () => undefined);
16
- pending = finished;
17
- void finished.then(() => {
18
- if (pending === finished) {
19
- pending = undefined;
20
- }
21
- });
22
- return result;
2
+ return {
3
+ ...args.page,
4
+ dataSourceKey: args.dataSourceKey ?? null,
5
+ getPropertyById: args.getPropertyById,
6
+ getPropertyByKey: args.getPropertyByKey,
7
+ updateById: args.updateById,
8
+ updateByKey: args.updateByKey,
9
+ archive: () => args.updateById({ is_archived: true }),
10
+ unarchive: () => args.updateById({ is_archived: false }),
23
11
  };
24
- const updateById = (payload) => updatePage(payload, undefined);
25
- const updateByKey = (payload) => {
26
- const dataSourceId = dataSource?.collectionPointer?.id;
27
- if (dataSourceId === undefined) {
28
- return Promise.resolve({
12
+ }
13
+ /** Prevents key-based property updates from falling back to raw IDs without data source context. */
14
+ export function makeUpdatePageByKey(args) {
15
+ return async (payload) => {
16
+ if (args.dataSource === undefined &&
17
+ Object.keys(payload.properties ?? {}).length > 0) {
18
+ return {
29
19
  status: "error",
30
20
  error: {
31
- code: "unmapped_data_source",
32
- message: "This page has no bound data source context.",
21
+ code: "unmapped_property",
22
+ message: "Key-based property updates require data source context.",
33
23
  isRetryable: false,
34
24
  },
35
- });
25
+ };
36
26
  }
37
- return updatePage(payload, dataSourceId);
38
- };
39
- const methods = {
40
- getPropertyById,
41
- getPropertyByKey: (key) => {
42
- const source = args.getDataSource();
43
- const id = source && Object.hasOwn(source.propertyIdsByKey, key)
44
- ? source.propertyIdsByKey[key]
45
- : undefined;
46
- return id === undefined ? null : getPropertyById(id);
47
- },
48
- updateById,
49
- updateByKey,
50
- archive: () => updateById({ is_archived: true }),
51
- unarchive: () => updateById({ is_archived: false }),
52
- };
53
- const sdkPage = {
54
- ...bridgePage,
55
- dataSourceKey: dataSource?.key ?? null,
56
- ...methods,
27
+ return args.update(payload);
57
28
  };
58
- return sdkPage;
59
29
  }
@@ -1 +1 @@
1
- {"version":3,"file":"sandboxClient.d.ts","sourceRoot":"","sources":["../../src/bridge/sandboxClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yCAAyC,CAAA;AAC3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mDAAmD,CAAA;AACpF,OAAO,KAAK,EACX,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,aAAa,EACb,eAAe,EACf,YAAY,EACZ,qBAAqB,EACrB,eAAe,EACf,cAAc,EACd,MAAM,aAAa,CAAA;AACpB,OAAO,EACN,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EAExB,MAAM,gBAAgB,CAAA;AACvB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AACjE,OAAO,EACN,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,aAAa,EACb,MAAM,oBAAoB,CAAA;AAI3B,wBAAgB,SAAS,IAAI,aAAa,CAKzC;AAED,eAAO,MAAM,eAAe;IAC3B,0BAA0B,gBACZ,kBAAkB,KAC7B,CAAC,MAAM,IAAI,CAAC;IAIf,WAAW,mBAAmB,kBAAkB;IAIhD,UAAU;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AAC,cAAc,CAAC;CAG1B,CAAA;AAED,sDAAsD;AACtD,eAAO,MAAM,KAAK;IACjB,gEAAgE;IAChE,IAAI,QAAQ,MAAM,KAAG,OAAO,CAAC,qBAAqB,CAAC;CAGnD,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,KAAK;IACjB;;OAEG;IACH,IAAI,WAAW,aAAa,KAAG,OAAO,CAAC,eAAe,CAAC;IAIvD;;OAEG;IACH,GAAG,WAAW,YAAY,KAAG,OAAO,CAAC,aAAa,CAAC;CAGnD,CAAA"}
@@ -1,4 +1,5 @@
1
1
  import { getDataSourceQueryView as getDataSourceQueryViewWithBridge, } from "./hostState.js";
2
+ import { makeUpdatePageByKey, toNotionPageFromNotionPageData } from "./page.js";
2
3
  import { SandboxBridge, } from "./SandboxBridge.js";
3
4
  let bridge;
4
5
  export function getBridge() {
@@ -46,10 +47,29 @@ export const customBlockDataSources = {
46
47
  getBridge().releaseDataSourceSubscription(subscriptionId);
47
48
  },
48
49
  getView: (hostState, key, subscriptionId) => {
49
- return getDataSourceQueryViewWithBridge(hostState, key, subscriptionId, ({ page, dataSource }) => getBridge().buildNotionPage({
50
- page,
51
- dataSourceId: dataSource?.collectionPointer?.id,
52
- }));
50
+ return getDataSourceQueryViewWithBridge(hostState, key, subscriptionId, ({ page: pageData, dataSource }) => {
51
+ const bridge = getBridge();
52
+ const update = async (args) => {
53
+ const result = await bridge.updatePage(args);
54
+ if (result.status === "error") {
55
+ return result;
56
+ }
57
+ Object.assign(page, result.page);
58
+ return { status: "success", page };
59
+ };
60
+ const page = toNotionPageFromNotionPageData({
61
+ page: pageData,
62
+ dataSourceKey: key,
63
+ getPropertyById: propertyId => bridge.getPropertyById({ page, propertyId }),
64
+ getPropertyByKey: propertyKey => bridge.getPropertyByKey({ page, dataSource, propertyKey }),
65
+ updateById: payload => update({ ...payload, pageId: page.id, dataSource: undefined }),
66
+ updateByKey: makeUpdatePageByKey({
67
+ dataSource,
68
+ update: payload => update({ ...payload, pageId: page.id, dataSource }),
69
+ }),
70
+ });
71
+ return page;
72
+ });
53
73
  },
54
74
  };
55
75
  export const messageLog = {
@@ -68,13 +88,19 @@ export const pages = {
68
88
  * Creates a new Notion page.
69
89
  */
70
90
  create: (input) => {
71
- return getBridge().createPage(input, undefined);
91
+ return getBridge().createPage({
92
+ ...input,
93
+ dataSource: undefined,
94
+ });
72
95
  },
73
96
  /**
74
97
  * Fetches a page by id.
75
98
  */
76
99
  get: (pageId) => {
77
- return getBridge().getPage(pageId, undefined);
100
+ return getBridge().getPage({
101
+ pageId,
102
+ dataSource: undefined,
103
+ });
78
104
  },
79
105
  /**
80
106
  * Requests that the Notion host open a page.
@@ -1 +1 @@
1
- {"version":3,"file":"dataSources.d.ts","sourceRoot":"","sources":["../src/dataSources.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEX,YAAY,EACZ,MAAM,yCAAyC,CAAA;AAIhD,OAAO,KAAK,EACX,cAAc,EAEd,sBAAsB,EACtB,qBAAqB,EACrB,sBAAsB,EACtB,MAAM,YAAY,CAAA;AAEnB,eAAO,MAAM,WAAW;IACjB,OAAO,MACP,MAAM,UACH,YAAY,GAClB,OAAO,CAAC,qBAAqB,CAAC;IAW3B,MAAM,MACN,MAAM,WACF,IAAI,CAAC,cAAc,EAAE,QAAQ,GAAG,UAAU,CAAC,GAClD,OAAO,CAAC,qBAAqB,CAAC;IAiB3B,KAAK,MACL,MAAM,YACD,sBAAsB,GAC9B,OAAO,CAAC,sBAAsB,CAAC;CAsDlC,CAAA"}
1
+ {"version":3,"file":"dataSources.d.ts","sourceRoot":"","sources":["../src/dataSources.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEX,YAAY,EACZ,MAAM,yCAAyC,CAAA;AAIhD,OAAO,KAAK,EACX,cAAc,EAEd,sBAAsB,EACtB,qBAAqB,EACrB,sBAAsB,EACtB,MAAM,YAAY,CAAA;AAEnB,eAAO,MAAM,WAAW;IACjB,OAAO,MACP,MAAM,UACH,YAAY,GAClB,OAAO,CAAC,qBAAqB,CAAC;IAW3B,MAAM,MACN,MAAM,WACF,IAAI,CAAC,cAAc,EAAE,QAAQ,GAAG,UAAU,CAAC,GAClD,OAAO,CAAC,qBAAqB,CAAC;IAe3B,KAAK,MACL,MAAM,YACD,sBAAsB,GAC9B,OAAO,CAAC,sBAAsB,CAAC;CAsDlC,CAAA"}
@@ -7,8 +7,8 @@ export const dataSources = {
7
7
  if (resolved.status === "error") {
8
8
  return resolved;
9
9
  }
10
- const { dataSource, dataSourceId } = resolved;
11
- const result = await getBridge().getPage(pageId, dataSourceId);
10
+ const { dataSource } = resolved;
11
+ const result = await getBridge().getPage({ pageId, dataSource });
12
12
  return result.status === "error"
13
13
  ? result
14
14
  : { ...result, propertyIdsByKey: dataSource.propertyIdsByKey };
@@ -22,7 +22,8 @@ export const dataSources = {
22
22
  const result = await getBridge().createPage({
23
23
  ...payload,
24
24
  parent: { type: "data_source_id", data_source_id: dataSourceId },
25
- }, dataSourceId);
25
+ dataSource,
26
+ });
26
27
  return result.status === "error"
27
28
  ? result
28
29
  : { ...result, propertyIdsByKey: dataSource.propertyIdsByKey };