g1t/packages/contracts/src/clients.ts

530 lines31,706 bytesCodeBlame
1import type { ActionsApi } from "./actions";
2import type { BillingAdminApi, BillingApi } from "./billing";
3import type { DeploymentsApi } from "./deployments";
4import type { ProjectsApi } from "./projects";
5import type { EventsApi } from "./events";
6import type { IdentityAdminApi, IdentityApi } from "./identity";
7import type { IntegrationsApi } from "./integrations";
8import type { WebhooksApi } from "./webhooks";
9import type { ReposApi } from "./repos";
10import type { WorkApi } from "./work";
11
12/** A service binding, as far as these clients need it. */
13export 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 */
21async 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
38export 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, inviteCode, client) =>
42 call("register", { username, email, password, invite_code: inviteCode ?? null, client: client ?? null }),
43 signIn: (username, password, client) => call("sign_in", { username, password, client: client ?? null }),
44 signOut: (sessionToken) => call("sign_out", { sessionToken }),
45 resendVerification: (user) => call("resend_verification", { user }),
46 verifyEmail: (token) => call("verify_email", { token }),
47 requestPasswordReset: (email, client) => call("request_password_reset", { email, client: client ?? null }),
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 renameWorkspace: (actor, slug, newSlug) => call("rename_workspace", { actor, slug, newSlug }),
71 checkWorkspaceRename: (actor, slug, newSlug) =>
72 call("check_workspace_rename", { actor, slug, newSlug }),
73 resolveSlug: (slug) => call("resolve_slug", { slug }),
74 deleteWorkspace: (actor, slug, confirm) => call("delete_workspace", { actor, slug, confirm }),
75 checkWorkspaceDeletion: (actor, slug) => call("check_workspace_deletion", { actor, slug }),
76 setWorkspaceAvatar: (actor, slug, image) => call("set_workspace_avatar", { actor, slug, image }),
77 setUserAvatar: (user, image) => call("set_user_avatar", { user, image }),
78 listWorkspaceTokens: (slug, viewer) => call("list_workspace_tokens", { slug, viewer }),
79 createWorkspaceToken: (actor, slug, name) =>
80 call("create_workspace_token", { actor, slug, name }),
81 removeWorkspaceToken: (actor, slug, id) =>
82 call("remove_workspace_token", { actor, slug, id }),
83 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
84 registration: () => call("registration", {}),
85 listInvites: (user) => call("list_invites", { user }),
86 createInvite: (user, options = {}) =>
87 call("create_invite", { user, email: options.email ?? null, workspace: options.workspace ?? null }),
88 revokeInvite: (user, id) => call("revoke_invite", { user, id }),
89 checkInvite: (code, client, options = {}) =>
90 call("check_invite", {
91 code,
92 client: client ?? null,
93 viewer: options.viewer ?? null,
94 any_status: options.anyStatus ?? false,
95 }),
96 acceptInvite: (user, code) => call("accept_invite", { user, code }),
97 inviteMember: (actor, slug, email) => call("invite_member", { actor, slug, email }),
98 workspaceInvites: (slug, viewer) => call("workspace_invites", { slug, viewer }),
99 revokeWorkspaceInvite: (actor, slug, id) => call("revoke_workspace_invite", { actor, slug, id }),
100 requestAccess: (email, about, client) => call("request_access", { email, about, client: client ?? null }),
101 userForGitCredentials: (username, secret) =>
102 call("user_for_git_credentials", { username, secret }),
103 userForAccessToken: (token) => call("user_for_access_token", { token }),
104 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
105 userByUsername: (username) => call("user_by_username", { username }),
106 usernames: (ids) => call("usernames", { ids }),
107 profile: (username) => call("profile", { username }),
108 updateProfile: (actor, fields) => call("update_profile", { actor, ...fields }),
109 profileWorkspaces: (username, viewer, publicIn) =>
110 call("profile_workspaces", { username, viewer, public: publicIn }),
111 listSshKeys: (user) => call("list_ssh_keys", { user }),
112 addSshKey: (user, title, publicKey) =>
113 call("add_ssh_key", { user, title, publicKey }),
114 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
115 listAccessTokens: (user) => call("list_access_tokens", { user }),
116 createAccessToken: (user, name, ttlSeconds) =>
117 call("create_access_token", { user, name, ttlSeconds }),
118 createAgentToken: (onBehalfOf, scope, ttlSeconds) =>
119 call("create_agent_token", { onBehalfOf, scope, ttlSeconds }),
120 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
121 createRunCredential: (input) => call("create_run_credential", input),
122 bindRunCredentials: (tokenHashes, runId) => call("bind_run_credentials", { tokenHashes, runId }),
123 revokeRunCredentials: (target) => call("revoke_run_credentials", target),
124 // Who has access to a repository; see access.ts.
125 repoAccess: (owner, name, viewer) => call("repo_access", { viewer, path: { namespace: owner, name } }),
126 addCollaborator: (actor, owner, name, invitee, role) =>
127 call("add_collaborator", { actor, path: { namespace: owner, name }, invitee, role }),
128 setCollaboratorRole: (actor, owner, name, username, role) =>
129 call("set_collaborator_role", { actor, path: { namespace: owner, name }, username, role }),
130 removeCollaborator: (actor, owner, name, username) =>
131 call("remove_collaborator", { actor, path: { namespace: owner, name }, username }),
132 collaboratorPermission: (viewer, owner, name, username) =>
133 call("collaborator_permission", { viewer, path: { namespace: owner, name }, username }),
134 myRepoInvitations: (user) => call("my_repo_invitations", { user }),
135 respondRepoInvitation: (user, id, accept) => call("respond_repo_invitation", { user, id, accept }),
136 revokeRepoInvitation: (actor, owner, name, id) =>
137 call("revoke_repo_invitation", { actor, path: { namespace: owner, name }, id }),
138 setBasePermission: (actor, slug, base) => call("set_base_permission", { actor, slug, base_permission: base }),
139 outsideCollaborators: (viewer, slug) => call("outside_collaborators", { viewer, slug }),
140 };
141}
142
143/**
144 * The slug a renamed workspace has now, for a `workspace.renamed` handler:
145 * asked of identity by the workspace's id, so that renames delivered twice
146 * or out of order converge. Falls back to the event's `to`.
147 */
148export async function currentWorkspaceSlug(
149 identity: ServiceBinding,
150 renamed: { workspaceId: string; to: string },
151): Promise<string> {
152 const names = await identityClient(identity).usernames([renamed.workspaceId]);
153 return names[renamed.workspaceId] ?? renamed.to;
154}
155
156/** The slugs whose rows move to `current`: the two a rename names, less `current`. */
157export function staleSlugs(renamed: { from: string; to: string }, current: string): string[] {
158 return [...new Set([renamed.from, renamed.to])].filter((slug) => slug !== current);
159}
160
161/**
162 * Where a transferred repository is now, as `namespace/name`, for a
163 * `repo.transferred` handler: asked of repos by id, so transfers delivered
164 * twice or out of order converge. Falls back to the event's destination.
165 */
166export async function currentRepoPath(
167 repos: ServiceBinding,
168 transferred: { repoId: string; name: string; to: string },
169): Promise<string> {
170 const path = await rpc<{ namespace: string; name: string } | null>(repos, "path_by_id", { id: transferred.repoId });
171 return path ? `${path.namespace}/${path.name}` : `${transferred.to}/${transferred.name}`;
172}
173
174/** The paths whose rows move to `current`: the two a transfer names, less `current`. */
175export function stalePaths(transferred: { name: string; from: string; to: string }, current: string): string[] {
176 return [...new Set([transferred.from, transferred.to].map((ns) => `${ns}/${transferred.name}`))].filter(
177 (path) => path !== current,
178 );
179}
180
181/**
182 * A repository's path change, from `repo.transferred` or `repo.renamed`,
183 * read the same way: the two paths (`namespace/name`) the event names, old
184 * then new. Null for any other event.
185 */
186export type RepoMove = { repoId: string; paths: [string, string] };
187
188export function repoMove(event: { type: string; data: unknown }): RepoMove | null {
189 const data = event.data as Record<string, string>;
190 if (event.type === "repo.transferred") {
191 return { repoId: data.repoId!, paths: [`${data.from}/${data.name}`, `${data.to}/${data.name}`] };
192 }
193 if (event.type === "repo.renamed") {
194 return { repoId: data.repoId!, paths: [`${data.namespace}/${data.from}`, `${data.namespace}/${data.to}`] };
195 }
196 return null;
197}
198
199/**
200 * Where a moved repository is now, as `namespace/name`: asked of repos by
201 * id, so moves delivered twice or out of order converge. Falls back to the
202 * event's new path.
203 */
204export async function currentMovedPath(repos: ServiceBinding, move: RepoMove): Promise<string> {
205 const path = await rpc<{ namespace: string; name: string } | null>(repos, "path_by_id", { id: move.repoId });
206 return path ? `${path.namespace}/${path.name}` : move.paths[1];
207}
208
209/** The paths whose rows move to `current`: the two a move names, less `current`. */
210export function staleMovedPaths(move: RepoMove, current: string): string[] {
211 return [...new Set(move.paths)].filter((path) => path !== current);
212}
213
214/** Staff-only identity. Only sudo binds to it; see `IdentityAdminApi`. */
215export function identityAdminClient(service: ServiceBinding): IdentityAdminApi {
216 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
217 return {
218 workspaces: (query) => call("admin_workspaces", { query: query ?? null }),
219 workspace: (slug) => call("admin_workspace", { slug }),
220 waitlist: (query, status) => call("admin_waitlist", { query: query ?? null, status: status ?? null }),
221 waitlistPending: () => call("admin_waitlist_pending", {}),
222 decideWaitlist: (id, approve, staff, note) => call("admin_decide_waitlist", { id, approve, staff, note: note ?? null }),
223 invites: (query) => call("admin_invites", { query: query ?? null }),
224 revokeInvite: (id, staff) => call("admin_revoke_invite", { id, staff }),
225 mintInvite: (email, staff) => call("admin_mint_invite", { email, staff }),
226 grantInvites: (target, name, amount, note, staff) =>
227 call("admin_grant_invites", { target, name, amount, note, staff }),
228 inviteTree: (username) => call("admin_invite_tree", { username }),
229 workspaceInvites: (slug) => call("admin_workspace_invites", { slug }),
230 };
231}
232
233export function reposClient(service: ServiceBinding): ReposApi {
234 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
235 return {
236 get: (path, viewer) => call("get", { path, viewer }),
237 getById: (id, viewer) => call("get_by_id", { id, viewer }),
238 publicNamespaces: (ownerId) => call("public_namespaces", { ownerId }),
239 list: (viewer, options = {}) => call("list", { viewer, ...options }),
240 create: (owner, input) => call("create", { owner, ...input }),
241 update: (actor, path, changes) => call("update", { actor, path, ...changes }),
242 transfer: (actor, path, to) => call("transfer", { actor, path, to }),
243 delete: (actor, path, confirm) => call("delete", { actor, path, confirm }),
244 deleted: (viewer, namespace) => call("deleted", { viewer, namespace }),
245 restore: (actor, path) => call("restore", { actor, path }),
246 purge: (actor, path, confirm) => call("purge", { actor, path, confirm }),
247 rename: (actor, path, name) => call("rename", { actor, path, name }),
248 archive: (actor, path, archived) => call("archive", { actor, path, archived }),
249 setVisibility: (actor, path, isPrivate, confirm) => call("set_visibility", { actor, path, isPrivate, confirm }),
250 setDefaultBranch: (actor, path, branch) => call("set_default_branch", { actor, path, branch }),
251 renameBranch: (actor, path, from, to) => call("rename_branch", { actor, path, from, to }),
252 resolveBranch: (repoId, branch) => call("resolve_branch", { repoId, branch }),
253 statusById: (id) => call("status_by_id", { id }),
254 resolvePath: (path) => call("resolve_path", { path }),
255 tree: (path, viewer, ref, treePath) =>
256 call("tree", { path, viewer, ref, treePath }),
257 blob: (path, viewer, ref, filePath) =>
258 call("blob", { path, viewer, ref, filePath }),
259 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
260 blame: (path, viewer, ref, filePath) => call("blame", { path, viewer, ref, filePath }),
261 forkForPull: (sourceId, pullId, actor) =>
262 call("fork_for_pull", { sourceId, pullId, actor }),
263 gitAccess: (path, viewer, service) =>
264 call("git_access", { path, viewer, service }),
265 branches: (path, viewer) => call("branches", { path, viewer }),
266 land: (sourceId, actor, branch) => call("land", { sourceId, actor, branch }),
267 compare: (repoId, viewer, base, head) => call("compare", { repoId, viewer, base, head }),
268 };
269}
270
271export function workClient(service: ServiceBinding): WorkApi {
272 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
273 return {
274 openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }),
275 listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }),
276 getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }),
277 updateIssue: (actor, repo, number, input) =>
278 call("update_issue", { actor, repo, number, ...input }),
279 closeIssue: (actor, repo, number, reason) =>
280 call("close_issue", { actor, repo, number, reason }),
281 reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }),
282 listLabels: (repo, viewer) => call("list_labels", { repo, viewer }),
283 counts: (repo, viewer) => call("counts", { repo, viewer }),
284 addComment: (actor, repo, number, comment) =>
285 call("add_comment", { actor, repo, number, ...comment }),
286 startChecks: (pullId) => call("start_checks", { pullId }),
287 reportChecks: (runId, token, report) =>
288 call("report_checks", { runId, token, ...report }),
289 startReview: (pullId) => call("start_review", { pullId }),
290 failReview: (runId, token, error) => call("report_review", { runId, token, error }),
291 startMergecheck: (pullId) => call("start_mergecheck", { pullId }),
292 failMergecheck: (pullId, token, error) => call("report_mergecheck", { pullId, token, error }),
293 advance: (pullId) => call("advance", { pullId }),
294 stall: (pullId, reason) => call("stall", { pullId, reason }),
295 managedPulls: (repoId) => call("managed_pulls", { repoId }),
296 queue: (repo, viewer) => call("queue", { repo, viewer }),
297 queueBuild: (repoId) => call("queue_build", { repoId }),
298 failQueue: (entryId, token, error) => call("report_queue", { entryId, token, error }),
299 removeFromQueue: (actor, repo, number) => call("remove_from_queue", { actor, repo, number }),
300 messageAgent: (actor, repo, number, body) => call("message_agent", { actor, repo, number, body }),
301 catchUpJob: (pullId) => call("catch_up_job", { pullId }),
302 wakeForMessages: (pullId) => call("wake_for_messages", { pullId }),
303 getSettings: (repo, viewer) => call("get_settings", { repo, viewer }),
304 updateSettings: (actor, repo, settings) =>
305 call("update_settings", { actor, repo, settings }),
306 openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }),
307 listPulls: (repo, viewer, state) => call("list_pulls", { repo, viewer, state }),
308 getPull: (repo, number, viewer) => call("get_pull", { repo, number, viewer }),
309 updatePull: (actor, repo, number, changes) =>
310 call("update_pull", { actor, repo, number, ...changes }),
311 catchUpPull: (actor, repo, number) => call("catch_up_pull", { actor, repo, number }),
312 readyPull: (actor, repo, number, summary) =>
313 call("ready_pull", { actor, repo, number, summary }),
314 closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }),
315 mergePull: (actor, repo, number, options = {}) =>
316 call("merge_pull", { actor, repo, number, ...options }),
317 listActivePulls: (viewer) => call("list_active_pulls", { viewer }),
318 byAuthor: (username, viewer, filter = {}) => call("by_author", { username, viewer, ...filter }),
319 startPlan: (actor, repo, brief) => call("start_plan", { actor, repo, brief }),
320 failPlan: (planId, token, error) => call("report_plan", { planId, token, error }),
321 getPlan: (repo, viewer, id) => call("get_plan", { repo, viewer, id }),
322 listPlans: (repo, viewer) => call("list_plans", { repo, viewer }),
323 applyPlan: (actor, repo, id, options = {}) =>
324 call("apply_plan", { actor, repo, id, ...options }),
325 queueIssue: (actor, repo, number, queued) =>
326 call("queue_issue", { actor, repo, number, queued }),
327 readyIssues: (repoId) => call("ready_issues", { repoId }),
328 listAssignedIssues: (viewer) => call("list_assigned_issues", { viewer }),
329 appendSession: (actor, repo, number, entries) =>
330 call("append_session", { actor, repo, number, entries }),
331 readSession: (repo, number, viewer, afterSeq = 0) =>
332 call("read_session", { repo, number, viewer, afterSeq }),
333 };
334}
335
336export function billingClient(service: ServiceBinding): BillingApi {
337 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
338 return {
339 status: () => call("status", {}),
340 account: (workspace, viewer) => call("account", { workspace, viewer }),
341 ledger: (workspace, viewer) => call("ledger", { workspace, viewer }),
342 statement: (workspace, viewer, month = null, group = "day") => call("statement", { workspace, viewer, month, group }),
343 statementEntries: (workspace, viewer, filter) =>
344 call("statement_entries", {
345 workspace,
346 viewer,
347 month: filter.month,
348 kind: filter.kind,
349 day: filter.day ?? null,
350 project: filter.project ?? null,
351 before: filter.before ?? null,
352 }),
353 usage: (workspace, viewer, since) => call("usage", { workspace, viewer, since }),
354 checkout: (actor, workspace, amountCents, returnUrl, method = "card") =>
355 call("checkout", { actor, workspace, amountCents, returnUrl, method }),
356 confirm: (workspace, viewer, session) => call("confirm", { workspace, viewer, session }),
357 canStart: (workspace) => call("can_start", { workspace }),
358 trial: (workspace, exempt) => call("trial", { workspace, exempt }),
359 startRun: (run) => call("start_run", run),
360 features: (workspace, viewer) => call("features", { workspace, viewer }),
361 subscribe: (actor, workspace, feature, returnUrl) =>
362 call("subscribe", { actor, workspace, feature, returnUrl }),
363 confirmSubscription: (workspace, viewer, session) =>
364 call("confirm_subscription", { workspace, viewer, session }),
365 cancelSubscription: (actor, workspace, feature, resume = false) =>
366 call("cancel_subscription", { actor, workspace, feature, resume }),
367 hasFeature: (workspace, feature) => call("has_feature", { workspace, feature }),
368 chargeFeature: (charge) => call("charge_feature", charge),
369 billingPortal: (actor, workspace, returnUrl) => call("billing_portal", { actor, workspace, return_url: returnUrl }),
370 recordSandbox: (usage) => call("record_sandbox", usage),
371 limit: (workspace, viewer) => call("limit", { workspace, viewer }),
372 checkLimit: (workspace) => call("check_limit", { workspace }),
373 prices: () => call("prices", {}),
374 notePending: (workspace, source, costMicros, detail = null) => call("note_pending", { workspace, source, costMicros, detail }),
375 usageMeters: (workspace, viewer) => call("usage_meters", { workspace, viewer }),
376 setSpendLimit: (actor, workspace, spendLimitMicros, useFullLimit = false, raiseOnce = false) =>
377 call("set_spend_limit", { actor, workspace, spendLimitMicros, use_full_limit: useFullLimit, raise_once: raiseOnce }),
378 invoices: (workspace, viewer) => call("invoices", { workspace, viewer }),
379 entitlements: (workspace) => call("entitlements", { workspace }),
380 reserve: (reservation) => call("reserve", reservation),
381 settle: (reservationId, actualMicros) => call("settle", { reservationId, actualMicros }),
382 cardCheck: (actor, workspace, returnUrl) => call("card_check", { actor, workspace, returnUrl }),
383 confirmCardCheck: (workspace, viewer, session) => call("confirm_card_check", { workspace, viewer, session }),
384 requestLimit: (actor, workspace, request) => call("request_limit", { actor, workspace, ...request }),
385 limitRequests: (workspace, viewer) => call("limit_requests", { workspace, viewer }),
386 confirmSpike: (actor, workspace, keepGoing) => call("confirm_spike", { actor, workspace, keepGoing }),
387 setCaps: (actor, workspace, caps) => call("set_caps", { actor, workspace, ...caps }),
388 };
389}
390
391export function billingAdminClient(service: ServiceBinding): BillingAdminApi {
392 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
393 return {
394 accounts: (query) => call("admin_accounts", { query: query ?? null }),
395 account: (id) => call("admin_account", { id }),
396 setTerms: (id, terms, by) => call("admin_set_terms", { id, terms, by }),
397 setAllowances: (id, allowances, note, by) => call("admin_set_allowances", { id, allowances, note, by }),
398 createEnterprise: (name, workspaces, by) => call("admin_create_enterprise", { name, workspaces, by }),
399 attach: (workspace, account, by) => call("admin_attach", { workspace, account, by }),
400 credit: (workspace, amountMicros, note, by) => call("admin_credit", { workspace, amount_micros: amountMicros, note, by }),
401 billingLink: (workspace, by) => call("admin_billing_link", { workspace, by }),
402 stripe: (setup = false, by) => call("admin_stripe", { setup, by: by ?? null }),
403 enterpriseBilling: (id, email, by) => call("admin_enterprise_billing", { id, email, by }),
404 invoiceEnterprise: (id, by) => call("admin_invoice_enterprise", { id, by }),
405 accountsFor: (workspaces) => call("admin_accounts", { query: null, workspaces }),
406 signals: () => call("admin_signals", {}),
407 overview: () => call("admin_overview", {}),
408 sales: (workspace) => call("admin_sales", { workspace }),
409 setSales: (workspace, record, by) =>
410 call("admin_set_sales", {
411 workspace,
412 stage: record.stage,
413 owner: record.owner ?? null,
414 next_step: record.nextStep ?? null,
415 next_at: record.nextAt ?? null,
416 by,
417 }),
418 addNote: (workspace, text, by) => call("admin_add_note", { workspace, text, by }),
419 workspaceInvoices: (workspace) => call("admin_workspace_invoices", { workspace }),
420 allInvoices: (filter = {}) => call("admin_invoices", { status: filter.status ?? null, month: filter.month ?? null }),
421 audit: (filter = {}) =>
422 call("admin_audit", { by: filter.by ?? null, action: filter.action ?? null, before: filter.before ?? null }),
423 limitRequests: (status = "open") => call("admin_limit_requests", { status }),
424 decideLimitRequest: (id, decision, amountMicros, note, by) =>
425 call("admin_decide_limit_request", { id, decision, amount_micros: amountMicros, note, by }),
426 overages: () => call("admin_overages", {}),
427 goodwill: (workspace, amountMicros, reason, by, day = null) =>
428 call("admin_goodwill", { workspace, amount_micros: amountMicros, reason, by, day }),
429 velocity: () => call("admin_velocity", {}),
430 recordPayment: (workspace, amountMicros, reference, note, by) =>
431 call("admin_record_payment", { workspace, amount_micros: amountMicros, reference, note, by }),
432 };
433}
434
435export function eventsClient(service: ServiceBinding): EventsApi {
436 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
437 return {
438 publish: (events) => call("publish", { events }),
439 list: (query) => call("list", query),
440 };
441}
442
443export function integrationsClient(service: ServiceBinding): IntegrationsApi {
444 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
445 return {
446 list: (workspace, viewer) => call("list", { workspace, viewer }),
447 connect: (actor, workspace, input) => call("connect", { actor, workspace, ...input }),
448 update: (actor, workspace, id, input) => call("update", { actor, workspace, id, ...input }),
449 disconnect: (actor, workspace, id) => call("disconnect", { actor, workspace, id }),
450 test: (actor, workspace, id) => call("test", { actor, workspace, id }),
451 deliveries: (workspace, viewer, id) => call("deliveries", { workspace, viewer, id }),
452 resolve: (workspace, viewer, reference) => call("resolve", { workspace, viewer, reference }),
453 references: (workspace, text, limit) => call("references", { workspace, text, limit }),
454 import: (actor, repo, reference, assign) => call("import", { actor, repo, reference, assign }),
455 links: (repo, number) => call("links", { repo, number }),
456 modelProvider: (workspace) => call("model_provider", { workspace }),
457 openModelSession: (run) => call("open_model_session", run),
458 modelUpstream: (token) => call("model_upstream", { token }),
459 routes: (workspace, viewer) => call("routes", { workspace, viewer }),
460 setRoutes: (actor, workspace, routes) => call("set_routes", { actor, workspace, routes }),
461 };
462}
463
464export function webhooksClient(service: ServiceBinding): WebhooksApi {
465 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
466 return {
467 list: (viewer, owner) => call("list", { viewer, ...owner }),
468 create: (actor, owner, input) => call("create", { actor, ...owner, ...input }),
469 update: (actor, owner, id, input) => call("update", { actor, ...owner, id, ...input }),
470 delete: (actor, owner, id) => call("delete", { actor, ...owner, id }),
471 ping: (actor, owner, id) => call("ping", { actor, ...owner, id }),
472 deliveries: (viewer, owner, id) => call("deliveries", { viewer, ...owner, id }),
473 redeliver: (actor, owner, deliveryId) => call("redeliver", { actor, ...owner, deliveryId }),
474 };
475}
476
477export function actionsClient(service: ServiceBinding): ActionsApi {
478 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
479 return {
480 workflows: (repo, viewer) => call("workflows", { repo, viewer }),
481 runs: (repo, viewer, filter = {}) => call("runs", { repo, viewer, ...filter }),
482 run: (repo, viewer, id) => call("run", { repo, viewer, id }),
483 logs: (repo, viewer, job, after = 0) => call("logs", { repo, viewer, job, after }),
484 dispatch: (actor, repo, workflow, ref, inputs) => call("dispatch", { actor, repo, workflow, ref, inputs }),
485 cancel: (actor, repo, id) => call("cancel", { actor, repo, id }),
486 rerun: (actor, repo, id, failedOnly = false) => call("rerun", { actor, repo, id, failed_only: failedOnly }),
487 setWorkflowEnabled: (actor, repo, workflow, enabled) =>
488 call("set_workflow_enabled", { actor, repo, workflow, enabled }),
489 settings: (actor, owner, kind) => call("settings", { actor, ...owner, kind }),
490 setSetting: (actor, owner, kind, name, value, options = {}) =>
491 call("set_setting", { actor, ...owner, kind, name, value, ...options }),
492 deleteSetting: (actor, owner, kind, name, id) => call("delete_setting", { actor, ...owner, kind, name, id }),
493 };
494}
495
496
497export function deploymentsClient(service: ServiceBinding): DeploymentsApi {
498 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
499 return {
500 settings: (project, viewer) => call("settings", { project, viewer }),
501 updateSettings: (actor, project, changes) => call("update_settings", { actor, project, changes }),
502 list: (project, viewer) => call("list", { project, viewer }),
503 get: (project, id, viewer) => call("get", { project, id, viewer }),
504 redeploy: (actor, project, branch) => call("redeploy", { actor, project, branch }),
505 takeDown: (actor, project, branch) => call("take_down", { actor, project, branch }),
506 stack: (actor, project, branch) => call("stack", { actor, project, branch }),
507 overview: (workspace, viewer) => call("overview", { workspace, viewer }),
508 usage: (workspace, viewer) => call("usage", { workspace, viewer }),
509 domains: (project, viewer) => call("domains", { project, viewer }),
510 addDomain: (actor, project, hostname, options = {}) =>
511 call("add_domain", { actor, project, hostname, twin: !!options.twin }),
512 removeDomain: (actor, project, id) => call("remove_domain", { actor, project, id }),
513 refreshDomain: (actor, project, id) => call("refresh_domain", { actor, project, id }),
514 };
515}
516
517export function projectsClient(service: ServiceBinding): ProjectsApi {
518 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
519 return {
520 list: (workspace, viewer) => call("list", { workspace, viewer }),
521 get: (workspace, slug, viewer) => call("get", { workspace, slug, viewer }),
522 byRepo: (repoId) => call("by_repo", { repoId }),
523 create: (actor, workspace, input) => call("create", { actor, workspace, input }),
524 update: (actor, workspace, slug, changes) => call("update", { actor, workspace, slug, changes }),
525 dependencies: (workspace, slug, viewer) => call("dependencies", { workspace, slug, viewer }),
526 addDependency: (actor, workspace, slug, on, as) => call("add_dependency", { actor, workspace, slug, on, as }),
527 removeDependency: (actor, workspace, slug, on) => call("remove_dependency", { actor, workspace, slug, on }),
528 graph: (projectId) => call("graph", { projectId }),
529 };
530}