g1t/packages/contracts/src/clients.ts

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