g1t/packages/contracts/src/clients.ts

121 lines6,213 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 createWorkspace: (user, slug, name) => call("create_workspace", { user, slug, name }),
49 getWorkspace: (slug) => call("get_workspace", { slug }),
50 listMembers: (slug, viewer) => call("list_members", { slug, viewer }),
51 addMember: (actor, slug, username) => call("add_member", { actor, slug, username }),
52 removeMember: (actor, slug, username) =>
53 call("remove_member", { actor, slug, username }),
54 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
55 userForGitCredentials: (username, secret) =>
56 call("user_for_git_credentials", { username, secret }),
57 userForAccessToken: (token) => call("user_for_access_token", { token }),
58 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
59 userByUsername: (username) => call("user_by_username", { username }),
60 listSshKeys: (user) => call("list_ssh_keys", { user }),
61 addSshKey: (user, title, publicKey) =>
62 call("add_ssh_key", { user, title, publicKey }),
63 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
64 listAccessTokens: (user) => call("list_access_tokens", { user }),
65 createAccessToken: (user, name, ttlSeconds) =>
66 call("create_access_token", { user, name, ttlSeconds }),
67 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
68 };
69}
70
71export function reposClient(service: ServiceBinding): ReposApi {
72 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
73 return {
74 get: (path, viewer) => call("get", { path, viewer }),
75 getById: (id, viewer) => call("get_by_id", { id, viewer }),
76 list: (viewer, options = {}) => call("list", { viewer, ...options }),
77 create: (owner, input) => call("create", { owner, ...input }),
78 tree: (path, viewer, ref, treePath) =>
79 call("tree", { path, viewer, ref, treePath }),
80 blob: (path, viewer, ref, filePath) =>
81 call("blob", { path, viewer, ref, filePath }),
82 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
83 forkForPull: (sourceId, pullId, actor) =>
84 call("fork_for_pull", { sourceId, pullId, actor }),
85 gitAccess: (path, viewer, service) =>
86 call("git_access", { path, viewer, service }),
87 land: (forkId, actor) => call("land", { forkId, actor }),
88 compare: (repoId, viewer, base) => call("compare", { repoId, viewer, base }),
89 };
90}
91
92export function workClient(service: ServiceBinding): WorkApi {
93 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
94 return {
95 openIssue: (actor, repo, input) => call("open_issue", { actor, repo, ...input }),
96 listIssues: (repo, viewer, filter = {}) => call("list_issues", { repo, viewer, ...filter }),
97 getIssue: (repo, number, viewer) => call("get_issue", { repo, number, viewer }),
98 updateIssue: (actor, repo, number, input) =>
99 call("update_issue", { actor, repo, number, ...input }),
100 closeIssue: (actor, repo, number, reason) =>
101 call("close_issue", { actor, repo, number, reason }),
102 reopenIssue: (actor, repo, number) => call("reopen_issue", { actor, repo, number }),
103 listLabels: (repo, viewer) => call("list_labels", { repo, viewer }),
104 counts: (repo, viewer) => call("counts", { repo, viewer }),
105 addComment: (actor, repo, number, body) =>
106 call("add_comment", { actor, repo, number, body }),
107 openPull: (actor, repo, input) => call("open_pull", { actor, repo, ...input }),
108 listPulls: (repo, viewer, state) => call("list_pulls", { repo, viewer, state }),
109 getPull: (repo, number, viewer) => call("get_pull", { repo, number, viewer }),
110 readyPull: (actor, repo, number, summary) =>
111 call("ready_pull", { actor, repo, number, summary }),
112 closePull: (actor, repo, number) => call("close_pull", { actor, repo, number }),
113 mergePull: (actor, repo, number, keepIssueOpen = false) =>
114 call("merge_pull", { actor, repo, number, keepIssueOpen }),
115 listActivePulls: (viewer) => call("list_active_pulls", { viewer }),
116 appendSession: (actor, repo, number, entries) =>
117 call("append_session", { actor, repo, number, entries }),
118 readSession: (repo, number, viewer, afterSeq = 0) =>
119 call("read_session", { repo, number, viewer, afterSeq }),
120 };
121}