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