@notionhq/custom-blocks 0.3.3 → 0.3.4

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 (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -11
  3. package/dist/bridge/SandboxBridge.d.ts +0 -2
  4. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  5. package/dist/bridge/SandboxBridge.js +5 -8
  6. package/dist/bridge/SandboxInitialization.d.ts +1 -3
  7. package/dist/bridge/SandboxInitialization.d.ts.map +1 -1
  8. package/dist/bridge/SandboxInitialization.js +13 -15
  9. package/dist/bridge/appearance.d.ts +2 -10
  10. package/dist/bridge/appearance.d.ts.map +1 -1
  11. package/dist/bridge/appearance.js +2 -10
  12. package/dist/bridge/dataSources/query.d.ts +1 -2
  13. package/dist/bridge/dataSources/query.d.ts.map +1 -1
  14. package/dist/bridge/dataSources/query.js +15 -398
  15. package/dist/bridge/dataSources/queryFilter.d.ts +10 -0
  16. package/dist/bridge/dataSources/queryFilter.d.ts.map +1 -0
  17. package/dist/bridge/dataSources/queryFilter.js +123 -0
  18. package/dist/bridge/dataSources/resolvePropertyAddress.d.ts +10 -0
  19. package/dist/bridge/dataSources/resolvePropertyAddress.d.ts.map +1 -0
  20. package/dist/bridge/dataSources/resolvePropertyAddress.js +55 -0
  21. package/dist/bridge/dataSources/resolveSorts.d.ts +10 -0
  22. package/dist/bridge/dataSources/resolveSorts.d.ts.map +1 -0
  23. package/dist/bridge/dataSources/resolveSorts.js +80 -0
  24. package/dist/bridge/hostState.d.ts +3 -7
  25. package/dist/bridge/hostState.d.ts.map +1 -1
  26. package/dist/bridge/hostState.js +0 -24
  27. package/dist/bridge/page.d.ts +3 -3
  28. package/dist/bridge/page.d.ts.map +1 -1
  29. package/dist/bridge/page.js +2 -2
  30. package/dist/bridge/readLocalManifest.d.ts +23 -0
  31. package/dist/bridge/readLocalManifest.d.ts.map +1 -0
  32. package/dist/bridge/readLocalManifest.js +46 -0
  33. package/dist/bridge/sandboxClient.d.ts +0 -2
  34. package/dist/bridge/sandboxClient.d.ts.map +1 -1
  35. package/dist/bridge/sandboxClient.js +50 -27
  36. package/dist/protocol/dataSources/dataSourcePage.d.ts +3 -4
  37. package/dist/protocol/dataSources/dataSourcePage.js +2 -3
  38. package/dist/protocol/index.d.ts +1 -0
  39. package/dist/protocol/index.js +1 -0
  40. package/dist/protocol/isLocalBlockServerUrl.d.ts +2 -0
  41. package/dist/protocol/isLocalBlockServerUrl.js +9 -0
  42. package/dist/protocol/messages/connect.d.ts +2 -2
  43. package/dist/protocol/messages/connect.js +2 -2
  44. package/dist/protocol/messages/createPage.d.ts +1 -1
  45. package/dist/protocol/messages/createPage.js +1 -1
  46. package/dist/protocol/messages/createPageResult.d.ts +3 -3
  47. package/dist/protocol/messages/createPageResult.js +4 -4
  48. package/dist/protocol/messages/getPage.d.ts +2 -2
  49. package/dist/protocol/messages/getPage.js +4 -4
  50. package/dist/protocol/messages/hostToSandbox.d.ts +3 -3
  51. package/dist/protocol/messages/hostToSandbox.js +10 -10
  52. package/dist/protocol/messages/init.d.ts +1 -1
  53. package/dist/protocol/messages/queryDataSource.d.ts +24 -0
  54. package/dist/protocol/messages/queryDataSource.js +2 -0
  55. package/dist/protocol/messages/queryDataSourceResult.d.ts +3 -3
  56. package/dist/protocol/messages/queryDataSourceResult.js +5 -5
  57. package/dist/protocol/messages/sandboxToHost.d.ts +8 -0
  58. package/dist/protocol/messages/updatePageResult.d.ts +3 -3
  59. package/dist/protocol/messages/updatePageResult.js +4 -4
  60. package/dist/protocol/pages/page.d.ts +4 -4
  61. package/dist/protocol/pages/page.js +2 -2
  62. package/dist/protocol/parent.d.ts +1 -1
  63. package/dist/protocol/protocolVersion.d.ts +2 -2
  64. package/dist/protocol/protocolVersion.js +2 -2
  65. package/dist/react/useDataSource.d.ts +2 -2
  66. package/dist/react/useDataSource.js +2 -2
  67. package/dist/types.d.ts +5 -5
  68. package/dist/types.d.ts.map +1 -1
  69. package/dist/version.js +1 -1
  70. package/docs/block-location.md +6 -4
  71. package/docs/bridge-interceptors.md +4 -0
  72. package/docs/build-tools.md +5 -2
  73. package/docs/data-sources.md +21 -17
  74. package/docs/deployment.md +16 -11
  75. package/docs/errors.md +11 -1
  76. package/docs/lifecycle.md +10 -3
  77. package/docs/pages.md +7 -3
  78. package/docs/users.md +4 -4
  79. package/package.json +3 -2
  80. package/src/bridge/SandboxBridge.ts +5 -10
  81. package/src/bridge/SandboxInitialization.ts +14 -20
  82. package/src/bridge/appearance.ts +2 -10
  83. package/src/bridge/dataSources/query.ts +17 -530
  84. package/src/bridge/dataSources/queryFilter.ts +181 -0
  85. package/src/bridge/dataSources/resolvePropertyAddress.ts +62 -0
  86. package/src/bridge/dataSources/resolveSorts.ts +107 -0
  87. package/src/bridge/hostState.ts +3 -43
  88. package/src/bridge/page.ts +4 -4
  89. package/src/bridge/readLocalManifest.ts +60 -0
  90. package/src/bridge/sandboxClient.ts +63 -41
  91. package/src/react/useDataSource.ts +2 -2
  92. package/src/types.ts +5 -5
  93. package/dist/bridge/loadManifest.d.ts +0 -21
  94. package/dist/bridge/loadManifest.d.ts.map +0 -1
  95. package/dist/bridge/loadManifest.js +0 -85
  96. package/src/bridge/loadManifest.ts +0 -114
@@ -1,21 +0,0 @@
1
- import type { CustomBlockErrorInfo } from "../protocol/errors.js";
2
- import { type CustomBlockManifest } from "../protocol/manifest.js";
3
- type CustomBlockConnectErrorCode = "manifest_unavailable" | "manifest_invalid" | (string & {});
4
- type CustomBlockConnectError = CustomBlockErrorInfo<CustomBlockConnectErrorCode>;
5
- export type ManifestLoadResult = {
6
- manifest: CustomBlockManifest | null;
7
- error?: undefined;
8
- } | {
9
- manifest: null;
10
- error: CustomBlockConnectError;
11
- };
12
- /**
13
- * On the literal `localhost` hostname, attempts to load the custom block's optional self-hosted
14
- * manifest exposed by the worker dev shell, requested at the page-relative `manifest` path so
15
- * blocks served from a subpath resolve it against their own base.
16
- *
17
- * It is up to the host to decide whether to use this manifest or provide its own persisted manifest.
18
- */
19
- export declare function attemptToLoadSelfHostedManifest(hostname?: string | undefined): Promise<ManifestLoadResult>;
20
- export {};
21
- //# sourceMappingURL=loadManifest.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"loadManifest.d.ts","sourceRoot":"","sources":["../../src/bridge/loadManifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4CAA4C,CAAA;AACtF,OAAO,EACN,KAAK,mBAAmB,EAExB,MAAM,8CAA8C,CAAA;AAKrD,KAAK,2BAA2B,GAC7B,sBAAsB,GACtB,kBAAkB,GAClB,CAAC,MAAM,GAAG,EAAE,CAAC,CAAA;AAEhB,KAAK,uBAAuB,GAAG,oBAAoB,CAAC,2BAA2B,CAAC,CAAA;AAEhF,MAAM,MAAM,kBAAkB,GAC3B;IACA,QAAQ,EAAE,mBAAmB,GAAG,IAAI,CAAA;IACpC,KAAK,CAAC,EAAE,SAAS,CAAA;CAChB,GACD;IACA,QAAQ,EAAE,IAAI,CAAA;IACd,KAAK,EAAE,uBAAuB,CAAA;CAC7B,CAAA;AAEJ;;;;;;GAMG;AACH,wBAAsB,+BAA+B,CACpD,QAAQ,GAAE,MAAM,GAAG,SAEQ,GACzB,OAAO,CAAC,kBAAkB,CAAC,CAgE7B"}
@@ -1,85 +0,0 @@
1
- import { manifestSchema, } from "../protocol/manifest.js";
2
- import * as v from "valibot";
3
- const MANIFEST_URL = "manifest";
4
- /**
5
- * On the literal `localhost` hostname, attempts to load the custom block's optional self-hosted
6
- * manifest exposed by the worker dev shell, requested at the page-relative `manifest` path so
7
- * blocks served from a subpath resolve it against their own base.
8
- *
9
- * It is up to the host to decide whether to use this manifest or provide its own persisted manifest.
10
- */
11
- export async function attemptToLoadSelfHostedManifest(hostname = typeof window === "undefined"
12
- ? undefined
13
- : window.location.hostname) {
14
- // Runtime manifests are a localhost-only development fallback. Deployed
15
- // blocks receive their authoritative manifest from the host during init.
16
- if (hostname !== "localhost") {
17
- return { manifest: null };
18
- }
19
- let response;
20
- try {
21
- response = await fetch(MANIFEST_URL, { credentials: "omit" });
22
- }
23
- catch (error) {
24
- const message = `Could not fetch ${MANIFEST_URL}.`;
25
- console.warn(`[custom-blocks-sdk] ${message}`, error);
26
- return {
27
- manifest: null,
28
- // Consider this retryable because it may have just been a network issue. Because `init`
29
- // errors are considered terminal, the host should "retry" by reloading the iframe and
30
- // sending another `connect` message.
31
- error: { code: "manifest_unavailable", message, isRetryable: true },
32
- };
33
- }
34
- if (response.status === 404) {
35
- const message = `No manifest found at ${MANIFEST_URL} (status ${response.status}).`;
36
- console.warn(`[custom-blocks-sdk] ${message}`);
37
- return { manifest: null };
38
- }
39
- if (!response.ok) {
40
- const message = `Could not fetch ${MANIFEST_URL} (status ${response.status}).`;
41
- console.warn(`[custom-blocks-sdk] ${message}`);
42
- return {
43
- manifest: null,
44
- error: {
45
- code: "manifest_unavailable",
46
- message,
47
- isRetryable: isRetryableManifestHttpStatus(response.status),
48
- },
49
- };
50
- }
51
- let json;
52
- try {
53
- json = await response.json();
54
- }
55
- catch (error) {
56
- const message = `Manifest at ${MANIFEST_URL} was not valid JSON.`;
57
- console.warn(`[custom-blocks-sdk] ${message}`, error);
58
- return {
59
- manifest: null,
60
- // Consider invalid manifests as unrecoverable errors.
61
- error: { code: "manifest_invalid", message, isRetryable: false },
62
- };
63
- }
64
- const parsed = v.safeParse(manifestSchema, json);
65
- if (!parsed.success) {
66
- const message = `Manifest at ${MANIFEST_URL} did not match schema.`;
67
- console.warn(`[custom-blocks-sdk] ${message}`, parsed.issues);
68
- return {
69
- manifest: null,
70
- // Consider invalid manifests as unrecoverable errors.
71
- error: { code: "manifest_invalid", message, isRetryable: false },
72
- };
73
- }
74
- return { manifest: parsed.output };
75
- }
76
- function isRetryableManifestHttpStatus(status) {
77
- // Consider these statuses retryable because they may reflect transient failures.
78
- return (status === 408 || // Request Timeout
79
- status === 425 || // Too Early
80
- status === 500 || // Internal Server Error
81
- status === 502 || // Bad Gateway
82
- status === 503 || // Service Unavailable
83
- status === 504 // Gateway Timeout
84
- );
85
- }
@@ -1,114 +0,0 @@
1
- import type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/errors.js"
2
- import {
3
- type CustomBlockManifest,
4
- manifestSchema,
5
- } from "@notionhq/custom-blocks-protocol/manifest.js"
6
- import * as v from "valibot"
7
-
8
- const MANIFEST_URL = "manifest"
9
-
10
- type CustomBlockConnectErrorCode =
11
- | "manifest_unavailable"
12
- | "manifest_invalid"
13
- | (string & {})
14
-
15
- type CustomBlockConnectError = CustomBlockErrorInfo<CustomBlockConnectErrorCode>
16
-
17
- export type ManifestLoadResult =
18
- | {
19
- manifest: CustomBlockManifest | null
20
- error?: undefined
21
- }
22
- | {
23
- manifest: null
24
- error: CustomBlockConnectError
25
- }
26
-
27
- /**
28
- * On the literal `localhost` hostname, attempts to load the custom block's optional self-hosted
29
- * manifest exposed by the worker dev shell, requested at the page-relative `manifest` path so
30
- * blocks served from a subpath resolve it against their own base.
31
- *
32
- * It is up to the host to decide whether to use this manifest or provide its own persisted manifest.
33
- */
34
- export async function attemptToLoadSelfHostedManifest(
35
- hostname: string | undefined = typeof window === "undefined"
36
- ? undefined
37
- : window.location.hostname,
38
- ): Promise<ManifestLoadResult> {
39
- // Runtime manifests are a localhost-only development fallback. Deployed
40
- // blocks receive their authoritative manifest from the host during init.
41
- if (hostname !== "localhost") {
42
- return { manifest: null }
43
- }
44
-
45
- let response: Response
46
- try {
47
- response = await fetch(MANIFEST_URL, { credentials: "omit" })
48
- } catch (error) {
49
- const message = `Could not fetch ${MANIFEST_URL}.`
50
- console.warn(`[custom-blocks-sdk] ${message}`, error)
51
- return {
52
- manifest: null,
53
- // Consider this retryable because it may have just been a network issue. Because `init`
54
- // errors are considered terminal, the host should "retry" by reloading the iframe and
55
- // sending another `connect` message.
56
- error: { code: "manifest_unavailable", message, isRetryable: true },
57
- }
58
- }
59
- if (response.status === 404) {
60
- const message = `No manifest found at ${MANIFEST_URL} (status ${response.status}).`
61
- console.warn(`[custom-blocks-sdk] ${message}`)
62
- return { manifest: null }
63
- }
64
- if (!response.ok) {
65
- const message = `Could not fetch ${MANIFEST_URL} (status ${response.status}).`
66
- console.warn(`[custom-blocks-sdk] ${message}`)
67
- return {
68
- manifest: null,
69
- error: {
70
- code: "manifest_unavailable",
71
- message,
72
- isRetryable: isRetryableManifestHttpStatus(response.status),
73
- },
74
- }
75
- }
76
-
77
- let json: unknown
78
- try {
79
- json = await response.json()
80
- } catch (error) {
81
- const message = `Manifest at ${MANIFEST_URL} was not valid JSON.`
82
- console.warn(`[custom-blocks-sdk] ${message}`, error)
83
- return {
84
- manifest: null,
85
- // Consider invalid manifests as unrecoverable errors.
86
- error: { code: "manifest_invalid", message, isRetryable: false },
87
- }
88
- }
89
-
90
- const parsed = v.safeParse(manifestSchema, json)
91
- if (!parsed.success) {
92
- const message = `Manifest at ${MANIFEST_URL} did not match schema.`
93
- console.warn(`[custom-blocks-sdk] ${message}`, parsed.issues)
94
- return {
95
- manifest: null,
96
- // Consider invalid manifests as unrecoverable errors.
97
- error: { code: "manifest_invalid", message, isRetryable: false },
98
- }
99
- }
100
-
101
- return { manifest: parsed.output }
102
- }
103
-
104
- function isRetryableManifestHttpStatus(status: number): boolean {
105
- // Consider these statuses retryable because they may reflect transient failures.
106
- return (
107
- status === 408 || // Request Timeout
108
- status === 425 || // Too Early
109
- status === 500 || // Internal Server Error
110
- status === 502 || // Bad Gateway
111
- status === 503 || // Service Unavailable
112
- status === 504 // Gateway Timeout
113
- )
114
- }