g1t/packages/contracts/src/clients.ts

106 lines5,232 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 userForSession: (sessionToken) => call("user_for_session", { sessionToken }),
49 userForGitCredentials: (username, secret) =>
50 call("user_for_git_credentials", { username, secret }),
51 userForAccessToken: (token) => call("user_for_access_token", { token }),
52 userForSshKey: (fingerprint) => call("user_for_ssh_key", { fingerprint }),
53 userByUsername: (username) => call("user_by_username", { username }),
54 listSshKeys: (user) => call("list_ssh_keys", { user }),
55 addSshKey: (user, title, publicKey) =>
56 call("add_ssh_key", { user, title, publicKey }),
57 removeSshKey: (user, id) => call("remove_ssh_key", { user, id }),
58 listAccessTokens: (user) => call("list_access_tokens", { user }),
59 createAccessToken: (user, name, ttlSeconds) =>
60 call("create_access_token", { user, name, ttlSeconds }),
61 removeAccessToken: (user, id) => call("remove_access_token", { user, id }),
62 };
63}
64
65export function reposClient(service: ServiceBinding): ReposApi {
66 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
67 return {
68 get: (path, viewer) => call("get", { path, viewer }),
69 getById: (id, viewer) => call("get_by_id", { id, viewer }),
70 list: (viewer, options = {}) => call("list", { viewer, ...options }),
71 create: (owner, input) => call("create", { owner, ...input }),
72 tree: (path, viewer, ref, treePath) =>
73 call("tree", { path, viewer, ref, treePath }),
74 blob: (path, viewer, ref, filePath) =>
75 call("blob", { path, viewer, ref, filePath }),
76 log: (path, viewer, ref, limit) => call("log", { path, viewer, ref, limit }),
77 forkForAttempt: (sourceId, attemptId, actor) =>
78 call("fork_for_attempt", { sourceId, attemptId, actor }),
79 gitAccess: (path, viewer, service) =>
80 call("git_access", { path, viewer, service }),
81 land: (forkId, actor) => call("land", { forkId, actor }),
82 compare: (repoId, viewer, base) => call("compare", { repoId, viewer, base }),
83 };
84}
85
86export function workClient(service: ServiceBinding): WorkApi {
87 const call = <T>(method: string, args: object) => rpc<T>(service, method, args);
88 return {
89 openIntent: (actor, repo, input) => call("open_intent", { actor, repo, ...input }),
90 listIntents: (repo, viewer, status) => call("list_intents", { repo, viewer, status }),
91 getIntent: (repo, number, viewer) => call("get_intent", { repo, number, viewer }),
92 withdrawIntent: (actor, intentId) => call("withdraw_intent", { actor, intentId }),
93 startAttempt: (actor, intentId, input) =>
94 call("start_attempt", { actor, intentId, ...input }),
95 getAttempt: (attemptId, viewer) => call("get_attempt", { attemptId, viewer }),
96 submitAttempt: (actor, attemptId, summary) =>
97 call("submit_attempt", { actor, attemptId, summary }),
98 abandonAttempt: (actor, attemptId) => call("abandon_attempt", { actor, attemptId }),
99 shipAttempt: (actor, attemptId) => call("ship_attempt", { actor, attemptId }),
100 listActiveAttempts: (viewer) => call("list_active_attempts", { viewer }),
101 appendSession: (actor, attemptId, entries) =>
102 call("append_session", { actor, attemptId, entries }),
103 readSession: (attemptId, viewer, afterSeq = 0) =>
104 call("read_session", { attemptId, viewer, afterSeq }),
105 };
106}