@notionhq/custom-blocks 0.3.5 → 0.4.0

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 (124) hide show
  1. package/README.md +2 -2
  2. package/dist/bridge/SandboxBridge.d.ts +4 -23
  3. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  4. package/dist/bridge/SandboxBridge.js +36 -104
  5. package/dist/bridge/requests.d.ts +35 -0
  6. package/dist/bridge/requests.d.ts.map +1 -0
  7. package/dist/bridge/requests.js +97 -0
  8. package/dist/dataSources/bridge/resolveDataSourceQuery.d.ts.map +1 -1
  9. package/dist/dataSources/bridge/resolveDataSourceQuery.js +15 -1
  10. package/dist/dataSources/createDataSourcePage.d.ts.map +1 -1
  11. package/dist/dataSources/createDataSourcePage.js +5 -1
  12. package/dist/dataSources/types.d.ts +2 -0
  13. package/dist/dataSources/types.d.ts.map +1 -1
  14. package/dist/index.d.ts +2 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/manifest/types.d.ts +1 -1
  17. package/dist/manifest/types.d.ts.map +1 -1
  18. package/dist/navigation/openExternalUrl.d.ts.map +1 -1
  19. package/dist/navigation/openExternalUrl.js +2 -1
  20. package/dist/pages/bridge/getPageForDataSource.d.ts.map +1 -1
  21. package/dist/pages/bridge/getPageForDataSource.js +2 -1
  22. package/dist/pages/bridge/sendCreatePageRequest.d.ts.map +1 -1
  23. package/dist/pages/bridge/sendCreatePageRequest.js +2 -1
  24. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.d.ts +11 -2
  25. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.d.ts.map +1 -1
  26. package/dist/pages/bridge/toNotionPageFieldsFromBridgePage.js +0 -1
  27. package/dist/pages/bridge/toNotionPagePropertyFromBridgeProperty.d.ts.map +1 -1
  28. package/dist/pages/bridge/toNotionPagePropertyFromBridgeProperty.js +0 -6
  29. package/dist/pages/bridge/updatePage.d.ts.map +1 -1
  30. package/dist/pages/bridge/updatePage.js +19 -4
  31. package/dist/pages/createPage.d.ts.map +1 -1
  32. package/dist/pages/createPage.js +5 -1
  33. package/dist/pages/openPage.d.ts.map +1 -1
  34. package/dist/pages/openPage.js +2 -1
  35. package/dist/pages/types.d.ts +5 -7
  36. package/dist/pages/types.d.ts.map +1 -1
  37. package/dist/protocol/compatibility/protocol3To5/bridgePage.d.ts +7 -5
  38. package/dist/protocol/context/parent.d.ts +2 -0
  39. package/dist/protocol/dataSources/bridgeArchiveStatus.d.ts +3 -0
  40. package/dist/protocol/dataSources/bridgeArchiveStatus.js +6 -0
  41. package/dist/protocol/dataSources/bridgeDataSourceBinding.d.ts +0 -4
  42. package/dist/protocol/dataSources/bridgeDataSourceBindings.d.ts +0 -4
  43. package/dist/protocol/dataSources/collectionSchema.d.ts +0 -4
  44. package/dist/protocol/dataSources/messages/dataSourcesChanged.d.ts +0 -4
  45. package/dist/protocol/dataSources/messages/queryDataSource.d.ts +17 -14
  46. package/dist/protocol/dataSources/messages/queryDataSource.js +2 -0
  47. package/dist/protocol/dataSources/notionDataSource.d.ts +0 -8
  48. package/dist/protocol/dataSources/properties/propertySchema.d.ts +3 -7
  49. package/dist/protocol/dataSources/properties/propertySchema.js +1 -3
  50. package/dist/protocol/handshake/connect.d.ts +15 -4
  51. package/dist/protocol/handshake/init.d.ts +15 -8
  52. package/dist/protocol/icons.d.ts +17 -0
  53. package/dist/protocol/icons.js +27 -0
  54. package/dist/protocol/index.d.ts +13 -9
  55. package/dist/protocol/index.js +8 -5
  56. package/dist/protocol/manifest/grants.d.ts +48 -0
  57. package/dist/protocol/manifest/grants.js +19 -0
  58. package/dist/protocol/manifest/manifest.d.ts +33 -10
  59. package/dist/protocol/manifest/manifest.js +11 -5
  60. package/dist/protocol/messages/hostToSandbox.d.ts +109 -87
  61. package/dist/protocol/messages/invalidHostMessage.d.ts +2 -0
  62. package/dist/protocol/messages/invalidHostMessage.js +2 -0
  63. package/dist/protocol/messages/invalidSandboxMessage.d.ts +2 -0
  64. package/dist/protocol/messages/invalidSandboxMessage.js +2 -0
  65. package/dist/protocol/messages/sandboxToHost.d.ts +104 -17
  66. package/dist/protocol/navigation/openExternalUrl.d.ts +2 -2
  67. package/dist/protocol/pages/bridgePage.d.ts +7 -6
  68. package/dist/protocol/pages/bridgePage.js +0 -1
  69. package/dist/protocol/pages/makeBridgePage.d.ts +3 -1
  70. package/dist/protocol/pages/messages/createPage.d.ts +103 -27
  71. package/dist/protocol/pages/messages/createPage.js +3 -1
  72. package/dist/protocol/pages/messages/getPage.d.ts +23 -19
  73. package/dist/protocol/pages/messages/openPage.d.ts +2 -2
  74. package/dist/protocol/pages/messages/updatePage.d.ts +30 -24
  75. package/dist/protocol/pages/pageAppearance.d.ts +36 -1
  76. package/dist/protocol/pages/pageAppearance.js +9 -0
  77. package/dist/protocol/pages/properties/bridgePagePropertyMaps.d.ts +0 -8
  78. package/dist/protocol/pages/properties/bridgePagePropertyValue.d.ts +0 -4
  79. package/dist/protocol/pages/properties/bridgePagePropertyValue.js +0 -5
  80. package/dist/protocol/users/makeBridgeUser.d.ts +3 -1
  81. package/dist/protocol/users/messages/getUser.d.ts +2 -2
  82. package/dist/protocol/users/messages/listUsers.d.ts +2 -2
  83. package/dist/protocol/utils/errors.d.ts +6 -4
  84. package/dist/protocol/utils/errors.js +3 -3
  85. package/dist/protocol/utils/protocolVersion.d.ts +2 -1
  86. package/dist/protocol/utils/protocolVersion.js +3 -1
  87. package/dist/protocol/utils/readRequestId.d.ts +2 -0
  88. package/dist/protocol/utils/readRequestId.js +7 -0
  89. package/dist/users/getUser.d.ts.map +1 -1
  90. package/dist/users/getUser.js +2 -1
  91. package/dist/users/listUsers.d.ts.map +1 -1
  92. package/dist/users/listUsers.js +4 -2
  93. package/dist/utils/version.js +1 -1
  94. package/docs/data-sources.md +38 -34
  95. package/docs/errors.md +47 -5
  96. package/docs/grants.md +127 -0
  97. package/docs/pages.md +45 -32
  98. package/docs/users.md +2 -2
  99. package/package.json +1 -1
  100. package/src/bridge/SandboxBridge.ts +44 -188
  101. package/src/bridge/requests.ts +138 -0
  102. package/src/dataSources/bridge/resolveDataSourceQuery.ts +21 -1
  103. package/src/dataSources/createDataSourcePage.ts +5 -1
  104. package/src/dataSources/types.ts +3 -0
  105. package/src/index.ts +8 -4
  106. package/src/manifest/types.ts +1 -1
  107. package/src/navigation/openExternalUrl.ts +5 -1
  108. package/src/pages/bridge/getPageForDataSource.ts +5 -1
  109. package/src/pages/bridge/sendCreatePageRequest.ts +5 -1
  110. package/src/pages/bridge/toNotionPageFieldsFromBridgePage.ts +0 -1
  111. package/src/pages/bridge/toNotionPagePropertyFromBridgeProperty.ts +0 -6
  112. package/src/pages/bridge/updatePage.ts +28 -8
  113. package/src/pages/createPage.ts +5 -1
  114. package/src/pages/openPage.ts +5 -1
  115. package/src/pages/types.ts +7 -5
  116. package/src/users/getUser.ts +5 -1
  117. package/src/users/listUsers.ts +9 -4
  118. package/dist/bridge/pendingRequests.d.ts +0 -14
  119. package/dist/bridge/pendingRequests.d.ts.map +0 -1
  120. package/dist/bridge/pendingRequests.js +0 -28
  121. package/dist/protocol/manifest/capabilities.d.ts +0 -37
  122. package/dist/protocol/manifest/capabilities.js +0 -13
  123. package/docs/capabilities.md +0 -111
  124. package/src/bridge/pendingRequests.ts +0 -28
