pr_01m47d15m3e54sn21z27rpy5n9/packages/contracts/src/clients.ts
| 1 | import type { ActionsApi } from "./actions"; |
| 2 | import type { BillingAdminApi, BillingApi } from "./billing"; |
| 3 | import type { DeploymentsApi } from "./deployments"; |
| 4 | import type { ProjectsApi } from "./projects"; |
| 5 | import type { EventsApi } from "./events"; |
| 6 | import type { IdentityAdminApi, IdentityApi } from "./identity"; |
| 7 | import type { IntegrationsApi } from "./integrations"; |
| 8 | import type { WebhooksApi } from "./webhooks"; |
| 9 | import type { ReposApi } from "./repos"; |
| 10 | import type { WorkApi } from "./work"; |
| 11 | |
| 12 | /** A service binding, as far as these clients need it. */ |
| 13 | export type ServiceBinding = { |
| 14 | fetch(input: string, init?: RequestInit): Promise<Response>; |
| 15 | }; |
| 16 | |
| 17 | /** |
| 18 | * Calls a method on a service that speaks the JSON protocol used by the |
| 19 | * Rust services: `POST /rpc/<method>` with the arguments as the body. |
| 20 | */ |
| 21 | async function rpc<T>( |
| 22 | service: ServiceBinding, |
| 23 | method: string, |
| 24 | args: object, |
| 25 | ): Promise<T> { |
| 26 | // The hostname is ignored; a service binding always reaches its service. |
| 27 | const response = await service.fetch(`https://service/rpc/${method}`, { |
| 28 | method: "POST", |
| 29 | headers: { "content-type": "application/json" }, |
| 30 | body: JSON.stringify(args), |
| 31 | }); |
| 32 | if (!response.ok) { |
| 33 | throw new Error(`${method} failed with status ${response.status}`); |
| 34 | } |
| 35 | return (await response.json()) as T; |
| 36 | } |
| 37 | |
| 38 | export function identityClient(service: ServiceBinding): IdentityApi { |
| 39 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 40 | return { |
| 41 | register: (username, email, password, inviteCode, client) => |
| 42 | call("register", { username, email, password, invite_code: inviteCode ?? null, client: client ?? null }), |
| 43 | signIn: (username, password, client) => call("sign_in", { username, password, client: client ?? null }), |
| 44 | signOut: (sessionToken) => call("sign_out", { sessionToken }), |
| 45 | resendVerification: (user) => call("resend_verification", { user }), |
| 46 | verifyEmail: (token) => call("verify_email", { token }), |
| 47 | requestPasswordReset: (email, client) => call("request_password_reset", { email, client: client ?? null }), |
| 48 | resetPassword: (token, password) => |
| 49 | call("reset_password", { token, password }), |
| 50 | deviceStart: (clientName) => call("device_start", { clientName }), |
| 51 | deviceLookup: (userCode) => call("device_lookup", { userCode }), |
| 52 | deviceResolve: (userCode, user, approve) => |
| 53 | call("device_resolve", { userCode, user, approve }), |
| 54 | deviceClaim: (deviceCode) => call("device_claim", { deviceCode }), |
| 55 | oauthAuthorize: (user, approval) => call("oauth_authorize", { user, ...approval }), |
| 56 | oauthExchange: (code, codeVerifier, clientId, redirectUri) => |
| 57 | call("oauth_exchange", { code, codeVerifier, clientId, redirectUri }), |
| 58 | oauthRefresh: (refreshToken, clientId) => |
| 59 | call("oauth_refresh", { refreshToken, clientId }), |
| 60 | listOAuthGrants: (user) => call("list_oauth_grants", { user }), |
| 61 | revokeOAuthGrant: (user, id) => call("revoke_oauth_grant", { user, id }), |
| 62 | createWorkspace: (user, slug, name) => call("create_workspace", { user, slug, name }), |
| 63 | getWorkspace: (slug) => call("get_workspace", { slug }), |
| 64 | listMembers: (slug, viewer) => call("list_members", { slug, viewer }), |
| 65 | addMember: (actor, slug, username) => call("add_member", { actor, slug, username }), |
| 66 | removeMember: (actor, slug, username) => |
| 67 | call("remove_member", { actor, slug, username }), |
| 68 | updateWorkspace: (actor, slug, details) => |
| 69 | call("update_workspace", { actor, slug, ...details }), |
| 70 | renameWorkspace: (actor, slug, newSlug) => call("rename_workspace", { actor, slug, newSlug }), |
| 71 | checkWorkspaceRename: (actor, slug, newSlug) => |
| 72 | call("check_workspace_rename", { actor, slug, newSlug }), |
| 73 | resolveSlug: (slug) => call("resolve_slug", { slug }), |
| 74 | deleteWorkspace: (actor, slug, confirm) => call("delete_workspace", { actor, slug, confirm }), |
| 75 | checkWorkspaceDeletion: (actor, slug) => call("check_workspace_deletion", { actor, slug }), |
| 76 | setWorkspaceAvatar: (actor, slug, image) => call("set_workspace_avatar", { actor, slug, image }), |
| 77 | setUserAvatar: (user, image) => call("set_user_avatar", { user, image }), |
| 78 | listWorkspaceTokens: (slug, viewer) => call("list_workspace_tokens", { slug, viewer }), |
| 79 | createWorkspaceToken: (actor, slug, name) => |
| 80 | call("create_workspace_token", { actor, slug, name }), |
| 81 | removeWorkspaceToken: (actor, slug, id) => |
| 82 | call("remove_workspace_token", { actor, slug, id }), |
| 83 | userForSession: (sessionToken) => call("user_for_session", { sessionToken }), |
| 84 | registration: () => call("registration", {}), |
| 85 | listInvites: (user) => call("list_invites", { user }), |
| 86 | createInvite: (user, options = {}) => |
| 87 | call("create_invite", { user, email: options.email ?? null, workspace: options.workspace ?? null }), |
| 88 | revokeInvite: (user, id) => call("revoke_invite", { user, id }), |
| 89 | checkInvite: (code, client) => call("check_invite", { code, client: client ?? null }), |
| 90 | acceptInvite: (user, code) => call("accept_invite", { user, code }), |
| 91 | inviteMember: (actor, slug, email) => call("invite_member", { actor, slug, email }), |
| 92 | workspaceInvites: (slug, viewer) => call("workspace_invites", { slug, viewer }), |
| 93 | revokeWorkspaceInvite: (actor, slug, id) => call("revoke_workspace_invite", { actor, slug, id }), |
| 94 | requestAccess: (email, about, client) => call("request_access", { email, about, client: client ?? null }), |
| 95 | userForGitCredentials: (username, secret) => |
| 96 | call("user_for_git_credentials", { username, secret }), |
| 97 | userForAccessToken: (token) => call("user_for_access_token", { token }), |
| 98 | userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }), |
| 99 | userByUsername: (username) => call("user_by_username", { username }), |
| 100 | usernames: (ids) => call("usernames", { ids }), |
| 101 | profile: (username) => call("profile", { username }), |
| 102 | updateProfile: (actor, fields) => call("update_profile", { actor, ...fields }), |
| 103 | profileWorkspaces: (username, viewer, publicIn) => |
| 104 | call("profile_workspaces", { username, viewer, public: publicIn }), |
| 105 | listSshKeys: (user) => call("list_ssh_keys", { user }), |
| 106 | addSshKey: (user, title, publicKey) => |
| 107 | call("add_ssh_key", { user, title, publicKey }), |
| 108 | removeSshKey: (user, id) => call("remove_ssh_key", { user, id }), |
| 109 | listAccessTokens: (user) => call("list_access_tokens", { user }), |
| 110 | createAccessToken: (user, name, ttlSeconds) => |
| 111 | call("create_access_token", { user, name, ttlSeconds }), |
| 112 | createAgentToken: (onBehalfOf, scope, ttlSeconds) => |
| 113 | call("create_agent_token", { onBehalfOf, scope, ttlSeconds }), |
| 114 | removeAccessToken: (user, id) => call("remove_access_token", { user, id }), |
| 115 | createRunCredential: (input) => call("create_run_credential", input), |
| 116 | bindRunCredentials: (tokenHashes, runId) => call("bind_run_credentials", { tokenHashes, runId }), |
| 117 | revokeRunCredentials: (target) => call("revoke_run_credentials", target), |
| 118 | // Who has access to a repository; see access.ts. |
| 119 | repoAccess: (owner, name, viewer) => call("repo_access", { viewer, path: { namespace: owner, name } }), |
| 120 | addCollaborator: (actor, owner, name, invitee, role) => |
| 121 | call("add_collaborator", { actor, path: { namespace: owner, name }, invitee, role }), |
| 122 | setCollaboratorRole: (actor, owner, name, username, role) => |
| 123 | call("set_collaborator_role", { actor, path: { namespace: owner, name }, username, role }), |
| 124 | removeCollaborator: (actor, owner, name, username) => |
| 125 | call("remove_collaborator", { actor, path: { namespace: owner, name }, username }), |
| 126 | collaboratorPermission: (viewer, owner, name, username) => |
| 127 | call("collaborator_permission", { viewer, path: { namespace: owner, name }, username }), |
| 128 | myRepoInvitations: (user) => call("my_repo_invitations", { user }), |
| 129 | respondRepoInvitation: (user, id, accept) => call("respond_repo_invitation", { user, id, accept }), |
| 130 | revokeRepoInvitation: (actor, owner, name, id) => |
| 131 | call("revoke_repo_invitation", { actor, path: { namespace: owner, name }, id }), |
| 132 | setBasePermission: (actor, slug, base) => call("set_base_permission", { actor, slug, base_permission: base }), |
| 133 | outsideCollaborators: (viewer, slug) => call("outside_collaborators", { viewer, slug }), |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * The slug a renamed workspace has now, for a `workspace.renamed` handler: |
| 139 | * asked of identity by the workspace's id, so that renames delivered twice |
| 140 | * or out of order converge. Falls back to the event's `to`. |
| 141 | */ |
| 142 | export async function currentWorkspaceSlug( |
| 143 | identity: ServiceBinding, |
| 144 | renamed: { workspaceId: string; to: string }, |
| 145 | ): Promise<string> { |
| 146 | const names = await identityClient(identity).usernames([renamed.workspaceId]); |
| 147 | return names[renamed.workspaceId] ?? renamed.to; |
| 148 | } |
| 149 | |
| 150 | /** The slugs whose rows move to `current`: the two a rename names, less `current`. */ |
| 151 | export function staleSlugs(renamed: { from: string; to: string }, current: string): string[] { |
| 152 | return [...new Set([renamed.from, renamed.to])].filter((slug) => slug !== current); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Where a transferred repository is now, as `namespace/name`, for a |
| 157 | * `repo.transferred` handler: asked of repos by id, so transfers delivered |
| 158 | * twice or out of order converge. Falls back to the event's destination. |
| 159 | */ |
| 160 | export async function currentRepoPath( |
| 161 | repos: ServiceBinding, |
| 162 | transferred: { repoId: string; name: string; to: string }, |
| 163 | ): Promise<string> { |
| 164 | const path = await rpc<{ namespace: string; name: string } | null>(repos, "path_by_id", { id: transferred.repoId }); |
| 165 | return path ? `${path.namespace}/${path.name}` : `${transferred.to}/${transferred.name}`; |
| 166 | } |
| 167 | |
| 168 | /** The paths whose rows move to `current`: the two a transfer names, less `current`. */ |
| 169 | export function stalePaths(transferred: { name: string; from: string; to: string }, current: string): string[] { |
| 170 | return [...new Set([transferred.from, transferred.to].map((ns) => `${ns}/${transferred.name}`))].filter( |
| 171 | (path) => path !== current, |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * A repository's path change, from `repo.transferred` or `repo.renamed`, |
| 177 | * read the same way: the two paths (`namespace/name`) the event names, old |
| 178 | * then new. Null for any other event. |
| 179 | */ |
| 180 | export type RepoMove = { repoId: string; paths: [string, string] }; |
| 181 | |
| 182 | export function repoMove(event: { type: string; data: unknown }): RepoMove | null { |
| 183 | const data = event.data as Record<string, string>; |
| 184 | if (event.type === "repo.transferred") { |
| 185 | return { repoId: data.repoId!, paths: [`${data.from}/${data.name}`, `${data.to}/${data.name}`] }; |
| 186 | } |
| 187 | if (event.type === "repo.renamed") { |
| 188 | return { repoId: data.repoId!, paths: [`${data.namespace}/${data.from}`, `${data.namespace}/${data.to}`] }; |
| 189 | } |
| 190 | return null; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Where a moved repository is now, as `namespace/name`: asked of repos by |
| 195 | * id, so moves delivered twice or out of order converge. Falls back to the |
| 196 | * event's new path. |
| 197 | */ |
| 198 | export async function currentMovedPath(repos: ServiceBinding, move: RepoMove): Promise<string> { |
| 199 | const path = await rpc<{ namespace: string; name: string } | null>(repos, "path_by_id", { id: move.repoId }); |
| 200 | return path ? `${path.namespace}/${path.name}` : move.paths[1]; |
| 201 | } |
| 202 | |
| 203 | /** The paths whose rows move to `current`: the two a move names, less `current`. */ |
| 204 | export function staleMovedPaths(move: RepoMove, current: string): string[] { |
| 205 | return [...new Set(move.paths)].filter((path) => path !== current); |
| 206 | } |
| 207 | |
| 208 | /** Staff-only identity. Only sudo binds to it; see `IdentityAdminApi`. */ |
| 209 | export function identityAdminClient(service: ServiceBinding): IdentityAdminApi { |
| 210 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 211 | return { |
| 212 | workspaces: (query) => call("admin_workspaces", { query: query ?? null }), |
| 213 | workspace: (slug) => call("admin_workspace", { slug }), |
| 214 | waitlist: (query, status) => call("admin_waitlist", { query: query ?? null, status: status ?? null }), |
| 215 | decideWaitlist: (id, approve, staff) => call("admin_decide_waitlist", { id, approve, staff }), |
| 216 | invites: (query) => call("admin_invites", { query: query ?? null }), |
| 217 | revokeInvite: (id, staff) => call("admin_revoke_invite", { id, staff }), |
| 218 | mintInvite: (email, staff) => call("admin_mint_invite", { email, staff }), |
| 219 | grantInvites: (target, name, amount, note, staff) => |
| 220 | call("admin_grant_invites", { target, name, amount, note, staff }), |
| 221 | inviteTree: (username) => call("admin_invite_tree", { username }), |
| 222 | workspaceInvites: (slug) => call("admin_workspace_invites", { slug }), |
| 223 | }; |
| 224 | } |
| 225 | |
| 226 | export function reposClient(service: ServiceBinding): ReposApi { |
| 227 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 228 | return { |
| 229 | get: (path, viewer) => call("get", { path, viewer }), |
| 230 | getById: (id, viewer) => call("get_by_id", { id, viewer }), |
| 231 | publicNamespaces: (ownerId) => call("public_namespaces", { ownerId }), |
| 232 | list: (viewer, options = {}) => call("list", { viewer, ...options }), |
| 233 | create: (owner, input) => call("create", { owner, ...input }), |
| 234 | update: (actor, path, changes) => call("update", { actor, path, ...changes }), |
| 235 | transfer: (actor, path, to) => call("transfer", { actor, path, to }), |
| 236 | delete: (actor, path, confirm) => call("delete", { actor, path, confirm }), |
| 237 | deleted: (viewer, namespace) => call("deleted", { viewer, namespace }), |
| 238 | restore: (actor, path) => call("restore", { actor, path }), |
| 239 | purge: (actor, path, confirm) => call("purge", { actor, path, confirm }), |
| 240 | rename: (actor, path, name) => call("rename", { actor, path, name }), |
| 241 | archive: (actor, path, archived) => call("archive", { actor, path, archived }), |
| 242 | setVisibility: (actor, path, isPrivate, confirm) => call("set_visibility", { actor, path, isPrivate, confirm }), |
| 243 | setDefaultBranch: (actor, path, branch) => call("set_default_branch", { actor, path, branch }), |
| 244 | renameBranch: (actor, path, from, to) => call("rename_branch", { actor, path, from, to }), |
| 245 | resolveBranch: (repoId, branch) => call("resolve_branch", { repoId, branch }), |
| 246 | statusById: (id) => call("status_by_id", { id }), |
| 247 | resolvePath: (path) => call("resolve_path", { path }), |
| 248 | tree: (path, viewer, ref, treePath) => |
| 249 | call("tree", { path, viewer, ref, treePath }), |
| 250 | blob: (path, viewer, ref, filePath) => |
| 251 | call("blob", { path, viewer, ref, filePath }), |
| 252 | log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }), |
| 253 | blame: (path, viewer, ref, filePath) => call("blame", { path, viewer, ref, filePath }), |
| 254 | forkForPull: (sourceId, pullId, actor) => |
| 255 | call("fork_for_pull", { sourceId, pullId, actor }), |
| 256 | gitAccess: (path, viewer, service) => |
| 257 | call("git_access", { path, viewer, service }), |
| 258 | branches: (path, viewer) => call("branches", { path, viewer }), |
| 259 | land: (sourceId, actor, branch) => call("land", { sourceId, actor, branch }), |
| 260 | compare: (repoId, viewer, base, head) => call("compare", { repoId, viewer, base, head }), |
| 261 | }; |
| 262 | } |
| 263 | |
| 264 | export function workClient(service: ServiceBinding): WorkApi { |
| 265 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 266 | return { |
| 267 | openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }), |
| 268 | listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }), |
| 269 | getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }), |
| 270 | updateIssue: (actor, repo, number, input) => |
| 271 | call("update_issue", { actor, repo, number, ...input }), |
| 272 | closeIssue: (actor, repo, number, reason) => |
| 273 | call("close_issue", { actor, repo, number, reason }), |
| 274 | reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }), |
| 275 | listLabels: (repo, viewer) => call("list_labels", { repo, viewer }), |
| 276 | counts: (repo, viewer) => call("counts", { repo, viewer }), |
| 277 | addComment: (actor, repo, number, comment) => |
| 278 | call("add_comment", { actor, repo, number, ...comment }), |
| 279 | startChecks: (pullId) => call("start_checks", { pullId }), |
| 280 | reportChecks: (runId, token, report) => |
| 281 | call("report_checks", { runId, token, ...report }), |
| 282 | startReview: (pullId) => call("start_review", { pullId }), |
| 283 | failReview: (runId, token, error) => call("report_review", { runId, token, error }), |
| 284 | startMergecheck: (pullId) => call("start_mergecheck", { pullId }), |
| 285 | failMergecheck: (pullId, token, error) => call("report_mergecheck", { pullId, token, error }), |
| 286 | advance: (pullId) => call("advance", { pullId }), |
| 287 | stall: (pullId, reason) => call("stall", { pullId, reason }), |
| 288 | managedPulls: (repoId) => call("managed_pulls", { repoId }), |
| 289 | queue: (repo, viewer) => call("queue", { repo, viewer }), |
| 290 | queueBuild: (repoId) => call("queue_build", { repoId }), |
| 291 | failQueue: (entryId, token, error) => call("report_queue", { entryId, token, error }), |
| 292 | removeFromQueue: (actor, repo, number) => call("remove_from_queue", { actor, repo, number }), |
| 293 | messageAgent: (actor, repo, number, body) => call("message_agent", { actor, repo, number, body }), |
| 294 | catchUpJob: (pullId) => call("catch_up_job", { pullId }), |
| 295 | wakeForMessages: (pullId) => call("wake_for_messages", { pullId }), |
| 296 | getSettings: (repo, viewer) => call("get_settings", { repo, viewer }), |
| 297 | updateSettings: (actor, repo, settings) => |
| 298 | call("update_settings", { actor, repo, settings }), |
| 299 | openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }), |
| 300 | listPulls: (repo, viewer, state) => call("list_pulls", { repo, viewer, state }), |
| 301 | getPull: (repo, number, viewer) => call("get_pull", { repo, number, viewer }), |
| 302 | updatePull: (actor, repo, number, changes) => |
| 303 | call("update_pull", { actor, repo, number, ...changes }), |
| 304 | catchUpPull: (actor, repo, number) => call("catch_up_pull", { actor, repo, number }), |
| 305 | readyPull: (actor, repo, number, summary) => |
| 306 | call("ready_pull", { actor, repo, number, summary }), |
| 307 | closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }), |
| 308 | mergePull: (actor, repo, number, options = {}) => |
| 309 | call("merge_pull", { actor, repo, number, ...options }), |
| 310 | listActivePulls: (viewer) => call("list_active_pulls", { viewer }), |
| 311 | byAuthor: (username, viewer, filter = {}) => call("by_author", { username, viewer, ...filter }), |
| 312 | startPlan: (actor, repo, brief) => call("start_plan", { actor, repo, brief }), |
| 313 | failPlan: (planId, token, error) => call("report_plan", { planId, token, error }), |
| 314 | getPlan: (repo, viewer, id) => call("get_plan", { repo, viewer, id }), |
| 315 | listPlans: (repo, viewer) => call("list_plans", { repo, viewer }), |
| 316 | applyPlan: (actor, repo, id, options = {}) => |
| 317 | call("apply_plan", { actor, repo, id, ...options }), |
| 318 | queueIssue: (actor, repo, number, queued) => |
| 319 | call("queue_issue", { actor, repo, number, queued }), |
| 320 | readyIssues: (repoId) => call("ready_issues", { repoId }), |
| 321 | listAssignedIssues: (viewer) => call("list_assigned_issues", { viewer }), |
| 322 | appendSession: (actor, repo, number, entries) => |
| 323 | call("append_session", { actor, repo, number, entries }), |
| 324 | readSession: (repo, number, viewer, afterSeq = 0) => |
| 325 | call("read_session", { repo, number, viewer, afterSeq }), |
| 326 | }; |
| 327 | } |
| 328 | |
| 329 | export function billingClient(service: ServiceBinding): BillingApi { |
| 330 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 331 | return { |
| 332 | status: () => call("status", {}), |
| 333 | account: (workspace, viewer) => call("account", { workspace, viewer }), |
| 334 | ledger: (workspace, viewer) => call("ledger", { workspace, viewer }), |
| 335 | statement: (workspace, viewer, month = null, group = "day") => call("statement", { workspace, viewer, month, group }), |
| 336 | statementEntries: (workspace, viewer, filter) => |
| 337 | call("statement_entries", { |
| 338 | workspace, |
| 339 | viewer, |
| 340 | month: filter.month, |
| 341 | kind: filter.kind, |
| 342 | day: filter.day ?? null, |
| 343 | project: filter.project ?? null, |
| 344 | before: filter.before ?? null, |
| 345 | }), |
| 346 | usage: (workspace, viewer, since) => call("usage", { workspace, viewer, since }), |
| 347 | checkout: (actor, workspace, amountCents, returnUrl, method = "card") => |
| 348 | call("checkout", { actor, workspace, amountCents, returnUrl, method }), |
| 349 | confirm: (workspace, viewer, session) => call("confirm", { workspace, viewer, session }), |
| 350 | canStart: (workspace) => call("can_start", { workspace }), |
| 351 | trial: (workspace, exempt) => call("trial", { workspace, exempt }), |
| 352 | startRun: (run) => call("start_run", run), |
| 353 | features: (workspace, viewer) => call("features", { workspace, viewer }), |
| 354 | subscribe: (actor, workspace, feature, returnUrl) => |
| 355 | call("subscribe", { actor, workspace, feature, returnUrl }), |
| 356 | confirmSubscription: (workspace, viewer, session) => |
| 357 | call("confirm_subscription", { workspace, viewer, session }), |
| 358 | cancelSubscription: (actor, workspace, feature, resume = false) => |
| 359 | call("cancel_subscription", { actor, workspace, feature, resume }), |
| 360 | hasFeature: (workspace, feature) => call("has_feature", { workspace, feature }), |
| 361 | chargeFeature: (charge) => call("charge_feature", charge), |
| 362 | billingPortal: (actor, workspace, returnUrl) => call("billing_portal", { actor, workspace, return_url: returnUrl }), |
| 363 | recordSandbox: (usage) => call("record_sandbox", usage), |
| 364 | limit: (workspace, viewer) => call("limit", { workspace, viewer }), |
| 365 | checkLimit: (workspace) => call("check_limit", { workspace }), |
| 366 | prices: () => call("prices", {}), |
| 367 | notePending: (workspace, source, costMicros) => call("note_pending", { workspace, source, costMicros }), |
| 368 | setSpendLimit: (actor, workspace, spendLimitMicros, useFullLimit = false, raiseOnce = false) => |
| 369 | call("set_spend_limit", { actor, workspace, spendLimitMicros, use_full_limit: useFullLimit, raise_once: raiseOnce }), |
| 370 | invoices: (workspace, viewer) => call("invoices", { workspace, viewer }), |
| 371 | entitlements: (workspace) => call("entitlements", { workspace }), |
| 372 | reserve: (reservation) => call("reserve", reservation), |
| 373 | settle: (reservationId, actualMicros) => call("settle", { reservationId, actualMicros }), |
| 374 | cardCheck: (actor, workspace, returnUrl) => call("card_check", { actor, workspace, returnUrl }), |
| 375 | confirmCardCheck: (workspace, viewer, session) => call("confirm_card_check", { workspace, viewer, session }), |
| 376 | requestLimit: (actor, workspace, request) => call("request_limit", { actor, workspace, ...request }), |
| 377 | limitRequests: (workspace, viewer) => call("limit_requests", { workspace, viewer }), |
| 378 | confirmSpike: (actor, workspace, keepGoing) => call("confirm_spike", { actor, workspace, keepGoing }), |
| 379 | setCaps: (actor, workspace, caps) => call("set_caps", { actor, workspace, ...caps }), |
| 380 | }; |
| 381 | } |
| 382 | |
| 383 | export function billingAdminClient(service: ServiceBinding): BillingAdminApi { |
| 384 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 385 | return { |
| 386 | accounts: (query) => call("admin_accounts", { query: query ?? null }), |
| 387 | account: (id) => call("admin_account", { id }), |
| 388 | setTerms: (id, terms, by) => call("admin_set_terms", { id, terms, by }), |
| 389 | setAllowances: (id, allowances, note, by) => call("admin_set_allowances", { id, allowances, note, by }), |
| 390 | createEnterprise: (name, workspaces, by) => call("admin_create_enterprise", { name, workspaces, by }), |
| 391 | attach: (workspace, account, by) => call("admin_attach", { workspace, account, by }), |
| 392 | credit: (workspace, amountMicros, note, by) => call("admin_credit", { workspace, amount_micros: amountMicros, note, by }), |
| 393 | billingLink: (workspace, by) => call("admin_billing_link", { workspace, by }), |
| 394 | stripe: (setup = false, by) => call("admin_stripe", { setup, by: by ?? null }), |
| 395 | enterpriseBilling: (id, email, by) => call("admin_enterprise_billing", { id, email, by }), |
| 396 | invoiceEnterprise: (id, by) => call("admin_invoice_enterprise", { id, by }), |
| 397 | accountsFor: (workspaces) => call("admin_accounts", { query: null, workspaces }), |
| 398 | signals: () => call("admin_signals", {}), |
| 399 | overview: () => call("admin_overview", {}), |
| 400 | sales: (workspace) => call("admin_sales", { workspace }), |
| 401 | setSales: (workspace, record, by) => |
| 402 | call("admin_set_sales", { |
| 403 | workspace, |
| 404 | stage: record.stage, |
| 405 | owner: record.owner ?? null, |
| 406 | next_step: record.nextStep ?? null, |
| 407 | next_at: record.nextAt ?? null, |
| 408 | by, |
| 409 | }), |
| 410 | addNote: (workspace, text, by) => call("admin_add_note", { workspace, text, by }), |
| 411 | workspaceInvoices: (workspace) => call("admin_workspace_invoices", { workspace }), |
| 412 | allInvoices: (filter = {}) => call("admin_invoices", { status: filter.status ?? null, month: filter.month ?? null }), |
| 413 | audit: (filter = {}) => |
| 414 | call("admin_audit", { by: filter.by ?? null, action: filter.action ?? null, before: filter.before ?? null }), |
| 415 | limitRequests: (status = "open") => call("admin_limit_requests", { status }), |
| 416 | decideLimitRequest: (id, decision, amountMicros, note, by) => |
| 417 | call("admin_decide_limit_request", { id, decision, amount_micros: amountMicros, note, by }), |
| 418 | overages: () => call("admin_overages", {}), |
| 419 | goodwill: (workspace, amountMicros, reason, by, day = null) => |
| 420 | call("admin_goodwill", { workspace, amount_micros: amountMicros, reason, by, day }), |
| 421 | velocity: () => call("admin_velocity", {}), |
| 422 | recordPayment: (workspace, amountMicros, reference, note, by) => |
| 423 | call("admin_record_payment", { workspace, amount_micros: amountMicros, reference, note, by }), |
| 424 | }; |
| 425 | } |
| 426 | |
| 427 | export function eventsClient(service: ServiceBinding): EventsApi { |
| 428 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 429 | return { |
| 430 | publish: (events) => call("publish", { events }), |
| 431 | list: (query) => call("list", query), |
| 432 | }; |
| 433 | } |
| 434 | |
| 435 | export function integrationsClient(service: ServiceBinding): IntegrationsApi { |
| 436 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 437 | return { |
| 438 | list: (workspace, viewer) => call("list", { workspace, viewer }), |
| 439 | connect: (actor, workspace, input) => call("connect", { actor, workspace, ...input }), |
| 440 | update: (actor, workspace, id, input) => call("update", { actor, workspace, id, ...input }), |
| 441 | disconnect: (actor, workspace, id) => call("disconnect", { actor, workspace, id }), |
| 442 | test: (actor, workspace, id) => call("test", { actor, workspace, id }), |
| 443 | deliveries: (workspace, viewer, id) => call("deliveries", { workspace, viewer, id }), |
| 444 | resolve: (workspace, viewer, reference) => call("resolve", { workspace, viewer, reference }), |
| 445 | references: (workspace, text, limit) => call("references", { workspace, text, limit }), |
| 446 | import: (actor, repo, reference, assign) => call("import", { actor, repo, reference, assign }), |
| 447 | links: (repo, number) => call("links", { repo, number }), |
| 448 | modelProvider: (workspace) => call("model_provider", { workspace }), |
| 449 | openModelSession: (run) => call("open_model_session", run), |
| 450 | modelUpstream: (token) => call("model_upstream", { token }), |
| 451 | routes: (workspace, viewer) => call("routes", { workspace, viewer }), |
| 452 | setRoutes: (actor, workspace, routes) => call("set_routes", { actor, workspace, routes }), |
| 453 | }; |
| 454 | } |
| 455 | |
| 456 | export function webhooksClient(service: ServiceBinding): WebhooksApi { |
| 457 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 458 | return { |
| 459 | list: (viewer, owner) => call("list", { viewer, ...owner }), |
| 460 | create: (actor, owner, input) => call("create", { actor, ...owner, ...input }), |
| 461 | update: (actor, owner, id, input) => call("update", { actor, ...owner, id, ...input }), |
| 462 | delete: (actor, owner, id) => call("delete", { actor, ...owner, id }), |
| 463 | ping: (actor, owner, id) => call("ping", { actor, ...owner, id }), |
| 464 | deliveries: (viewer, owner, id) => call("deliveries", { viewer, ...owner, id }), |
| 465 | redeliver: (actor, owner, deliveryId) => call("redeliver", { actor, ...owner, deliveryId }), |
| 466 | }; |
| 467 | } |
| 468 | |
| 469 | export function actionsClient(service: ServiceBinding): ActionsApi { |
| 470 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 471 | return { |
| 472 | workflows: (repo, viewer) => call("workflows", { repo, viewer }), |
| 473 | runs: (repo, viewer, filter = {}) => call("runs", { repo, viewer, ...filter }), |
| 474 | run: (repo, viewer, id) => call("run", { repo, viewer, id }), |
| 475 | logs: (repo, viewer, job, after = 0) => call("logs", { repo, viewer, job, after }), |
| 476 | dispatch: (actor, repo, workflow, ref, inputs) => call("dispatch", { actor, repo, workflow, ref, inputs }), |
| 477 | cancel: (actor, repo, id) => call("cancel", { actor, repo, id }), |
| 478 | rerun: (actor, repo, id, failedOnly = false) => call("rerun", { actor, repo, id, failed_only: failedOnly }), |
| 479 | setWorkflowEnabled: (actor, repo, workflow, enabled) => |
| 480 | call("set_workflow_enabled", { actor, repo, workflow, enabled }), |
| 481 | settings: (actor, owner, kind) => call("settings", { actor, ...owner, kind }), |
| 482 | setSetting: (actor, owner, kind, name, value, options = {}) => |
| 483 | call("set_setting", { actor, ...owner, kind, name, value, ...options }), |
| 484 | deleteSetting: (actor, owner, kind, name, id) => call("delete_setting", { actor, ...owner, kind, name, id }), |
| 485 | }; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | export function deploymentsClient(service: ServiceBinding): DeploymentsApi { |
| 490 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 491 | return { |
| 492 | settings: (project, viewer) => call("settings", { project, viewer }), |
| 493 | updateSettings: (actor, project, changes) => call("update_settings", { actor, project, changes }), |
| 494 | list: (project, viewer) => call("list", { project, viewer }), |
| 495 | get: (project, id, viewer) => call("get", { project, id, viewer }), |
| 496 | redeploy: (actor, project, branch) => call("redeploy", { actor, project, branch }), |
| 497 | takeDown: (actor, project, branch) => call("take_down", { actor, project, branch }), |
| 498 | stack: (actor, project, branch) => call("stack", { actor, project, branch }), |
| 499 | overview: (workspace, viewer) => call("overview", { workspace, viewer }), |
| 500 | usage: (workspace, viewer) => call("usage", { workspace, viewer }), |
| 501 | domains: (project, viewer) => call("domains", { project, viewer }), |
| 502 | addDomain: (actor, project, hostname, options = {}) => |
| 503 | call("add_domain", { actor, project, hostname, twin: !!options.twin }), |
| 504 | removeDomain: (actor, project, id) => call("remove_domain", { actor, project, id }), |
| 505 | refreshDomain: (actor, project, id) => call("refresh_domain", { actor, project, id }), |
| 506 | }; |
| 507 | } |
| 508 | |
| 509 | export function projectsClient(service: ServiceBinding): ProjectsApi { |
| 510 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 511 | return { |
| 512 | list: (workspace, viewer) => call("list", { workspace, viewer }), |
| 513 | get: (workspace, slug, viewer) => call("get", { workspace, slug, viewer }), |
| 514 | byRepo: (repoId) => call("by_repo", { repoId }), |
| 515 | create: (actor, workspace, input) => call("create", { actor, workspace, input }), |
| 516 | update: (actor, workspace, slug, changes) => call("update", { actor, workspace, slug, changes }), |
| 517 | dependencies: (workspace, slug, viewer) => call("dependencies", { workspace, slug, viewer }), |
| 518 | addDependency: (actor, workspace, slug, on, as) => call("add_dependency", { actor, workspace, slug, on, as }), |
| 519 | removeDependency: (actor, workspace, slug, on) => call("remove_dependency", { actor, workspace, slug, on }), |
| 520 | graph: (projectId) => call("graph", { projectId }), |
| 521 | }; |
| 522 | } |