@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.5

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 (206) hide show
  1. package/README.md +2 -1
  2. package/dist/bin/server.mjs +1 -1
  3. package/dist/bin/server.mjs.map +1 -1
  4. package/dist/bin/services.mjs +1 -1
  5. package/dist/bin/services.mjs.map +1 -1
  6. package/dist/client.d.ts +296 -27
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +649 -295
  9. package/dist/client.js.map +1 -1
  10. package/dist/contract.d.ts +2 -2
  11. package/dist/contract.js +2 -2
  12. package/dist/contract2.js +331 -21
  13. package/dist/contract2.js.map +1 -1
  14. package/dist/email.d.ts +1773 -32
  15. package/dist/email.d.ts.map +1 -1
  16. package/dist/email.js +3478 -288
  17. package/dist/email.js.map +1 -1
  18. package/dist/errors.d.ts +2 -0
  19. package/dist/errors.js +48 -0
  20. package/dist/errors.js.map +1 -0
  21. package/dist/index.d.ts +466 -13
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index2.d.ts +35 -0
  24. package/dist/index2.d.ts.map +1 -0
  25. package/dist/listen.mjs +447 -290
  26. package/dist/listen.mjs.map +1 -1
  27. package/dist/runtime-cloudflare.d.ts +89 -0
  28. package/dist/runtime-cloudflare.d.ts.map +1 -0
  29. package/dist/runtime-cloudflare.js +15667 -0
  30. package/dist/runtime-cloudflare.js.map +1 -0
  31. package/dist/runtime-node.d.mts +2963 -294
  32. package/dist/runtime-node.d.mts.map +1 -1
  33. package/dist/server.d.ts +3473 -842
  34. package/dist/server.d.ts.map +1 -1
  35. package/dist/server.js +12187 -8244
  36. package/dist/server.js.map +1 -1
  37. package/dist/server.mjs +7739 -3548
  38. package/dist/server.mjs.map +1 -1
  39. package/dist/server2.mjs +5 -5
  40. package/dist/server2.mjs.map +1 -1
  41. package/package.json +22 -5
  42. package/src/admin/audit/service.ts +2 -2
  43. package/src/admin/documents/service.ts +76 -6
  44. package/src/admin/reports/service.ts +7 -1
  45. package/src/client/api.ts +23 -471
  46. package/src/client/auth/client.ts +22 -3
  47. package/src/client/billing.ts +80 -0
  48. package/src/client/community.ts +40 -0
  49. package/src/client/discovery.ts +35 -9
  50. package/src/client/documents.ts +160 -0
  51. package/src/client/index.ts +7 -0
  52. package/src/client/portal/account.ts +54 -2
  53. package/src/client/portal/request.ts +2 -0
  54. package/src/client/response.ts +21 -0
  55. package/src/client/schema.ts +8 -0
  56. package/src/client/services.ts +23 -18
  57. package/src/client/sharing.ts +262 -0
  58. package/src/client/versions.ts +104 -0
  59. package/src/contract/account.ts +50 -0
  60. package/src/contract/billing.ts +166 -0
  61. package/src/contract/collaboration.ts +5 -0
  62. package/src/contract/community.ts +60 -0
  63. package/src/contract/discovery.ts +63 -2
  64. package/src/contract/entitlements.ts +10 -0
  65. package/src/contract/index.ts +54 -5
  66. package/src/contract/links.ts +57 -4
  67. package/src/contract/locale.ts +27 -0
  68. package/src/contract/portal.ts +12 -1
  69. package/src/contract/routes.ts +10 -0
  70. package/src/contract/services.ts +18 -16
  71. package/src/email/i18n.ts +174 -0
  72. package/src/email/index.ts +13 -2
  73. package/src/email/locales/ar.json +141 -0
  74. package/src/email/locales/de.json +141 -0
  75. package/src/email/locales/es.json +141 -0
  76. package/src/email/locales/fr.json +141 -0
  77. package/src/email/locales/it.json +141 -0
  78. package/src/email/locales/ja.json +141 -0
  79. package/src/email/locales/pl.json +141 -0
  80. package/src/email/locales/pt-br.json +141 -0
  81. package/src/email/locales/ru.json +141 -0
  82. package/src/email/locales/zh-cn.json +141 -0
  83. package/src/email/messages.ts +200 -0
  84. package/src/email/render.ts +142 -276
  85. package/src/email/samples.ts +115 -0
  86. package/src/email/schema.ts +70 -6
  87. package/src/email/templates/BrandEmail.vue +54 -0
  88. package/src/email/templates/DocumentBlocked.vue +45 -0
  89. package/src/email/templates/DocumentInvitation.vue +24 -0
  90. package/src/email/templates/DocumentReport.vue +32 -0
  91. package/src/email/templates/DocumentReportDecided.vue +36 -0
  92. package/src/email/templates/DocumentReportReceived.vue +18 -0
  93. package/src/email/templates/DocumentRestored.vue +24 -0
  94. package/src/email/templates/EmailChangeRequested.vue +24 -0
  95. package/src/email/templates/EmailRoot.vue +19 -0
  96. package/src/email/templates/EmailText.vue +9 -0
  97. package/src/email/templates/EmailVerification.vue +38 -0
  98. package/src/email/templates/EnrollmentNotification.vue +24 -0
  99. package/src/email/templates/EnrollmentUpdate.vue +52 -0
  100. package/src/email/templates/NewSignIn.vue +37 -0
  101. package/src/email/templates/PasswordChanged.vue +22 -0
  102. package/src/email/templates/PasswordReset.vue +23 -0
  103. package/src/email/templates/StorageLimitReached.vue +28 -0
  104. package/src/email/templates/TwoStepChanged.vue +32 -0
  105. package/src/email/templates/WaitlistConfirmation.vue +29 -0
  106. package/src/email/templates/WorkspaceInvitation.vue +28 -0
  107. package/src/email/templates/brand.ts +9 -61
  108. package/src/email/templates/localization.ts +17 -0
  109. package/src/errors/index.ts +6 -0
  110. package/src/global.d.ts +7 -0
  111. package/src/runtime/cloudflare/relay/index.ts +7 -1
  112. package/src/runtime/cloudflare/tsconfig.json +2 -1
  113. package/src/runtime/cloudflare/worker.ts +8 -1
  114. package/src/runtime/node/cleanup.ts +5 -1
  115. package/src/runtime/node/email/transport.ts +13 -10
  116. package/src/runtime/node/main.ts +1 -1
  117. package/src/runtime/node/server.ts +12 -2
  118. package/src/runtime/node/services/main.ts +1 -1
  119. package/src/runtime/posthog/errors.ts +2 -1
  120. package/src/runtime/s3/objects.ts +17 -0
  121. package/src/runtime/site/community.ts +101 -0
  122. package/src/runtime/site/server.ts +17 -1
  123. package/src/server/account/deletion.ts +2 -0
  124. package/src/server/account/routes.ts +271 -240
  125. package/src/server/account/tokens.ts +57 -0
  126. package/src/server/api/router.ts +23 -0
  127. package/src/server/app/checks.ts +84 -0
  128. package/src/server/app/editors.ts +20 -0
  129. package/src/server/app/index.ts +128 -0
  130. package/src/server/app/indexing.ts +40 -0
  131. package/src/server/app/limiters.ts +53 -0
  132. package/src/server/app/routes.ts +114 -0
  133. package/src/server/app/services.ts +185 -0
  134. package/src/server/auth/access-tokens.ts +62 -0
  135. package/src/server/auth/adapter.ts +21 -1
  136. package/src/server/auth/email.ts +4 -1
  137. package/src/server/auth/enrollment.ts +116 -0
  138. package/src/server/auth/factory.ts +88 -131
  139. package/src/server/auth/logger.ts +23 -0
  140. package/src/server/auth/mfa-assurance.ts +51 -0
  141. package/src/server/auth/passwords.ts +43 -0
  142. package/src/server/auth/session.ts +2 -0
  143. package/src/server/billing/index.ts +6 -0
  144. package/src/server/billing/routes.ts +27 -0
  145. package/src/server/billing/tokens.ts +37 -0
  146. package/src/server/cleanup/configured.ts +16 -3
  147. package/src/server/cleanup/worker.ts +10 -1
  148. package/src/server/collaboration/relay/hub.ts +25 -142
  149. package/src/server/collaboration/relay/room.ts +154 -0
  150. package/src/server/collaboration/service.ts +21 -2
  151. package/src/server/community/index.ts +2 -0
  152. package/src/server/community/routes.ts +93 -0
  153. package/src/server/community/service.ts +134 -0
  154. package/src/server/config/deployment.ts +17 -5
  155. package/src/server/config/index.ts +1 -1
  156. package/src/server/config/public.ts +7 -1
  157. package/src/server/config/schema.ts +40 -292
  158. package/src/server/config/toml/schema.ts +307 -0
  159. package/src/server/db/index.ts +1 -0
  160. package/src/server/db/migrate.ts +13 -1
  161. package/src/server/db/migrations/009_access_tokens.ts +47 -0
  162. package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
  163. package/src/server/db/migrations/011_account_ids.ts +30 -0
  164. package/src/server/db/migrations/012_email_locales.ts +34 -0
  165. package/src/server/db/migrations/013_document_block_emails.ts +40 -0
  166. package/src/server/db/migrations/014_report_replies.ts +28 -0
  167. package/src/server/db/migrations/schema.ts +26 -1
  168. package/src/server/db/tables.ts +30 -1
  169. package/src/server/db/users.ts +22 -0
  170. package/src/server/documents/index.ts +1 -1
  171. package/src/server/documents/limits.ts +7 -1
  172. package/src/server/documents/service.ts +12 -402
  173. package/src/server/documents/uploads.ts +450 -0
  174. package/src/server/documents/versions/service.ts +81 -24
  175. package/src/server/email/invitations.ts +1 -0
  176. package/src/server/email/service.ts +31 -4
  177. package/src/server/email/types.ts +12 -0
  178. package/src/server/errors/index.ts +1 -9
  179. package/src/server/index.ts +12 -5
  180. package/src/server/invitations/types.ts +5 -1
  181. package/src/server/objects/types.ts +5 -0
  182. package/src/server/policy/configured.ts +9 -3
  183. package/src/server/policy/database.ts +13 -2
  184. package/src/server/policy/policy.ts +39 -12
  185. package/src/server/policy/service.ts +71 -52
  186. package/src/server/policy/storage.ts +33 -0
  187. package/src/server/quota/index.ts +6 -0
  188. package/src/server/quota/notices.ts +121 -0
  189. package/src/server/quota/storage.ts +5 -0
  190. package/src/server/rate-limit/middleware.ts +4 -0
  191. package/src/server/reports/index.ts +1 -0
  192. package/src/server/reports/replies.ts +55 -0
  193. package/src/server/reports/routes.ts +3 -1
  194. package/src/server/reports/service.ts +24 -2
  195. package/src/server/sharing/capabilities/service.ts +2 -3
  196. package/src/server/sharing/invitations/service.ts +14 -4
  197. package/src/server/validation.ts +3 -0
  198. package/src/server/workspaces/members.ts +7 -19
  199. package/src/services/app.ts +1 -3
  200. package/src/services/config.ts +1 -0
  201. package/src/services/usage.ts +6 -14
  202. package/src/email/templates/document-invitation.ts +0 -35
  203. package/src/email/templates/enrollment.ts +0 -27
  204. package/src/email/templates/layout.ts +0 -18
  205. package/src/server/app.ts +0 -453
  206. /package/src/{server/errors → errors}/posthog.ts +0 -0