@@ -0,0 +1,138 @@
1
+ import type { SandboxToHostMessage } from "@notionhq/custom-blocks-protocol/messages/sandboxToHost.js"
2
+ import type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/utils/errors.js"
3
+ import * as v from "valibot"
4
+
5
+ export type RequestMessage<Message = SandboxToHostMessage> = Message extends {
6
+ requestId: string
7
+ }
8
+ ? Omit<Message, "requestId">
9
+ : never
10
+
11
+ export type RequestError = { status: "error"; error: CustomBlockErrorInfo }
12
+
13
+ type PendingRequest = {
14
+ receive: (message: unknown) => CustomBlockErrorInfo | undefined
15
+ fail: (error: CustomBlockErrorInfo) => void
16
+ timeout: ReturnType<typeof setTimeout>
17
+ }
18
+
19
+ export type ResponseSchema = v.GenericSchema<
20
+ unknown,
21
+ { type: string; requestId: string; status: "success" } | RequestError
22
+ >
23
+ export type RequestResult<Schema extends ResponseSchema> =
24
+ | Omit<
25
+ Extract<v.InferOutput<Schema>, { status: "success" }>,
26
+ "type" | "requestId"
27
+ >
28
+ | RequestError
29
+
30
+ /** Owns correlation, response validation, and completion for one bridge's requests. */
31
+ export class Requests {
32
+ private nextId = 1
33
+ private isDisposed = false
34
+ private readonly pending = new Map<string, PendingRequest>()
35
+
36
+ constructor(
37
+ private readonly post: (message: SandboxToHostMessage) => void,
38
+ private readonly timeoutMs = 60_000,
39
+ ) {}
40
+
41
+ send<Schema extends ResponseSchema>(
42
+ message: RequestMessage,
43
+ responseSchema: Schema,
44
+ ): Promise<RequestResult<Schema>> {
45
+ if (this.isDisposed) {
46
+ return Promise.resolve({ status: "error", error: disposedError() })
47
+ }
48
+ return new Promise(resolve => {
49
+ const requestId = `custom-block-request-${this.nextId++}`
50
+ const timeout = setTimeout(() => {
51
+ this.fail(requestId, {
52
+ code: "request_timeout",
53
+ message: `The host did not return a result within ${this.timeoutMs} ms. The operation may still complete.`,
54
+ isRetryable: false,
55
+ })
56
+ }, this.timeoutMs)
57
+ this.pending.set(requestId, {
58
+ timeout,
59
+ fail: error => resolve({ status: "error", error }),
60
+ receive: response => {
61
+ const parsed = v.safeParse(responseSchema, response)
62
+ if (!parsed.success) {
63
+ const error: CustomBlockErrorInfo = {
64
+ code: "invalid_host_message",
65
+ message: `The host returned an invalid response to ${message.type}.`,
66
+ isRetryable: false,
67
+ }
68
+ resolve({ status: "error", error })
69
+ return error
70
+ }
71
+ if (isSuccessResponse<Schema>(parsed.output)) {
72
+ const { type: _, requestId: __, ...result } = parsed.output
73
+ resolve(result)
74
+ } else if (parsed.output.status === "error") {
75
+ resolve({ status: "error", error: parsed.output.error })
76
+ }
77
+ },
78
+ })
79
+ try {
80
+ this.post({ ...message, requestId })
81
+ } catch (error) {
82
+ console.warn("[custom-blocks-sdk] could not send request", error)
83
+ this.fail(requestId, {
84
+ code: "request_send_failed",
85
+ message: "The SDK could not send the request to the host.",
86
+ isRetryable: false,
87
+ })
88
+ }
89
+ })
90
+ }
91
+
92
+ receive<Message extends { requestId: string }>(
93
+ message: Message,
94
+ ): CustomBlockErrorInfo | undefined {
95
+ const request = this.finish(message.requestId)
96
+ if (request === undefined) {
97
+ console.warn(
98
+ `[custom-blocks-sdk] result for unknown requestId ${message.requestId}`,
99
+ )
100
+ return
101
+ }
102
+ return request.receive(message)
103
+ }
104
+
105
+ fail(requestId: string, error: CustomBlockErrorInfo) {
106
+ this.finish(requestId)?.fail(error)
107
+ }
108
+
109
+ disposeAll() {
110
+ this.isDisposed = true
111
+ for (const requestId of this.pending.keys()) {
112
+ this.fail(requestId, disposedError())
113
+ }
114
+ }
115
+
116
+ private finish(requestId: string): PendingRequest | undefined {
117
+ const request = this.pending.get(requestId)
118
+ if (request !== undefined) {
119
+ this.pending.delete(requestId)
120
+ clearTimeout(request.timeout)
121
+ }
122
+ return request
123
+ }
124
+ }
125
+
126
+ function disposedError(): CustomBlockErrorInfo {
127
+ return {
128
+ code: "request_disposed",
129
+ message: "The SDK bridge was disposed. The operation may still complete.",
130
+ isRetryable: false,
131
+ }
132
+ }
133
+
134
+ function isSuccessResponse<Schema extends ResponseSchema>(
135
+ response: v.InferOutput<Schema>,
136
+ ): response is Extract<v.InferOutput<Schema>, { status: "success" }> {
137
+ return response.status === "success"
138
+ }
@@ -15,6 +15,7 @@ type DataSourceQueryOptions = {
15
15
  limit?: unknown
16
16
  filter?: unknown
17
17
  sorts?: unknown
18
+ archiveStatus?: unknown
18
19
  }
