pr_01m47d15m3e54sn21z27rpy5n9/packages/contracts/src/clients.ts

242 lines13,465 bytesCodeBlame
1import type { AutomationsApi } from "./automations";
2import type { BillingApi } from "./billing";
3import type { EventsApi } from "./events";
4import type { IdentityApi } from "./identity";
5import type { IntegrationsApi } from "./integrations";
6import type { WebhooksApi } from "./webhooks";
7import type { ReposApi } from "./repos";
8import type { WorkApi } from "./work";
9
10/** A service binding, as far as these clients need it. */
11export type ServiceBinding = {
12 fetch(input: string, init?: RequestInit): Promise<Response>;
13};
14
15/**
16 * Calls a method on a service that speaks the JSON protocol used by the
17 * Rust services: `POST /rpc/<method>` with the arguments as the body.
18 */
19async function rpc<T>(
20 service: ServiceBinding,
21 method: string,
22 args: object,
23): Promise<T> {
24 // The hostname is ignored; a service binding always reaches its service.
25 const response = await service.fetch(`https://service/rpc/${method}`, {
26 method: "POST",
27 headers: { "content-type": "application/json" },
28 body: JSON.stringify(args),
29 });
30 if (!response.ok) {
31 throw new Error(`${method} failed with status ${response.status}`);
32 }
33 return (await response.json()) as T;
34}
35
36export function identityClient(service: ServiceBinding): IdentityApi {
37 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
38 return {
39 register: (username, email, password) =>
40 call("register", { username, email, password }),
41 signIn: (username, password) => call("sign_in", { username, password }),
42 signOut: (sessionToken) => call("sign_out", { sessionToken }),
43 resendVerification: (user) => call("resend_verification", { user }),
44 verifyEmail: (token) => call("verify_email", { token }),
45 requestPasswordReset: (email) => call("request_password_reset", { email }),
46 resetPassword: (token, password) =>
47 call("reset_password", { token, password }),
48 deviceStart: (clientName) => call("device_start", { clientName }),
49 deviceLookup: (userCode) => call("device_lookup", { userCode }),
50 deviceResolve: (userCode, user, approve) =>
51 call("device_resolve", { userCode, user, approve }),
52 deviceClaim: (deviceCode) => call("device_claim", { deviceCode }),
53 oauthAuthorize: (user, approval) => call("oauth_authorize", { user, ...approval }),
54 oauthExchange: (code, codeVerifier, clientId, redirectUri) =>
55 call("oauth_exchange", { code, codeVerifier, clientId, redirectUri }),
56 oauthRefresh: (refreshToken, clientId) =>
57 call("oauth_refresh", { refreshToken, clientId }),
58 listOAuthGrants: (user) => call("list_oauth_grants", { user }),
59 revokeOAuthGrant: (user, id) => call("revoke_oauth_grant", { user, id }),
60 createWorkspace: (user, slug, name) => call("create_workspace", { user, slug, name }),
61 getWorkspace: (slug) => call("get_workspace", { slug }),
62 listMembers: (slug, viewer) => call("list_members", { slug, viewer }),
63 addMember: (actor, slug, username) => call("add_member", { actor, slug, username }),
64 removeMember: (actor, slug, username) =>
65 call("remove_member", { actor, slug, username }),
66 updateWorkspace: (actor, slug, details) =>
67 call("update_workspace", { actor, slug, ...details }),
68 listWorkspaceTokens: (slug, viewer) => call("list_workspace_tokens", { slug, viewer }),
69 createWorkspaceToken: (actor, slug, name) =>
70 call("create_workspace_token", { actor, slug, name }),
71 removeWorkspaceToken: (actor, slug, id) =>
72 call("remove_workspace_token", { actor, slug, id }),
73 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
74 userForGitCredentials: (username, secret) =>
75 call("user_for_git_credentials", { username, secret }),
76 userForAccessToken: (token) => call("user_for_access_token", { token }),
77 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
78 userByUsername: (username) => call("user_by_username", { username }),
79 usernames: (ids) => call("usernames", { ids }),
80 listSshKeys: (user) => call("list_ssh_keys", { user }),
81 addSshKey: (user, title, publicKey) =>
82 call("add_ssh_key", { user, title, publicKey }),
83 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
84 listAccessTokens: (user) => call("list_access_tokens", { user }),
85 createAccessToken: (user, name, ttlSeconds) =>
86 call("create_access_token", { user, name, ttlSeconds }),
87 createAgentToken: (onBehalfOf, scope, ttlSeconds) =>
88 call("create_agent_token", { onBehalfOf, scope, ttlSeconds }),
89 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
90 };
91}
92
93export function reposClient(service: ServiceBinding): ReposApi {
94 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
95 return {
96 get: (path, viewer) => call("get", { path, viewer }),
97 getById: (id, viewer) => call("get_by_id", { id, viewer }),
98 list: (viewer, options = {}) => call("list", { viewer, ...options }),
99 create: (owner, input) => call("create", { owner, ...input }),
100 update: (actor, path, changes) => call("update", { actor, path, ...changes }),
101 tree: (path, viewer, ref, treePath) =>
102 call("tree", { path, viewer, ref, treePath }),
103 blob: (path, viewer, ref, filePath) =>
104 call("blob", { path, viewer, ref, filePath }),
105 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
106 blame: (path, viewer, ref, filePath) => call("blame", { path, viewer, ref, filePath }),
107 forkForPull: (sourceId, pullId, actor) =>
108 call("fork_for_pull", { sourceId, pullId, actor }),
109 gitAccess: (path, viewer, service) =>
110 call("git_access", { path, viewer, service }),
111 branches: (path, viewer) => call("branches", { path, viewer }),
112 land: (sourceId, actor, branch) => call("land", { sourceId, actor, branch }),
113 compare: (repoId, viewer, base, head) => call("compare", { repoId, viewer, base, head }),
114 };
115}
116
117export function workClient(service: ServiceBinding): WorkApi {
118 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
119 return {
120 openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }),
121 listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }),
122 getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }),
123 updateIssue: (actor, repo, number, input) =>
124 call("update_issue", { actor, repo, number, ...input }),
125 closeIssue: (actor, repo, number, reason) =>
126 call("close_issue", { actor, repo, number, reason }),
127 reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }),
128 listLabels: (repo, viewer) => call("list_labels", { repo, viewer }),
129 counts: (repo, viewer) => call("counts", { repo, viewer }),
130 addComment: (actor, repo, number, comment) =>
131 call("add_comment", { actor, repo, number, ...comment }),
132 startChecks: (pullId) => call("start_checks", { pullId }),
133 reportChecks: (runId, token, report) =>
134 call("report_checks", { runId, token, ...report }),
135 startReview: (pullId) => call("start_review", { pullId }),
136 failReview: (runId, token, error) => call("report_review", { runId, token, error }),
137 advance: (pullId) => call("advance", { pullId }),
138 stall: (pullId, reason) => call("stall", { pullId, reason }),
139 managedPulls: (repoId) => call("managed_pulls", { repoId }),
140 queue: (repo, viewer) => call("queue", { repo, viewer }),
141 queueBuild: (repoId) => call("queue_build", { repoId }),
142 failQueue: (entryId, token, error) => call("report_queue", { entryId, token, error }),
143 removeFromQueue: (actor, repo, number) => call("remove_from_queue", { actor, repo, number }),
144 messageAgent: (actor, repo, number, body) => call("message_agent", { actor, repo, number, body }),
145 catchUpJob: (pullId) => call("catch_up_job", { pullId }),
146 getSettings: (repo, viewer) => call("get_settings", { repo, viewer }),
147 updateSettings: (actor, repo, settings) =>
148 call("update_settings", { actor, repo, settings }),
149 openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }),
150 listPulls: (repo, viewer, state) => call("list_pulls", { repo, viewer, state }),
151 getPull: (repo, number, viewer) => call("get_pull", { repo, number, viewer }),
152 updatePull: (actor, repo, number, changes) =>
153 call("update_pull", { actor, repo, number, ...changes }),
154 readyPull: (actor, repo, number, summary) =>
155 call("ready_pull", { actor, repo, number, summary }),
156 closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }),
157 mergePull: (actor, repo, number, options = {}) =>
158 call("merge_pull", { actor, repo, number, ...options }),
159 listActivePulls: (viewer) => call("list_active_pulls", { viewer }),
160 startPlan: (actor, repo, brief) => call("start_plan", { actor, repo, brief }),
161 failPlan: (planId, token, error) => call("report_plan", { planId, token, error }),
162 getPlan: (repo, viewer, id) => call("get_plan", { repo, viewer, id }),
163 listPlans: (repo, viewer) => call("list_plans", { repo, viewer }),
164 applyPlan: (actor, repo, id, options = {}) =>
165 call("apply_plan", { actor, repo, id, ...options }),
166 queueIssue: (actor, repo, number, queued) =>
167 call("queue_issue", { actor, repo, number, queued }),
168 readyIssues: (repoId) => call("ready_issues", { repoId }),
169 listAssignedIssues: (viewer) => call("list_assigned_issues", { viewer }),
170 appendSession: (actor, repo, number, entries) =>
171 call("append_session", { actor, repo, number, entries }),
172 readSession: (repo, number, viewer, afterSeq = 0) =>
173 call("read_session", { repo, number, viewer, afterSeq }),
174 };
175}
176
177export function billingClient(service: ServiceBinding): BillingApi {
178 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
179 return {
180 status: () => call("status", {}),
181 account: (workspace, viewer) => call("account", { workspace, viewer }),
182 ledger: (workspace, viewer) => call("ledger", { workspace, viewer }),
183 usage: (workspace, viewer, since) => call("usage", { workspace, viewer, since }),
184 checkout: (actor, workspace, amountCents, returnUrl) =>
185 call("checkout", { actor, workspace, amountCents, returnUrl }),
186 confirm: (workspace, viewer, session) => call("confirm", { workspace, viewer, session }),
187 canStart: (workspace) => call("can_start", { workspace }),
188 startRun: (run) => call("start_run", run),
189 };
190}
191
192export function eventsClient(service: ServiceBinding): EventsApi {
193 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
194 return {
195 publish: (events) => call("publish", { events }),
196 list: (query) => call("list", query),
197 };
198}
199
200export function integrationsClient(service: ServiceBinding): IntegrationsApi {
201 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
202 return {
203 list: (workspace, viewer) => call("list", { workspace, viewer }),
204 connect: (actor, workspace, input) => call("connect", { actor, workspace, ...input }),
205 update: (actor, workspace, id, input) => call("update", { actor, workspace, id, ...input }),
206 disconnect: (actor, workspace, id) => call("disconnect", { actor, workspace, id }),
207 test: (actor, workspace, id) => call("test", { actor, workspace, id }),
208 deliveries: (workspace, viewer, id) => call("deliveries", { workspace, viewer, id }),
209 resolve: (workspace, viewer, reference) => call("resolve", { workspace, viewer, reference }),
210 references: (workspace, text, limit) => call("references", { workspace, text, limit }),
211 import: (actor, repo, reference, assign) => call("import", { actor, repo, reference, assign }),
212 links: (repo, number) => call("links", { repo, number }),
213 modelProvider: (workspace) => call("model_provider", { workspace }),
214 openModelSession: (run) => call("open_model_session", run),
215 modelUpstream: (token) => call("model_upstream", { token }),
216 routes: (workspace, viewer) => call("routes", { workspace, viewer }),
217 setRoutes: (actor, workspace, routes) => call("set_routes", { actor, workspace, routes }),
218 };
219}
220
221export function webhooksClient(service: ServiceBinding): WebhooksApi {
222 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
223 return {
224 list: (viewer, owner) => call("list", { viewer, ...owner }),
225 create: (actor, owner, input) => call("create", { actor, ...owner, ...input }),
226 update: (actor, owner, id, input) => call("update", { actor, ...owner, id, ...input }),
227 delete: (actor, owner, id) => call("delete", { actor, ...owner, id }),
228 ping: (actor, owner, id) => call("ping", { actor, ...owner, id }),
229 deliveries: (viewer, owner, id) => call("deliveries", { viewer, ...owner, id }),
230 redeliver: (actor, owner, deliveryId) => call("redeliver", { actor, ...owner, deliveryId }),
231 };
232}
233
234export function automationsClient(service: ServiceBinding): AutomationsApi {
235 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
236 return {
237 list: (repo, viewer) => call("list", { repo, viewer }),
238 runs: (repo, viewer, automation) => call("runs", { repo, viewer, automation }),
239 run: (actor, repo, id, number) => call("run", { actor, repo, id, number }),
240 setEnabled: (actor, repo, id, enabled) => call("set_enabled", { actor, repo, id, enabled }),
241 };
242}