package/src/client/api.ts CHANGED
@@ -1,104 +1,28 @@
1
1
  import type { CloudFetch } from '#cloud/client/discovery'
2
2
  import {
3
- acceptedInvitationSchema,
3
+ accountEntitlementsResponseSchema,
4
4
  servicesTokenSchema,
5
+ billingTokenSchema,
5
6
  cloudSessionSchema,
6
- cloudUserProfileSchema,
7
- collaborationTicketSchema,
8
- createInvitationContinuationSchema,
9
- documentAccessSchema,
10
- documentDownloadSchema,
11
- objectUploadSchema,
12
- documentGrantSchema,
13
- documentInvitationSchema,
14
- documentShareSchema,
15
- documentSummarySchema,
16
- documentVersionDownloadSchema,
17
- documentVersionResponseSchema,
18
- documentVersionsSchema,
19
- restoredDocumentSchema,
20
- sharedDocumentSchema,
21
- trashSchema,
22
- invitationContinuationSchema,
23
- invitationPreviewSchema,
24
- resolvedDocumentShareSchema,
25
- type AcceptDocumentInvitationInput,
7
+ type AccountEntitlements,
26
8
  type ServicesToken,
27
- type CloudSession,
28
- type CloudUserProfile,
29
- type CollaborationTicket,
30
- type CreateInvitationContinuationInput,
31
- type InvitationContinuation,
32
- type CommitUploadRequest,
33
- type CreateDocumentRequest,
34
- type CreateDocumentInvitationInput,
35
- type CreateDocumentShareInput,
36
- type CreateUploadInput,
37
- type DocumentAccess,
38
- type DocumentDownload,
39
- type DocumentGrant,
40
- type DocumentInvitation,
41
- type DocumentShare,
42
- type DocumentSummary,
43
- type DocumentVersion,
44
- type DocumentVersionDownload,
45
- type DocumentVersions,
46
- type DuplicateDocumentVersionInput,
47
- type SaveDocumentVersionInput,
48
- type UpdateDocumentVersionInput,
49
- type SharedDocument,
50
- type TrashedDocument,
51
- type InvitationPreview,
52
- type PutDocumentGrantInput,
53
- type ResolveDocumentShareInput,
54
- fileDocumentReportResponseSchema,
55
- type FileDocumentReportInput,
56
- type UpdateDocumentShareInput,
57
- type AcceptedInvitation
9
+ type BillingToken,
10
+ type CloudSession
58
11
  } from '#cloud/contract'
