@wirecat/notion-cli 0.0.0-stage → 0.2.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 (262) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/LICENSE +204 -0
  3. package/README.md +42 -2
  4. package/contracts/wirecat.json +1964 -0
  5. package/dist/bin/notion.d.ts +3 -0
  6. package/dist/bin/notion.d.ts.map +1 -0
  7. package/dist/bin/notion.js +40 -0
  8. package/dist/bin/notion.js.map +1 -0
  9. package/dist/bundle/all-profiles-JGYLQZ5C.js +278 -0
  10. package/dist/bundle/anthropic-S2VSMIKQ.js +91 -0
  11. package/dist/bundle/app-server-SASYDBCA.js +240 -0
  12. package/dist/bundle/bun-LU5LP3JI.js +169 -0
  13. package/dist/bundle/bun-sqlite-JBSINB4O.js +76 -0
  14. package/dist/bundle/chunk-22QQUKWA.js +53 -0
  15. package/dist/bundle/chunk-2F4QQ7WB.js +73 -0
  16. package/dist/bundle/chunk-2F6VO5HA.js +73 -0
  17. package/dist/bundle/chunk-2LWO3PDX.js +726 -0
  18. package/dist/bundle/chunk-3AL7QUS3.js +2778 -0
  19. package/dist/bundle/chunk-3Z4YTSRY.js +731 -0
  20. package/dist/bundle/chunk-4HWJOU7N.js +38 -0
  21. package/dist/bundle/chunk-4IPURN72.js +1221 -0
  22. package/dist/bundle/chunk-4ZTVJHWU.js +206 -0
  23. package/dist/bundle/chunk-5D4AEUVA.js +24783 -0
  24. package/dist/bundle/chunk-5IYKVRH2.js +44811 -0
  25. package/dist/bundle/chunk-5NFK5EM2.js +362 -0
  26. package/dist/bundle/chunk-5QIQEUOG.js +188 -0
  27. package/dist/bundle/chunk-5RDNBG7D.js +72 -0
  28. package/dist/bundle/chunk-5RSC54NA.js +1328 -0
  29. package/dist/bundle/chunk-6EVKHZKN.js +158 -0
  30. package/dist/bundle/chunk-6RA3EABS.js +4012 -0
  31. package/dist/bundle/chunk-AOCUKEAX.js +131 -0
  32. package/dist/bundle/chunk-B26TQZKZ.js +217 -0
  33. package/dist/bundle/chunk-B7ON6KPT.js +1197 -0
  34. package/dist/bundle/chunk-BNPTZ5ZB.js +161 -0
  35. package/dist/bundle/chunk-D72URWJE.js +532 -0
  36. package/dist/bundle/chunk-D7QSEEYW.js +274 -0
  37. package/dist/bundle/chunk-DFDN6AUB.js +21 -0
  38. package/dist/bundle/chunk-DH5PRHT4.js +392 -0
  39. package/dist/bundle/chunk-EH6ZEJGT.js +932 -0
  40. package/dist/bundle/chunk-EUIVXFLJ.js +259 -0
  41. package/dist/bundle/chunk-FPY6EJ7B.js +9568 -0
  42. package/dist/bundle/chunk-FT3LN3HP.js +6848 -0
  43. package/dist/bundle/chunk-FWVBPRL4.js +4436 -0
  44. package/dist/bundle/chunk-FXEJY4OL.js +12538 -0
  45. package/dist/bundle/chunk-G3EZFRSS.js +1297 -0
  46. package/dist/bundle/chunk-GZOUFX3C.js +230 -0
  47. package/dist/bundle/chunk-HL52XXZX.js +78 -0
  48. package/dist/bundle/chunk-KCLOUUYF.js +100 -0
  49. package/dist/bundle/chunk-KFVDL2QS.js +146 -0
  50. package/dist/bundle/chunk-KGERXYTV.js +24 -0
  51. package/dist/bundle/chunk-KM6WHPHI.js +288 -0
  52. package/dist/bundle/chunk-LAYPFBOM.js +95 -0
  53. package/dist/bundle/chunk-LLG2POQ7.js +170 -0
  54. package/dist/bundle/chunk-M3KB25D3.js +1392 -0
  55. package/dist/bundle/chunk-M7S2NDWB.js +76 -0
  56. package/dist/bundle/chunk-MGUEZYFA.js +5546 -0
  57. package/dist/bundle/chunk-MY742GAD.js +1977 -0
  58. package/dist/bundle/chunk-MZU2YFBZ.js +823 -0
  59. package/dist/bundle/chunk-NOQ7B5N7.js +58 -0
  60. package/dist/bundle/chunk-NQWV4WQO.js +300 -0
  61. package/dist/bundle/chunk-ODBZNSZ6.js +1028 -0
  62. package/dist/bundle/chunk-OTV53F34.js +370 -0
  63. package/dist/bundle/chunk-P7CUM5IB.js +18 -0
  64. package/dist/bundle/chunk-RV64YUQX.js +185 -0
  65. package/dist/bundle/chunk-SHJQ6JKG.js +106 -0
  66. package/dist/bundle/chunk-SWAOWNKW.js +52 -0
  67. package/dist/bundle/chunk-SZOVLP44.js +29 -0
  68. package/dist/bundle/chunk-TPX2M45D.js +385 -0
  69. package/dist/bundle/chunk-TSDBSIFM.js +66 -0
  70. package/dist/bundle/chunk-VBHETQVV.js +64146 -0
  71. package/dist/bundle/chunk-VCIALTEM.js +3139 -0
  72. package/dist/bundle/chunk-VKE77B3F.js +2 -0
  73. package/dist/bundle/chunk-WCSOH4JS.js +142 -0
  74. package/dist/bundle/chunk-WF4D56CA.js +54 -0
  75. package/dist/bundle/chunk-WJHZMR2W.js +335 -0
  76. package/dist/bundle/chunk-X4TIMMKJ.js +16 -0
  77. package/dist/bundle/chunk-XF26XKTS.js +82 -0
  78. package/dist/bundle/chunk-XFLCVHSQ.js +50 -0
  79. package/dist/bundle/chunk-XQUUANA2.js +24600 -0
  80. package/dist/bundle/chunk-XYIT2XB3.js +1372 -0
  81. package/dist/bundle/chunk-Y5E2WNWB.js +35 -0
  82. package/dist/bundle/chunk-YMYUBZWT.js +1068 -0
  83. package/dist/bundle/chunk-YNTLT3Z3.js +2310 -0
  84. package/dist/bundle/chunk-YVI5SQPY.js +46 -0
  85. package/dist/bundle/chunk-Z56QFOBR.js +63 -0
  86. package/dist/bundle/chunk-ZKAEZXUP.js +787 -0
  87. package/dist/bundle/chunk-ZVVMGPBJ.js +50 -0
  88. package/dist/bundle/chunk-ZZ32DXTH.js +23 -0
  89. package/dist/bundle/command-contract-JS5V27NX.js +38 -0
  90. package/dist/bundle/command-server-entry-UFSG7KRO.js +167 -0
  91. package/dist/bundle/decode-7LLWTSPT.js +297 -0
  92. package/dist/bundle/definitions-M3U4UYHN.js +45607 -0
  93. package/dist/bundle/dispatch-2GY6JIJH.js +411 -0
  94. package/dist/bundle/dist-ILNOLGYN.js +7577 -0
  95. package/dist/bundle/echarts-BXX4OQWC.js +42501 -0
  96. package/dist/bundle/embed-JXFNBTKH.js +38 -0
  97. package/dist/bundle/embedder-57YZ44KO.js +111 -0
  98. package/dist/bundle/failure-MYT4HPE3.js +25 -0
  99. package/dist/bundle/first-run-3PF7T74J.js +29 -0
  100. package/dist/bundle/first-screen-7BEROEQR.js +36 -0
  101. package/dist/bundle/instructions-2VECIDXF.js +11 -0
  102. package/dist/bundle/mcp-O6ACR2DX.js +167 -0
  103. package/dist/bundle/mdast-util-from-markdown-R66F4HZA.js +1958 -0
  104. package/dist/bundle/mdast-util-gfm-LN2ELINH.js +2699 -0
  105. package/dist/bundle/meeting-combined-PPUQ3KBR.js +24 -0
  106. package/dist/bundle/meeting-model-ZNMYKMCD.js +26 -0
  107. package/dist/bundle/meeting-semantic-ETN3A4BJ.js +22 -0
  108. package/dist/bundle/micromark-extension-gfm-5X7YT43T.js +1838 -0
  109. package/dist/bundle/node-KM3CUABH.js +2340 -0
  110. package/dist/bundle/node-MPEQSFI3.js +176 -0
  111. package/dist/bundle/node-TLZSIHIJ.js +21 -0
  112. package/dist/bundle/node-sqlite-WA32MYSJ.js +74 -0
  113. package/dist/bundle/notion.js +185 -0
  114. package/dist/bundle/oauth-IAFZG3IR.js +20 -0
  115. package/dist/bundle/open-M3JQUC5G.js +23 -0
  116. package/dist/bundle/openai-HXUEHUSD.js +240 -0
  117. package/dist/bundle/process-F6TKXWWA.js +91 -0
  118. package/dist/bundle/program-Y7JAB6TG.js +33219 -0
  119. package/dist/bundle/prompt-IMUQLZD2.js +17 -0
  120. package/dist/bundle/recognize-2MZAYHAA.js +26 -0
  121. package/dist/bundle/remote-OV4LB6VR.js +32 -0
  122. package/dist/bundle/rerank-ZIULUS34.js +52 -0
  123. package/dist/bundle/schemas-DIVZS6TU.js +12214 -0
  124. package/dist/bundle/serve-YU7QBP6L.js +199 -0
  125. package/dist/bundle/server-FKQET4ZO.js +71 -0
  126. package/dist/bundle/server-LGMNONSB.js +202 -0
  127. package/dist/bundle/services-5WY4DCPJ.js +581 -0
  128. package/dist/bundle/sqlite-runtime-5MXZDETH.js +20 -0
  129. package/dist/bundle/state-LWMZH2KI.js +36 -0
  130. package/dist/bundle/store-EPZYV4VW.js +168 -0
  131. package/dist/bundle/store-QE6FEIZD.js +286 -0
  132. package/dist/bundle/store-maintenance-command-ZUUUFCHL.js +62 -0
  133. package/dist/commands/api.d.ts +4 -0
  134. package/dist/commands/api.d.ts.map +1 -0
  135. package/dist/commands/api.js +140 -0
  136. package/dist/commands/api.js.map +1 -0
  137. package/dist/commands/databases.d.ts +3 -0
  138. package/dist/commands/databases.d.ts.map +1 -0
  139. package/dist/commands/databases.js +65 -0
  140. package/dist/commands/databases.js.map +1 -0
  141. package/dist/commands/documents.d.ts +4 -0
  142. package/dist/commands/documents.d.ts.map +1 -0
  143. package/dist/commands/documents.js +41 -0
  144. package/dist/commands/documents.js.map +1 -0
  145. package/dist/commands/library.d.ts +10 -0
  146. package/dist/commands/library.d.ts.map +1 -0
  147. package/dist/commands/library.js +468 -0
  148. package/dist/commands/library.js.map +1 -0
  149. package/dist/commands/session.d.ts +3 -0
  150. package/dist/commands/session.d.ts.map +1 -0
  151. package/dist/commands/session.js +105 -0
  152. package/dist/commands/session.js.map +1 -0
  153. package/dist/config.d.ts +5 -0
  154. package/dist/config.d.ts.map +1 -0
  155. package/dist/config.js +13 -0
  156. package/dist/config.js.map +1 -0
  157. package/dist/context.d.ts +51 -0
  158. package/dist/context.d.ts.map +1 -0
  159. package/dist/context.js +67 -0
  160. package/dist/context.js.map +1 -0
  161. package/dist/generated/definitions.d.ts +3 -0
  162. package/dist/generated/definitions.d.ts.map +1 -0
  163. package/dist/generated/definitions.js +45603 -0
  164. package/dist/generated/definitions.js.map +1 -0
  165. package/dist/generated/manifest.d.ts +3 -0
  166. package/dist/generated/manifest.d.ts.map +1 -0
  167. package/dist/generated/manifest.js +2089 -0
  168. package/dist/generated/manifest.js.map +1 -0
  169. package/dist/generated/metadata.d.ts +6 -0
  170. package/dist/generated/metadata.d.ts.map +1 -0
  171. package/dist/generated/metadata.js +24 -0
  172. package/dist/generated/metadata.js.map +1 -0
  173. package/dist/generated/schemas.d.ts +1223 -0
  174. package/dist/generated/schemas.d.ts.map +1 -0
  175. package/dist/generated/schemas.js +11557 -0
  176. package/dist/generated/schemas.js.map +1 -0
  177. package/dist/generated/types.d.ts +11120 -0
  178. package/dist/generated/types.d.ts.map +1 -0
  179. package/dist/generated/types.js +5 -0
  180. package/dist/generated/types.js.map +1 -0
  181. package/dist/identity.d.ts +7 -0
  182. package/dist/identity.d.ts.map +1 -0
  183. package/dist/identity.js +30 -0
  184. package/dist/identity.js.map +1 -0
  185. package/dist/index.d.ts +3 -0
  186. package/dist/index.d.ts.map +1 -0
  187. package/dist/index.js +3 -0
  188. package/dist/index.js.map +1 -0
  189. package/dist/knowledge.d.ts +27 -0
  190. package/dist/knowledge.d.ts.map +1 -0
  191. package/dist/knowledge.js +87 -0
  192. package/dist/knowledge.js.map +1 -0
  193. package/dist/library.d.ts +44 -0
  194. package/dist/library.d.ts.map +1 -0
  195. package/dist/library.js +149 -0
  196. package/dist/library.js.map +1 -0
  197. package/dist/notion/client.d.ts +45 -0
  198. package/dist/notion/client.d.ts.map +1 -0
  199. package/dist/notion/client.js +121 -0
  200. package/dist/notion/client.js.map +1 -0
  201. package/dist/notion/errors.d.ts +24 -0
  202. package/dist/notion/errors.d.ts.map +1 -0
  203. package/dist/notion/errors.js +127 -0
  204. package/dist/notion/errors.js.map +1 -0
  205. package/dist/notion/ids.d.ts +7 -0
  206. package/dist/notion/ids.d.ts.map +1 -0
  207. package/dist/notion/ids.js +37 -0
  208. package/dist/notion/ids.js.map +1 -0
  209. package/dist/notion/links.d.ts +6 -0
  210. package/dist/notion/links.d.ts.map +1 -0
  211. package/dist/notion/links.js +22 -0
  212. package/dist/notion/links.js.map +1 -0
  213. package/dist/notion/notion.d.ts +95 -0
  214. package/dist/notion/notion.d.ts.map +1 -0
  215. package/dist/notion/notion.js +269 -0
  216. package/dist/notion/notion.js.map +1 -0
  217. package/dist/notion/port.d.ts +13 -0
  218. package/dist/notion/port.d.ts.map +1 -0
  219. package/dist/notion/port.js +60 -0
  220. package/dist/notion/port.js.map +1 -0
  221. package/dist/notion/read-posts.d.ts +36 -0
  222. package/dist/notion/read-posts.d.ts.map +1 -0
  223. package/dist/notion/read-posts.js +24 -0
  224. package/dist/notion/read-posts.js.map +1 -0
  225. package/dist/notion/reads.d.ts +331 -0
  226. package/dist/notion/reads.d.ts.map +1 -0
  227. package/dist/notion/reads.js +18 -0
  228. package/dist/notion/reads.js.map +1 -0
  229. package/dist/program.d.ts +6 -0
  230. package/dist/program.d.ts.map +1 -0
  231. package/dist/program.js +62 -0
  232. package/dist/program.js.map +1 -0
  233. package/dist/sync/cursor.d.ts +135 -0
  234. package/dist/sync/cursor.d.ts.map +1 -0
  235. package/dist/sync/cursor.js +113 -0
  236. package/dist/sync/cursor.js.map +1 -0
  237. package/dist/sync/files.d.ts +77 -0
  238. package/dist/sync/files.d.ts.map +1 -0
  239. package/dist/sync/files.js +12 -0
  240. package/dist/sync/files.js.map +1 -0
  241. package/dist/sync/front-matter.d.ts +10 -0
  242. package/dist/sync/front-matter.d.ts.map +1 -0
  243. package/dist/sync/front-matter.js +114 -0
  244. package/dist/sync/front-matter.js.map +1 -0
  245. package/dist/sync/markdown.d.ts +11 -0
  246. package/dist/sync/markdown.d.ts.map +1 -0
  247. package/dist/sync/markdown.js +42 -0
  248. package/dist/sync/markdown.js.map +1 -0
  249. package/dist/sync/source.d.ts +11 -0
  250. package/dist/sync/source.d.ts.map +1 -0
  251. package/dist/sync/source.js +995 -0
  252. package/dist/sync/source.js.map +1 -0
  253. package/dist/version.d.ts +2 -0
  254. package/dist/version.d.ts.map +1 -0
  255. package/dist/version.js +2 -0
  256. package/dist/version.js.map +1 -0
  257. package/dist/wcat.d.ts +3 -0
  258. package/dist/wcat.d.ts.map +1 -0
  259. package/dist/wcat.js +4 -0
  260. package/dist/wcat.js.map +1 -0
  261. package/package.json +85 -4
  262. package/skills/notion-cli/SKILL.md +45 -0
