g1t/packages/contracts/src/clients.ts

129 lines6,793 bytesCodeBlame
1import type { IdentityApi } from "./identity";
2import type { ReposApi } from "./repos";
3import type { WorkApi } from "./work";
4
5/** A service binding, as far as these clients need it. */
6export type ServiceBinding = {
7 fetch(input: string, init?: RequestInit): Promise<Response>;
8};
9
10/**
11 * Calls a method on a service that speaks the JSON protocol used by the
12 * Rust services: `POST /rpc/<method>` with the arguments as the body.
13 */
14async function rpc<T>(
15 service: ServiceBinding,
16 method: string,
17 args: object,
18): Promise<T> {
19 // The hostname is ignored; a service binding always reaches its service.
20 const response = await service.fetch(`https://service/rpc/${method}`, {
21 method: "POST",
22 headers: { "content-type": "application/json" },
23 body: JSON.stringify(args),
24 });
25 if (!response.ok) {
26 throw new Error(`${method} failed with status ${response.status}`);
27 }
28 return response.json();
29}
30
31export function identityClient(service: ServiceBinding): IdentityApi {
32 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
33 return {
34 register: (username, email, password) =>
35 call("register", { username, email, password }),
36 signIn: (username, password) => call("sign_in", { username, password }),
37 signOut: (sessionToken) => call("sign_out", { sessionToken }),
38 resendVerification: (user) => call("resend_verification", { user }),
39 verifyEmail: (token) => call("verify_email", { token }),
40 requestPasswordReset: (email) => call("request_password_reset", { email }),
41 resetPassword: (token, password) =>
42 call("reset_password", { token, password }),
43 deviceStart: (clientName) => call("device_start", { clientName }),
44 deviceLookup: (userCode) => call("device_lookup", { userCode }),
45 deviceResolve: (userCode, user, approve) =>
46 call("device_resolve", { userCode, user, approve }),
47 deviceClaim: (deviceCode) => call("device_claim", { deviceCode }),
48 oauthAuthorize: (user, approval) => call("oauth_authorize", { user, ...approval }),
49 oauthExchange: (code, codeVerifier, clientId, redirectUri) =>
50 call("oauth_exchange", { code, codeVerifier, clientId, redirectUri }),
51 oauthRefresh: (refreshToken, clientId) =>
52 call("oauth_refresh", { refreshToken, clientId }),
53 listOAuthGrants: (user) => call("list_oauth_grants", { user }),
54 revokeOAuthGrant: (user, id) => call("revoke_oauth_grant", { user, id }),
55 createWorkspace: (user, slug, name) => call("create_workspace", { user, slug, name }),
56 getWorkspace: (slug) => call("get_workspace", { slug }),
57 listMembers: (slug, viewer) => call("list_members", { slug, viewer }),
58 addMember: (actor, slug, username) => call("add_member", { actor, slug, username }),
59 removeMember: (actor, slug, username) =>
60 call("remove_member", { actor, slug, username }),
61 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
62 userForGitCredentials: (username, secret) =>
63 call("user_for_git_credentials", { username, secret }),
64 userForAccessToken: (token) => call("user_for_access_token", { token }),
65 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
66 userByUsername: (username) => call("user_by_username", { username }),
67 listSshKeys: (user) => call("list_ssh_keys", { user }),
68 addSshKey: (user, title, publicKey) =>
69 call("add_ssh_key", { user, title, publicKey }),
70 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
71 listAccessTokens: (user) => call("list_access_tokens", { user }),
72 createAccessToken: (user, name, ttlSeconds) =>
73 call("create_access_token", { user, name, ttlSeconds }),
74 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
75 };
76}
77
78export function reposClient(service: ServiceBinding): ReposApi {
79 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
80 return {
81 get: (path, viewer) => call("get", { path, viewer }),
82 getById: (id, viewer) => call("get_by_id", { id, viewer }),
83 list: (viewer, options = {}) => call("list", { viewer, ...options }),
84 create: (owner, input) => call("create", { owner, ...input }),
85 tree: (path, viewer, ref, treePath) =>
86 call("tree", { path, viewer, ref, treePath }),
87 blob: (path, viewer, ref, filePath) =>
88 call("blob", { path, viewer, ref, filePath }),
89 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
90 forkForPull: (sourceId, pullId, actor) =>
91 call("fork_for_pull", { sourceId, pullId, actor }),
92 gitAccess: (path, viewer, service) =>
93 call("git_access", { path, viewer, service }),
94 branches: (path, viewer) => call("branches", { path, viewer }),
95 land: (sourceId, actor, branch) => call("land", { sourceId, actor, branch }),
96 compare: (repoId, viewer, base, head) => call("compare", { repoId, viewer, base, head }),
97 };
98}
99
100export function workClient(service: ServiceBinding): WorkApi {
101 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
102 return {
103 openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }),
104 listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }),
105 getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }),
106 updateIssue: (actor, repo, number, input) =>
107 call("update_issue", { actor, repo, number, ...input }),
108 closeIssue: (actor, repo, number, reason) =>
109 call("close_issue", { actor, repo, number, reason }),
110 reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }),
111 listLabels: (repo, viewer) => call("list_labels", { repo, viewer }),
112 counts: (repo, viewer) => call("counts", { repo, viewer }),
113 addComment: (actor, repo, number, body) =>
114 call("add_comment", { actor, repo, number, body }),
115 openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }),
116 listPulls: (repo, viewer, state) => call("list_pulls", { repo, viewer, state }),
117 getPull: (repo, number, viewer) => call("get_pull", { repo, number, viewer }),
118 readyPull: (actor, repo, number, summary) =>
119 call("ready_pull", { actor, repo, number, summary }),
120 closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }),
121 mergePull: (actor, repo, number, keepIssueOpen = false) =>
122 call("merge_pull", { actor, repo, number, keepIssueOpen }),
123 listActivePulls: (viewer) => call("list_active_pulls", { viewer }),
124 appendSession: (actor, repo, number, entries) =>
125 call("append_session", { actor, repo, number, entries }),
126 readSession: (repo, number, viewer, afterSeq = 0) =>
127 call("read_session", { repo, number, viewer, afterSeq }),
128 };
129}