59
12
  import type { CloudAPI, PublicCloudAPI } from '#cloud/server/api'
60
- import { pickBy } from 'es-toolkit/object'
61
13
  import { hc } from 'hono/client'
62
14
  import * as v from 'valibot'
63
15
 
16
+ import { createCommunityRequests } from './community'
17
+ import { createDocumentRequests } from './documents'
64
18
  import { createImageRequests } from './images'
65
19
  import { CloudAPIError, parseResponse, responseBody, type CloudClientDiagnostic } from './response'
20
+ import { createSharingRequests } from './sharing'
21
+ import { createVersionRequests } from './versions'
66
22
  import { createWorkspaceRequests } from './workspaces'
67
23
 
68
24
  export { CloudAPIError, type CloudClientDiagnostic } from './response'
69
-
70
- const documentsResponseSchema = v.object({ documents: v.array(documentSummarySchema) })
71
- const sharedDocumentsResponseSchema = v.object({ documents: v.array(sharedDocumentSchema) })
72
- const documentResponseSchema = v.object({ document: documentSummarySchema })
73
- const downloadResponseSchema = v.object({ document: documentDownloadSchema })
74
- const accessResponseSchema = v.object({ access: documentAccessSchema })
75
- const sharesResponseSchema = v.object({ shares: v.array(documentShareSchema) })
76
- const shareResponseSchema = v.object({ share: documentShareSchema })
77
- const shareCapabilityResponseSchema = v.object({
78
- share: documentShareSchema,
79
- secret: v.string(),
80
- path: v.string()
81
- })
82
- const grantsResponseSchema = v.object({ grants: v.array(documentGrantSchema) })
83
- const grantResponseSchema = v.object({ grant: documentGrantSchema })
84
- const acceptedResponseSchema = v.object({ accepted: acceptedInvitationSchema })
85
- const invitationPreviewResponseSchema = v.object({ invitation: invitationPreviewSchema })
86
- const invitationsResponseSchema = v.object({ invitations: v.array(documentInvitationSchema) })
87
- const invitationCapabilityResponseSchema = v.object({
88
- invitation: documentInvitationSchema,
89
- token: v.string()
90
- })
91
- const collaborationTicketResponseSchema = v.object({ ticket: collaborationTicketSchema })
92
- const resolvedShareResponseSchema = v.object({ resolution: resolvedDocumentShareSchema })
93
- const sharedDocumentResponseSchema = v.object({
94
- resolution: resolvedDocumentShareSchema,
95
- document: documentDownloadSchema
96
- })
97
- const userLookupResponseSchema = v.object({ user: v.nullable(cloudUserProfileSchema) })
98
- const uploadResponseSchema = v.object({
99
- id: v.pipe(v.string(), v.uuid()),
100
- upload: objectUploadSchema
101
- })
25
+ export type { CloudUpload } from './documents'
102
26
 