@@ -0,0 +1,24 @@
1
+ import { CliError } from "@wirecat/cli-base";
2
+ /** notion's own reasons; the shared ones (`session.missing`, `offline.refused`, `provider.*`) are cli-core's and cli-base's. */
3
+ export declare const REASONS: {
4
+ reason: string;
5
+ code: "authentication_error" | "cancelled" | "configuration_error" | "confirmation_required" | "conflict" | "internal_error" | "invalid_response" | "network_error" | "not_found" | "outcome_unknown" | "permission_error" | "provider_error" | "provider_unavailable" | "rate_limited" | "timeout" | "validation_error";
6
+ message: string;
7
+ details: string[];
8
+ step?: string | undefined;
9
+ }[];
10
+ /** Notion's error code and rate-limit reason are fixed words from its docs, never text it was sent. */
11
+ export declare const DETAILS: string[];
12
+ export declare const NOTION = "Notion";
13
+ /** A 429 with this reason is not a pace to wait out: "Retrying won't help" (Notion's request limits page). */
14
+ export declare const BLOCKED = "public_api_request_blocked";
15
+ export declare const blocked: (base?: Record<string, unknown>) => CliError;
16
+ /**
17
+ * Every Notion refusal as a CliError, by Notion's documented codes
18
+ * ([status codes](https://developers.notion.com/reference/status-codes)). Notion's own message is never repeated: it
19
+ * can quote what was sent, and names a connection (WC-SECR-6).
20
+ */
21
+ export declare const failureOf: (error: unknown) => unknown;
22
+ /** An SDK call with its failure mapped; a CliError from the fetch chain passes through as it is. */
23
+ export declare const mapped: <T>(call: () => Promise<T>) => Promise<T>;
24
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/notion/errors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAc,MAAM,mBAAmB,CAAA;AAIxD,gIAAgI;AAChI,eAAO,MAAM,OAAO;;;;;;GAyCnB,CAAA;AAED,uGAAuG;AACvG,eAAO,MAAM,OAAO,UAAuC,CAAA;AAE3D,eAAO,MAAM,MAAM,WAAW,CAAA;AAE9B,8GAA8G;AAC9G,eAAO,MAAM,OAAO,+BAA+B,CAAA;AAEnD,eAAO,MAAM,OAAO,UAAU,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAQ,QAK1D,CAAA;AASH;;;;GAIG;AACH,eAAO,MAAM,SAAS,UAAW,OAAO,KAAG,OA8D1C,CAAA;AAED,oGAAoG;AACpG,eAAO,MAAM,MAAM,GAAU,CAAC,QAAQ,MAAM,OAAO,CAAC,CAAC,CAAC,KAAG,OAAO,CAAC,CAAC,CAMjE,CAAA"}
@@ -0,0 +1,127 @@
1
+ import { APIResponseError, isHTTPResponseError, RequestTimeoutError } from "@notionhq/client";
2
+ import { CliError, isCliError } from "@wirecat/cli-base";
3
+ import { providerWaitMs, statusToCode } from "@wirecat/cli-base/http";
4
+ import { defineReasons } from "@wirecat/cli-base/standards";
5
+ /** notion's own reasons; the shared ones (`session.missing`, `offline.refused`, `provider.*`) are cli-core's and cli-base's. */
6
+ export const REASONS = defineReasons([
7
+ ["notion.token-refused", "authentication_error", "Notion refused the token"],
8
+ ["notion.restricted", "permission_error", "the token may not read or change this"],
9
+ ["notion.blocked", "permission_error", "Notion has restricted this connection's API access"],
10
+ ["notion.not-shared", "not_found", "no such page or database, or it is not shared with this token"],
11
+ ["notion.refused", "validation_error", "Notion refused the request as it was sent"],
12
+ ["notion.conflict", "conflict", "Notion could not save it because it changed at the same time"],
13
+ ["notion.rate-limited", "rate_limited", "Notion asked to slow down"],
14
+ ["notion.unavailable", "provider_unavailable", "Notion is down or overloaded"],
15
+ ["notion.invalid-response", "invalid_response", "Notion's answer does not have the documented shape"],
16
+ ["notion.id-invalid", "validation_error", "that is not a Notion id or link"],
17
+ ["notion.truncated", "provider_error", "Notion gave only part of the page"],
18
+ ["notion.no-workspace", "invalid_response", "Notion did not name the token's workspace"],
19
+ ["notion.failed", "provider_error", "a Notion read failed"],
20
+ ["notion.search-incomplete", "provider_error", "Notion cut the search for edits short"],
21
+ ["notion.window-too-dense", "validation_error", "more than 10,000 rows of one database share one minute"],
22
+ ["notion.search-capped", "provider_error", "Notion's search gave only its 10,000 most recent results"],
23
+ ["notion.database-unreadable", "not_found", "a database search listed could not be read"],
24
+ ["notion.child-unreadable", "not_found", "a child page or database Notion would not give was left out"],
25
+ ["notion.kept-unconfirmed", "permission_error", "a page Notion would not show was kept as it was"],
26
+ ["notion.left-out", "not_found", "an item Notion refused was left out of this sync"],
27
+ ["notion.stopped-early", "provider_error", "the sync stopped early and goes on next time"],
28
+ ["notion.listing-restarted", "validation_error", "Notion no longer took a saved list position; it started over"],
29
+ ["notion.listing-stale", "not_found", "Notion no longer takes a folder's saved position; it is listed again"],
30
+ ["notion.timeout-too-short", "timeout", "the time left was too short for one step of the sync"],
31
+ ["source.text-limit", "validation_error", "the page is longer than the text the store keeps"],
32
+ ["profile.not-synced", "not_found", "the profile has no local copy yet; sync first"],
33
+ ["profile.busy", "provider_unavailable", "another sync or write holds the profile's local copy"],
34
+ ["state.unreadable", "configuration_error", "the profile's sync state cannot be read"],
35
+ ["folder.not-a-folder", "validation_error", "only a page or a database can be chosen as a folder"],
36
+ ["folder.not-chosen", "not_found", "that page or database is not a chosen folder"],
37
+ ["folder.workspace", "validation_error", "the whole workspace is the scope when no folder is chosen"],
38
+ ["identity.not-seen", "not_found", "this Notion user was not seen on the document"],
39
+ ["identity.target", "validation_error", "the target must be a person: reference"],
40
+ ["identity.no-person", "not_found", "the target person has no known identity"],
41
+ ["profile.account-taken", "conflict", "another profile already uses this Notion connection", ["profile"]],
42
+ ["profile.other-account", "conflict", "the profile's local copy was synced from another Notion connection"],
43
+ ].map(([reason, code, message, details = []]) => ({ reason, code, message, details: [...details] })));
44
+ /** Notion's error code and rate-limit reason are fixed words from its docs, never text it was sent. */
45
+ export const DETAILS = ["notion_code", "rate_limit_reason"];
46
+ export const NOTION = "Notion";
47
+ /** A 429 with this reason is not a pace to wait out: "Retrying won't help" (Notion's request limits page). */
48
+ export const BLOCKED = "public_api_request_blocked";
49
+ export const blocked = (base = {}) => new CliError("permission_error", `${NOTION} has restricted this connection's API access; retrying will not help, contact Notion support`, { ...base, rate_limit_reason: BLOCKED, reason: "notion.blocked" });
50
+ const SIGN_IN = "sign in again with `notion <profile> session start`";
51
+ const additional = (error, key) => {
52
+ const value = error.additional_data?.[key];
53
+ return typeof value === "string" ? value : undefined;
54
+ };
55
+ /**
56
+ * Every Notion refusal as a CliError, by Notion's documented codes
57
+ * ([status codes](https://developers.notion.com/reference/status-codes)). Notion's own message is never repeated: it
58
+ * can quote what was sent, and names a connection (WC-SECR-6).
59
+ */
60
+ export const failureOf = (error) => {
61
+ if (isCliError(error))
62
+ return error;
63
+ if (error instanceof RequestTimeoutError)
64
+ return new CliError("timeout", `${NOTION} did not answer in time`, { retryable: true });
65
+ if (!isHTTPResponseError(error))
66
+ return error;
67
+ const status = error.status;
68
+ const notion = APIResponseError.isAPIResponseError(error) ? error : undefined;
69
+ const code = notion?.code ?? "unknown";
70
+ const base = {
71
+ status,
72
+ notion_code: code,
73
+ ...(error.request_id ? { request_id: error.request_id } : {}),
74
+ };
75
+ if (code === "unauthorized" || status === 401)
76
+ return new CliError("authentication_error", `${NOTION} refused the token; ${SIGN_IN}`, {
77
+ ...base,
78
+ reason: "notion.token-refused",
79
+ });
80
+ if (code === "rate_limited" || status === 429 || code === "service_overload" || status === 529) {
81
+ const reason = notion ? additional(notion, "rate_limit_reason") : undefined;
82
+ if (reason === BLOCKED)
83
+ return blocked(base);
84
+ const wait = error.headers instanceof Headers ? providerWaitMs(error.headers, () => new Date()) : undefined;
85
+ return new CliError("rate_limited", `${NOTION} asked to slow down; run the command again later`, {
86
+ ...base,
87
+ ...(reason ? { rate_limit_reason: reason } : {}),
88
+ ...(wait === undefined ? {} : { retry_after_ms: wait }),
89
+ retryable: true,
90
+ reason: "notion.rate-limited",
91
+ });
92
+ }
93
+ if (code === "restricted_resource" || status === 403)
94
+ return new CliError("permission_error", `${NOTION} says this token may not do that`, {
95
+ ...base,
96
+ reason: "notion.restricted",
97
+ });
98
+ if (code === "object_not_found" || status === 404)
99
+ return new CliError("not_found", `${NOTION} has no such page or database, or it is not shared with this token: in Notion, open it, ` +
100
+ "choose ••• → Connections, and add the connection", { ...base, reason: "notion.not-shared" });
101
+ if (code === "conflict_error" || status === 409)
102
+ return new CliError("conflict", `${NOTION} could not save it because it changed at the same time`, {
103
+ ...base,
104
+ reason: "notion.conflict",
105
+ });
106
+ if (status >= 500)
107
+ return new CliError("provider_unavailable", `${NOTION} is unavailable (HTTP ${status}); try again later`, {
108
+ ...base,
109
+ retryable: true,
110
+ reason: "notion.unavailable",
111
+ });
112
+ const refused = statusToCode(status) === "validation_error";
113
+ return new CliError(refused ? "validation_error" : "provider_error", `${NOTION} refused the request (HTTP ${status}, ${code})`, {
114
+ ...base,
115
+ reason: refused ? "notion.refused" : "provider.refused",
116
+ });
117
+ };
118
+ /** An SDK call with its failure mapped; a CliError from the fetch chain passes through as it is. */
119
+ export const mapped = async (call) => {
120
+ try {
121
+ return await call();
122
+ }
123
+ catch (error) {
124
+ throw failureOf(error);
125
+ }
126
+ };
127
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/notion/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AAC7F,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAE3D,gIAAgI;AAChI,MAAM,CAAC,MAAM,OAAO,GAAG,aAAa,CAEhC;IACE,CAAC,sBAAsB,EAAE,sBAAsB,EAAE,0BAA0B,CAAC;IAC5E,CAAC,mBAAmB,EAAE,kBAAkB,EAAE,uCAAuC,CAAC;IAClF,CAAC,gBAAgB,EAAE,kBAAkB,EAAE,oD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X,GAAG,MAAM,0FAA0F;YACjG,kDAAkD,EACpD,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,mBAAmB,EAAE,CACzC,CAAA;IACH,IAAI,IAAI,KAAK,gBAAgB,IAAI,MAAM,KAAK,GAAG;QAC7C,OAAO,IAAI,QAAQ,CAAC,UAAU,EAAE,GAAG,MAAM,wDAAwD,EAAE;YACjG,GAAG,IAAI;YACP,MAAM,EAAE,iBAAiB;SAC1B,CAAC,CAAA;IACJ,IAAI,MAAM,IAAI,GAAG;QACf,OAAO,IAAI,QAAQ,CAAC,sBAAsB,EAAE,GAAG,MAAM,yBAAyB,MAAM,oBAAoB,EAAE;YACxG,GAAG,IAAI;YACP,SAAS,EAAE,IAAI;YACf,MAAM,EAAE,oBAAoB;SAC7B,CAAC,CAAA;IACJ,MAAM,OAAO,GAAG,YAAY,CAAC,MAAM,CAAC,KAAK,kBAAkB,CAAA;IAC3D,OAAO,IAAI,QAAQ,CACjB,OAAO,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,gBAAgB,EAC/C,GAAG,MAAM,8BAA8B,MAAM,KAAK,IAAI,GAAG,EACzD;QACE,GAAG,IAAI;QACP,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,kBAAkB;KACxD,CACF,CAAA;AACH,CAAC,CAAA;AAED,oGAAoG;AACpG,MAAM,CAAC,MAAM,MAAM,GAAG,KAAK,EAAK,IAAsB,EAAc,EAAE;IACpE,IAAI,CAAC;QACH,OAAO,MAAM,IAAI,EAAE,CAAA;IACrB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,SAAS,CAAC,KAAK,CAAC,CAAA;IACxB,CAAC;AACH,CAAC,CAAA"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * A page or database id from what a person copies: the id, with or without dashes, or its Notion link. In a link the
3
+ * id is the last 32 hex characters of the path; `?v=` names a view, never the database. A page opened over a
4
+ * database, `…/<database>?v=<view>&p=<page>`, is the page in `p=`, and the database is the one in the path.
5
+ */
6
+ export declare const notionId: (input: string, what?: string) => string;
7
+ //# sourceMappingURL=ids.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../../src/notion/ids.ts"],"names":[],"mappings":"AAUA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,UAAW,MAAM,oBAAkB,MAmBvD,CAAA"}
@@ -0,0 +1,37 @@
1
+ import { CliError } from "@wirecat/cli-base";
2
+ const DASHED = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu;
3
+ const COMPACT = /^[0-9a-f]{32}$/iu;
4
+ const IN_PATH = /([0-9a-f]{32})(?:[^0-9a-f]|$)/giu;
5
+ const NOTION_HOSTS = /(^|\.)(notion\.so|notion\.site|notion\.com)$/u;
6
+ const dashed = (hex) => `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`.toLowerCase();
7
+ /**
8
+ * A page or database id from what a person copies: the id, with or without dashes, or its Notion link. In a link the
9
+ * id is the last 32 hex characters of the path; `?v=` names a view, never the database. A page opened over a
10
+ * database, `…/<database>?v=<view>&p=<page>`, is the page in `p=`, and the database is the one in the path.
11
+ */
12
+ export const notionId = (input, what = "page") => {
13
+ const text = input.trim();
14
+ if (DASHED.test(text))
15
+ return text.toLowerCase();
16
+ if (COMPACT.test(text))
17
+ return dashed(text);
18
+ let url;
19
+ try {
20
+ url = new URL(text);
21
+ }
22
+ catch {
23
+ url = undefined;
24
+ }
25
+ if (url && url.protocol === "https:" && NOTION_HOSTS.test(url.hostname)) {
26
+ const peek = url.searchParams.get("p")?.replaceAll("-", "");
27
+ if (what === "page" && peek && COMPACT.test(peek))
28
+ return dashed(peek);
29
+ const found = [...url.pathname.matchAll(IN_PATH)].at(-1)?.[1];
30
+ if (found)
31
+ return dashed(found);
32
+ }
33
+ throw new CliError("validation_error", `give the ${what}'s id or its Notion link, such as https://www.notion.so/…`, {
34
+ reason: "notion.id-invalid",
35
+ });
36
+ };
37
+ //# sourceMappingURL=ids.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.js","sourceRoot":"","sources":["../../src/notion/ids.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAE5C,MAAM,MAAM,GAAG,kEAAkE,CAAA;AACjF,MAAM,OAAO,GAAG,kBAAkB,CAAA;AAClC,MAAM,OAAO,GAAG,kCAAkC,CAAA;AAClD,MAAM,YAAY,GAAG,+CAA+C,CAAA;AAEpE,MAAM,MAAM,GAAG,CAAC,GAAW,EAAE,EAAE,CAC7B,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC,WAAW,EAAE,CAAA;AAEnH;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAE,IAAI,GAAG,MAAM,EAAU,EAAE;IAC/D,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;IACzB,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC,WAAW,EAAE,CAAA;IAChD,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,MAAM,CAAC,IAAI,CAAC,CAAA;IAC3C,IAAI,GAAoB,CAAA;IACxB,IAAI,CAAC;QACH,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAA;IACrB,CAAC;IAAC,MAAM,CAAC;QACP,GAAG,GAAG,SAAS,CAAA;IACjB,CAAC;IACD,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QACxE,MAAM,IAAI,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;QAC3D,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;YAAE,OAAO,MAAM,CAAC,IAAI,CAAC,CAAA;QACtE,MAAM,KAAK,GAAG,CAAC,GAAG,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;QAC7D,IAAI,KAAK;YAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;IACjC,CAAC;IACD,MAAM,IAAI,QAAQ,CAAC,kBAAkB,EAAE,YAAY,IAAI,2DAA2D,EAAE;QAClH,MAAM,EAAE,mBAAmB;KAC5B,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -0,0 +1,6 @@
1
+ /** The text with every signed query cut, and how many were cut. */
2
+ export declare const withoutSignedLinks: (text: string) => {
3
+ text: string;
4
+ removed: number;
5
+ };
6
+ //# sourceMappingURL=links.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"links.d.ts","sourceRoot":"","sources":["../../src/notion/links.ts"],"names":[],"mappings":"AAWA,mEAAmE;AACnE,eAAO,MAAM,kBAAkB,SAAU,MAAM,KAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAQhF,CAAA"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Notion-hosted file links are signed and valid for an hour
3
+ * ([file object](https://developers.notion.com/reference/file-object)): whoever reads one gets the file. The signature
4
+ * is in the query, so every query that carries one is cut before the text is printed or stored. Where a link ends is
5
+ * not known: a file name can hold `(`, `'` or `[`, and Notion's image proxy carries the signed link inside its own
6
+ * path, percent-encoded. So the search is for the query itself, plain or encoded, not for the link around it.
7
+ */
8
+ const QUERY = /(?<=\S)(?:\?|%3F)[\w%.~=&;+:@/-]+/giu;
9
+ const KEYS = "x-amz-[\\w-]+|signature|expirationtimestamp|expires|key-pair-id|policy|token|sig|se|sp|sv";
10
+ const SIGNED = new RegExp(`(?:^|\\?|&|;|%3F|%26)(?:${KEYS})(?:=|%3D)`, "iu");
11
+ /** The text with every signed query cut, and how many were cut. */
12
+ export const withoutSignedLinks = (text) => {
13
+ let removed = 0;
14
+ const clean = text.replace(QUERY, (query) => {
15
+ if (!SIGNED.test(query))
16
+ return query;
17
+ removed += 1;
18
+ return "";
19
+ });
20
+ return { text: clean, removed };
21
+ };
22
+ //# sourceMappingURL=links.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"links.js","sourceRoot":"","sources":["../../src/notion/links.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,KAAK,GAAG,sCAAsC,CAAA;AACpD,MAAM,IAAI,GAAG,2FAA2F,CAAA;AACxG,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,2BAA2B,IAAI,YAAY,EAAE,IAAI,CAAC,CAAA;AAE5E,mEAAmE;AACnE,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAY,EAAqC,EAAE;IACpF,IAAI,OAAO,GAAG,CAAC,CAAA;IACf,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;QAC1C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;YAAE,OAAO,KAAK,CAAA;QACrC,OAAO,IAAI,CAAC,CAAA;QACZ,OAAO,EAAE,CAAA;IACX,CAAC,CAAC,CAAA;IACF,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,CAAA;AACjC,CAAC,CAAA"}
@@ -0,0 +1,95 @@
1
+ import type { Client } from "@notionhq/client";
2
+ import * as v from "valibot";
3
+ export declare const accountSchema: v.StrictObjectSchema<{
4
+ readonly workspace: v.StrictObjectSchema<{
5
+ readonly id: v.StringSchema<undefined>;
6
+ readonly name: v.NullableSchema<v.StringSchema<undefined>, undefined>;
7
+ }, undefined>;
8
+ readonly user: v.StrictObjectSchema<{
9
+ readonly id: v.StringSchema<undefined>;
10
+ readonly name: v.NullableSchema<v.StringSchema<undefined>, undefined>;
11
+ readonly type: v.StringSchema<undefined>;
12
+ readonly owner: v.NullableSchema<v.StringSchema<undefined>, undefined>;
13
+ }, undefined>;
14
+ }, undefined>;
15
+ export type Account = v.InferOutput<typeof accountSchema>;
16
+ export declare const documentSchema: v.StrictObjectSchema<{
17
+ readonly id: v.StringSchema<undefined>;
18
+ readonly markdown: v.StringSchema<undefined>;
19
+ readonly truncated: v.BooleanSchema<undefined>;
20
+ readonly missing_block_ids: v.ArraySchema<v.StringSchema<undefined>, undefined>;
21
+ readonly signed_links_removed: v.NumberSchema<undefined>;
22
+ }, undefined>;
23
+ export type Document = v.InferOutput<typeof documentSchema>;
24
+ /** A part of the answer that is not there (WC-OUT-7). */
25
+ export type Issue = {
26
+ code: "not_found" | "provider_error";
27
+ reason: string;
28
+ source: "notion";
29
+ };
30
+ export declare const databaseRowSchema: v.StrictObjectSchema<{
31
+ readonly id: v.NullableSchema<v.StringSchema<undefined>, undefined>;
32
+ readonly data_source_id: v.StringSchema<undefined>;
33
+ readonly title: v.StringSchema<undefined>;
34
+ readonly last_edited_time: v.NullableSchema<v.StringSchema<undefined>, undefined>;
35
+ }, undefined>;
36
+ export type DatabaseRow = v.InferOutput<typeof databaseRowSchema>;
37
+ export declare const databaseSchema: v.StrictObjectSchema<{
38
+ readonly id: v.StringSchema<undefined>;
39
+ readonly title: v.StringSchema<undefined>;
40
+ readonly description: v.StringSchema<undefined>;
41
+ readonly url: v.NullableSchema<v.StringSchema<undefined>, undefined>;
42
+ readonly parent: v.NullableSchema<v.StrictObjectSchema<{
43
+ readonly type: v.StringSchema<undefined>;
44
+ readonly id: v.NullableSchema<v.StringSchema<undefined>, undefined>;
45
+ }, undefined>, undefined>;
46
+ readonly is_inline: v.BooleanSchema<undefined>;
47
+ readonly in_trash: v.BooleanSchema<undefined>;
48
+ readonly created_time: v.NullableSchema<v.StringSchema<undefined>, undefined>;
49
+ readonly last_edited_time: v.NullableSchema<v.StringSchema<undefined>, undefined>;
50
+ readonly data_sources: v.ArraySchema<v.StrictObjectSchema<{
51
+ readonly id: v.StringSchema<undefined>;
52
+ readonly title: v.StringSchema<undefined>;
53
+ readonly properties: v.ArraySchema<v.StrictObjectSchema<{
54
+ readonly id: v.StringSchema<undefined>;
55
+ readonly name: v.StringSchema<undefined>;
56
+ readonly type: v.StringSchema<undefined>;
57
+ readonly options: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
58
+ }, undefined>, undefined>;
59
+ }, undefined>, undefined>;
60
+ }, undefined>;
61
+ export type Database = v.InferOutput<typeof databaseSchema>;
62
+ /** Every Notion read the commands make, each answer checked against its documented shape. */
63
+ export declare const liveReads: (client: Client) => {
64
+ port: import("./reads.js").NotionReads;
65
+ /** The token's bot user; its workspace id and bot id are the account (DESIGN.md, Sign-in). */
66
+ me: () => Promise<Account>;
67
+ /**
68
+ * A page as Notion-flavoured Markdown (v2). A truncated page's unknown blocks are fetched again and put in place of
69
+ * their `<unknown>` tag, or after the page when no tag names them. What is still not there is an issue: a block
70
+ * the token cannot see, or a part past Notion's limit or past the blocks fetched again.
71
+ */
72
+ document: (id: string) => Promise<{
73
+ document: Document;
74
+ issues: Issue[];
75
+ }>;
76
+ /** The data sources the token sees, one row each with its database, by Notion's own search. */
77
+ databases: ({ limit, cursor, title, }: {
78
+ limit: number;
79
+ cursor?: string | undefined;
80
+ title?: string | undefined;
81
+ }) => Promise<{
82
+ items: {
83
+ id: string | null;
84
+ data_source_id: string;
85
+ title: string;
86
+ last_edited_time: string | null;
87
+ }[];
88
+ hasMore: boolean;
89
+ cursor: string | undefined;
90
+ }>;
91
+ /** A database with each data source's id and property schema: what a query or a filter needs. */
92
+ database: (id: string) => Promise<Database>;
93
+ };
94
+ export type LiveReads = ReturnType<typeof liveReads>;
95
+ //# sourceMappingURL=notion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notion.d.ts","sourceRoot":"","sources":["../../src/notion/notion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAE9C,OAAO,KAAK,CAAC,MAAM,SAAS,CAAA;AAmH5B,eAAO,MAAM,aAAa;;;;;;;;;;;aAQxB,CAAA;AACF,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,aAAa,CAAC,CAAA;AAEzD,eAAO,MAAM,cAAc;;;;;;aAMzB,CAAA;AACF,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,cAAc,CAAC,CAAA;AAE3D,yDAAyD;AACzD,MAAM,MAAM,KAAK,GAAG;IAAE,IAAI,EAAE,WAAW,GAAG,gBAAgB,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,QAAQ,CAAA;CAAE,CAAA;AAE9F,eAAO,MAAM,iBAAiB;;;;;aAK5B,CAAA;AACF,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,iBAAiB,CAAC,CAAA;AASjE,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;aAWzB,CAAA;AACF,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,cAAc,CAAC,CAAA;AAa3D,6FAA6F;AAC7F,eAAO,MAAM,SAAS,WAAY,MAAM;;IAIpC,8FAA8F;cAChF,OAAO,CAAC,OAAO,CAAC;IAa9B;;;;OAIG;mBACkB,MAAM,KAAG,OAAO,CAAC;QAAE,QAAQ,EAAE,QAAQ,CAAC;QAAC,MAAM,EAAE,KAAK,EAAE,CAAA;KAAE,CAAC;IAyC9E,+FAA+F;2CAK5F;QACD,KAAK,EAAE,MAAM,CAAA;QACb,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC3B,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAC3B;;;;;;;;;;IAwBD,iGAAiG;mBAC5E,MAAM,KAAG,OAAO,CAAC,QAAQ,CAAC;CAoClD,CAAA;AAED,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,SAAS,CAAC,CAAA"}
@@ -0,0 +1,269 @@
1
+ import { CliError, isCliError } from "@wirecat/cli-base";
2
+ import * as v from "valibot";
3
+ import { mapped, NOTION } from "./errors.js";
4
+ import { withoutSignedLinks } from "./links.js";
5
+ import { notionPort } from "./port.js";
6
+ // Each wire name is from Notion's reference, linked at its schema; the strict fake in src/testing/fake-notion.ts
7
+ // refuses any other. No wire type leaves this file (WC-ARCH-3).
8
+ const richText = v.array(v.looseObject({ plain_text: v.string() }));
9
+ const textOf = (parts) => (parts ?? []).map((part) => part.plain_text).join("");
10
+ /** [Retrieve your token's bot user](https://developers.notion.com/reference/get-self), [user](https://developers.notion.com/reference/user). */
11
+ const wireMe = v.looseObject({
12
+ object: v.literal("user"),
13
+ id: v.string(),
14
+ type: v.optional(v.string()),
15
+ name: v.nullish(v.string()),
16
+ bot: v.optional(v.looseObject({
17
+ owner: v.optional(v.looseObject({ type: v.string() })),
18
+ workspace_name: v.nullish(v.string()),
19
+ workspace_id: v.nullish(v.string()),
20
+ })),
21
+ });
22
+ /** [Retrieve a page as Markdown](https://developers.notion.com/reference/retrieve-page-markdown). */
23
+ const wireMarkdown = v.looseObject({
24
+ object: v.literal("page_markdown"),
25
+ id: v.string(),
26
+ markdown: v.string(),
27
+ truncated: v.boolean(),
28
+ unknown_block_ids: v.array(v.string()),
29
+ });
30
+ /** [Parent object](https://developers.notion.com/reference/parent-object). */
31
+ const wireParent = v.looseObject({
32
+ type: v.string(),
33
+ database_id: v.optional(v.string()),
34
+ page_id: v.optional(v.string()),
35
+ data_source_id: v.optional(v.string()),
36
+ block_id: v.optional(v.string()),
37
+ });
38
+ /** [Data source](https://developers.notion.com/reference/data-source); search may answer the partial shape. */
39
+ const wireDataSource = v.looseObject({
40
+ object: v.literal("data_source"),
41
+ id: v.string(),
42
+ title: v.optional(richText),
43
+ parent: v.optional(wireParent),
44
+ last_edited_time: v.optional(v.string()),
45
+ in_trash: v.optional(v.boolean()),
46
+ properties: v.optional(v.record(v.string(), v.looseObject({
47
+ id: v.string(),
48
+ name: v.string(),
49
+ type: v.string(),
50
+ }))),
51
+ });
52
+ /** [Search](https://developers.notion.com/reference/post-search), [pagination](https://developers.notion.com/reference/pagination). */
53
+ const wireSearch = v.looseObject({
54
+ object: v.literal("list"),
55
+ results: v.array(v.unknown()),
56
+ has_more: v.boolean(),
57
+ next_cursor: v.nullish(v.string()),
58
+ });
59
+ /** [Retrieve a database](https://developers.notion.com/reference/retrieve-database). */
60
+ const wireDatabase = v.looseObject({
61
+ object: v.literal("database"),
62
+ id: v.string(),
63
+ title: richText,
64
+ description: v.optional(richText),
65
+ parent: wireParent,
66
+ is_inline: v.optional(v.boolean()),
67
+ in_trash: v.optional(v.boolean()),
68
+ created_time: v.optional(v.string()),
69
+ last_edited_time: v.optional(v.string()),
70
+ url: v.optional(v.string()),
71
+ data_sources: v.array(v.looseObject({ id: v.string(), name: v.optional(v.string()) })),
72
+ });
73
+ const parsed = (schema, value) => {
74
+ const result = v.safeParse(schema, value);
75
+ if (result.success)
76
+ return result.output;
77
+ throw new CliError("invalid_response", `${NOTION}'s answer does not have the documented shape`, {
78
+ reason: "notion.invalid-response",
79
+ });
80
+ };
81
+ const parentOf = (parent) => {
82
+ if (!parent)
83
+ return null;
84
+ const id = parent.page_id ?? parent.database_id ?? parent.data_source_id ?? parent.block_id ?? null;
85
+ return { type: parent.type, id };
86
+ };
87
+ /** Option names of a select, multi-select or status property: what a filter like `status:Done` takes. */
88
+ const optionsOf = (property) => {
89
+ const config = property[String(property.type)];
90
+ if (!config || typeof config !== "object")
91
+ return undefined;
92
+ const options = config.options;
93
+ if (!Array.isArray(options))
94
+ return undefined;
95
+ return options.flatMap((option) => option && typeof option === "object" && typeof option.name === "string"
96
+ ? [option.name]
97
+ : []);
98
+ };
99
+ export const accountSchema = v.strictObject({
100
+ workspace: v.strictObject({ id: v.string(), name: v.nullable(v.string()) }),
101
+ user: v.strictObject({
102
+ id: v.string(),
103
+ name: v.nullable(v.string()),
104
+ type: v.string(),
105
+ owner: v.nullable(v.string()),
106
+ }),
107
+ });
108
+ export const documentSchema = v.strictObject({
109
+ id: v.string(),
110
+ markdown: v.string(),
111
+ truncated: v.boolean(),
112
+ missing_block_ids: v.array(v.string()),
113
+ signed_links_removed: v.number(),
114
+ });
115
+ export const databaseRowSchema = v.strictObject({
116
+ id: v.nullable(v.string()),
117
+ data_source_id: v.string(),
118
+ title: v.string(),
119
+ last_edited_time: v.nullable(v.string()),
120
+ });
121
+ const propertySchema = v.strictObject({
122
+ id: v.string(),
123
+ name: v.string(),
124
+ type: v.string(),
125
+ options: v.optional(v.array(v.string())),
126
+ });
127
+ export const databaseSchema = v.strictObject({
128
+ id: v.string(),
129
+ title: v.string(),
130
+ description: v.string(),
131
+ url: v.nullable(v.string()),
132
+ parent: v.nullable(v.strictObject({ type: v.string(), id: v.nullable(v.string()) })),
133
+ is_inline: v.boolean(),
134
+ in_trash: v.boolean(),
135
+ created_time: v.nullable(v.string()),
136
+ last_edited_time: v.nullable(v.string()),
137
+ data_sources: v.array(v.strictObject({ id: v.string(), title: v.string(), properties: v.array(propertySchema) })),
138
+ });
139
+ /** At most this many unknown blocks are fetched again; Notion lists at most 100. */
140
+ const MORE_BLOCKS = 100;
141
+ /** A database's data sources read for `databases show`; Notion lists at most 100. */
142
+ const MORE_SOURCES = 100;
143
+ const placeholderFor = (markdown, id) => {
144
+ const compact = id.replaceAll("-", "");
145
+ const tags = markdown.match(/<unknown\b[^>]*\/>/giu) ?? [];
146
+ return tags.find((tag) => tag.includes(id) || tag.includes(compact));
147
+ };
148
+ /** Every Notion read the commands make, each answer checked against its documented shape. */
149
+ export const liveReads = (client) => {
150
+ const port = notionPort(client);
151
+ return {
152
+ port,
153
+ /** The token's bot user; its workspace id and bot id are the account (DESIGN.md, Sign-in). */
154
+ me: async () => {
155
+ const me = parsed(wireMe, await port.self());
156
+ const workspace = me.bot?.workspace_id;
157
+ if (!workspace)
158
+ throw new CliError("invalid_response", `${NOTION} did not say which workspace this token belongs to`, {
159
+ reason: "notion.invalid-response",
160
+ });
161
+ return {
162
+ workspace: { id: workspace, name: me.bot?.workspace_name ?? null },
163
+ user: { id: me.id, name: me.name ?? null, type: me.type ?? "bot", owner: me.bot?.owner?.type ?? null },
164
+ };
165
+ },
166
+ /**
167
+ * A page as Notion-flavoured Markdown (v2). A truncated page's unknown blocks are fetched again and put in place of
168
+ * their `<unknown>` tag, or after the page when no tag names them. What is still not there is an issue: a block
169
+ * the token cannot see, or a part past Notion's limit or past the blocks fetched again.
170
+ */
171
+ document: async (id) => {
172
+ const read = async (pageId) => parsed(wireMarkdown, { object: "page_markdown", id: pageId, ...(await port.pageMarkdown(pageId)) });
173
+ const page = await read(id);
174
+ let markdown = page.markdown;
175
+ const hidden = [];
176
+ const unread = [];
177
+ // Notion lists at most 100 unknown blocks, so a cut page with none, or with a full list, may have more.
178
+ let cut = page.truncated && (page.unknown_block_ids.length === 0 || page.unknown_block_ids.length >= MORE_BLOCKS);
179
+ for (const block of page.unknown_block_ids.slice(0, MORE_BLOCKS)) {
180
+ try {
181
+ const part = await read(block);
182
+ const tag = placeholderFor(markdown, block);
183
+ markdown = tag ? markdown.replace(tag, () => part.markdown) : `${markdown}\n\n${part.markdown}`;
184
+ for (const more of part.unknown_block_ids)
185
+ unread.push(more);
186
+ if (part.truncated && part.unknown_block_ids.length === 0)
187
+ cut = true;
188
+ }
189
+ catch (error) {
190
+ if (!isCliError(error) || error.code !== "not_found")
191
+ throw error;
192
+ hidden.push(block);
193
+ }
194
+ }
195
+ for (const more of page.unknown_block_ids.slice(MORE_BLOCKS))
196
+ unread.push(more);
197
+ const clean = withoutSignedLinks(markdown);
198
+ const issues = [
199
+ ...(hidden.length > 0 ? [{ code: "not_found", reason: "notion.not-shared", source: "notion" }] : []),
200
+ ...(cut || unread.length > 0
201
+ ? [{ code: "provider_error", reason: "notion.truncated", source: "notion" }]
202
+ : []),
203
+ ];
204
+ return {
205
+ document: {
206
+ id: page.id,
207
+ markdown: clean.text,
208
+ truncated: issues.length > 0,
209
+ missing_block_ids: [...hidden, ...unread],
210
+ signed_links_removed: clean.removed,
211
+ },
212
+ issues,
213
+ };
214
+ },
215
+ /** The data sources the token sees, one row each with its database, by Notion's own search. */
216
+ databases: async ({ limit, cursor, title, }) => {
217
+ const page = parsed(wireSearch, await mapped(() => client.search({
218
+ filter: { property: "object", value: "data_source" },
219
+ page_size: limit,
220
+ ...(title ? { query: title } : {}),
221
+ ...(cursor ? { start_cursor: cursor } : {}),
222
+ })));
223
+ const items = page.results.map((result) => {
224
+ const source = parsed(wireDataSource, result);
225
+ return {
226
+ id: source.parent?.database_id ?? null,
227
+ data_source_id: source.id,
228
+ title: textOf(source.title),
229
+ last_edited_time: source.last_edited_time ?? null,
230
+ };
231
+ });
232
+ return { items, hasMore: page.has_more, cursor: page.has_more ? (page.next_cursor ?? undefined) : undefined };
233
+ },
234
+ /** A database with each data source's id and property schema: what a query or a filter needs. */
235
+ database: async (id) => {
236
+ const database = parsed(wireDatabase, await mapped(() => client.databases.retrieve({ database_id: id })));
237
+ const sources = [];
238
+ for (const reference of database.data_sources.slice(0, MORE_SOURCES)) {
239
+ const source = parsed(wireDataSource, await mapped(() => client.dataSources.retrieve({ data_source_id: reference.id })));
240
+ sources.push({
241
+ id: source.id,
242
+ title: textOf(source.title) || reference.name || "",
243
+ properties: Object.values(source.properties ?? {}).map((property) => {
244
+ const options = optionsOf(property);
245
+ return {
246
+ id: property.id,
247
+ name: property.name,
248
+ type: property.type,
249
+ ...(options ? { options } : {}),
250
+ };
251
+ }),
252
+ });
253
+ }
254
+ return {
255
+ id: database.id,
256
+ title: textOf(database.title),
257
+ description: textOf(database.description),
258
+ url: database.url ?? null,
259
+ parent: parentOf(database.parent),
260
+ is_inline: database.is_inline ?? false,
261
+ in_trash: database.in_trash ?? false,
262
+ created_time: database.created_time ?? null,
263
+ last_edited_time: database.last_edited_time ?? null,
264
+ data_sources: sources,
265
+ };
266
+ },
267
+ };
268
+ };
269
+ //# sourceMappingURL=notion.js.map