g1t/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 { 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) => |
| 42 | call("register", { username, email, password }), |
| 43 | signIn: (username, password) => call("sign_in", { username, password }), |
| 44 | signOut: (sessionToken) => call("sign_out", { sessionToken }), |
| 45 | resendVerification: (user) => call("resend_verification", { user }), |
| 46 | verifyEmail: (token) => call("verify_email", { token }), |
| 47 | requestPasswordReset: (email) => call("request_password_reset", { email }), |
| 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 | listWorkspaceTokens: (slug, viewer) => call("list_workspace_tokens", { slug, viewer }), |
| 71 | createWorkspaceToken: (actor, slug, name) => |
| 72 | call("create_workspace_token", { actor, slug, name }), |
| 73 | removeWorkspaceToken: (actor, slug, id) => |
| 74 | call("remove_workspace_token", { actor, slug, id }), |
| 75 | userForSession: (sessionToken) => call("user_for_session", { sessionToken }), |
| 76 | userForGitCredentials: (username, secret) => |
| 77 | call("user_for_git_credentials", { username, secret }), |
| 78 | userForAccessToken: (token) => call("user_for_access_token", { token }), |
| 79 | userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }), |
| 80 | userByUsername: (username) => call("user_by_username", { username }), |
| 81 | usernames: (ids) => call("usernames", { ids }), |
| 82 | listSshKeys: (user) => call("list_ssh_keys", { user }), |
| 83 | addSshKey: (user, title, publicKey) => |
| 84 | call("add_ssh_key", { user, title, publicKey }), |
| 85 | removeSshKey: (user, id) => call("remove_ssh_key", { user, id }), |
| 86 | listAccessTokens: (user) => call("list_access_tokens", { user }), |
| 87 | createAccessToken: (user, name, ttlSeconds) => |
| 88 | call("create_access_token", { user, name, ttlSeconds }), |
| 89 | createAgentToken: (onBehalfOf, scope, ttlSeconds) => |
| 90 | call("create_agent_token", { onBehalfOf, scope, ttlSeconds }), |
| 91 | removeAccessToken: (user, id) => call("remove_access_token", { user, id }), |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | export function reposClient(service: ServiceBinding): ReposApi { |
| 96 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 97 | return { |
| 98 | get: (path, viewer) => call("get", { path, viewer }), |
| 99 | getById: (id, viewer) => call("get_by_id", { id, viewer }), |
| 100 | list: (viewer, options = {}) => call("list", { viewer, ...options }), |
| 101 | create: (owner, input) => call("create", { owner, ...input }), |
| 102 | update: (actor, path, changes) => call("update", { actor, path, ...changes }), |
| 103 | tree: (path, viewer, ref, treePath) => |
| 104 | call("tree", { path, viewer, ref, treePath }), |
| 105 | blob: (path, viewer, ref, filePath) => |
| 106 | call("blob", { path, viewer, ref, filePath }), |
| 107 | log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }), |
| 108 | blame: (path, viewer, ref, filePath) => call("blame", { path, viewer, ref, filePath }), |
| 109 | forkForPull: (sourceId, pullId, actor) => |
| 110 | call("fork_for_pull", { sourceId, pullId, actor }), |
| 111 | gitAccess: (path, viewer, service) => |
| 112 | call("git_access", { path, viewer, service }), |
| 113 | branches: (path, viewer) => call("branches", { path, viewer }), |
| 114 | land: (sourceId, actor, branch) => call("land", { sourceId, actor, branch }), |
| 115 | compare: (repoId, viewer, base, head) => call("compare", { repoId, viewer, base, head }), |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | export function workClient(service: ServiceBinding): WorkApi { |
| 120 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 121 | return { |
| 122 | openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }), |
| 123 | listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }), |
| 124 | getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }), |
| 125 | updateIssue: (actor, repo, number, input) => |
| 126 | call("update_issue", { actor, repo, number, ...input }), |
| 127 | closeIssue: (actor, repo, number, reason) => |
| 128 | call("close_issue", { actor, repo, number, reason }), |
| 129 | reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }), |
| 130 | listLabels: (repo, viewer) => call("list_labels", { repo, viewer }), |
| 131 | counts: (repo, viewer) => call("counts", { repo, viewer }), |
| 132 | addComment: (actor, repo, number, comment) => |
| 133 | call("add_comment", { actor, repo, number, ...comment }), |
| 134 | startChecks: (pullId) => call("start_checks", { pullId }), |
| 135 | reportChecks: (runId, token, report) => |
| 136 | call("report_checks", { runId, token, ...report }), |
| 137 | startReview: (pullId) => call("start_review", { pullId }), |
| 138 | failReview: (runId, token, error) => call("report_review", { runId, token, error }), |
| 139 | advance: (pullId) => call("advance", { pullId }), |
| 140 | stall: (pullId, reason) => call("stall", { pullId, reason }), |
| 141 | managedPulls: (repoId) => call("managed_pulls", { repoId }), |
| 142 | queue: (repo, viewer) => call("queue", { repo, viewer }), |
| 143 | queueBuild: (repoId) => call("queue_build", { repoId }), |
| 144 | failQueue: (entryId, token, error) => call("report_queue", { entryId, token, error }), |
| 145 | removeFromQueue: (actor, repo, number) => call("remove_from_queue", { actor, repo, number }), |
| 146 | messageAgent: (actor, repo, number, body) => call("message_agent", { actor, repo, number, body }), |
| 147 | catchUpJob: (pullId) => call("catch_up_job", { pullId }), |
| 148 | wakeForMessages: (pullId) => call("wake_for_messages", { pullId }), |
| 149 | getSettings: (repo, viewer) => call("get_settings", { repo, viewer }), |
| 150 | updateSettings: (actor, repo, settings) => |
| 151 | call("update_settings", { actor, repo, settings }), |
| 152 | openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }), |
| 153 | listPulls: (repo, viewer, state) => call("list_pulls", { repo, viewer, state }), |
| 154 | getPull: (repo, number, viewer) => call("get_pull", { repo, number, viewer }), |
| 155 | updatePull: (actor, repo, number, changes) => |
| 156 | call("update_pull", { actor, repo, number, ...changes }), |
| 157 | readyPull: (actor, repo, number, summary) => |
| 158 | call("ready_pull", { actor, repo, number, summary }), |
| 159 | closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }), |
| 160 | mergePull: (actor, repo, number, options = {}) => |
| 161 | call("merge_pull", { actor, repo, number, ...options }), |
| 162 | listActivePulls: (viewer) => call("list_active_pulls", { viewer }), |
| 163 | startPlan: (actor, repo, brief) => call("start_plan", { actor, repo, brief }), |
| 164 | failPlan: (planId, token, error) => call("report_plan", { planId, token, error }), |
| 165 | getPlan: (repo, viewer, id) => call("get_plan", { repo, viewer, id }), |
| 166 | listPlans: (repo, viewer) => call("list_plans", { repo, viewer }), |
| 167 | applyPlan: (actor, repo, id, options = {}) => |
| 168 | call("apply_plan", { actor, repo, id, ...options }), |
| 169 | queueIssue: (actor, repo, number, queued) => |
| 170 | call("queue_issue", { actor, repo, number, queued }), |
| 171 | readyIssues: (repoId) => call("ready_issues", { repoId }), |
| 172 | listAssignedIssues: (viewer) => call("list_assigned_issues", { viewer }), |
| 173 | appendSession: (actor, repo, number, entries) => |
| 174 | call("append_session", { actor, repo, number, entries }), |
| 175 | readSession: (repo, number, viewer, afterSeq = 0) => |
| 176 | call("read_session", { repo, number, viewer, afterSeq }), |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | export function billingClient(service: ServiceBinding): BillingApi { |
| 181 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 182 | return { |
| 183 | status: () => call("status", {}), |
| 184 | account: (workspace, viewer) => call("account", { workspace, viewer }), |
| 185 | ledger: (workspace, viewer) => call("ledger", { workspace, viewer }), |
| 186 | usage: (workspace, viewer, since) => call("usage", { workspace, viewer, since }), |
| 187 | checkout: (actor, workspace, amountCents, returnUrl) => |
| 188 | call("checkout", { actor, workspace, amountCents, returnUrl }), |
| 189 | confirm: (workspace, viewer, session) => call("confirm", { workspace, viewer, session }), |
| 190 | canStart: (workspace) => call("can_start", { workspace }), |
| 191 | trial: (workspace, exempt) => call("trial", { workspace, exempt }), |
| 192 | startRun: (run) => call("start_run", run), |
| 193 | features: (workspace, viewer) => call("features", { workspace, viewer }), |
| 194 | subscribe: (actor, workspace, feature, returnUrl) => |
| 195 | call("subscribe", { actor, workspace, feature, returnUrl }), |
| 196 | confirmSubscription: (workspace, viewer, session) => |
| 197 | call("confirm_subscription", { workspace, viewer, session }), |
| 198 | cancelSubscription: (actor, workspace, feature, resume = false) => |
| 199 | call("cancel_subscription", { actor, workspace, feature, resume }), |
| 200 | hasFeature: (workspace, feature) => call("has_feature", { workspace, feature }), |
| 201 | chargeFeature: (charge) => call("charge_feature", charge), |
| 202 | recordSandbox: (usage) => call("record_sandbox", usage), |
| 203 | limit: (workspace, viewer) => call("limit", { workspace, viewer }), |
| 204 | checkLimit: (workspace) => call("check_limit", { workspace }), |
| 205 | prices: () => call("prices", {}), |
| 206 | notePending: (workspace, source, costMicros) => call("note_pending", { workspace, source, costMicros }), |
| 207 | setSpendLimit: (actor, workspace, spendLimitMicros) => |
| 208 | call("set_spend_limit", { actor, workspace, spendLimitMicros }), |
| 209 | }; |
| 210 | } |
| 211 | |
| 212 | export function billingAdminClient(service: ServiceBinding): BillingAdminApi { |
| 213 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 214 | return { |
| 215 | accounts: (query) => call("admin_accounts", { query: query ?? null }), |
| 216 | account: (id) => call("admin_account", { id }), |
| 217 | setTerms: (id, terms, by) => call("admin_set_terms", { id, terms, by }), |
| 218 | createEnterprise: (name, workspaces, by) => call("admin_create_enterprise", { name, workspaces, by }), |
| 219 | attach: (workspace, account, by) => call("admin_attach", { workspace, account, by }), |
| 220 | credit: (workspace, amountMicros, note, by) => call("admin_credit", { workspace, amount_micros: amountMicros, note, by }), |
| 221 | }; |
| 222 | } |
| 223 | |
| 224 | export function eventsClient(service: ServiceBinding): EventsApi { |
| 225 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 226 | return { |
| 227 | publish: (events) => call("publish", { events }), |
| 228 | list: (query) => call("list", query), |
| 229 | }; |
| 230 | } |
| 231 | |
| 232 | export function integrationsClient(service: ServiceBinding): IntegrationsApi { |
| 233 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 234 | return { |
| 235 | list: (workspace, viewer) => call("list", { workspace, viewer }), |
| 236 | connect: (actor, workspace, input) => call("connect", { actor, workspace, ...input }), |
| 237 | update: (actor, workspace, id, input) => call("update", { actor, workspace, id, ...input }), |
| 238 | disconnect: (actor, workspace, id) => call("disconnect", { actor, workspace, id }), |
| 239 | test: (actor, workspace, id) => call("test", { actor, workspace, id }), |
| 240 | deliveries: (workspace, viewer, id) => call("deliveries", { workspace, viewer, id }), |
| 241 | resolve: (workspace, viewer, reference) => call("resolve", { workspace, viewer, reference }), |
| 242 | references: (workspace, text, limit) => call("references", { workspace, text, limit }), |
| 243 | import: (actor, repo, reference, assign) => call("import", { actor, repo, reference, assign }), |
| 244 | links: (repo, number) => call("links", { repo, number }), |
| 245 | modelProvider: (workspace) => call("model_provider", { workspace }), |
| 246 | openModelSession: (run) => call("open_model_session", run), |
| 247 | modelUpstream: (token) => call("model_upstream", { token }), |
| 248 | routes: (workspace, viewer) => call("routes", { workspace, viewer }), |
| 249 | setRoutes: (actor, workspace, routes) => call("set_routes", { actor, workspace, routes }), |
| 250 | }; |
| 251 | } |
| 252 | |
| 253 | export function webhooksClient(service: ServiceBinding): WebhooksApi { |
| 254 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 255 | return { |
| 256 | list: (viewer, owner) => call("list", { viewer, ...owner }), |
| 257 | create: (actor, owner, input) => call("create", { actor, ...owner, ...input }), |
| 258 | update: (actor, owner, id, input) => call("update", { actor, ...owner, id, ...input }), |
| 259 | delete: (actor, owner, id) => call("delete", { actor, ...owner, id }), |
| 260 | ping: (actor, owner, id) => call("ping", { actor, ...owner, id }), |
| 261 | deliveries: (viewer, owner, id) => call("deliveries", { viewer, ...owner, id }), |
| 262 | redeliver: (actor, owner, deliveryId) => call("redeliver", { actor, ...owner, deliveryId }), |
| 263 | }; |
| 264 | } |
| 265 | |
| 266 | export function actionsClient(service: ServiceBinding): ActionsApi { |
| 267 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 268 | return { |
| 269 | workflows: (repo, viewer) => call("workflows", { repo, viewer }), |
| 270 | runs: (repo, viewer, filter = {}) => call("runs", { repo, viewer, ...filter }), |
| 271 | run: (repo, viewer, id) => call("run", { repo, viewer, id }), |
| 272 | logs: (repo, viewer, job, after = 0) => call("logs", { repo, viewer, job, after }), |
| 273 | dispatch: (actor, repo, workflow, ref, inputs) => call("dispatch", { actor, repo, workflow, ref, inputs }), |
| 274 | cancel: (actor, repo, id) => call("cancel", { actor, repo, id }), |
| 275 | rerun: (actor, repo, id, failedOnly = false) => call("rerun", { actor, repo, id, failed_only: failedOnly }), |
| 276 | setWorkflowEnabled: (actor, repo, workflow, enabled) => |
| 277 | call("set_workflow_enabled", { actor, repo, workflow, enabled }), |
| 278 | settings: (actor, owner, kind) => call("settings", { actor, ...owner, kind }), |
| 279 | setSetting: (actor, owner, kind, name, value, options = {}) => |
| 280 | call("set_setting", { actor, ...owner, kind, name, value, ...options }), |
| 281 | deleteSetting: (actor, owner, kind, name, id) => call("delete_setting", { actor, ...owner, kind, name, id }), |
| 282 | }; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | export function deploymentsClient(service: ServiceBinding): DeploymentsApi { |
| 287 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 288 | return { |
| 289 | settings: (project, viewer) => call("settings", { project, viewer }), |
| 290 | updateSettings: (actor, project, changes) => call("update_settings", { actor, project, changes }), |
| 291 | list: (project, viewer) => call("list", { project, viewer }), |
| 292 | get: (project, id, viewer) => call("get", { project, id, viewer }), |
| 293 | redeploy: (actor, project, branch) => call("redeploy", { actor, project, branch }), |
| 294 | takeDown: (actor, project, branch) => call("take_down", { actor, project, branch }), |
| 295 | stack: (actor, project, branch) => call("stack", { actor, project, branch }), |
| 296 | overview: (workspace, viewer) => call("overview", { workspace, viewer }), |
| 297 | usage: (workspace, viewer) => call("usage", { workspace, viewer }), |
| 298 | }; |
| 299 | } |
| 300 | |
| 301 | export function projectsClient(service: ServiceBinding): ProjectsApi { |
| 302 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 303 | return { |
| 304 | list: (workspace, viewer) => call("list", { workspace, viewer }), |
| 305 | get: (workspace, slug, viewer) => call("get", { workspace, slug, viewer }), |
| 306 | byRepo: (repoId) => call("by_repo", { repoId }), |
| 307 | create: (actor, workspace, input) => call("create", { actor, workspace, input }), |
| 308 | update: (actor, workspace, slug, changes) => call("update", { actor, workspace, slug, changes }), |
| 309 | dependencies: (workspace, slug, viewer) => call("dependencies", { workspace, slug, viewer }), |
| 310 | addDependency: (actor, workspace, slug, on, as) => call("add_dependency", { actor, workspace, slug, on, as }), |
| 311 | removeDependency: (actor, workspace, slug, on) => call("remove_dependency", { actor, workspace, slug, on }), |
| 312 | graph: (projectId) => call("graph", { projectId }), |
| 313 | }; |
| 314 | } |