19
20
 
20
21
  export function resolveDataSourceQuery(args: {
@@ -100,6 +101,21 @@ export function resolveDataSourceQuery(args: {
100
101
  return { status: "error", error: getInvalidQueryError(sorts.error) }
101
102
  }
102
103
 
104
+ const archiveStatus = queryOptions?.archiveStatus
105
+ if (
106
+ archiveStatus !== undefined &&
107
+ archiveStatus !== "active" &&
108
+ archiveStatus !== "archived" &&
109
+ archiveStatus !== "all"
110
+ ) {
111
+ return {
112
+ status: "error",
113
+ error: getInvalidQueryError(
114
+ 'Data source archiveStatus must be "active", "archived", or "all".',
115
+ ),
116
+ }
117
+ }
118
+
103
119
  return {
104
120
  status: "ok",
105
121
  query: {
@@ -107,6 +123,7 @@ export function resolveDataSourceQuery(args: {
107
123
  limit: limit.limit,
108
124
  filter: filter.filter,
109
125
  sorts: sorts.sorts,
126
+ ...(archiveStatus !== undefined ? { archive_status: archiveStatus } : {}),
110
127
  },
111
128
  }
112
129
  }
@@ -146,7 +163,7 @@ function resolveDataSourceQueryOptions(
146
163
  const optionsObject = options as Record<string, unknown>
147
164
  if (
148
165
  Object.keys(optionsObject).some(
149
- key => !["limit", "filter", "sorts"].includes(key),
166
+ key => !["limit", "filter", "sorts", "archiveStatus"].includes(key),
150
167
  )
151
168
  ) {
152
169
  return {
@@ -157,6 +174,9 @@ function resolveDataSourceQueryOptions(
157
174
  return {
158
175
  status: "ok",
159
176
  options: {
177
+ archiveStatus: Object.hasOwn(optionsObject, "archiveStatus")
178
+ ? optionsObject.archiveStatus
179
+ : undefined,
160
180
  limit: Object.hasOwn(optionsObject, "limit")
161
181
  ? optionsObject.limit
162
182
  : undefined,
@@ -32,7 +32,11 @@ export async function createDataSourcePage(
32
32
  }
33
33
  const result = await sendCreatePageRequest({
34
34
  bridge,
35
- request: prepared.request,
35
+ request: {
36
+ ...prepared.request,
37
+ ...(payload.icon === undefined ? {} : { icon: payload.icon }),
38
+ ...(payload.cover === undefined ? {} : { cover: payload.cover }),
39
+ },
36
40
  dataSource,
37
41
  })
38
42
  if (result.status === "error") {
@@ -30,6 +30,8 @@ export type DataSourceSnapshot = {
30
30
  hasMore: boolean
31
31
  }
32
32
 
33
+ export type NotionArchiveStatus = "active" | "archived" | "all"
34
+
33
35
  export type DataSourceQueryOptions = {
34
36
  /** Maximum number of rows to request from the host. Defaults to 20. */
35
37
  limit?: number
@@ -37,6 +39,7 @@ export type DataSourceQueryOptions = {
37
39
  filter?: NotionDataSourceFilter
38
40
  /** Optional property sorts. The host applies them in array order. */
39
41
  sorts?: NotionDataSourceSort[]
42
+ archiveStatus?: NotionArchiveStatus
40
43
  }
41
44
 
42
45
  export type SubscribeToDataSourceArgs = {
package/src/index.ts CHANGED
@@ -45,10 +45,14 @@ export type {
45
45
  } from "@notionhq/custom-blocks-protocol/handshake/init.js"
46
46
  export { CustomBlockInitializationError } from "@notionhq/custom-blocks-protocol/handshake/init.js"
47
47
  export type {
48
- CustomBlockCapabilityDeclaration,
49
- CustomBlockCapabilityName,
50
- CustomBlockCapabilityResourceName,
51
- } from "@notionhq/custom-blocks-protocol/manifest/capabilities.js"
48
+ NotionIconColor,
49
+ NotionIconName,
50
+ } from "@notionhq/custom-blocks-protocol/icons.js"
51
+ export type {
52
+ CustomBlockGrant,
53
+ CustomBlockGrantAction,
54
+ CustomBlockGrantResourceName,
55
+ } from "@notionhq/custom-blocks-protocol/manifest/grants.js"
52
56
  export type {
53
57
  ManifestDataSource,
54
58
  ManifestIcon,
@@ -1,6 +1,6 @@
1
1
  import type { BridgeCustomBlockManifest } from "@notionhq/custom-blocks-protocol/manifest/manifest.js"
2
2
 
3
- /** A block's declared metadata, data sources, and capabilities. */
3
+ /** A block's declared metadata, data sources, and grants. */
4
4
  export type NotionCustomBlockManifest = BridgeCustomBlockManifest
5
5
 
6
6
  /** @deprecated Use NotionCustomBlockManifest. */
@@ -1,3 +1,4 @@
1
+ import { openExternalUrlResultMessageSchema } from "@notionhq/custom-blocks-protocol/navigation/openExternalUrl.js"
1
2
  import { getBridge } from "../bridge/getBridge.js"
2
3
  import type { OpenExternalUrlResult } from "./types.js"
3
4
 
@@ -5,5 +6,8 @@ import type { OpenExternalUrlResult } from "./types.js"
5
6
  export const openExternalUrl = (
6
7
  url: string,
7
8
  ): Promise<OpenExternalUrlResult> => {
8
- return getBridge().requestOpenExternalUrl({ url })
9
+ return getBridge().request(
10
+ { type: "openExternalUrl", url },
11
+ openExternalUrlResultMessageSchema,
12
+ )
9
13
  }
@@ -1,4 +1,5 @@
1
1
  import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/notionDataSource.js"
2
+ import { getPageResultMessageSchema } from "@notionhq/custom-blocks-protocol/pages/messages/getPage.js"
2
3
  import type { NotionPageId } from "@notionhq/custom-blocks-protocol/utils/ids.js"
3
4
  import type { SandboxBridge } from "../../bridge/SandboxBridge.js"
4
5
  import { makeNotionPage } from "../makeNotionPage.js"
@@ -13,7 +14,10 @@ export async function getPageForDataSource(
13
14
  ): Promise<GetPageResult> {
14
15
  const { pageId, dataSource } = args
15
16
  const dataSourceId = dataSource?.collectionPointer?.id
16
- const result = await bridge.requestGetPage({ dataSourceId, pageId })
17
+ const result = await bridge.request(
18
+ { type: "getPage", dataSourceId, pageId },
19
+ getPageResultMessageSchema,
20
+ )
17
21
 
18
22
  if (result.status === "error") {
19
23
  return result
@@ -1,6 +1,7 @@
1
1
  import type { NotionDataSource } from "@notionhq/custom-blocks-protocol/dataSources/notionDataSource.js"
2
2
  import type { BridgeMessagePayload } from "@notionhq/custom-blocks-protocol/messages/messagePayload.js"
3
3
  import type { CreatePageMessage } from "@notionhq/custom-blocks-protocol/pages/messages/createPage.js"
4
+ import { createPageResultMessageSchema } from "@notionhq/custom-blocks-protocol/pages/messages/createPage.js"
4
5
  import type { SandboxBridge } from "../../bridge/SandboxBridge.js"
5
6
  import { makeNotionPage } from "../makeNotionPage.js"
6
7
  import type { CreatePageResult } from "../types.js"
@@ -11,7 +12,10 @@ export async function sendCreatePageRequest(args: {
11
12
  dataSource: NotionDataSource | undefined
12
13
  }): Promise<CreatePageResult> {
13
14
  const { bridge, request, dataSource } = args
14
- const result = await bridge.requestCreatePage(request)
15
+ const result = await bridge.request(
16
+ { type: "createPage", ...request },
17
+ createPageResultMessageSchema,
18
+ )
15
19
  if (result.status === "error") {
16
20
  return result
17
21
  }
@@ -15,7 +15,6 @@ export function toNotionPageFieldsFromBridgePage(page: BridgePage) {
15
15
  properties,
16
16
  created_by: page.created_by,
17
17
  last_edited_by: page.last_edited_by,
18
- last_visited_time: page.last_visited_time,
19
18
  created_time: page.created_time,
20
19
  last_edited_time: page.last_edited_time,
21
20
  icon: page.icon,
@@ -69,12 +69,6 @@ export function toNotionPagePropertyFromBridgeProperty(
69
69
  type: value.type,
70
70
  last_edited_by: value.last_edited_by,
71
71
  }
72
- case "last_visited_time":
73
- return {
74
- id: value.id,
75
- type: value.type,
76
- last_visited_time: value.last_visited_time,
77
- }
78
72
  case "unique_id":
79
73
  return { id: value.id, type: value.type, unique_id: value.unique_id }
80
74
  case "button":
@@ -4,6 +4,7 @@ import type {
4
4
  UpdatePageMessage,
5
5
  UpdatePageResultMessage,
6
6
  } from "@notionhq/custom-blocks-protocol/pages/messages/updatePage.js"
7
+ import { updatePageResultMessageSchema } from "@notionhq/custom-blocks-protocol/pages/messages/updatePage.js"
7
8
  import type { NotionPageId } from "@notionhq/custom-blocks-protocol/utils/ids.js"
8
9
 
9
10
  import type { SandboxBridge } from "../../bridge/SandboxBridge.js"
@@ -17,7 +18,7 @@ export type UpdatePageArgs = PageUpdatePayload & {
17
18
 
18
19
  const pendingPageUpdatesByBridge = new WeakMap<
19
20
  SandboxBridge,
20
- Map<NotionPageId, Promise<void>>
21
+ Map<NotionPageId, Promise<boolean>>
21
22
  >()
22
23
 
23
24
  export function updatePage(
@@ -29,9 +30,20 @@ export function updatePage(
29
30
  pendingPageUpdates = new Map()
30
31
  pendingPageUpdatesByBridge.set(bridge, pendingPageUpdates)
31
32
  }
32
- const operation = async (): Promise<
33
- BridgeMessagePayload<UpdatePageResultMessage>
34
- > => {
33
+ const operation = async (
34
+ previousWriteIncomplete: boolean,
35
+ ): Promise<BridgeMessagePayload<UpdatePageResultMessage>> => {
36
+ if (previousWriteIncomplete) {
37
+ return {
38
+ status: "error",
39
+ error: {
40
+ code: "previous_write_incomplete",
41
+ message:
42
+ "This queued update was not sent because a previous write timed out and may still complete.",
43
+ isRetryable: false,
44
+ },
45
+ }
46
+ }
35
47
  const { dataSource } = args
36
48
  const dataSourceId = dataSource?.collectionPointer?.id
37
49
  const resolved = resolvePropertyWriteMapForDataSource({
@@ -72,13 +84,21 @@ export function updatePage(
72
84
  if (args.is_archived !== undefined) {
73
85
  outbound.archived = args.is_archived
74
86
  }
75
- return bridge.requestUpdatePage(outbound)
87
+ return bridge.request(
88
+ { type: "updatePage", ...outbound },
89
+ updatePageResultMessageSchema,
90
+ )
76
91
  }
77
92
  const pending = pendingPageUpdates.get(args.pageId)
78
- const result = pending ? pending.then(operation) : operation()
93
+ const result = pending ? pending.then(operation) : operation(false)
94
+ // A timeout does not cancel host work. Skip the rest of this queue to avoid
95
+ // sending writes that could be applied before the timed-out write.
79
96
  const finished = result.then(
80
- () => undefined,
81
- () => undefined,
97
+ result =>
98
+ result.status === "error" &&
99
+ (result.error.code === "request_timeout" ||
100
+ result.error.code === "previous_write_incomplete"),
101
+ () => false,
82
102
  )
83
103
  pendingPageUpdates.set(args.pageId, finished)
84
104
  void finished.then(() => {
@@ -25,7 +25,11 @@ export async function createPage(
25
25
  }
26
26
  return sendCreatePageRequest({
27
27
  bridge,
28
- request: prepared.request,
28
+ request: {
29
+ ...prepared.request,
30
+ ...(input.icon === undefined ? {} : { icon: input.icon }),
31
+ ...(input.cover === undefined ? {} : { cover: input.cover }),
32
+ },
29
33
  dataSource: undefined,
30
34
  })
31
35
  }
@@ -1,3 +1,4 @@
1
+ import { openPageResultMessageSchema } from "@notionhq/custom-blocks-protocol/pages/messages/openPage.js"
1
2
  import type { NotionPageId } from "@notionhq/custom-blocks-protocol/utils/ids.js"
2
3
  import { getBridge } from "../bridge/getBridge.js"
3
4
  import type { OpenPageOptions, OpenPageResult } from "./types.js"
@@ -9,5 +10,8 @@ export const openPage = (
9
10
  pageId: NotionPageId,
10
11
  options: OpenPageOptions,
11
12
  ): Promise<OpenPageResult> => {
12
- return getBridge().requestOpenPage({ pageId, mode: options.mode })
13
+ return getBridge().request(
14
+ { type: "openPage", pageId, mode: options.mode },
15
+ openPageResultMessageSchema,
16
+ )
13
17
  }
@@ -14,6 +14,7 @@ import type {
14
14
  } from "@notionhq/custom-blocks-protocol/pages/messages/openPage.js"
15
15
  import type { CustomBlockUpdatePageErrorInfo } from "@notionhq/custom-blocks-protocol/pages/messages/updatePage.js"
16
16
  import type {
17
+ NotionCreatePageIcon,
17
18
  NotionPageCover,
18
19
  NotionPageIcon,
19
20
  } from "@notionhq/custom-blocks-protocol/pages/pageAppearance.js"
@@ -54,7 +55,6 @@ export type NotionPagePropertyValue = { id: string } & (
54
55
  | { type: "last_edited_time"; last_edited_time: string | null }
55
56
  | { type: "created_by"; created_by: PageMetadataActor | null }
56
57
  | { type: "last_edited_by"; last_edited_by: PageMetadataActor | null }
57
- | { type: "last_visited_time"; last_visited_time: string | null }
58
58
  | { type: "unique_id"; unique_id: string }
59
59
  | { type: "button"; button: string }
60
60
  | { type: "formula"; formula: string }
@@ -83,7 +83,6 @@ export type NotionPagePropertyInputValue =
83
83
  | "last_edited_time"
84
84
  | "created_by"
85
85
  | "last_edited_by"
86
- | "last_visited_time"
87
86
  | "unique_id"
88
87
  | "button"
89
88
  | "formula"
@@ -108,7 +107,6 @@ export type NotionPage = {
108
107
  properties: NotionPagePropertyReadMap
109
108
  created_by: PageMetadataActor | null
110
109
  last_edited_by: PageMetadataActor | null
111
- last_visited_time: string | null
112
110
  created_time: string | null
113
111
  last_edited_time: string | null
114
112
  icon: NotionPageIcon | null
@@ -156,9 +154,13 @@ export type CreatePageResult =
156
154
  export type { CreatePageParent } from "./parent.js"
157
155
 
158
156
  /**
159
- * Arguments accepted by `sdk.pages.create`. Mirrors the Notion public API `POST /v1/pages` API.
157
+ * Arguments accepted by `sdk.pages.create`.
160
158
  */
161
- export type CreatePageArgs = { properties: NotionPagePropertyInputMap } & (
159
+ export type CreatePageArgs = {
160
+ properties: NotionPagePropertyInputMap
161
+ icon?: NotionCreatePageIcon
162
+ cover?: NotionPageCover
163
+ } & (
162
164
  | {
163
165
  parent: PageParent
164
166
  position?: NotionCreatePagePosition
@@ -1,3 +1,4 @@
1
+ import { getUserResultMessageSchema } from "@notionhq/custom-blocks-protocol/users/messages/getUser.js"
1
2
  import type { NotionUserId } from "@notionhq/custom-blocks-protocol/users/userId.js"
2
3
  import { getBridge } from "../bridge/getBridge.js"
3
4
  import { toNotionUserFromBridgeUser } from "./bridge/toNotionUserFromBridgeUser.js"
@@ -7,7 +8,10 @@ import type { GetUserResult } from "./types.js"
7
8
  * Fetches a user by id.
8
9
  */
9
10
  export async function getUser(userId: NotionUserId): Promise<GetUserResult> {
10
- const result = await getBridge().requestGetUser({ userId })
11
+ const result = await getBridge().request(
12
+ { type: "getUser", userId },
13
+ getUserResultMessageSchema,
14
+ )
11
15
  return result.status === "error"
12
16
  ? result
13
17
  : { status: "success", user: toNotionUserFromBridgeUser(result.user) }
@@ -1,3 +1,4 @@
1
+ import { listUsersResultMessageSchema } from "@notionhq/custom-blocks-protocol/users/messages/listUsers.js"
1
2
  import { getBridge } from "../bridge/getBridge.js"
2
3
  import { toNotionUserFromBridgeUser } from "./bridge/toNotionUserFromBridgeUser.js"
3
4
  import type { ListUsersArgs, ListUsersResult } from "./types.js"
@@ -8,10 +9,14 @@ import type { ListUsersArgs, ListUsersResult } from "./types.js"
8
9
  export async function listUsers(
9
10
  input: ListUsersArgs = {},
10
11
  ): Promise<ListUsersResult> {
11
- const result = await getBridge().requestListUsers({
12
- startCursor: input.startCursor,
13
- pageSize: input.pageSize,
14
- })
12
+ const result = await getBridge().request(
13
+ {
14
+ type: "listUsers",
15
+ startCursor: input.startCursor,
16
+ pageSize: input.pageSize,
17
+ },
18
+ listUsersResultMessageSchema,
19
+ )
15
20
  return result.status === "error"
16
21
  ? result
17
22
  : {
@@ -1,14 +0,0 @@
1
- /**
2
- * Tracks one-shot request/response pairs over the `postMessage` bridge. Each `allocate` reserves a
3
- * unique `requestId` and stores its resolver. `resolve` finds the pending resolver by `requestId`
4
- * and calls it, then removes the entry so the same `requestId` cannot be resolved twice.
5
- */
6
- export declare class PendingRequests<T> {
7
- private readonly prefix;
8
- private nextId;
9
- private readonly entries;
10
- constructor(prefix: string);
11
- allocate(resolver: (value: T) => void): string;
12
- resolve(requestId: string, value: T): boolean;
13
- }
14
- //# sourceMappingURL=pendingRequests.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pendingRequests.d.ts","sourceRoot":"","sources":["../../src/bridge/pendingRequests.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,eAAe,CAAC,CAAC;IAIjB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAHnC,OAAO,CAAC,MAAM,CAAI;IAClB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAwC;IAEhE,YAA6B,MAAM,EAAE,MAAM,EAAI;IAE/C,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,MAAM,CAK7C;IAED,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAQ5C;CACD"}
@@ -1,28 +0,0 @@
1
- /**
2
- * Tracks one-shot request/response pairs over the `postMessage` bridge. Each `allocate` reserves a
3
- * unique `requestId` and stores its resolver. `resolve` finds the pending resolver by `requestId`
4
- * and calls it, then removes the entry so the same `requestId` cannot be resolved twice.
5
- */
6
- export class PendingRequests {
7
- prefix;
8
- nextId = 1;
9
- entries = new Map();
10
- constructor(prefix) {
11
- this.prefix = prefix;
12
- }
13
- allocate(resolver) {
14
- const requestId = `${this.prefix}-${this.nextId}`;
15
- this.nextId += 1;
16
- this.entries.set(requestId, resolver);
17
- return requestId;
18
- }
19
- resolve(requestId, value) {
20
- const resolver = this.entries.get(requestId);
21
- if (resolver === undefined) {
22
- return false;
23
- }
24
- this.entries.delete(requestId);
25
- resolver(value);
26
- return true;
27
- }
28
- }
@@ -1,37 +0,0 @@
1
- import * as v from "valibot";
2
- /**
3
- * One entry in the block manifest's `capabilities`: what the block needs to do
4
- * (`capability`) and what it acts on (`resource`). Viewers approve declared
5
- * entries before the block loads.
6
- *
7
- * Both fields accept any string so a newer vocabulary never makes an older host
8
- * or SDK reject a manifest.
9
- */
10
- export declare const customBlockCapabilityDeclarationSchema: v.ObjectSchema<{
11
- readonly capability: v.StringSchema<undefined>;
12
- readonly resource: v.StringSchema<undefined>;
13
- }, undefined>;
14
- export type CustomBlockCapabilityDeclaration = v.InferOutput<typeof customBlockCapabilityDeclarationSchema>;
15
- /**
16
- * Capability names that hosts recognize. `user` is shorthand for `user.read`
17
- * and `user.read_email` on the same resource. `page` is shorthand for
18
- * `page.read`, `page.insert`, `page.update`, and `page.archive` on the same
19
- * resource. `page.trash` is reserved for a future Trash call and is not
20
- * accepted yet.
21
- */
22
- export type CustomBlockCapabilityName = "user" | "user.read" | "user.read_email" | "page" | "page.read" | "page.insert" | "page.update" | "page.archive" | "network.fetch";
23
- /**
24
- * Resource names that hosts recognize. User capabilities take `current_user`
25
- * or `workspace` (every member of the workspace). Page capabilities take
26
- * `workspace` (every page the viewer can access), `containing_page`, or
27
- * `data_source`. `private` is reserved for a future
28
- * `page.insert` into the viewer's Private section and is not accepted yet.
29
- * `network.fetch` takes a bare HTTPS origin.
30
- */
31
- export type CustomBlockCapabilityResourceName = "current_user" | "workspace" | "containing_page" | "data_source" | CustomBlockOriginResourceName;
32
- /**
33
- * An HTTPS origin the block loads content from and connects to, such as
34
- * `https://api.example.com`. `https://*.example.com` covers subdomains. Hosts
35
- * allow only origins that are also on the workspace's network allowlist.
36
- */
37
- export type CustomBlockOriginResourceName = `https://${string}`;
@@ -1,13 +0,0 @@
1
- import * as v from "valibot";
2
- /**
3
- * One entry in the block manifest's `capabilities`: what the block needs to do
4
- * (`capability`) and what it acts on (`resource`). Viewers approve declared
5
- * entries before the block loads.
6
- *
7
- * Both fields accept any string so a newer vocabulary never makes an older host
8
- * or SDK reject a manifest.
9
- */
10
- export const customBlockCapabilityDeclarationSchema = v.object({
11
- capability: v.string(),
12
- resource: v.string(),
13
- });