g1t/packages/contracts/src/clients.ts

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