@pairdraw/cloud 0.0.0-stage → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +7 -0
- package/README.md +37 -2
- package/dist/bin/ai-relay.d.mts +1 -0
- package/dist/bin/ai-relay.mjs +31 -0
- package/dist/bin/ai-relay.mjs.map +1 -0
- package/dist/bin/server.d.mts +1 -0
- package/dist/bin/server.mjs +29 -0
- package/dist/bin/server.mjs.map +1 -0
- package/dist/client.d.ts +1578 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +2008 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/contract2.js +1673 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +155 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +700 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1945 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1905 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10715 -0
- package/dist/runtime-node.d.mts.map +1 -0
- package/dist/runtime-node.mjs +32 -0
- package/dist/runtime-node.mjs.map +1 -0
- package/dist/server.d.ts +13772 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8856 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8836 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +132 -4
- package/src/admin/api/routes.ts +223 -0
- package/src/admin/api/security.ts +12 -0
- package/src/admin/audit/service.ts +138 -0
- package/src/admin/email/service.ts +140 -0
- package/src/admin/enrollment/email.ts +77 -0
- package/src/admin/enrollment/service.ts +308 -0
- package/src/admin/errors.ts +20 -0
- package/src/admin/index.ts +19 -0
- package/src/admin/list.ts +61 -0
- package/src/admin/operations/service.ts +70 -0
- package/src/admin/schema.ts +26 -0
- package/src/admin/users/service.ts +369 -0
- package/src/ai-relay/app.ts +251 -0
- package/src/ai-relay/config.ts +120 -0
- package/src/ai-relay/db/index.ts +10 -0
- package/src/ai-relay/db/migrate.ts +32 -0
- package/src/ai-relay/db/migrations/001_usage.ts +26 -0
- package/src/ai-relay/db/schema.ts +20 -0
- package/src/ai-relay/index.ts +18 -0
- package/src/ai-relay/metering.ts +68 -0
- package/src/ai-relay/usage.ts +115 -0
- package/src/client/ai.ts +96 -0
- package/src/client/api.ts +530 -0
- package/src/client/auth/client.ts +188 -0
- package/src/client/auth/failure.ts +54 -0
- package/src/client/auth/navigation.ts +45 -0
- package/src/client/device-authorization.ts +134 -0
- package/src/client/discovery.ts +105 -0
- package/src/client/images.ts +81 -0
- package/src/client/index.ts +53 -0
- package/src/client/portal/account.ts +183 -0
- package/src/client/portal/administration.ts +130 -0
- package/src/client/portal/client.ts +18 -0
- package/src/client/portal/request.ts +115 -0
- package/src/client/profiles.ts +79 -0
- package/src/client/response.ts +51 -0
- package/src/client/workspaces.ts +142 -0
- package/src/contract/account.ts +204 -0
- package/src/contract/admin/index.ts +187 -0
- package/src/contract/ai.ts +150 -0
- package/src/contract/collaboration.ts +45 -0
- package/src/contract/comments.ts +149 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +263 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +210 -0
- package/src/contract/links.ts +108 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/profiles.ts +88 -0
- package/src/contract/relay.ts +370 -0
- package/src/contract/sharing.ts +215 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +13 -0
- package/src/email/render.ts +304 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +125 -0
- package/src/email/templates/document-invitation.ts +39 -0
- package/src/email/templates/enrollment.ts +30 -0
- package/src/email/templates/layout.ts +20 -0
- package/src/runtime/cloudflare/email.ts +70 -0
- package/src/runtime/cloudflare/environment.ts +57 -0
- package/src/runtime/cloudflare/index.ts +3 -0
- package/src/runtime/cloudflare/relay.ts +136 -0
- package/src/runtime/cloudflare/site.ts +13 -0
- package/src/runtime/cloudflare/tsconfig.json +21 -0
- package/src/runtime/cloudflare/worker.ts +161 -0
- package/src/runtime/node/admin.ts +59 -0
- package/src/runtime/node/ai-relay/main.ts +35 -0
- package/src/runtime/node/ai-relay/server.ts +63 -0
- package/src/runtime/node/application.ts +50 -0
- package/src/runtime/node/bootstrap.ts +22 -0
- package/src/runtime/node/cleanup.ts +24 -0
- package/src/runtime/node/command.ts +17 -0
- package/src/runtime/node/config.ts +16 -0
- package/src/runtime/node/email/runtime.ts +44 -0
- package/src/runtime/node/email/transport.ts +112 -0
- package/src/runtime/node/entitlements/main.ts +41 -0
- package/src/runtime/node/entitlements/refusal.ts +15 -0
- package/src/runtime/node/index.ts +25 -0
- package/src/runtime/node/listen.ts +35 -0
- package/src/runtime/node/main.ts +29 -0
- package/src/runtime/node/migrate.ts +13 -0
- package/src/runtime/node/quota-reconcile.ts +9 -0
- package/src/runtime/node/relay.ts +54 -0
- package/src/runtime/node/server.ts +119 -0
- package/src/runtime/node/site.ts +64 -0
- package/src/runtime/postgres/database.ts +19 -0
- package/src/runtime/s3/objects.ts +244 -0
- package/src/runtime/site/server.ts +82 -0
- package/src/server/account/actions.ts +66 -0
- package/src/server/account/routes.ts +300 -0
- package/src/server/ai/errors.ts +7 -0
- package/src/server/ai/index.ts +4 -0
- package/src/server/ai/keys.ts +43 -0
- package/src/server/ai/routes.ts +43 -0
- package/src/server/ai/service.ts +70 -0
- package/src/server/api/index.ts +8 -0
- package/src/server/api/router.ts +129 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +385 -0
- package/src/server/auth/account.ts +73 -0
- package/src/server/auth/activity.ts +213 -0
- package/src/server/auth/adapter.ts +79 -0
- package/src/server/auth/email.ts +125 -0
- package/src/server/auth/endpoints.ts +57 -0
- package/src/server/auth/factory.ts +583 -0
- package/src/server/auth/index.ts +17 -0
- package/src/server/auth/mfa-assurance.ts +55 -0
- package/src/server/auth/runtime.ts +26 -0
- package/src/server/auth/session.ts +19 -0
- package/src/server/auth/social-providers.ts +55 -0
- package/src/server/auth/user-agent.ts +16 -0
- package/src/server/auth/workspace-access.ts +23 -0
- package/src/server/cleanup/configured.ts +38 -0
- package/src/server/cleanup/documents.ts +231 -0
- package/src/server/cleanup/index.ts +29 -0
- package/src/server/cleanup/objects.ts +27 -0
- package/src/server/cleanup/revisions.ts +246 -0
- package/src/server/cleanup/uploads.ts +129 -0
- package/src/server/cleanup/worker.ts +79 -0
- package/src/server/collaboration/index.ts +28 -0
- package/src/server/collaboration/relay/authorize.ts +54 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/comments.ts +86 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +567 -0
- package/src/server/collaboration/relay/index.ts +15 -0
- package/src/server/collaboration/relay/persistence.ts +48 -0
- package/src/server/collaboration/routes.ts +77 -0
- package/src/server/collaboration/service.ts +190 -0
- package/src/server/config/deployment.ts +178 -0
- package/src/server/config/index.ts +20 -0
- package/src/server/config/limits.ts +8 -0
- package/src/server/config/public.ts +66 -0
- package/src/server/config/schema.ts +512 -0
- package/src/server/db/database.ts +15 -0
- package/src/server/db/expressions.ts +13 -0
- package/src/server/db/index.ts +21 -0
- package/src/server/db/migrate.ts +177 -0
- package/src/server/db/migrations/001_foundation.ts +164 -0
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
- package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
- package/src/server/db/migrations/004_document_sharing.ts +82 -0
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
- package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
- package/src/server/db/migrations/007_collaboration_state.ts +25 -0
- package/src/server/db/migrations/008_storage_reservations.ts +37 -0
- package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
- package/src/server/db/migrations/010_upload_finalization.ts +22 -0
- package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
- package/src/server/db/migrations/012_transactional_email.ts +48 -0
- package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
- package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
- package/src/server/db/migrations/016_auth_schema.ts +16 -0
- package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
- package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
- package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
- package/src/server/db/migrations/021_document_versions.ts +66 -0
- package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
- package/src/server/db/migrations/023_document_images.ts +61 -0
- package/src/server/db/migrations/024_account_activity.ts +66 -0
- package/src/server/db/migrations/025_table_names.ts +58 -0
- package/src/server/db/migrations/026_user_picture.ts +30 -0
- package/src/server/db/migrations/027_sharing_comments_snapshots_profiles.ts +74 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +399 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +405 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +131 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +607 -0
- package/src/server/documents/summary.ts +36 -0
- package/src/server/documents/types.ts +13 -0
- package/src/server/documents/versions/checkpoint.ts +98 -0
- package/src/server/documents/versions/history.ts +25 -0
- package/src/server/documents/versions/index.ts +4 -0
- package/src/server/documents/versions/routes.ts +112 -0
- package/src/server/documents/versions/service.ts +396 -0
- package/src/server/email/crypto.ts +57 -0
- package/src/server/email/index.ts +18 -0
- package/src/server/email/invitations.ts +30 -0
- package/src/server/email/service.ts +247 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +288 -0
- package/src/server/internal/index.ts +8 -0
- package/src/server/internal/routes.ts +41 -0
- package/src/server/internal/schema.ts +10 -0
- package/src/server/internal/service.ts +44 -0
- package/src/server/invitations/index.ts +6 -0
- package/src/server/invitations/types.ts +30 -0
- package/src/server/objects/index.ts +16 -0
- package/src/server/objects/types.ts +81 -0
- package/src/server/pictures/index.ts +9 -0
- package/src/server/pictures/provider.ts +61 -0
- package/src/server/pictures/routes.ts +21 -0
- package/src/server/pictures/service.ts +235 -0
- package/src/server/policy/configured.ts +23 -0
- package/src/server/policy/database.ts +108 -0
- package/src/server/policy/entitlements.ts +29 -0
- package/src/server/policy/index.ts +17 -0
- package/src/server/policy/keys.ts +23 -0
- package/src/server/policy/policy.ts +117 -0
- package/src/server/policy/provider.ts +67 -0
- package/src/server/policy/schema.ts +69 -0
- package/src/server/policy/service.ts +85 -0
- package/src/server/policy/static.ts +62 -0
- package/src/server/profiles/index.ts +12 -0
- package/src/server/profiles/routes.ts +103 -0
- package/src/server/profiles/service.ts +266 -0
- package/src/server/quota/index.ts +7 -0
- package/src/server/quota/reconcile.ts +61 -0
- package/src/server/quota/storage.ts +165 -0
- package/src/server/rate-limit/cleanup.ts +16 -0
- package/src/server/rate-limit/index.ts +12 -0
- package/src/server/rate-limit/keys.ts +38 -0
- package/src/server/rate-limit/middleware.ts +137 -0
- package/src/server/rate-limit/postgres.ts +79 -0
- package/src/server/sharing/access.ts +29 -0
- package/src/server/sharing/actor.ts +12 -0
- package/src/server/sharing/capabilities/service.ts +318 -0
- package/src/server/sharing/continuation.ts +83 -0
- package/src/server/sharing/contracts.ts +82 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +20 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +45 -0
- package/src/server/sharing/index.ts +40 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +42 -0
- package/src/server/sharing/routes.ts +251 -0
- package/src/server/sharing/service.ts +117 -0
- package/src/server/validation.ts +24 -0
- package/src/server/worker/interval.ts +23 -0
- package/src/server/worker/periodic.ts +42 -0
- package/src/server/workspaces/index.ts +16 -0
- package/src/server/workspaces/members.ts +502 -0
- package/src/server/workspaces/repository.ts +91 -0
- package/src/server/workspaces/routes.ts +198 -0
- package/src/server/workspaces/service.ts +84 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cloudAccountMutationResponseSchema,
|
|
3
|
+
cloudAccountSessionsResponseSchema,
|
|
4
|
+
cloudAccountStatusResponseSchema,
|
|
5
|
+
cloudAuthenticationMethodsResponseSchema,
|
|
6
|
+
cloudMFAEnableResponseSchema,
|
|
7
|
+
cloudMFARecoveryCodesResponseSchema,
|
|
8
|
+
cloudMFAStatusResponseSchema,
|
|
9
|
+
cloudMFAVerifyResponseSchema,
|
|
10
|
+
cloudPasskeysResponseSchema,
|
|
11
|
+
cloudPictureResponseSchema,
|
|
12
|
+
cloudProfileResponseSchema,
|
|
13
|
+
cloudSignInsResponseSchema,
|
|
14
|
+
cloudSocialLinkResponseSchema
|
|
15
|
+
} from '#cloud/contract'
|
|
16
|
+
|
|
17
|
+
import type { CloudCall, PortalRouteClients } from './request'
|
|
18
|
+
|
|
19
|
+
export function createAccountAPI(call: CloudCall, { account }: PortalRouteClients) {
|
|
20
|
+
return {
|
|
21
|
+
accountStatus(signal?: AbortSignal) {
|
|
22
|
+
return call(
|
|
23
|
+
(options) => account.status.$get(undefined, options),
|
|
24
|
+
cloudAccountStatusResponseSchema,
|
|
25
|
+
signal
|
|
26
|
+
)
|
|
27
|
+
},
|
|
28
|
+
authenticationMethods(signal?: AbortSignal) {
|
|
29
|
+
return call(
|
|
30
|
+
(options) => account.authentication.$get(undefined, options),
|
|
31
|
+
cloudAuthenticationMethodsResponseSchema,
|
|
32
|
+
signal
|
|
33
|
+
)
|
|
34
|
+
},
|
|
35
|
+
mfaStatus(signal?: AbortSignal) {
|
|
36
|
+
return call(
|
|
37
|
+
(options) => account.mfa.$get(undefined, options),
|
|
38
|
+
cloudMFAStatusResponseSchema,
|
|
39
|
+
signal
|
|
40
|
+
)
|
|
41
|
+
},
|
|
42
|
+
enableTOTP(password?: string, signal?: AbortSignal) {
|
|
43
|
+
return call(
|
|
44
|
+
(options) => account.mfa.totp.enable.$post({ json: { password } }, options),
|
|
45
|
+
cloudMFAEnableResponseSchema,
|
|
46
|
+
signal
|
|
47
|
+
)
|
|
48
|
+
},
|
|
49
|
+
verifyTOTP(code: string, signal?: AbortSignal) {
|
|
50
|
+
return call(
|
|
51
|
+
(options) => account.mfa.totp.verify.$post({ json: { code } }, options),
|
|
52
|
+
cloudMFAVerifyResponseSchema,
|
|
53
|
+
signal
|
|
54
|
+
)
|
|
55
|
+
},
|
|
56
|
+
verifyRecoveryCode(code: string, signal?: AbortSignal) {
|
|
57
|
+
return call(
|
|
58
|
+
(options) => account.mfa.recovery.verify.$post({ json: { code } }, options),
|
|
59
|
+
cloudMFAVerifyResponseSchema,
|
|
60
|
+
signal
|
|
61
|
+
)
|
|
62
|
+
},
|
|
63
|
+
regenerateRecoveryCodes(password?: string, signal?: AbortSignal) {
|
|
64
|
+
return call(
|
|
65
|
+
(options) => account.mfa.recovery.regenerate.$post({ json: { password } }, options),
|
|
66
|
+
cloudMFARecoveryCodesResponseSchema,
|
|
67
|
+
signal
|
|
68
|
+
)
|
|
69
|
+
},
|
|
70
|
+
disableTOTP(password?: string, signal?: AbortSignal) {
|
|
71
|
+
return call(
|
|
72
|
+
(options) => account.mfa.totp.disable.$post({ json: { password } }, options),
|
|
73
|
+
cloudAccountMutationResponseSchema,
|
|
74
|
+
signal
|
|
75
|
+
)
|
|
76
|
+
},
|
|
77
|
+
passkeys(signal?: AbortSignal) {
|
|
78
|
+
return call(
|
|
79
|
+
(options) => account.mfa.passkeys.$get(undefined, options),
|
|
80
|
+
cloudPasskeysResponseSchema,
|
|
81
|
+
signal
|
|
82
|
+
)
|
|
83
|
+
},
|
|
84
|
+
deletePasskey(id: string, signal?: AbortSignal) {
|
|
85
|
+
return call(
|
|
86
|
+
(options) => account.mfa.passkeys.delete.$post({ json: { id } }, options),
|
|
87
|
+
cloudAccountMutationResponseSchema,
|
|
88
|
+
signal
|
|
89
|
+
)
|
|
90
|
+
},
|
|
91
|
+
changePassword(currentPassword: string, newPassword: string, signal?: AbortSignal) {
|
|
92
|
+
return call(
|
|
93
|
+
(options) =>
|
|
94
|
+
account.authentication['change-password'].$post(
|
|
95
|
+
{ json: { currentPassword, newPassword } },
|
|
96
|
+
options
|
|
97
|
+
),
|
|
98
|
+
cloudAccountMutationResponseSchema,
|
|
99
|
+
signal
|
|
100
|
+
)
|
|
101
|
+
},
|
|
102
|
+
linkSocial(provider: 'google' | 'apple', callbackURL: string, signal?: AbortSignal) {
|
|
103
|
+
return call(
|
|
104
|
+
(options) =>
|
|
105
|
+
account.authentication['link-social'].$post({ json: { provider, callbackURL } }, options),
|
|
106
|
+
cloudSocialLinkResponseSchema,
|
|
107
|
+
signal
|
|
108
|
+
)
|
|
109
|
+
},
|
|
110
|
+
profile(signal?: AbortSignal) {
|
|
111
|
+
return call(
|
|
112
|
+
(options) => account.profile.$get(undefined, options),
|
|
113
|
+
cloudProfileResponseSchema,
|
|
114
|
+
signal
|
|
115
|
+
)
|
|
116
|
+
},
|
|
117
|
+
updateProfile(name: string, signal?: AbortSignal) {
|
|
118
|
+
return call(
|
|
119
|
+
(options) => account.profile.$post({ json: { name } }, options),
|
|
120
|
+
cloudAccountMutationResponseSchema,
|
|
121
|
+
signal
|
|
122
|
+
)
|
|
123
|
+
},
|
|
124
|
+
/** Shows `data`, a Base64 PNG or JPEG the page cropped and shrank, as the account's picture. */
|
|
125
|
+
uploadPicture(data: string, signal?: AbortSignal) {
|
|
126
|
+
return call(
|
|
127
|
+
(options) => account.profile.picture.$put({ json: { data } }, options),
|
|
128
|
+
cloudPictureResponseSchema,
|
|
129
|
+
signal
|
|
130
|
+
)
|
|
131
|
+
},
|
|
132
|
+
removePicture(signal?: AbortSignal) {
|
|
133
|
+
return call(
|
|
134
|
+
(options) => account.profile.picture.$delete(undefined, options),
|
|
135
|
+
cloudAccountMutationResponseSchema,
|
|
136
|
+
signal
|
|
137
|
+
)
|
|
138
|
+
},
|
|
139
|
+
/** Sends a confirmation to the new address; the email changes once it is followed. */
|
|
140
|
+
changeEmail(newEmail: string, callbackURL: string, signal?: AbortSignal) {
|
|
141
|
+
return call(
|
|
142
|
+
(options) => account.profile.email.$post({ json: { newEmail, callbackURL } }, options),
|
|
143
|
+
cloudAccountMutationResponseSchema,
|
|
144
|
+
signal
|
|
145
|
+
)
|
|
146
|
+
},
|
|
147
|
+
sessions(signal?: AbortSignal) {
|
|
148
|
+
return call(
|
|
149
|
+
(options) => account.sessions.$get(undefined, options),
|
|
150
|
+
cloudAccountSessionsResponseSchema,
|
|
151
|
+
signal
|
|
152
|
+
)
|
|
153
|
+
},
|
|
154
|
+
revokeSession(sessionId: string, signal?: AbortSignal) {
|
|
155
|
+
return call(
|
|
156
|
+
(options) => account.sessions.revoke.$post({ json: { sessionId } }, options),
|
|
157
|
+
cloudAccountMutationResponseSchema,
|
|
158
|
+
signal
|
|
159
|
+
)
|
|
160
|
+
},
|
|
161
|
+
revokeOtherSessions(signal?: AbortSignal) {
|
|
162
|
+
return call(
|
|
163
|
+
(options) => account.sessions['revoke-others'].$post(undefined, options),
|
|
164
|
+
cloudAccountMutationResponseSchema,
|
|
165
|
+
signal
|
|
166
|
+
)
|
|
167
|
+
},
|
|
168
|
+
signIns(signal?: AbortSignal) {
|
|
169
|
+
return call(
|
|
170
|
+
(options) => account['sign-ins'].$get(undefined, options),
|
|
171
|
+
cloudSignInsResponseSchema,
|
|
172
|
+
signal
|
|
173
|
+
)
|
|
174
|
+
},
|
|
175
|
+
unlinkAuthenticationMethod(methodId: string, signal?: AbortSignal) {
|
|
176
|
+
return call(
|
|
177
|
+
(options) => account.authentication.unlink.$post({ json: { methodId } }, options),
|
|
178
|
+
cloudAccountMutationResponseSchema,
|
|
179
|
+
signal
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
adminAuditResponseSchema,
|
|
3
|
+
adminEmailResponseSchema,
|
|
4
|
+
adminMutationResponseSchema,
|
|
5
|
+
adminOperationsResponseSchema,
|
|
6
|
+
cloudAdminSessionResponseSchema,
|
|
7
|
+
cloudAdminUserDetailSchema,
|
|
8
|
+
cloudAdminUsersResponseSchema,
|
|
9
|
+
enrollmentReviewResponseSchema,
|
|
10
|
+
enrollmentsResponseSchema,
|
|
11
|
+
type CloudAdminAuditFilter,
|
|
12
|
+
type CloudAdminEmailFilter,
|
|
13
|
+
type CloudAdminUserFilter,
|
|
14
|
+
type EnrollmentStatus
|
|
15
|
+
} from '#cloud/contract'
|
|
16
|
+
|
|
17
|
+
import type { CloudCall, PortalRouteClients } from './request'
|
|
18
|
+
|
|
19
|
+
/** A page of an admin list: words to search for, one of its filters, and where the page starts. */
|
|
20
|
+
export type AdminListRequest<Filter extends string> = {
|
|
21
|
+
search?: string
|
|
22
|
+
filter?: Filter
|
|
23
|
+
limit?: number
|
|
24
|
+
offset?: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The query string for `request`; an empty search asks for everything. */
|
|
28
|
+
function listQuery<Filter extends string>(request: AdminListRequest<Filter>) {
|
|
29
|
+
return {
|
|
30
|
+
query: {
|
|
31
|
+
search: request.search || undefined,
|
|
32
|
+
filter: request.filter,
|
|
33
|
+
limit: request.limit,
|
|
34
|
+
offset: request.offset
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createAdministrationAPI(call: CloudCall, { admin, api }: PortalRouteClients) {
|
|
40
|
+
return {
|
|
41
|
+
session(signal?: AbortSignal) {
|
|
42
|
+
return call(
|
|
43
|
+
(options) => api.session.$get(undefined, options),
|
|
44
|
+
cloudAdminSessionResponseSchema,
|
|
45
|
+
signal
|
|
46
|
+
)
|
|
47
|
+
},
|
|
48
|
+
enrollments(request: AdminListRequest<EnrollmentStatus>, signal?: AbortSignal) {
|
|
49
|
+
return call(
|
|
50
|
+
(options) => admin.enrollments.$get(listQuery(request), options),
|
|
51
|
+
enrollmentsResponseSchema,
|
|
52
|
+
signal
|
|
53
|
+
)
|
|
54
|
+
},
|
|
55
|
+
reviewEnrollment(
|
|
56
|
+
id: string,
|
|
57
|
+
action: 'approve' | 'reject' | 'revoke',
|
|
58
|
+
note?: string,
|
|
59
|
+
signal?: AbortSignal
|
|
60
|
+
) {
|
|
61
|
+
return call(
|
|
62
|
+
(options) =>
|
|
63
|
+
admin.enrollments[':id'][action].$post({ param: { id }, json: { note } }, options),
|
|
64
|
+
enrollmentReviewResponseSchema,
|
|
65
|
+
signal
|
|
66
|
+
)
|
|
67
|
+
},
|
|
68
|
+
users(request: AdminListRequest<CloudAdminUserFilter>, signal?: AbortSignal) {
|
|
69
|
+
return call(
|
|
70
|
+
(options) => admin.users.$get(listQuery(request), options),
|
|
71
|
+
cloudAdminUsersResponseSchema,
|
|
72
|
+
signal
|
|
73
|
+
)
|
|
74
|
+
},
|
|
75
|
+
user(id: string, signal?: AbortSignal) {
|
|
76
|
+
return call(
|
|
77
|
+
(options) => admin.users[':id'].$get({ param: { id } }, options),
|
|
78
|
+
cloudAdminUserDetailSchema,
|
|
79
|
+
signal
|
|
80
|
+
)
|
|
81
|
+
},
|
|
82
|
+
userAction(
|
|
83
|
+
action: 'ban' | 'unban' | 'revoke-sessions' | 'reset-two-step' | 'remove-picture',
|
|
84
|
+
userId: string,
|
|
85
|
+
reason?: string,
|
|
86
|
+
signal?: AbortSignal
|
|
87
|
+
) {
|
|
88
|
+
return call(
|
|
89
|
+
(options) => admin.users[action].$post({ json: { userId, reason } }, options),
|
|
90
|
+
adminMutationResponseSchema,
|
|
91
|
+
signal
|
|
92
|
+
)
|
|
93
|
+
},
|
|
94
|
+
setAdmin(userId: string, enabled: boolean, signal?: AbortSignal) {
|
|
95
|
+
return call(
|
|
96
|
+
(options) => admin.users['set-admin'].$post({ json: { userId, enabled } }, options),
|
|
97
|
+
adminMutationResponseSchema,
|
|
98
|
+
signal
|
|
99
|
+
)
|
|
100
|
+
},
|
|
101
|
+
email(request: AdminListRequest<CloudAdminEmailFilter>, signal?: AbortSignal) {
|
|
102
|
+
return call(
|
|
103
|
+
(options) => admin.email.$get(listQuery(request), options),
|
|
104
|
+
adminEmailResponseSchema,
|
|
105
|
+
signal
|
|
106
|
+
)
|
|
107
|
+
},
|
|
108
|
+
regenerateEmail(id: string, signal?: AbortSignal) {
|
|
109
|
+
return call(
|
|
110
|
+
(options) => admin.email[':id'].regenerate.$post({ param: { id } }, options),
|
|
111
|
+
adminMutationResponseSchema,
|
|
112
|
+
signal
|
|
113
|
+
)
|
|
114
|
+
},
|
|
115
|
+
audit(request: AdminListRequest<CloudAdminAuditFilter>, signal?: AbortSignal) {
|
|
116
|
+
return call(
|
|
117
|
+
(options) => admin.audit.$get(listQuery(request), options),
|
|
118
|
+
adminAuditResponseSchema,
|
|
119
|
+
signal
|
|
120
|
+
)
|
|
121
|
+
},
|
|
122
|
+
operations(signal?: AbortSignal) {
|
|
123
|
+
return call(
|
|
124
|
+
(options) => admin.operations.$get(undefined, options),
|
|
125
|
+
adminOperationsResponseSchema,
|
|
126
|
+
signal
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createAccountAPI } from './account'
|
|
2
|
+
import { createAdministrationAPI } from './administration'
|
|
3
|
+
import { createCloudCall, createPortalRouteClients, type CloudPortalClientOptions } from './request'
|
|
4
|
+
|
|
5
|
+
export { CloudPortalAPIError, type CloudPortalAPIErrorKind } from './request'
|
|
6
|
+
export type { CloudPortalClientOptions } from './request'
|
|
7
|
+
|
|
8
|
+
/** The account and administration routes Cloud's own web pages use, validated against the contract. */
|
|
9
|
+
export function createCloudPortalClient(options: CloudPortalClientOptions = {}) {
|
|
10
|
+
const call = createCloudCall(options)
|
|
11
|
+
const routes = createPortalRouteClients(options)
|
|
12
|
+
return {
|
|
13
|
+
...createAccountAPI(call, routes),
|
|
14
|
+
...createAdministrationAPI(call, routes)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type CloudPortalClient = ReturnType<typeof createCloudPortalClient>
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { CloudAdminRoutes } from '#cloud/admin/api/routes'
|
|
2
|
+
import type { CloudFetch } from '#cloud/client/discovery'
|
|
3
|
+
import {
|
|
4
|
+
accountSecurityErrorResponseSchema,
|
|
5
|
+
adminErrorResponseSchema,
|
|
6
|
+
type AccountSecurityErrorCode,
|
|
7
|
+
type AdminErrorCode
|
|
8
|
+
} from '#cloud/contract'
|
|
9
|
+
import type { CloudAccountRoutes } from '#cloud/server/account/routes'
|
|
10
|
+
import type { CloudAPI } from '#cloud/server/api'
|
|
11
|
+
import { hc } from 'hono/client'
|
|
12
|
+
import * as v from 'valibot'
|
|
13
|
+
|
|
14
|
+
export type CloudPortalAPIErrorKind =
|
|
15
|
+
| 'authentication-required'
|
|
16
|
+
| 'authorization-required'
|
|
17
|
+
| 'cancelled'
|
|
18
|
+
| 'domain'
|
|
19
|
+
| 'network'
|
|
20
|
+
| 'protocol'
|
|
21
|
+
| 'timeout'
|
|
22
|
+
|
|
23
|
+
export class CloudPortalAPIError extends Error {
|
|
24
|
+
override readonly name = 'CloudPortalAPIError'
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
readonly kind: CloudPortalAPIErrorKind,
|
|
28
|
+
readonly code?: AdminErrorCode | AccountSecurityErrorCode,
|
|
29
|
+
options?: ErrorOptions
|
|
30
|
+
) {
|
|
31
|
+
super(kind, options)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type CloudPortalClientOptions = {
|
|
36
|
+
baseURL?: string
|
|
37
|
+
fetch?: CloudFetch
|
|
38
|
+
timeoutMs?: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The parts of a response the portal reads; the typed Hono clients' responses have them. */
|
|
42
|
+
type PortalResponse = Pick<Response, 'ok' | 'status' | 'json'>
|
|
43
|
+
|
|
44
|
+
/** Sends one request with the call's `init` (its signal); the typed Hono clients fit this shape. */
|
|
45
|
+
type CloudSend = (options: { init: RequestInit }) => Promise<PortalResponse>
|
|
46
|
+
|
|
47
|
+
export type CloudCall = <Output>(
|
|
48
|
+
send: CloudSend,
|
|
49
|
+
schema: v.GenericSchema<unknown, Output>,
|
|
50
|
+
signal?: AbortSignal
|
|
51
|
+
) => Promise<Output>
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Sends a request and reads its answer the same way for every portal route: transport failures,
|
|
55
|
+
* missing sessions and permissions, and domain errors become `CloudPortalAPIError`, and a success
|
|
56
|
+
* body is checked against the contract.
|
|
57
|
+
*/
|
|
58
|
+
export function createCloudCall(options: CloudPortalClientOptions = {}): CloudCall {
|
|
59
|
+
const timeoutMs = options.timeoutMs ?? 10_000
|
|
60
|
+
return async (send, schema, callerSignal) => {
|
|
61
|
+
const timeout = AbortSignal.timeout(timeoutMs)
|
|
62
|
+
const signal = callerSignal ? AbortSignal.any([callerSignal, timeout]) : timeout
|
|
63
|
+
let response: PortalResponse
|
|
64
|
+
try {
|
|
65
|
+
response = await send({ init: { signal } })
|
|
66
|
+
} catch (cause) {
|
|
67
|
+
if (callerSignal?.aborted) throw new CloudPortalAPIError('cancelled', undefined, { cause })
|
|
68
|
+
if (timeout.aborted) throw new CloudPortalAPIError('timeout', undefined, { cause })
|
|
69
|
+
throw new CloudPortalAPIError('network', undefined, { cause })
|
|
70
|
+
}
|
|
71
|
+
if (response.status === 401) throw new CloudPortalAPIError('authentication-required')
|
|
72
|
+
let body: unknown
|
|
73
|
+
try {
|
|
74
|
+
body = await response.json()
|
|
75
|
+
} catch (cause) {
|
|
76
|
+
if (response.status === 403) throw new CloudPortalAPIError('authorization-required')
|
|
77
|
+
throw new CloudPortalAPIError('protocol', undefined, { cause })
|
|
78
|
+
}
|
|
79
|
+
if (!response.ok) {
|
|
80
|
+
const accountError = v.safeParse(accountSecurityErrorResponseSchema, body)
|
|
81
|
+
const adminError = v.safeParse(adminErrorResponseSchema, body)
|
|
82
|
+
let code: AdminErrorCode | AccountSecurityErrorCode | undefined
|
|
83
|
+
if (accountError.success) code = accountError.output.error.code
|
|
84
|
+
else if (adminError.success) code = adminError.output.error.code
|
|
85
|
+
// A refusal that says why, such as a session that must sign in again, is the domain's;
|
|
86
|
+
// a bare or plain `forbidden` one is a missing permission.
|
|
87
|
+
if (response.status === 403 && (!code || code === 'forbidden')) {
|
|
88
|
+
throw new CloudPortalAPIError('authorization-required')
|
|
89
|
+
}
|
|
90
|
+
throw new CloudPortalAPIError('domain', code)
|
|
91
|
+
}
|
|
92
|
+
const parsed = v.safeParse(schema, body)
|
|
93
|
+
if (!parsed.success) {
|
|
94
|
+
throw new CloudPortalAPIError('protocol', undefined, { cause: parsed.issues })
|
|
95
|
+
}
|
|
96
|
+
return parsed.output
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Typed clients for the routes the portal calls, all on the server's own address. */
|
|
101
|
+
export function createPortalRouteClients(options: CloudPortalClientOptions = {}) {
|
|
102
|
+
const origin = options.baseURL ?? globalThis.location.origin
|
|
103
|
+
const clientOptions = {
|
|
104
|
+
fetch: options.fetch,
|
|
105
|
+
headers: { Accept: 'application/json' },
|
|
106
|
+
init: { credentials: 'include' as const }
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
account: hc<CloudAccountRoutes>(origin, clientOptions).api.account,
|
|
110
|
+
admin: hc<CloudAdminRoutes>(new URL('/api/admin', origin).href, clientOptions),
|
|
111
|
+
api: hc<CloudAPI>(new URL('/api', origin).href, clientOptions)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type PortalRouteClients = ReturnType<typeof createPortalRouteClients>
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {
|
|
2
|
+
publicDocumentDownloadSchema,
|
|
3
|
+
publicProfileSchema,
|
|
4
|
+
publicProfileSettingsSchema,
|
|
5
|
+
type DocumentDownload,
|
|
6
|
+
type PublicProfile,
|
|
7
|
+
type PublicProfileSettings,
|
|
8
|
+
type UpdatePublicProfileInput
|
|
9
|
+
} from '#cloud/contract'
|
|
10
|
+
import type { CloudAPI, PublicCloudAPI } from '#cloud/server/api'
|
|
11
|
+
import type { hc } from 'hono/client'
|
|
12
|
+
import * as v from 'valibot'
|
|
13
|
+
|
|
14
|
+
import { CloudAPIError, parseResponse, type CloudClientDiagnostic } from './response'
|
|
15
|
+
|
|
16
|
+
const settingsResponseSchema = v.object({ profile: publicProfileSettingsSchema })
|
|
17
|
+
const profileResponseSchema = v.object({ profile: publicProfileSchema })
|
|
18
|
+
|
|
19
|
+
/** Public profiles: the signed-in person's own settings, and anyone's profile and documents. */
|
|
20
|
+
export function createProfileRequests(
|
|
21
|
+
client: ReturnType<typeof hc<CloudAPI>>,
|
|
22
|
+
publicClient: ReturnType<typeof hc<PublicCloudAPI>>,
|
|
23
|
+
onDiagnostic?: CloudClientDiagnostic
|
|
24
|
+
) {
|
|
25
|
+
return {
|
|
26
|
+
async getPublicProfileSettings(): Promise<PublicProfileSettings> {
|
|
27
|
+
const response = await parseResponse(
|
|
28
|
+
client.profile.$get(),
|
|
29
|
+
settingsResponseSchema,
|
|
30
|
+
onDiagnostic
|
|
31
|
+
)
|
|
32
|
+
return response.profile
|
|
33
|
+
},
|
|
34
|
+
/** Claims or changes the handle and bio; a null handle removes the public profile. */
|
|
35
|
+
async updatePublicProfile(input: UpdatePublicProfileInput): Promise<PublicProfileSettings> {
|
|
36
|
+
const response = await parseResponse(
|
|
37
|
+
client.profile.$put({ json: input }),
|
|
38
|
+
settingsResponseSchema,
|
|
39
|
+
onDiagnostic
|
|
40
|
+
)
|
|
41
|
+
return response.profile
|
|
42
|
+
},
|
|
43
|
+
/** Lists a workspace on its administrators' public profiles, or takes it off. */
|
|
44
|
+
async publishWorkspace(workspaceId: string, published: boolean) {
|
|
45
|
+
const response = await parseResponse(
|
|
46
|
+
client.workspaces[':workspaceId'].public.$put({
|
|
47
|
+
param: { workspaceId },
|
|
48
|
+
json: { public: published }
|
|
49
|
+
}),
|
|
50
|
+
settingsResponseSchema,
|
|
51
|
+
onDiagnostic
|
|
52
|
+
)
|
|
53
|
+
return response.profile
|
|
54
|
+
},
|
|
55
|
+
/** Someone's public profile, or null when no profile has the handle. */
|
|
56
|
+
async getPublicProfile(handle: string): Promise<PublicProfile | null> {
|
|
57
|
+
try {
|
|
58
|
+
const response = await parseResponse(
|
|
59
|
+
publicClient.public.profiles[':handle'].$get({ param: { handle } }),
|
|
60
|
+
profileResponseSchema,
|
|
61
|
+
onDiagnostic
|
|
62
|
+
)
|
|
63
|
+
return response.profile
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (error instanceof CloudAPIError && error.status === 404) return null
|
|
66
|
+
throw error
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
/** A document a public profile lists, as anyone may download it. */
|
|
70
|
+
async getPublicDocument(documentId: string): Promise<DocumentDownload> {
|
|
71
|
+
const response = await parseResponse(
|
|
72
|
+
publicClient.public.documents[':documentId'].document.$post({ param: { documentId } }),
|
|
73
|
+
publicDocumentDownloadSchema,
|
|
74
|
+
onDiagnostic
|
|
75
|
+
)
|
|
76
|
+
return response.document
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
const cloudErrorResponseSchema = v.object({
|
|
4
|
+
error: v.object({ code: v.string() })
|
|
5
|
+
})
|
|
6
|
+
|
|
7
|
+
/** The part of a fetch or Hono client response the client reads. */
|
|
8
|
+
export type CloudResponse = Pick<Response, 'ok' | 'status' | 'json'>
|
|
9
|
+
|
|
10
|
+
export type CloudClientDiagnostic = (message: string, error: unknown) => void
|
|
11
|
+
|
|
12
|
+
export class CloudAPIError extends Error {
|
|
13
|
+
override readonly name = 'CloudAPIError'
|
|
14
|
+
|
|
15
|
+
constructor(
|
|
16
|
+
message: string,
|
|
17
|
+
readonly status: number,
|
|
18
|
+
readonly code?: string
|
|
19
|
+
) {
|
|
20
|
+
super(message)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function responseBody(
|
|
25
|
+
response: CloudResponse,
|
|
26
|
+
onDiagnostic?: CloudClientDiagnostic
|
|
27
|
+
): Promise<unknown> {
|
|
28
|
+
if (response.ok) return response.status === 204 ? null : response.json()
|
|
29
|
+
let code: string | undefined
|
|
30
|
+
try {
|
|
31
|
+
const body = v.safeParse(cloudErrorResponseSchema, await response.json())
|
|
32
|
+
if (body.success) code = body.output.error.code
|
|
33
|
+
} catch (error) {
|
|
34
|
+
onDiagnostic?.('Cloud API error response was not JSON', error)
|
|
35
|
+
}
|
|
36
|
+
throw new CloudAPIError(
|
|
37
|
+
`Pairdraw Cloud request failed with HTTP ${response.status}`,
|
|
38
|
+
response.status,
|
|
39
|
+
code
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function parseResponse<
|
|
44
|
+
TSchema extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>
|
|
45
|
+
>(
|
|
46
|
+
request: Promise<CloudResponse>,
|
|
47
|
+
schema: TSchema,
|
|
48
|
+
onDiagnostic?: CloudClientDiagnostic
|
|
49
|
+
): Promise<v.InferOutput<TSchema>> {
|
|
50
|
+
return v.parse(schema, await responseBody(await request, onDiagnostic))
|
|
51
|
+
}
|