@open-pencil/cloud 0.17.0-beta.2 → 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.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -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
|
+
}
|
package/src/client/discovery.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
CLOUD_DISCOVERY_PATH,
|
|
3
3
|
CLOUD_PROTOCOL_VERSION,
|
|
4
4
|
cloudDiscoverySchema,
|
|
5
|
+
isEditorVersionAtLeast,
|
|
5
6
|
type CloudDiscovery
|
|
6
7
|
} from '#cloud/contract'
|
|
7
8
|
import * as v from 'valibot'
|
|
@@ -13,13 +14,21 @@ export type CloudFetch = (input: RequestInfo | URL, init?: RequestInit) => Promi
|
|
|
13
14
|
export type DiscoverCloudOptions = {
|
|
14
15
|
fetch?: CloudFetch
|
|
15
16
|
signal?: AbortSignal
|
|
17
|
+
/** The editor's own version, checked against the server's minimum. */
|
|
18
|
+
editorVersion?: string
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
22
|
* Why a server could not be used: the address is not a web address, nothing answered, something
|
|
20
|
-
* answered but not as OpenPencil Cloud,
|
|
23
|
+
* answered but not as OpenPencil Cloud, the server is older than this editor supports, or this
|
|
24
|
+
* editor is older than the server supports.
|
|
21
25
|
*/
|
|
22
|
-
export type CloudDiscoveryFailure =
|
|
26
|
+
export type CloudDiscoveryFailure =
|
|
27
|
+
| 'invalid-address'
|
|
28
|
+
| 'unreachable'
|
|
29
|
+
| 'not-cloud'
|
|
30
|
+
| 'server-outdated'
|
|
31
|
+
| 'editor-outdated'
|
|
23
32
|
|
|
24
33
|
export class CloudClientError extends Error {
|
|
25
34
|
constructor(
|
|
@@ -55,21 +64,38 @@ function discoveryURL(serverURL: string): URL {
|
|
|
55
64
|
return url
|
|
56
65
|
}
|
|
57
66
|
|
|
58
|
-
|
|
67
|
+
/** Which side is behind when the server speaks another protocol, or null when it doesn't. */
|
|
68
|
+
function protocolMismatch(version: string): CloudDiscoveryFailure | null {
|
|
69
|
+
if (version === CLOUD_PROTOCOL_VERSION) return null
|
|
70
|
+
return Number(version) > Number(CLOUD_PROTOCOL_VERSION) ? 'editor-outdated' : 'server-outdated'
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseDiscoveryBody(text: string, editorVersion: string | undefined): CloudDiscovery {
|
|
59
74
|
const body = v.safeParse(discoveryBodySchema, text)
|
|
60
75
|
if (!body.success) {
|
|
61
76
|
throw new CloudClientError('OpenPencil server discovery is not JSON', body.issues, 'not-cloud')
|
|
62
77
|
}
|
|
63
78
|
const discovery = v.safeParse(cloudDiscoverySchema, body.output)
|
|
64
|
-
if (discovery.success)
|
|
79
|
+
if (discovery.success) {
|
|
80
|
+
const minimum = discovery.output.minimumEditorVersion
|
|
81
|
+
if (minimum && editorVersion && !isEditorVersionAtLeast(editorVersion, minimum)) {
|
|
82
|
+
throw new CloudClientError(
|
|
83
|
+
`OpenPencil server needs editor ${minimum} or later`,
|
|
84
|
+
undefined,
|
|
85
|
+
'editor-outdated'
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
return discovery.output
|
|
89
|
+
}
|
|
65
90
|
const protocol = v.safeParse(protocolSchema, body.output)
|
|
66
|
-
const
|
|
91
|
+
const version = protocol.success ? protocol.output.protocolVersion : null
|
|
92
|
+
const mismatch = version === null ? null : protocolMismatch(version)
|
|
67
93
|
throw new CloudClientError(
|
|
68
|
-
|
|
69
|
-
? `OpenPencil server speaks protocol ${
|
|
94
|
+
mismatch
|
|
95
|
+
? `OpenPencil server speaks protocol ${version}`
|
|
70
96
|
: 'OpenPencil server discovery is invalid',
|
|
71
97
|
discovery.issues,
|
|
72
|
-
|
|
98
|
+
mismatch ?? 'not-cloud'
|
|
73
99
|
)
|
|
74
100
|
}
|
|
75
101
|
|
|
@@ -94,7 +120,7 @@ export async function discoverCloud(
|
|
|
94
120
|
response.status >= 500 ? 'unreachable' : 'not-cloud'
|
|
95
121
|
)
|
|
96
122
|
}
|
|
97
|
-
return parseDiscoveryBody(await response.text())
|
|
123
|
+
return parseDiscoveryBody(await response.text(), options.editorVersion)
|
|
98
124
|
} catch (error) {
|
|
99
125
|
if (error instanceof CloudClientError) throw error
|
|
100
126
|
if (signal.aborted) {
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cloudUserProfileSchema,
|
|
3
|
+
documentAccessSchema,
|
|
4
|
+
documentDownloadSchema,
|
|
5
|
+
objectUploadSchema,
|
|
6
|
+
documentSummarySchema,
|
|
7
|
+
sharedDocumentSchema,
|
|
8
|
+
trashSchema,
|
|
9
|
+
type CloudUserProfile,
|
|
10
|
+
type CollaborationTicket,
|
|
11
|
+
type CommitUploadRequest,
|
|
12
|
+
type CreateDocumentRequest,
|
|
13
|
+
type CreateUploadInput,
|
|
14
|
+
type DocumentAccess,
|
|
15
|
+
type DocumentDownload,
|
|
16
|
+
type DocumentSummary,
|
|
17
|
+
type SharedDocument,
|
|
18
|
+
type TrashedDocument
|
|
19
|
+
} from '#cloud/contract'
|
|
20
|
+
import type { CloudAPI } from '#cloud/server/api'
|
|
21
|
+
import type { hc } from 'hono/client'
|
|
22
|
+
import * as v from 'valibot'
|
|
23
|
+
|
|
24
|
+
import { parseResponse, responseBody, type CloudClientDiagnostic } from './response'
|
|
25
|
+
import { collaborationTicketResponseSchema, documentResponseSchema } from './schema'
|
|
26
|
+
|
|
27
|
+
const documentsResponseSchema = v.object({ documents: v.array(documentSummarySchema) })
|
|
28
|
+
const sharedDocumentsResponseSchema = v.object({ documents: v.array(sharedDocumentSchema) })
|
|
29
|
+
const downloadResponseSchema = v.object({ document: documentDownloadSchema })
|
|
30
|
+
const accessResponseSchema = v.object({ access: documentAccessSchema })
|
|
31
|
+
const userLookupResponseSchema = v.object({ user: v.nullable(cloudUserProfileSchema) })
|
|
32
|
+
const uploadResponseSchema = v.object({
|
|
33
|
+
id: v.pipe(v.string(), v.uuid()),
|
|
34
|
+
upload: objectUploadSchema
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
export type CloudUpload = v.InferOutput<typeof uploadResponseSchema>
|
|
38
|
+
|
|
39
|
+
/** Documents: listing and creating them, Trash, downloads, uploads, and collaboration tickets. */
|
|
40
|
+
export function createDocumentRequests(
|
|
41
|
+
client: ReturnType<typeof hc<CloudAPI>>,
|
|
42
|
+
onDiagnostic?: CloudClientDiagnostic
|
|
43
|
+
) {
|
|
44
|
+
return {
|
|
45
|
+
async getUserProfile(documentId: string, userId: string): Promise<CloudUserProfile | null> {
|
|
46
|
+
const response = await parseResponse(
|
|
47
|
+
client.documents[':documentId'].users[':userId'].$get({
|
|
48
|
+
param: { documentId, userId }
|
|
49
|
+
}),
|
|
50
|
+
userLookupResponseSchema,
|
|
51
|
+
onDiagnostic
|
|
52
|
+
)
|
|
53
|
+
return response.user
|
|
54
|
+
},
|
|
55
|
+
/** Documents shared with the person directly, outside their workspaces. */
|
|
56
|
+
async listSharedDocuments(): Promise<SharedDocument[]> {
|
|
57
|
+
const response = await parseResponse(
|
|
58
|
+
client['shared-documents'].$get(),
|
|
59
|
+
sharedDocumentsResponseSchema,
|
|
60
|
+
onDiagnostic
|
|
61
|
+
)
|
|
62
|
+
return response.documents
|
|
63
|
+
},
|
|
64
|
+
async listDocuments(workspaceId: string): Promise<DocumentSummary[]> {
|
|
65
|
+
const response = await parseResponse(
|
|
66
|
+
client.workspaces[':workspaceId'].documents.$get({
|
|
67
|
+
param: { workspaceId }
|
|
68
|
+
}),
|
|
69
|
+
documentsResponseSchema,
|
|
70
|
+
onDiagnostic
|
|
71
|
+
)
|
|
72
|
+
return response.documents
|
|
73
|
+
},
|
|
74
|
+
async createDocument(
|
|
75
|
+
workspaceId: string,
|
|
76
|
+
input: CreateDocumentRequest
|
|
77
|
+
): Promise<DocumentSummary> {
|
|
78
|
+
const response = await parseResponse(
|
|
79
|
+
client.workspaces[':workspaceId'].documents.$post({
|
|
80
|
+
param: { workspaceId },
|
|
81
|
+
json: { ...input, kind: input.kind ?? 'design' }
|
|
82
|
+
}),
|
|
83
|
+
documentResponseSchema,
|
|
84
|
+
onDiagnostic
|
|
85
|
+
)
|
|
86
|
+
return response.document
|
|
87
|
+
},
|
|
88
|
+
/** Documents in Trash the person may restore, across their workspaces, newest first. */
|
|
89
|
+
async listTrash(): Promise<TrashedDocument[]> {
|
|
90
|
+
const response = await parseResponse(client.trash.$get(), trashSchema, onDiagnostic)
|
|
91
|
+
return response.documents
|
|
92
|
+
},
|
|
93
|
+
async restoreDocument(documentId: string): Promise<void> {
|
|
94
|
+
await responseBody(
|
|
95
|
+
await client.trash[':documentId'].restore.$post({ param: { documentId } }),
|
|
96
|
+
onDiagnostic
|
|
97
|
+
)
|
|
98
|
+
},
|
|
99
|
+
/** Deletes a document in Trash for good. */
|
|
100
|
+
async purgeDocument(documentId: string): Promise<void> {
|
|
101
|
+
await responseBody(
|
|
102
|
+
await client.trash[':documentId'].$delete({ param: { documentId } }),
|
|
103
|
+
onDiagnostic
|
|
104
|
+
)
|
|
105
|
+
},
|
|
106
|
+
async deleteDocument(documentId: string): Promise<void> {
|
|
107
|
+
await responseBody(
|
|
108
|
+
await client.documents[':documentId'].$delete({ param: { documentId } }),
|
|
109
|
+
onDiagnostic
|
|
110
|
+
)
|
|
111
|
+
},
|
|
112
|
+
async getDocument(documentId: string): Promise<DocumentDownload> {
|
|
113
|
+
const response = await parseResponse(
|
|
114
|
+
client.documents[':documentId'].$get({ param: { documentId } }),
|
|
115
|
+
downloadResponseSchema,
|
|
116
|
+
onDiagnostic
|
|
117
|
+
)
|
|
118
|
+
return response.document
|
|
119
|
+
},
|
|
120
|
+
async getDocumentAccess(documentId: string): Promise<DocumentAccess> {
|
|
121
|
+
const response = await parseResponse(
|
|
122
|
+
client.documents[':documentId'].access.$get({ param: { documentId } }),
|
|
123
|
+
accessResponseSchema,
|
|
124
|
+
onDiagnostic
|
|
125
|
+
)
|
|
126
|
+
return response.access
|
|
127
|
+
},
|
|
128
|
+
async getCollaborationTicket(documentId: string): Promise<CollaborationTicket> {
|
|
129
|
+
const response = await parseResponse(
|
|
130
|
+
client.documents[':documentId']['collaboration-ticket'].$post({
|
|
131
|
+
param: { documentId }
|
|
132
|
+
}),
|
|
133
|
+
collaborationTicketResponseSchema,
|
|
134
|
+
onDiagnostic
|
|
135
|
+
)
|
|
136
|
+
return response.ticket
|
|
137
|
+
},
|
|
138
|
+
async createUpload(documentId: string, input: CreateUploadInput): Promise<CloudUpload> {
|
|
139
|
+
return parseResponse(
|
|
140
|
+
client.documents[':documentId'].uploads.$post({
|
|
141
|
+
param: { documentId },
|
|
142
|
+
json: input
|
|
143
|
+
}),
|
|
144
|
+
uploadResponseSchema,
|
|
145
|
+
onDiagnostic
|
|
146
|
+
)
|
|
147
|
+
},
|
|
148
|
+
async commitUpload(uploadId: string, input: CommitUploadRequest): Promise<DocumentSummary> {
|
|
149
|
+
const response = await parseResponse(
|
|
150
|
+
client.uploads[':uploadId'].commit.$post({
|
|
151
|
+
param: { uploadId },
|
|
152
|
+
json: { ...input, images: input.images ?? [] }
|
|
153
|
+
}),
|
|
154
|
+
documentResponseSchema,
|
|
155
|
+
onDiagnostic
|
|
156
|
+
)
|
|
157
|
+
return response.document
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
package/src/client/index.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
|
|
2
|
+
CloudServiceError,
|
|
3
|
+
CloudServiceInsecureEndpointError,
|
|
4
|
+
cloudServicesFetch,
|
|
5
|
+
createCloudServicesClient,
|
|
6
|
+
type CloudServicesClient,
|
|
7
|
+
type CloudServicesClientOptions,
|
|
8
|
+
type CloudServicesTokenSource
|
|
9
|
+
} from './services'
|
|
10
|
+
export {
|
|
11
|
+
CloudBillingError,
|
|
12
|
+
createCloudBillingClient,
|
|
13
|
+
type CloudBillingClient,
|
|
14
|
+
type CloudBillingClientOptions
|
|
15
|
+
} from './billing'
|
|
9
16
|
export { cloudEditorReturnURL, cloudRedirectPath, cloudSignInURL } from './auth/navigation'
|
|
10
17
|
export {
|
|
11
18
|
CloudAPIError,
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
+
accountEntitlementsResponseSchema,
|
|
3
|
+
billingTokenSchema,
|
|
4
|
+
cloudAccessTokenCreatedResponseSchema,
|
|
5
|
+
cloudAccessTokensResponseSchema,
|
|
6
|
+
cloudAccountDeletionResponseSchema,
|
|
2
7
|
cloudAccountMutationResponseSchema,
|
|
3
8
|
cloudAccountSessionsResponseSchema,
|
|
4
9
|
cloudAccountStatusResponseSchema,
|
|
@@ -11,13 +16,30 @@ import {
|
|
|
11
16
|
cloudPictureResponseSchema,
|
|
12
17
|
cloudProfileResponseSchema,
|
|
13
18
|
cloudSignInsResponseSchema,
|
|
14
|
-
cloudSocialLinkResponseSchema
|
|
19
|
+
cloudSocialLinkResponseSchema,
|
|
20
|
+
type CloudAccessTokenCreate
|
|
15
21
|
} from '#cloud/contract'
|
|
16
22
|
|
|
17
23
|
import type { CloudCall, PortalRouteClients } from './request'
|
|
18
24
|
|
|
19
|
-
export function createAccountAPI(call: CloudCall, { account }: PortalRouteClients) {
|
|
25
|
+
export function createAccountAPI(call: CloudCall, { account, tokens, api }: PortalRouteClients) {
|
|
20
26
|
return {
|
|
27
|
+
/** The account's plan, and the storage its workspaces hold together. */
|
|
28
|
+
entitlements(signal?: AbortSignal) {
|
|
29
|
+
return call(
|
|
30
|
+
(options) => api.account.entitlements.$get(undefined, options),
|
|
31
|
+
accountEntitlementsResponseSchema,
|
|
32
|
+
signal
|
|
33
|
+
)
|
|
34
|
+
},
|
|
35
|
+
/** A short-lived token for the deployment's billing, for this account. */
|
|
36
|
+
billingToken(signal?: AbortSignal) {
|
|
37
|
+
return call(
|
|
38
|
+
(options) => api.billing.token.$post(undefined, options),
|
|
39
|
+
billingTokenSchema,
|
|
40
|
+
signal
|
|
41
|
+
)
|
|
42
|
+
},
|
|
21
43
|
accountStatus(signal?: AbortSignal) {
|
|
22
44
|
return call(
|
|
23
45
|
(options) => account.status.$get(undefined, options),
|
|
@@ -25,6 +47,14 @@ export function createAccountAPI(call: CloudCall, { account }: PortalRouteClient
|
|
|
25
47
|
signal
|
|
26
48
|
)
|
|
27
49
|
},
|
|
50
|
+
/** Accepts the server's terms at `version`, the one its discovery names. */
|
|
51
|
+
acceptTerms(version: string, signal?: AbortSignal) {
|
|
52
|
+
return call(
|
|
53
|
+
(options) => account.terms.$post({ json: { version } }, options),
|
|
54
|
+
cloudAccountMutationResponseSchema,
|
|
55
|
+
signal
|
|
56
|
+
)
|
|
57
|
+
},
|
|
28
58
|
authenticationMethods(signal?: AbortSignal) {
|
|
29
59
|
return call(
|
|
30
60
|
(options) => account.authentication.$get(undefined, options),
|
|
@@ -114,6 +144,14 @@ export function createAccountAPI(call: CloudCall, { account }: PortalRouteClient
|
|
|
114
144
|
signal
|
|
115
145
|
)
|
|
116
146
|
},
|
|
147
|
+
/** The app's language tag; the server writes to the account in it, or the nearest it has. */
|
|
148
|
+
setLocale(locale: string, signal?: AbortSignal) {
|
|
149
|
+
return call(
|
|
150
|
+
(options) => account.locale.$put({ json: { locale } }, options),
|
|
151
|
+
cloudAccountMutationResponseSchema,
|
|
152
|
+
signal
|
|
153
|
+
)
|
|
154
|
+
},
|
|
117
155
|
updateProfile(name: string, signal?: AbortSignal) {
|
|
118
156
|
return call(
|
|
119
157
|
(options) => account.profile.$post({ json: { name } }, options),
|
|
@@ -144,6 +182,29 @@ export function createAccountAPI(call: CloudCall, { account }: PortalRouteClient
|
|
|
144
182
|
signal
|
|
145
183
|
)
|
|
146
184
|
},
|
|
185
|
+
/** The account's personal access tokens, by their first characters; never a whole one. */
|
|
186
|
+
accessTokens(signal?: AbortSignal) {
|
|
187
|
+
return call(
|
|
188
|
+
(options) => tokens.$get(undefined, options),
|
|
189
|
+
cloudAccessTokensResponseSchema,
|
|
190
|
+
signal
|
|
191
|
+
)
|
|
192
|
+
},
|
|
193
|
+
/** Makes a token; the answer holds the whole token, which is shown this once. */
|
|
194
|
+
createAccessToken(input: CloudAccessTokenCreate, signal?: AbortSignal) {
|
|
195
|
+
return call(
|
|
196
|
+
(options) => tokens.create.$post({ json: input }, options),
|
|
197
|
+
cloudAccessTokenCreatedResponseSchema,
|
|
198
|
+
signal
|
|
199
|
+
)
|
|
200
|
+
},
|
|
201
|
+
revokeAccessToken(id: string, signal?: AbortSignal) {
|
|
202
|
+
return call(
|
|
203
|
+
(options) => tokens.revoke.$post({ json: { id } }, options),
|
|
204
|
+
cloudAccountMutationResponseSchema,
|
|
205
|
+
signal
|
|
206
|
+
)
|
|
207
|
+
},
|
|
147
208
|
sessions(signal?: AbortSignal) {
|
|
148
209
|
return call(
|
|
149
210
|
(options) => account.sessions.$get(undefined, options),
|
|
@@ -172,6 +233,22 @@ export function createAccountAPI(call: CloudCall, { account }: PortalRouteClient
|
|
|
172
233
|
signal
|
|
173
234
|
)
|
|
174
235
|
},
|
|
236
|
+
/** What deleting the account takes, and the workspaces that stop it. */
|
|
237
|
+
accountDeletion(signal?: AbortSignal) {
|
|
238
|
+
return call(
|
|
239
|
+
(options) => account.deletion.$get(undefined, options),
|
|
240
|
+
cloudAccountDeletionResponseSchema,
|
|
241
|
+
signal
|
|
242
|
+
)
|
|
243
|
+
},
|
|
244
|
+
/** Deletes the account; a session that signed in a while ago gives its password. */
|
|
245
|
+
deleteAccount(password?: string, signal?: AbortSignal) {
|
|
246
|
+
return call(
|
|
247
|
+
(options) => account.delete.$post({ json: { password } }, options),
|
|
248
|
+
cloudAccountMutationResponseSchema,
|
|
249
|
+
signal
|
|
250
|
+
)
|
|
251
|
+
},
|
|
175
252
|
unlinkAuthenticationMethod(methodId: string, signal?: AbortSignal) {
|
|
176
253
|
return call(
|
|
177
254
|
(options) => account.authentication.unlink.$post({ json: { methodId } }, options),
|
|
@@ -3,12 +3,16 @@ import {
|
|
|
3
3
|
adminEmailResponseSchema,
|
|
4
4
|
adminMutationResponseSchema,
|
|
5
5
|
adminOperationsResponseSchema,
|
|
6
|
+
cloudAdminDocumentsResponseSchema,
|
|
7
|
+
cloudAdminReportsResponseSchema,
|
|
6
8
|
cloudAdminSessionResponseSchema,
|
|
7
9
|
cloudAdminUserDetailSchema,
|
|
8
10
|
cloudAdminUsersResponseSchema,
|
|
9
11
|
enrollmentReviewResponseSchema,
|
|
10
12
|
enrollmentsResponseSchema,
|
|
11
13
|
type CloudAdminAuditFilter,
|
|
14
|
+
type CloudAdminDocumentFilter,
|
|
15
|
+
type CloudAdminReportFilter,
|
|
12
16
|
type CloudAdminEmailFilter,
|
|
13
17
|
type CloudAdminUserFilter,
|
|
14
18
|
type EnrollmentStatus
|
|
@@ -119,6 +123,58 @@ export function createAdministrationAPI(call: CloudCall, { admin, api }: PortalR
|
|
|
119
123
|
signal
|
|
120
124
|
)
|
|
121
125
|
},
|
|
126
|
+
reports(request: AdminListRequest<CloudAdminReportFilter>, signal?: AbortSignal) {
|
|
127
|
+
return call(
|
|
128
|
+
(options) => admin.reports.$get(listQuery(request), options),
|
|
129
|
+
cloudAdminReportsResponseSchema,
|
|
130
|
+
signal
|
|
131
|
+
)
|
|
132
|
+
},
|
|
133
|
+
/** Closes a report: dismissed, or acted on outside a takedown. */
|
|
134
|
+
resolveReport(id: string, action: 'dismiss' | 'action', signal?: AbortSignal) {
|
|
135
|
+
return call(
|
|
136
|
+
(options) => admin.reports[':id'][action].$post({ param: { id } }, options),
|
|
137
|
+
adminMutationResponseSchema,
|
|
138
|
+
signal
|
|
139
|
+
)
|
|
140
|
+
},
|
|
141
|
+
documents(request: AdminListRequest<CloudAdminDocumentFilter>, signal?: AbortSignal) {
|
|
142
|
+
return call(
|
|
143
|
+
(options) => admin.documents.$get(listQuery(request), options),
|
|
144
|
+
cloudAdminDocumentsResponseSchema,
|
|
145
|
+
signal
|
|
146
|
+
)
|
|
147
|
+
},
|
|
148
|
+
/** Takes a document down, or puts it back. */
|
|
149
|
+
documentAction(
|
|
150
|
+
id: string,
|
|
151
|
+
action: 'disable' | 'enable' | 'undelete',
|
|
152
|
+
reason?: string,
|
|
153
|
+
signal?: AbortSignal
|
|
154
|
+
) {
|
|
155
|
+
return call(
|
|
156
|
+
(options) => {
|
|
157
|
+
if (action === 'disable') {
|
|
158
|
+
return admin.documents[':id'].disable.$post(
|
|
159
|
+
{ param: { id }, json: { reason } },
|
|
160
|
+
options
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
return action === 'enable'
|
|
164
|
+
? admin.documents[':id'].enable.$post({ param: { id } }, options)
|
|
165
|
+
: admin.documents[':id'].undelete.$post({ param: { id } }, options)
|
|
166
|
+
},
|
|
167
|
+
adminMutationResponseSchema,
|
|
168
|
+
signal
|
|
169
|
+
)
|
|
170
|
+
},
|
|
171
|
+
revokeShare(id: string, signal?: AbortSignal) {
|
|
172
|
+
return call(
|
|
173
|
+
(options) => admin.shares[':id'].revoke.$post({ param: { id } }, options),
|
|
174
|
+
adminMutationResponseSchema,
|
|
175
|
+
signal
|
|
176
|
+
)
|
|
177
|
+
},
|
|
122
178
|
operations(signal?: AbortSignal) {
|
|
123
179
|
return call(
|
|
124
180
|
(options) => admin.operations.$get(undefined, options),
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
type AdminErrorCode
|
|
8
8
|
} from '#cloud/contract'
|
|
9
9
|
import type { CloudAccountRoutes } from '#cloud/server/account/routes'
|
|
10
|
+
import type { CloudAccessTokenRoutes } from '#cloud/server/account/tokens'
|
|
10
11
|
import type { CloudAPI } from '#cloud/server/api'
|
|
11
12
|
import { hc } from 'hono/client'
|
|
12
13
|
import * as v from 'valibot'
|
|
@@ -107,6 +108,7 @@ export function createPortalRouteClients(options: CloudPortalClientOptions = {})
|
|
|
107
108
|
}
|
|
108
109
|
return {
|
|
109
110
|
account: hc<CloudAccountRoutes>(origin, clientOptions).api.account,
|
|
111
|
+
tokens: hc<CloudAccessTokenRoutes>(origin, clientOptions).api.account.tokens,
|
|
110
112
|
admin: hc<CloudAdminRoutes>(new URL('/api/admin', origin).href, clientOptions),
|
|
111
113
|
api: hc<CloudAPI>(new URL('/api', origin).href, clientOptions)
|
|
112
114
|
}
|
package/src/client/response.ts
CHANGED
|
@@ -49,3 +49,24 @@ export async function parseResponse<
|
|
|
49
49
|
): Promise<v.InferOutput<TSchema>> {
|
|
50
50
|
return v.parse(schema, await responseBody(await request, onDiagnostic))
|
|
51
51
|
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The JSON a service deployed apart from Cloud answered, parsed with `schema` rather than trusted;
|
|
55
|
+
* a refusal throws what `refused` makes of its status and body.
|
|
56
|
+
*/
|
|
57
|
+
export async function serviceJSON<TSchema extends v.GenericSchema>(
|
|
58
|
+
response: Pick<Response, 'ok' | 'status' | 'text'>,
|
|
59
|
+
schema: TSchema,
|
|
60
|
+
refused: (status: number, body: string) => Error
|
|
61
|
+
): Promise<v.InferOutput<TSchema>> {
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
// An unreadable body still leaves the status, which callers tell refusals apart by.
|
|
64
|
+
const body = await response.text().catch((failure: unknown) => {
|
|
65
|
+
const error = refused(response.status, '')
|
|
66
|
+
error.cause = failure
|
|
67
|
+
throw error
|
|
68
|
+
})
|
|
69
|
+
throw refused(response.status, body)
|
|
70
|
+
}
|
|
71
|
+
return v.parse(v.pipe(v.string(), v.parseJson(), schema), await response.text())
|
|
72
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { collaborationTicketSchema, documentSummarySchema } from '#cloud/contract'
|
|
2
|
+
import * as v from 'valibot'
|
|
3
|
+
|
|
4
|
+
/** What the API answers with one document, as creating, restoring, or duplicating one does. */
|
|
5
|
+
export const documentResponseSchema = v.object({ document: documentSummarySchema })
|
|
6
|
+
|
|
7
|
+
/** What the API answers with a ticket into a document's live room. */
|
|
8
|
+
export const collaborationTicketResponseSchema = v.object({ ticket: collaborationTicketSchema })
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { CloudFetch } from '#cloud/client/discovery'
|
|
2
|
+
import { serviceJSON } from '#cloud/client/response'
|
|
3
|
+
import {
|
|
4
|
+
serviceCatalogSchema,
|
|
5
|
+
serviceErrorSchema,
|
|
6
|
+
creditWalletSchema,
|
|
7
|
+
searchResponseSchema,
|
|
8
|
+
CLOUD_SERVICES_API_PATHS,
|
|
9
|
+
sendsTokenSafely,
|
|
10
|
+
type ServiceCatalog,
|
|
11
|
+
type ServiceErrorCode,
|
|
12
|
+
type CreditWallet,
|
|
13
|
+
type SearchRequestInput,
|
|
14
|
+
type SearchResponse,
|
|
15
|
+
type CloudServicesAPI
|
|
16
|
+
} from '#cloud/contract'
|
|
17
|
+
import { hc } from 'hono/client'
|
|
18
|
+
import { joinURL, withoutTrailingSlash } from 'ufo'
|
|
19
|
+
import * as v from 'valibot'
|
|
20
|
+
|
|
21
|
+
/** Where a request gets the token it sends, and how it drops one the service refused. */
|
|
22
|
+
export type CloudServicesTokenSource = {
|
|
23
|
+
token(): Promise<string>
|
|
24
|
+
/** Forgets `token`, so the next call signs a new one. */
|
|
25
|
+
invalidate(token: string): void
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class CloudServiceError extends Error {
|
|
29
|
+
override readonly name = 'CloudServiceError'
|
|
30
|
+
|
|
31
|
+
constructor(
|
|
32
|
+
readonly status: number,
|
|
33
|
+
readonly code?: ServiceErrorCode
|
|
34
|
+
) {
|
|
35
|
+
super(`OpenPencil services request failed with HTTP ${status}`)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A service endpoint a token would reach in the clear, which no request is sent to. */
|
|
40
|
+
export class CloudServiceInsecureEndpointError extends Error {
|
|
41
|
+
override readonly name = 'CloudServiceInsecureEndpointError'
|
|
42
|
+
|
|
43
|
+
constructor(readonly url: string) {
|
|
44
|
+
super('OpenPencil services and billing are reached only over HTTPS')
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Whether a request can be sent twice: a stream, in `init` or a `Request`, is read by the first. */
|
|
49
|
+
function replayable(input: RequestInfo | URL, body: RequestInit['body']): boolean {
|
|
50
|
+
if (input instanceof Request && input.body) return false
|
|
51
|
+
return !(body instanceof ReadableStream)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Sends each request with a current token. A token the service refuses, for instance one signed
|
|
56
|
+
* by a key Cloud has since replaced, is dropped and a request that can be replayed is sent once
|
|
57
|
+
* more with a new one.
|
|
58
|
+
*/
|
|
59
|
+
export function cloudServicesFetch(
|
|
60
|
+
tokens: CloudServicesTokenSource,
|
|
61
|
+
fetch: CloudFetch
|
|
62
|
+
): CloudFetch {
|
|
63
|
+
const send = async (input: RequestInfo | URL, init: RequestInit | undefined, token: string) => {
|
|
64
|
+
const headers = new Headers(init?.headers)
|
|
65
|
+
headers.set('Authorization', `Bearer ${token}`)
|
|
66
|
+
return fetch(input, { ...init, headers })
|
|
67
|
+
}
|
|
68
|
+
return async (input, init) => {
|
|
69
|
+
const url = input instanceof Request ? input.url : input
|
|
70
|
+
// Refused before a token is even signed, so none is sent where the network could read it.
|
|
71
|
+
if (!sendsTokenSafely(url)) throw new CloudServiceInsecureEndpointError(String(url))
|
|
72
|
+
const token = await tokens.token()
|
|
73
|
+
const response = await send(input, init, token)
|
|
74
|
+
if (response.status !== 401) return response
|
|
75
|
+
tokens.invalidate(token)
|
|
76
|
+
if (!replayable(input, init?.body)) return response
|
|
77
|
+
return send(input, init, await tokens.token())
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const errorBodySchema = v.pipe(v.string(), v.parseJson(), serviceErrorSchema)
|
|
82
|
+
|
|
83
|
+
function refused(status: number, body: string): CloudServiceError {
|
|
84
|
+
const error = v.safeParse(errorBodySchema, body)
|
|
85
|
+
return new CloudServiceError(status, error.success ? error.output.error.code : undefined)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type CloudServicesClientOptions = {
|
|
89
|
+
/** The service's endpoint, as Cloud's discovery names it. */
|
|
90
|
+
endpoint: string
|
|
91
|
+
tokens: CloudServicesTokenSource
|
|
92
|
+
fetch: CloudFetch
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The services a Cloud advertises: the model API base URL, catalog, wallet, and search. */
|
|
96
|
+
export function createCloudServicesClient(options: CloudServicesClientOptions) {
|
|
97
|
+
const endpoint = withoutTrailingSlash(options.endpoint)
|
|
98
|
+
const fetch = cloudServicesFetch(options.tokens, options.fetch)
|
|
99
|
+
// Answers are parsed rather than trusted: the service is deployed apart from this client.
|
|
100
|
+
const { v1 } = hc<CloudServicesAPI>(endpoint, { fetch })
|
|
101
|
+
return {
|
|
102
|
+
/** The base URL an OpenAI-compatible client sends chat completions below. */
|
|
103
|
+
modelBaseURL: joinURL(endpoint, CLOUD_SERVICES_API_PATHS.base),
|
|
104
|
+
fetch,
|
|
105
|
+
async catalog(): Promise<ServiceCatalog> {
|
|
106
|
+
return serviceJSON(await v1.catalog.$get(), serviceCatalogSchema, refused)
|
|
107
|
+
},
|
|
108
|
+
async wallet(): Promise<CreditWallet> {
|
|
109
|
+
return serviceJSON(await v1.wallet.$get(), creditWalletSchema, refused)
|
|
110
|
+
},
|
|
111
|
+
/** A web search, charged to the workspace; only when the catalog lists `search`. */
|
|
112
|
+
async search(input: SearchRequestInput): Promise<SearchResponse> {
|
|
113
|
+
return serviceJSON(await v1.search.$post({ json: input }), searchResponseSchema, refused)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type CloudServicesClient = ReturnType<typeof createCloudServicesClient>
|