103
27
  export type CloudRequestOptions = {
104
28
  fetch?: CloudFetch
@@ -107,8 +31,6 @@ export type CloudRequestOptions = {
107
31
  onDiagnostic?: CloudClientDiagnostic
108
32
  }
109
33
 
110
- export type CloudUpload = v.InferOutput<typeof uploadResponseSchema>
111
-
112
34
  function apiURL(baseURL: string): string {
113
35
  const url = new URL(baseURL)
114
36
  if (url.protocol !== 'https:' && url.protocol !== 'http:') {
@@ -148,395 +70,25 @@ export function createCloudAPIClient(baseURL: string, options: CloudRequestOptio
148
70
  options.onDiagnostic
149
71
  )
150
72
  },
151
- async getUserProfile(documentId: string, userId: string): Promise<CloudUserProfile | null> {
73
+ /** A short-lived token for the deployment's billing, for the signed-in person's own account. */
74
+ async requestBillingToken(): Promise<BillingToken> {
75
+ return parseResponse(client.billing.token.$post(), billingTokenSchema, options.onDiagnostic)
76
+ },
77
+ /** The signed-in account's plan, and the storage its workspaces hold together. */
78
+ async getAccountEntitlements(): Promise<AccountEntitlements> {
152
79
  const response = await parseResponse(
153
- client.documents[':documentId'].users[':userId'].$get({
154
- param: { documentId, userId }
155
- }),
156
- userLookupResponseSchema,
80
+ client.account.entitlements.$get(),
81
+ accountEntitlementsResponseSchema,
157
82
  options.onDiagnostic
158
83
  )
159
- return response.user
84
+ return response.entitlements
160
85
  },
161
86
  ...createWorkspaceRequests(client, options.onDiagnostic),
162
87
  ...createImageRequests(client, publicClient, options.onDiagnostic),
163
- /** Documents shared with the person directly, outside their workspaces. */
164
- async listSharedDocuments(): Promise<SharedDocument[]> {
165
- const response = await parseResponse(
166
- client['shared-documents'].$get(),
167
- sharedDocumentsResponseSchema,
168
- options.onDiagnostic
169
- )
170
- return response.documents
171
- },
172
- async listDocuments(workspaceId: string): Promise<DocumentSummary[]> {
173
- const response = await parseResponse(
174
- client.workspaces[':workspaceId'].documents.$get({
175
- param: { workspaceId }
176
- }),
177
- documentsResponseSchema,
178
- options.onDiagnostic
179
- )
180
- return response.documents
181
- },
182
- async createDocument(
183
- workspaceId: string,
184
- input: CreateDocumentRequest
185
- ): Promise<DocumentSummary> {
186
- const response = await parseResponse(
187
- client.workspaces[':workspaceId'].documents.$post({
188
- param: { workspaceId },
189
- json: { ...input, kind: input.kind ?? 'design' }
190
- }),
191
- documentResponseSchema,
192
- options.onDiagnostic
193
- )
194
- return response.document
195
- },
196
- /** Documents in Trash the person may restore, across their workspaces, newest first. */
197
- async listTrash(): Promise<TrashedDocument[]> {
198
- const response = await parseResponse(client.trash.$get(), trashSchema, options.onDiagnostic)
199
- return response.documents
200
- },
201
- async restoreDocument(documentId: string): Promise<void> {
202
- await responseBody(
203
- await client.trash[':documentId'].restore.$post({ param: { documentId } }),
204
- options.onDiagnostic
205
- )
206
- },
207
- /** Deletes a document in Trash for good. */
208
- async purgeDocument(documentId: string): Promise<void> {
209
- await responseBody(
210
- await client.trash[':documentId'].$delete({ param: { documentId } }),
211
- options.onDiagnostic
212
- )
213
- },
214
- async deleteDocument(documentId: string): Promise<void> {
215
- await responseBody(
216
- await client.documents[':documentId'].$delete({ param: { documentId } }),
217
- options.onDiagnostic
218
- )
219
- },
220
- async getDocument(documentId: string): Promise<DocumentDownload> {
221
- const response = await parseResponse(
222
- client.documents[':documentId'].$get({ param: { documentId } }),
223
- downloadResponseSchema,
224
- options.onDiagnostic
225
- )
226
- return response.document
227
- },
228
- async getDocumentAccess(documentId: string): Promise<DocumentAccess> {
229
- const response = await parseResponse(
230
- client.documents[':documentId'].access.$get({ param: { documentId } }),
231
- accessResponseSchema,
232
- options.onDiagnostic
233
- )
234
- return response.access
235
- },
236
- async listDocumentShares(documentId: string): Promise<DocumentShare[]> {
237
- const response = await parseResponse(
238
- client.documents[':documentId'].shares.$get({ param: { documentId } }),
239
- sharesResponseSchema,
240
- options.onDiagnostic
241
- )
242
- return response.shares
243
- },
244
- async createDocumentShare(documentId: string, input: CreateDocumentShareInput) {
245
- return parseResponse(
246
- client.documents[':documentId'].shares.$post({
247
- param: { documentId },
248
- json: input
249
- }),
250
- shareCapabilityResponseSchema,
251
- options.onDiagnostic
252
- )
253
- },
254
- async updateDocumentShare(
255
- documentId: string,
256
- shareId: string,
257
- input: UpdateDocumentShareInput
258
- ): Promise<DocumentShare> {
259
- const response = await parseResponse(
260
- client.documents[':documentId'].shares[':shareId'].$patch({
261
- param: { documentId, shareId },
262
- json: input
263
- }),
264
- shareResponseSchema,
265
- options.onDiagnostic
266
- )
267
- return response.share
268
- },
269
- async rotateDocumentShare(documentId: string, shareId: string) {
270
- return parseResponse(
271
- client.documents[':documentId'].shares[':shareId'].rotate.$post({
272
- param: { documentId, shareId }
273
- }),
274
- shareCapabilityResponseSchema,
275
- options.onDiagnostic
276
- )
277
- },
278
- async revokeDocumentShare(documentId: string, shareId: string): Promise<void> {
279
- await responseBody(
280
- await client.documents[':documentId'].shares[':shareId'].$delete({
281
- param: { documentId, shareId }
282
- }),
283
- options.onDiagnostic
284
- )
285
- },
286
- async listDocumentGrants(documentId: string): Promise<DocumentGrant[]> {
287
- const response = await parseResponse(
288
- client.documents[':documentId'].grants.$get({ param: { documentId } }),
289
- grantsResponseSchema,
290
- options.onDiagnostic
291
- )
292
- return response.grants
293
- },
294
- async putDocumentGrant(
295
- documentId: string,
296
- userId: string,
297
- input: PutDocumentGrantInput
298
- ): Promise<DocumentGrant> {
299
- const response = await parseResponse(
300
- client.documents[':documentId'].grants[':userId'].$put({
301
- param: { documentId, userId },
302
- json: input
303
- }),
304
- grantResponseSchema,
305
- options.onDiagnostic
306
- )
307
- return response.grant
308
- },
309
- async revokeDocumentGrant(documentId: string, userId: string): Promise<void> {
310
- await responseBody(
311
- await client.documents[':documentId'].grants[':userId'].$delete({
312
- param: { documentId, userId }
313
- }),
314
- options.onDiagnostic
315
- )
316
- },
317
- async listDocumentInvitations(documentId: string): Promise<DocumentInvitation[]> {
318
- const response = await parseResponse(
319
- client.documents[':documentId'].invitations.$get({ param: { documentId } }),
320
- invitationsResponseSchema,
321
- options.onDiagnostic
322
- )
323
- return response.invitations
324
- },
325
- async createDocumentInvitation(documentId: string, input: CreateDocumentInvitationInput) {
326
- return parseResponse(
327
- client.documents[':documentId'].invitations.$post({
328
- param: { documentId },
329
- json: input
330
- }),
331
- invitationCapabilityResponseSchema,
332
- options.onDiagnostic
333
- )
334
- },
335
- async createInvitationContinuation(
336
- input: CreateInvitationContinuationInput
337
- ): Promise<InvitationContinuation> {
338
- return parseResponse(
339
- publicClient.invitations.continuations.$post({ json: input }),
340
- invitationContinuationSchema,
341
- options.onDiagnostic
342
- )
343
- },
344
- async consumeInvitationContinuation(id: string): Promise<CreateInvitationContinuationInput> {
345
- return parseResponse(
346
- publicClient.invitations.continuations[':continuationId'].consume.$post({
347
- param: { continuationId: id }
348
- }),
349
- createInvitationContinuationSchema,
350
- options.onDiagnostic
351
- )
352
- },
353
- async previewInvitation(
354
- invitationId: string,
355
- input: AcceptDocumentInvitationInput
356
- ): Promise<InvitationPreview> {
357
- const response = await parseResponse(
358
- publicClient.invitations[':invitationId'].preview.$post({
359
- param: { invitationId },
360
- json: input
361
- }),
362
- invitationPreviewResponseSchema,
363
- options.onDiagnostic
364
- )
365
- return response.invitation
366
- },
367
- /** Accepts a document or workspace invitation, and says which it was. */
368
- async acceptInvitation(
369
- invitationId: string,
370
- input: AcceptDocumentInvitationInput
371
- ): Promise<AcceptedInvitation> {
372
- const response = await parseResponse(
373
- client.invitations[':invitationId'].accept.$post({
374
- param: { invitationId },
375
- json: input
376
- }),
377
- acceptedResponseSchema,
378
- options.onDiagnostic
379
- )
380
- return response.accepted
381
- },
382
- async revokeDocumentInvitation(documentId: string, invitationId: string): Promise<void> {
383
- await responseBody(
384
- await client.documents[':documentId'].invitations[':invitationId'].$delete({
385
- param: { documentId, invitationId }
386
- }),
387
- options.onDiagnostic
388
- )
389
- },
390
- /** Reports the document a share link opens to the server's administrators. */
391
- async fileDocumentReport(shareId: string, input: FileDocumentReportInput): Promise<void> {
392
- await parseResponse(
393
- publicClient.shares[':shareId'].reports.$post({ param: { shareId }, json: input }),
394
- fileDocumentReportResponseSchema,
395
- options.onDiagnostic
396
- )
397
- },
398
- async resolveDocumentShare(shareId: string, input: ResolveDocumentShareInput) {
399
- const response = await parseResponse(
400
- publicClient.shares[':shareId'].resolve.$post({
401
- param: { shareId },
402
- json: input
403
- }),
404
- resolvedShareResponseSchema,
405
- options.onDiagnostic
406
- )
407
- return response.resolution
408
- },
409
- async getSharedDocument(shareId: string, input: ResolveDocumentShareInput) {
410
- return parseResponse(
411
- publicClient.shares[':shareId'].document.$post({
412
- param: { shareId },
413
- json: input
414
- }),
415
- sharedDocumentResponseSchema,
416
- options.onDiagnostic
417
- )
418
- },
419
- async getSharedCollaborationTicket(
420
- shareId: string,
421
- input: ResolveDocumentShareInput
422
- ): Promise<CollaborationTicket> {
423
- const response = await parseResponse(
424
- publicClient.shares[':shareId']['collaboration-ticket'].$post({
425
- param: { shareId },
426
- json: input
427
- }),
428
- collaborationTicketResponseSchema,
429
- options.onDiagnostic
430
- )
431
- return response.ticket
432
- },
433
- async getCollaborationTicket(documentId: string): Promise<CollaborationTicket> {
434
- const response = await parseResponse(
435
- client.documents[':documentId']['collaboration-ticket'].$post({
436
- param: { documentId }
437
- }),
438
- collaborationTicketResponseSchema,
439
- options.onDiagnostic
440
- )
441
- return response.ticket
442
- },
443
- async createUpload(documentId: string, input: CreateUploadInput): Promise<CloudUpload> {
444
- return parseResponse(
445
- client.documents[':documentId'].uploads.$post({
446
- param: { documentId },
447
- json: input
448
- }),
449
- uploadResponseSchema,
450
- options.onDiagnostic
451
- )
452
- },
453
- async commitUpload(uploadId: string, input: CommitUploadRequest): Promise<DocumentSummary> {
454
- const response = await parseResponse(
455
- client.uploads[':uploadId'].commit.$post({
456
- param: { uploadId },
457
- json: { ...input, images: input.images ?? [] }
458
- }),
459
- documentResponseSchema,
460
- options.onDiagnostic
461
- )
462
- return response.document
463
- },
464
- /** A page of the document's version history, newest first. */
465
- async listDocumentVersions(
466
- documentId: string,
467
- page: { before?: string; limit?: number } = {}
468
- ): Promise<DocumentVersions> {
469
- return parseResponse(
470
- client.documents[':documentId'].versions.$get({
471
- param: { documentId },
472
- query: pickBy(page, (value) => value !== undefined)
473
- }),
474
- documentVersionsSchema,
475
- options.onDiagnostic
476
- )
477
- },
478
- async saveDocumentVersion(
479
- documentId: string,
480
- input: SaveDocumentVersionInput
481
- ): Promise<DocumentVersion> {
482
- const response = await parseResponse(
483
- client.documents[':documentId'].versions.$post({ param: { documentId }, json: input }),
484
- documentVersionResponseSchema,
485
- options.onDiagnostic
486
- )
487
- return response.version
488
- },
489
- async updateDocumentVersion(
490
- documentId: string,
491
- versionId: string,
492
- input: UpdateDocumentVersionInput
493
- ): Promise<DocumentVersion> {
494
- const response = await parseResponse(
495
- client.documents[':documentId'].versions[':versionId'].$patch({
496
- param: { documentId, versionId },
497
- json: input
498
- }),
499
- documentVersionResponseSchema,
500
- options.onDiagnostic
501
- )
502
- return response.version
503
- },
504
- async getDocumentVersion(
505
- documentId: string,
506
- versionId: string
507
- ): Promise<DocumentVersionDownload> {
508
- return parseResponse(
509
- client.documents[':documentId'].versions[':versionId'].$get({
510
- param: { documentId, versionId }
511
- }),
512
- documentVersionDownloadSchema,
513
- options.onDiagnostic
514
- )
515
- },
516
- async restoreDocumentVersion(documentId: string, versionId: string) {
517
- return parseResponse(
518
- client.documents[':documentId'].versions[':versionId'].restore.$post({
519
- param: { documentId, versionId }
520
- }),
521
- restoredDocumentSchema,
522
- options.onDiagnostic
523
- )
524
- },
525
- async duplicateDocumentVersion(
526
- documentId: string,
527
- versionId: string,
528
- input: DuplicateDocumentVersionInput
529
- ): Promise<DocumentSummary> {
530
- const response = await parseResponse(
531
- client.documents[':documentId'].versions[':versionId'].duplicate.$post({
532
- param: { documentId, versionId },
533
- json: input
534
- }),
535
- documentResponseSchema,
536
- options.onDiagnostic
537
- )
538
- return response.document
539
- }
88
+ ...createDocumentRequests(client, options.onDiagnostic),
89
+ ...createVersionRequests(client, options.onDiagnostic),
90
+ ...createSharingRequests(client, publicClient, options.onDiagnostic),
91
+ ...createCommunityRequests(publicClient, options.onDiagnostic)
540
92
  }
541
93
  }
542
94
 
@@ -23,11 +23,20 @@ export type CloudAuthClient = Pick<
23
23
  | 'getSession'
24
24
  | 'signIn'
25
25
  | 'signOut'
26
- | 'signUp'
27
26
  | 'sendVerificationEmail'
28
27
  | 'requestPasswordReset'
29
28
  | 'resetPassword'
30
29
  > & {
30
+ signUp: {
31
+ /** Also takes the language the server writes to the account in, a field Cloud adds. */
32
+ email(input: {
33
+ name: string
34
+ email: string
35
+ password: string
36
+ locale: string
37
+ callbackURL?: string
38
+ }): Promise<{ data: unknown; error: { code?: string; status: number } | null }>
39
+ }
31
40
  signIn: ReturnType<typeof createAuthClient>['signIn'] & {
32
41
  passkey(): Promise<{
33
42
  data: unknown
@@ -157,10 +166,19 @@ export async function signInToCloudWithEmail(
157
166
  return { ok: true, value: { status: 'signed-in', token: result.data.token } }
158
167
  }
159
168
 
160
- /** Creates an account; the server emails a link that opens `verifiedURL` once it is confirmed. */
169
+ /**
170
+ * Creates an account; the server emails a link that opens `verifiedURL` once it is confirmed,
171
+ * written in `locale`, the app's language.
172
+ */
161
173
  export async function signUpToCloudWithEmail(
162
174
  discovery: CloudDiscovery,
163
- input: { name: string; email: string; password: string; verifiedURL: string },
175
+ input: {
176
+ name: string
177
+ email: string
178
+ password: string
179
+ verifiedURL: string
180
+ locale: string
181
+ },
164
182
  options: CloudEmailAuthOptions = {}
165
183
  ): Promise<CloudAuthResult> {
166
184
  const result = await createCloudAuthClient(discovery, options)
@@ -168,6 +186,7 @@ export async function signUpToCloudWithEmail(
168
186
  name: input.name,
169
187
  email: input.email,
170
188
  password: input.password,
189
+ locale: input.locale,
171
190
  callbackURL: input.verifiedURL
172
191
  })
173
192
  .catch(() => null)
@@ -0,0 +1,80 @@
1
+ import type { CloudFetch } from '#cloud/client/discovery'
2
+ import { serviceJSON } from '#cloud/client/response'
3
+ import { cloudServicesFetch, type CloudServicesTokenSource } from '#cloud/client/services'
4
+ import {
5
+ billingAccountSchema,
6
+ billingCatalogSchema,
7
+ billingErrorSchema,
8
+ billingRedirectSchema,
9
+ type BillingAccount,
10
+ type BillingCatalog,
11
+ type BillingErrorCode,
12
+ type CheckoutRequest,
13
+ type CloudBillingAPI
14
+ } from '#cloud/contract'
15
+ import { hc } from 'hono/client'
16
+ import { withoutTrailingSlash } from 'ufo'
17
+ import * as v from 'valibot'
18
+
19
+ export class CloudBillingError extends Error {
20
+ override readonly name = 'CloudBillingError'
21
+
22
+ constructor(
23
+ readonly status: number,
24
+ readonly code?: BillingErrorCode
25
+ ) {
26
+ super(`OpenPencil billing request failed with HTTP ${status}`)
27
+ }
28
+ }
29
+
30
+ const errorBodySchema = v.pipe(v.string(), v.parseJson(), billingErrorSchema)
31
+
32
+ function refused(status: number, body: string): CloudBillingError {
33
+ const error = v.safeParse(errorBodySchema, body)
34
+ return new CloudBillingError(status, error.success ? error.output.error.code : undefined)
35
+ }
36
+
37
+ export type CloudBillingClientOptions = {
38
+ /** The billing service's endpoint, as Cloud's discovery names it. */
39
+ endpoint: string
40
+ /** Billing tokens for the signed-in person, from `requestBillingToken`. */
41
+ tokens: CloudServicesTokenSource
42
+ fetch: CloudFetch
43
+ }
44
+
45
+ /**
46
+ * The billing a Cloud advertises: what it sells, the account's subscription, and where to buy or
47
+ * manage it. Checkout and the portal answer with the payment provider's page to open, so a token
48
+ * never travels in a URL.
49
+ */
50
+ export function createCloudBillingClient(options: CloudBillingClientOptions) {
51
+ const endpoint = withoutTrailingSlash(options.endpoint)
52
+ // Answers are parsed rather than trusted: the service is deployed apart from this client.
53
+ const { v1 } = hc<CloudBillingAPI>(endpoint, {
54
+ fetch: cloudServicesFetch(options.tokens, options.fetch)
55
+ })
56
+ return {
57
+ async catalog(): Promise<BillingCatalog> {
58
+ return serviceJSON(await v1.catalog.$get(), billingCatalogSchema, refused)
59
+ },
60
+ async account(): Promise<BillingAccount> {
61
+ return serviceJSON(await v1.account.$get(), billingAccountSchema, refused)
62
+ },
63
+ /** The checkout page that buys `request` for the account. */
64
+ async checkout(request: CheckoutRequest): Promise<string> {
65
+ return (
66
+ await serviceJSON(
67
+ await v1.checkout.$post({ json: request }),
68
+ billingRedirectSchema,
69
+ refused
70
+ )
71
+ ).url
72
+ },
73
+ /** The page where the account cancels, changes its card, and reads its invoices. */
74
+ async portal(): Promise<string> {
75
+ return (await serviceJSON(await v1.portal.$post(), billingRedirectSchema, refused)).url
76
+ }
77
+ }
78
+ }
79
+
80
+ export type CloudBillingClient = ReturnType<typeof createCloudBillingClient>
@@ -0,0 +1,40 @@
1
+ import {
2
+ communityDocumentsSchema,
3
+ communityDownloadSchema,
4
+ type CommunityDocuments,
5
+ type CommunityDownload
6
+ } from '#cloud/contract'
7
+ import type { PublicCloudAPI } from '#cloud/server/api'
8
+ import type { hc } from 'hono/client'
9
+
10
+ import { parseResponse, type CloudClientDiagnostic } from './response'
11
+
12
+ /** The server’s community gallery, which anyone may open without signing in. */
13
+ export function createCommunityRequests(
14
+ publicClient: ReturnType<typeof hc<PublicCloudAPI>>,
15
+ onDiagnostic?: CloudClientDiagnostic
16
+ ) {
17
+ return {
18
+ /** The gallery's documents, most recently changed first. */
19
+ async listCommunityDocuments(): Promise<CommunityDocuments> {
20
+ return parseResponse(publicClient.community.$get(), communityDocumentsSchema, onDiagnostic)
21
+ },
22
+ /**
23
+ * A document of the server's community gallery as it is now, or one of its named versions,
24
+ * which anyone may open without signing in.
25
+ */
26
+ async getCommunityDocument(
27
+ documentId: string,
28
+ versionId: string | null = null
29
+ ): Promise<CommunityDownload> {
30
+ return parseResponse(
31
+ publicClient.community[':documentId'].document.$get({
32
+ param: { documentId },
33
+ query: versionId ? { version: versionId } : {}
34
+ }),
35
+ communityDownloadSchema,
36
+ onDiagnostic
37
+ )
38
+ }
39
+ }
40
+ }