@notionhq/custom-blocks 0.3.2 → 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.
- package/LICENSE +21 -0
- package/README.md +13 -19
- package/dist/bridge/SandboxBridge.d.ts +0 -2
- package/dist/bridge/SandboxBridge.d.ts.map +1 -1
- package/dist/bridge/SandboxBridge.js +16 -39
- package/dist/bridge/SandboxInitialization.d.ts +1 -3
- package/dist/bridge/SandboxInitialization.d.ts.map +1 -1
- package/dist/bridge/SandboxInitialization.js +13 -15
- package/dist/bridge/appearance.d.ts +2 -10
- package/dist/bridge/appearance.d.ts.map +1 -1
- package/dist/bridge/appearance.js +2 -10
- package/dist/bridge/dataSources/query.d.ts +1 -2
- package/dist/bridge/dataSources/query.d.ts.map +1 -1
- package/dist/bridge/dataSources/query.js +15 -398
- package/dist/bridge/dataSources/queryFilter.d.ts +10 -0
- package/dist/bridge/dataSources/queryFilter.d.ts.map +1 -0
- package/dist/bridge/dataSources/queryFilter.js +123 -0
- package/dist/bridge/dataSources/resolveProperty.d.ts +1 -20
- package/dist/bridge/dataSources/resolveProperty.d.ts.map +1 -1
- package/dist/bridge/dataSources/resolveProperty.js +37 -32
- package/dist/bridge/dataSources/resolvePropertyAddress.d.ts +10 -0
- package/dist/bridge/dataSources/resolvePropertyAddress.d.ts.map +1 -0
- package/dist/bridge/dataSources/resolvePropertyAddress.js +55 -0
- package/dist/bridge/dataSources/resolveSorts.d.ts +10 -0
- package/dist/bridge/dataSources/resolveSorts.d.ts.map +1 -0
- package/dist/bridge/dataSources/resolveSorts.js +80 -0
- package/dist/bridge/hostState.d.ts +3 -7
- package/dist/bridge/hostState.d.ts.map +1 -1
- package/dist/bridge/hostState.js +0 -24
- package/dist/bridge/page.d.ts +9 -3
- package/dist/bridge/page.d.ts.map +1 -1
- package/dist/bridge/page.js +19 -2
- package/dist/bridge/readLocalManifest.d.ts +23 -0
- package/dist/bridge/readLocalManifest.d.ts.map +1 -0
- package/dist/bridge/readLocalManifest.js +46 -0
- package/dist/bridge/sandboxClient.d.ts +0 -2
- package/dist/bridge/sandboxClient.d.ts.map +1 -1
- package/dist/bridge/sandboxClient.js +50 -24
- package/dist/protocol/dataSources/dataSourcePage.d.ts +3 -4
- package/dist/protocol/dataSources/dataSourcePage.js +2 -3
- package/dist/protocol/index.d.ts +1 -0
- package/dist/protocol/index.js +1 -0
- package/dist/protocol/isLocalBlockServerUrl.d.ts +2 -0
- package/dist/protocol/isLocalBlockServerUrl.js +9 -0
- package/dist/protocol/messages/connect.d.ts +2 -2
- package/dist/protocol/messages/connect.js +2 -2
- package/dist/protocol/messages/createPage.d.ts +1 -1
- package/dist/protocol/messages/createPage.js +1 -1
- package/dist/protocol/messages/createPageResult.d.ts +3 -3
- package/dist/protocol/messages/createPageResult.js +4 -4
- package/dist/protocol/messages/getPage.d.ts +2 -2
- package/dist/protocol/messages/getPage.js +4 -4
- package/dist/protocol/messages/hostToSandbox.d.ts +3 -3
- package/dist/protocol/messages/hostToSandbox.js +10 -10
- package/dist/protocol/messages/init.d.ts +1 -1
- package/dist/protocol/messages/queryDataSource.d.ts +24 -0
- package/dist/protocol/messages/queryDataSource.js +2 -0
- package/dist/protocol/messages/queryDataSourceResult.d.ts +3 -3
- package/dist/protocol/messages/queryDataSourceResult.js +5 -5
- package/dist/protocol/messages/sandboxToHost.d.ts +8 -0
- package/dist/protocol/messages/updatePageResult.d.ts +3 -3
- package/dist/protocol/messages/updatePageResult.js +4 -4
- package/dist/protocol/pages/page.d.ts +4 -4
- package/dist/protocol/pages/page.js +2 -2
- package/dist/protocol/parent.d.ts +1 -1
- package/dist/protocol/protocolVersion.d.ts +3 -2
- package/dist/protocol/protocolVersion.js +4 -2
- package/dist/react/useDataSource.d.ts +2 -2
- package/dist/react/useDataSource.js +2 -2
- package/dist/types.d.ts +5 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/docs/block-location.md +6 -4
- package/docs/bridge-interceptors.md +4 -0
- package/docs/build-tools.md +58 -0
- package/docs/data-sources.md +24 -18
- package/docs/deployment.md +23 -36
- package/docs/errors.md +75 -52
- package/docs/lifecycle.md +10 -3
- package/docs/pages.md +84 -76
- package/docs/users.md +4 -4
- package/package.json +4 -19
- package/src/bridge/SandboxBridge.ts +18 -48
- package/src/bridge/SandboxInitialization.ts +14 -20
- package/src/bridge/appearance.ts +2 -10
- package/src/bridge/dataSources/query.ts +17 -530
- package/src/bridge/dataSources/queryFilter.ts +181 -0
- package/src/bridge/dataSources/resolveProperty.ts +48 -39
- package/src/bridge/dataSources/resolvePropertyAddress.ts +62 -0
- package/src/bridge/dataSources/resolveSorts.ts +107 -0
- package/src/bridge/hostState.ts +3 -43
- package/src/bridge/page.ts +28 -4
- package/src/bridge/readLocalManifest.ts +60 -0
- package/src/bridge/sandboxClient.ts +63 -38
- package/src/react/useDataSource.ts +2 -2
- package/src/types.ts +5 -5
- package/dist/bridge/loadManifest.d.ts +0 -21
- package/dist/bridge/loadManifest.d.ts.map +0 -1
- package/dist/bridge/loadManifest.js +0 -85
- package/src/bridge/loadManifest.ts +0 -114
|
@@ -16,12 +16,11 @@ import type {
|
|
|
16
16
|
} from "../types.js"
|
|
17
17
|
import {
|
|
18
18
|
type CustomBlockHostState,
|
|
19
|
+
createEmptyDataSourceQueryState,
|
|
19
20
|
type DataSourceQueryView,
|
|
20
|
-
getDataSourceQueryView as getDataSourceQueryViewWithBridge,
|
|
21
21
|
} from "./hostState.js"
|
|
22
|
-
import type { ManifestLoadResult } from "./loadManifest.js"
|
|
23
22
|
import type { MessageInterceptor } from "./messageInterceptor.js"
|
|
24
|
-
import {
|
|
23
|
+
import { makeUpdatePageByKey, toNotionPageFromBridgePage } from "./page.js"
|
|
25
24
|
import {
|
|
26
25
|
type MessageLogEntry,
|
|
27
26
|
type QueryDataSourceArgs,
|
|
@@ -44,10 +43,6 @@ export const customBlockHost = {
|
|
|
44
43
|
return getBridge().registerMessageInterceptor(interceptor)
|
|
45
44
|
},
|
|
46
45
|
|
|
47
|
-
sendConnect: (manifestResult: ManifestLoadResult) => {
|
|
48
|
-
getBridge().sendConnect(manifestResult)
|
|
49
|
-
},
|
|
50
|
-
|
|
51
46
|
initialize: () => getBridge().initialize(),
|
|
52
47
|
|
|
53
48
|
subscribe: (listener: () => void) => {
|
|
@@ -75,6 +70,13 @@ export const customBlockHost = {
|
|
|
75
70
|
},
|
|
76
71
|
}
|
|
77
72
|
|
|
73
|
+
const EMPTY_QUERY_VIEW: DataSourceQueryView = {
|
|
74
|
+
items: [],
|
|
75
|
+
propertyIdsByKey: {},
|
|
76
|
+
isLoading: false,
|
|
77
|
+
hasMore: false,
|
|
78
|
+
}
|
|
79
|
+
|
|
78
80
|
export const customBlockDataSources = {
|
|
79
81
|
createSubscriptionId: () => {
|
|
80
82
|
return getBridge().createDataSourceSubscriptionId()
|
|
@@ -93,37 +95,60 @@ export const customBlockDataSources = {
|
|
|
93
95
|
key: string,
|
|
94
96
|
subscriptionId: string,
|
|
95
97
|
): DataSourceQueryView => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
98
|
+
if (hostState.status !== "initialized") {
|
|
99
|
+
return EMPTY_QUERY_VIEW
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const dataSource = hostState.dataSources.find(entry => entry.key === key)
|
|
103
|
+
const subscriptionState = hostState.dataSourceState[subscriptionId]
|
|
104
|
+
const queryState =
|
|
105
|
+
subscriptionState?.dataSourceKey === key
|
|
106
|
+
? subscriptionState
|
|
107
|
+
: createEmptyDataSourceQueryState(key)
|
|
108
|
+
|
|
109
|
+
const items =
|
|
110
|
+
dataSource === undefined
|
|
111
|
+
? []
|
|
112
|
+
: queryState.items.map(bridgePage => {
|
|
113
|
+
const bridge = getBridge()
|
|
114
|
+
const update = async (
|
|
115
|
+
args: Parameters<SandboxBridge["updatePage"]>[0],
|
|
116
|
+
): Promise<UpdatePageResult> => {
|
|
117
|
+
const result = await bridge.updatePage(args)
|
|
118
|
+
if (result.status === "error") {
|
|
119
|
+
return result
|
|
120
|
+
}
|
|
121
|
+
Object.assign(page, result.page)
|
|
122
|
+
return { status: "success", page }
|
|
123
|
+
}
|
|
124
|
+
const page: NotionPage = toNotionPageFromBridgePage({
|
|
125
|
+
bridgePage,
|
|
126
|
+
dataSourceKey: key,
|
|
127
|
+
getPropertyById: propertyId =>
|
|
128
|
+
bridge.getPropertyById({ page, propertyId }),
|
|
129
|
+
getPropertyByKey: propertyKey =>
|
|
130
|
+
bridge.getPropertyByKey({ page, dataSource, propertyKey }),
|
|
131
|
+
updateById: payload =>
|
|
132
|
+
update({
|
|
133
|
+
...payload,
|
|
134
|
+
pageId: page.id,
|
|
135
|
+
dataSource: undefined,
|
|
136
|
+
}),
|
|
137
|
+
updateByKey: makeUpdatePageByKey({
|
|
138
|
+
dataSource,
|
|
139
|
+
update: payload =>
|
|
140
|
+
update({ ...payload, pageId: page.id, dataSource }),
|
|
141
|
+
}),
|
|
142
|
+
})
|
|
143
|
+
return page
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
items,
|
|
148
|
+
propertyIdsByKey: dataSource?.propertyIdsByKey ?? {},
|
|
149
|
+
isLoading: queryState.isLoading,
|
|
150
|
+
hasMore: queryState.hasMore,
|
|
151
|
+
}
|
|
127
152
|
},
|
|
128
153
|
}
|
|
129
154
|
|
|
@@ -21,8 +21,8 @@ type DataSourceHookState = {
|
|
|
21
21
|
* Reads from the data source mapped to the given semantic `key`.
|
|
22
22
|
*
|
|
23
23
|
* @param key - The semantic data source key the block is wired to (e.g. `"people"`).
|
|
24
|
-
* @param options - Optional live snapshot options.
|
|
25
|
-
*
|
|
24
|
+
* @param options - Optional live snapshot options. The host enforces the maximum row
|
|
25
|
+
* limit. `sorts` are applied in array order.
|
|
26
26
|
*
|
|
27
27
|
* @example
|
|
28
28
|
* const { items, propertyIdsByKey, isLoading, hasMore, error } = useDataSource("default", { limit: 25 })
|
package/src/types.ts
CHANGED
|
@@ -41,11 +41,11 @@ import type {
|
|
|
41
41
|
import type { CustomBlockQueryDataSourceErrorInfo } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
|
|
42
42
|
import type { CustomBlockUpdatePageErrorInfo } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
|
|
43
43
|
import type {
|
|
44
|
+
bridgePageSchema,
|
|
44
45
|
NotionPageCover,
|
|
45
46
|
NotionPageIcon,
|
|
46
47
|
NotionPagePropertyValue,
|
|
47
48
|
NotionPagePropertyWriteMap,
|
|
48
|
-
notionPageDataSchema,
|
|
49
49
|
} from "@notionhq/custom-blocks-protocol/pages/page.js"
|
|
50
50
|
import type { InferOutput } from "valibot"
|
|
51
51
|
|
|
@@ -104,7 +104,7 @@ export type NotionPagePropertyInputMap = {
|
|
|
104
104
|
[propertyIdOrKey: string]: NotionPagePropertyInputValue
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
export type NotionPage = InferOutput<typeof
|
|
107
|
+
export type NotionPage = InferOutput<typeof bridgePageSchema> & {
|
|
108
108
|
dataSourceKey: string | null
|
|
109
109
|
getPropertyById: (propertyId: string) => NotionPagePropertyValue | null
|
|
110
110
|
getPropertyByKey: (propertyKey: string) => NotionPagePropertyValue | null
|
|
@@ -232,9 +232,9 @@ export type UseDataSourceOptions = DataSourceQueryOptions
|
|
|
232
232
|
* parent shape; see https://developers.notion.com/reference/data-source.
|
|
233
233
|
*
|
|
234
234
|
* - `page_id`: Create a child page under the given Notion page.
|
|
235
|
-
* - `data_source_id`: Create a row inside the given Notion data source
|
|
236
|
-
*
|
|
237
|
-
*
|
|
235
|
+
* - `data_source_id`: Create a row inside the given Notion data source. The public API's
|
|
236
|
+
* `data_source_id` is the same UUID as the `collectionPointer.id` exposed in `dataSources`,
|
|
237
|
+
* so either value works here.
|
|
238
238
|
* - `data_source_key`: Create a row inside the data source that the custom block was configured with
|
|
239
239
|
* under this semantic key. The SDK resolves the key to a `data_source_id` locally before
|
|
240
240
|
* sending the request to the host.
|
|
@@ -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
|
-
}
|