@notionhq/custom-blocks-host 0.1.29 → 0.1.30

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.
@@ -1 +1 @@
1
- {"version":3,"file":"createCustomBlockHost.d.ts","sourceRoot":"","sources":["../src/createCustomBlockHost.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,kBAAkB,EACvB,MAAM,8CAA8C,CAAA;AAErD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AACjF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAA;AAavF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AAoBtG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAA;AACrF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4CAA4C,CAAA;AAE9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2CAA2C,CAAA;AAC5E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gDAAgD,CAAA;AAIhF,OAAO,KAAK,EACX,iCAAiC,EACjC,2BAA2B,EAC3B,MAAM,sBAAsB,CAAA;AAE7B,OAAO,KAAK,EACX,sBAAsB,EACtB,uBAAuB,EACvB,2BAA2B,EAC3B,MAAM,qBAAqB,CAAA;AAc5B,MAAM,MAAM,sBAAsB,GAAG;IACpC,MAAM,EAAE,iBAAiB,CAAA;IACzB,aAAa,EAAE,MAAM,CAAA;IACrB;;;;OAIG;IACH,wBAAwB,CAAC,EAAE,MAAM,CAAA;IACjC,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,YAAY,EAAE,2BAA2B,CAAA;IACzC,QAAQ,EAAE,uBAAuB,CAAA;IACjC;;;OAGG;IACH,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,mBAAmB,KAAK,IAAI,CAAA;IACpD,gG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qB,GAAG;IACnC,IAAI,EAAE,MAAM,IAAI,CAAA;IAChB,IAAI,EAAE,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAA;IAC7C,QAAQ,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAA;IACtC,eAAe,EAAE,CAAC,YAAY,EAAE,kBAAkB,KAAK,IAAI,CAAA;IAC3D,SAAS,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAA;IACzC,OAAO,EAAE,CAAC,IAAI,EAAE,eAAe,KAAK,IAAI,CAAA;IACxC,cAAc,EAAE,CAAC,WAAW,EAAE,iCAAiC,KAAK,IAAI,CAAA;IACxE,cAAc,EAAE,CAAC,WAAW,EAAE,UAAU,KAAK,IAAI,CAAA;IACjD,YAAY,EAAE,CAAC,IAAI,EAAE;QAAE,YAAY,EAAE,kBAAkB,CAAA;KAAE,KAAK,IAAI,CAAA;CAClE,CAAA;AAyBD,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,sBAAsB,GAC7B,qBAAqB,CAqXvB"}
@@ -45,6 +45,22 @@ export function createCustomBlockHost(options) {
45
45
  emit("out", sandboxMessage);
46
46
  iframe.contentWindow?.postMessage(sandboxMessage, sandboxOrigin);
47
47
  }
48
+ /**
49
+ * Invokes a one-shot handler and posts its result with the request's ID.
50
+ * Each result type is named `<request type>Result` by the bridge protocol.
51
+ */
52
+ async function respondToRequest(message, handler) {
53
+ if (handler === undefined) {
54
+ return;
55
+ }
56
+ const response = await handler(message);
57
+ const result = {
58
+ type: `${message.type}Result`,
59
+ requestId: message.requestId,
60
+ ...response,
61
+ };
62
+ post(result);
63
+ }
48
64
  function postQueryDataSourceResult(args) {
49
65
  const message = args.response.status === "error"
50
66
  ? {
@@ -83,78 +99,6 @@ export function createCustomBlockHost(options) {
83
99
  response,
84
100
  });
85
101
  }
86
- async function handleCreatePage(message) {
87
- if (handlers.createPage === undefined) {
88
- return;
89
- }
90
- const response = await handlers.createPage(message);
91
- const result = {
92
- type: "createPageResult",
93
- requestId: message.requestId,
94
- ...response,
95
- };
96
- post(result);
97
- }
98
- async function handleGetPage(message) {
99
- if (handlers.getPage === undefined) {
100
- return;
101
- }
102
- const response = await handlers.getPage(message);
103
- const result = {
104
- type: "getPageResult",
105
- requestId: message.requestId,
106
- ...response,
107
- };
108
- post(result);
109
- }
110
- async function handleGetUser(message) {
111
- if (handlers.getUser === undefined) {
112
- return;
113
- }
114
- const response = await handlers.getUser(message);
115
- const result = {
116
- type: "getUserResult",
117
- requestId: message.requestId,
118
- ...response,
119
- };
120
- post(result);
121
- }
122
- async function handleListUsers(message) {
123
- if (handlers.listUsers === undefined) {
124
- return;
125
- }
126
- const response = await handlers.listUsers(message);
127
- const result = {
128
- type: "listUsersResult",
129
- requestId: message.requestId,
130
- ...response,
131
- };
132
- post(result);
133
- }
134
- async function handleOpenPage(message) {
135
- if (handlers.openPage === undefined) {
136
- return;
137
- }
138
- const response = await handlers.openPage(message);
139
- const result = {
140
- type: "openPageResult",
141
- requestId: message.requestId,
142
- ...response,
143
- };
144
- post(result);
145
- }
146
- async function handleUpdatePage(message) {
147
- if (handlers.updatePage === undefined) {
148
- return;
149
- }
150
- const response = await handlers.updatePage(message);
151
- const result = {
152
- type: "updatePageResult",
153
- requestId: message.requestId,
154
- ...response,
155
- };
156
- post(result);
157
- }
158
102
  /**
159
103
  * Sends any pending host state changes to the sandbox. It is possible for these to change
160
104
  * between when the host sends `init` and when the sandbox responds with `initResult`. The host
@@ -258,12 +202,12 @@ export function createCustomBlockHost(options) {
258
202
  onInitResult: initializationController.handleInitResult,
259
203
  onQueryDataSource: handleQuery,
260
204
  onUnsubscribeDataSourceQuery: message => querySubscriptions.unsubscribe(message.subscriptionId),
261
- onCreatePage: handleCreatePage,
262
- onGetPage: handleGetPage,
263
- onOpenPage: handleOpenPage,
264
- onGetUser: handleGetUser,
265
- onListUsers: handleListUsers,
266
- onUpdatePage: handleUpdatePage,
205
+ onCreatePage: message => respondToRequest(message, handlers.createPage),
206
+ onGetPage: message => respondToRequest(message, handlers.getPage),
207
+ onOpenPage: message => respondToRequest(message, handlers.openPage),
208
+ onGetUser: message => respondToRequest(message, handlers.getUser),
209
+ onListUsers: message => respondToRequest(message, handlers.listUsers),
210
+ onUpdatePage: message => respondToRequest(message, handlers.updatePage),
267
211
  onResize: message => handlers.resize?.(message),
268
212
  });
269
213
  window.addEventListener("message", onMessage);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notionhq/custom-blocks-host",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
4
4
  "description": "Reusable host runtime for Notion custom blocks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -6,27 +6,10 @@ import type { NotionCollectionSchema } from "@notionhq/custom-blocks-protocol/da
6
6
  import type { NotionDataSourceId } from "@notionhq/custom-blocks-protocol/ids.js"
7
7
  import type { CustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest.js"
8
8
  import type { ContrastModeChangedMessage } from "@notionhq/custom-blocks-protocol/messages/contrastModeChanged.js"
9
- import type { CreatePageMessage } from "@notionhq/custom-blocks-protocol/messages/createPage.js"
10
- import type { CreatePageResultMessage } from "@notionhq/custom-blocks-protocol/messages/createPageResult.js"
11
9
  import type { DataSourcesChangedMessage } from "@notionhq/custom-blocks-protocol/messages/dataSourcesChanged.js"
12
- import type {
13
- GetPageMessage,
14
- GetPageResultMessage,
15
- } from "@notionhq/custom-blocks-protocol/messages/getPage.js"
16
- import type {
17
- GetUserMessage,
18
- GetUserResultMessage,
19
- } from "@notionhq/custom-blocks-protocol/messages/getUser.js"
20
10
  import type { HostToSandboxMessage } from "@notionhq/custom-blocks-protocol/messages/hostToSandbox.js"
21
11
  import type { CustomBlockInitErrorCode } from "@notionhq/custom-blocks-protocol/messages/init.js"
22
- import type {
23
- ListUsersMessage,
24
- ListUsersResultMessage,
25
- } from "@notionhq/custom-blocks-protocol/messages/listUsers.js"
26
- import type {
27
- OpenPageMessage,
28
- OpenPageResultMessage,
29
- } from "@notionhq/custom-blocks-protocol/messages/openPage.js"
12
+ import type { BridgeMessagePayload } from "@notionhq/custom-blocks-protocol/messages/messagePayload.js"
30
13
  import type { PageChangedMessage } from "@notionhq/custom-blocks-protocol/messages/pageChanged.js"
31
14
  import type { ParentChangedMessage } from "@notionhq/custom-blocks-protocol/messages/parentChanged.js"
32
15
  import type { QueryDataSourceMessage } from "@notionhq/custom-blocks-protocol/messages/queryDataSource.js"
@@ -34,9 +17,8 @@ import type {
34
17
  CustomBlockQueryDataSourceErrorInfo,
35
18
  QueryDataSourceResultMessage,
36
19
  } from "@notionhq/custom-blocks-protocol/messages/queryDataSourceResult.js"
20
+ import type { SandboxToHostMessage } from "@notionhq/custom-blocks-protocol/messages/sandboxToHost.js"
37
21
  import type { ThemeChangedMessage } from "@notionhq/custom-blocks-protocol/messages/themeChanged.js"
38
- import type { UpdatePageMessage } from "@notionhq/custom-blocks-protocol/messages/updatePage.js"
39
- import type { UpdatePageResultMessage } from "@notionhq/custom-blocks-protocol/messages/updatePageResult.js"
40
22
  import type { CustomBlockPage } from "@notionhq/custom-blocks-protocol/pages/page.js"
41
23
  import type { NotionParent } from "@notionhq/custom-blocks-protocol/parent.js"
42
24
  import { LATEST_BRIDGE_PROTOCOL_VERSION } from "@notionhq/custom-blocks-protocol/protocolVersion.js"
@@ -113,6 +95,29 @@ export type CustomBlockHostHandle = {
113
95
  refreshQuery: (args: { dataSourceId: NotionDataSourceId }) => void
114
96
  }
115
97
 
98
+ /**
99
+ * A request that expects one result correlated by `requestId`.
100
+ * Query subscriptions use `subscriptionId` and can receive repeated results.
101
+ */
102
+ type OneShotRequest = Extract<SandboxToHostMessage, { requestId: string }>
103
+
104
+ /** The matching host result message, named `<request type>Result` by the protocol. */
105
+ type ResultForRequest<Request extends OneShotRequest> = Extract<
106
+ HostToSandboxMessage,
107
+ { type: `${Request["type"]}Result` }
108
+ >
109
+
110
+ /** The result's success or error payload, without the bridge envelope fields. */
111
+ type ResultPayloadForRequest<Request extends OneShotRequest> =
112
+ BridgeMessagePayload<ResultForRequest<Request>>
113
+
114
+ /** Returns one result payload. `respondToRequest` adds the message type and request ID. */
115
+ type OneShotHandler<Request extends OneShotRequest> = (
116
+ message: Request,
117
+ ) =>
118
+ | ResultPayloadForRequest<Request>
119
+ | Promise<ResultPayloadForRequest<Request>>
120
+
116
121
  export function createCustomBlockHost(
117
122
  options: CustomBlockHostOptions,
118
123
  ): CustomBlockHostHandle {
@@ -171,6 +176,26 @@ export function createCustomBlockHost(
171
176
  iframe.contentWindow?.postMessage(sandboxMessage, sandboxOrigin)
172
177
  }
173
178
 
179
+ /**
180
+ * Invokes a one-shot handler and posts its result with the request's ID.
181
+ * Each result type is named `<request type>Result` by the bridge protocol.
182
+ */
183
+ async function respondToRequest<Request extends OneShotRequest>(
184
+ message: Request,
185
+ handler: OneShotHandler<Request> | undefined,
186
+ ) {
187
+ if (handler === undefined) {
188
+ return
189
+ }
190
+ const response = await handler(message)
191
+ const result = {
192
+ type: `${message.type}Result` as const,
193
+ requestId: message.requestId,
194
+ ...response,
195
+ } as HostToSandboxMessage
196
+ post(result)
197
+ }
198
+
174
199
  function postQueryDataSourceResult(args: {
175
200
  subscriptionId: string
176
201
  response: CustomBlockHostQueryDataSourceResult
@@ -221,84 +246,6 @@ export function createCustomBlockHost(
221
246
  })
222
247
  }
223
248
 
224
- async function handleCreatePage(message: CreatePageMessage) {
225
- if (handlers.createPage === undefined) {
226
- return
227
- }
228
- const response = await handlers.createPage(message)
229
- const result: CreatePageResultMessage = {
230
- type: "createPageResult",
231
- requestId: message.requestId,
232
- ...response,
233
- }
234
- post(result)
235
- }
236
-
237
- async function handleGetPage(message: GetPageMessage) {
238
- if (handlers.getPage === undefined) {
239
- return
240
- }
241
- const response = await handlers.getPage(message)
242
- const result: GetPageResultMessage = {
243
- type: "getPageResult",
244
- requestId: message.requestId,
245
- ...response,
246
- }
247
- post(result)
248
- }
249
-
250
- async function handleGetUser(message: GetUserMessage) {
251
- if (handlers.getUser === undefined) {
252
- return
253
- }
254
- const response = await handlers.getUser(message)
255
- const result: GetUserResultMessage = {
256
- type: "getUserResult",
257
- requestId: message.requestId,
258
- ...response,
259
- }
260
- post(result)
261
- }
262
-
263
- async function handleListUsers(message: ListUsersMessage) {
264
- if (handlers.listUsers === undefined) {
265
- return
266
- }
267
- const response = await handlers.listUsers(message)
268
- const result: ListUsersResultMessage = {
269
- type: "listUsersResult",
270
- requestId: message.requestId,
271
- ...response,
272
- }
273
- post(result)
274
- }
275
-
276
- async function handleOpenPage(message: OpenPageMessage) {
277
- if (handlers.openPage === undefined) {
278
- return
279
- }
280
- const response = await handlers.openPage(message)
281
- const result: OpenPageResultMessage = {
282
- type: "openPageResult",
283
- requestId: message.requestId,
284
- ...response,
285
- }
286
- post(result)
287
- }
288
-
289
- async function handleUpdatePage(message: UpdatePageMessage) {
290
- if (handlers.updatePage === undefined) {
291
- return
292
- }
293
- const response = await handlers.updatePage(message)
294
- const result: UpdatePageResultMessage = {
295
- type: "updatePageResult",
296
- requestId: message.requestId,
297
- ...response,
298
- }
299
- post(result)
300
- }
301
-
302
249
  /**
303
250
  * Sends any pending host state changes to the sandbox. It is possible for these to change
304
251
  * between when the host sends `init` and when the sandbox responds with `initResult`. The host
@@ -411,12 +358,12 @@ export function createCustomBlockHost(
411
358
  onQueryDataSource: handleQuery,
412
359
  onUnsubscribeDataSourceQuery: message =>
413
360
  querySubscriptions.unsubscribe(message.subscriptionId),
414
- onCreatePage: handleCreatePage,
415
- onGetPage: handleGetPage,
416
- onOpenPage: handleOpenPage,
417
- onGetUser: handleGetUser,
418
- onListUsers: handleListUsers,
419
- onUpdatePage: handleUpdatePage,
361
+ onCreatePage: message => respondToRequest(message, handlers.createPage),
362
+ onGetPage: message => respondToRequest(message, handlers.getPage),
363
+ onOpenPage: message => respondToRequest(message, handlers.openPage),
364
+ onGetUser: message => respondToRequest(message, handlers.getUser),
365
+ onListUsers: message => respondToRequest(message, handlers.listUsers),
366
+ onUpdatePage: message => respondToRequest(message, handlers.updatePage),
420
367
  onResize: message => handlers.resize?.(message),
421
368
  })
422
369