g1t/packages/contracts/src/clients.ts

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