The best way to control Claude Code, Codex and more from any computer or phone

main
ts 78 lines 3.22 KB
Raw
1 import * as HttpApi from "effect/http-api/HttpApi";
2 /**
3 * The typed HTTP contract for git-service (DESIGN.md §5): every plane as
4 * one Effect `HttpApi`, so the Worker, the clients
5 * (`HttpApiClient.make(GitApi, ...)`), and OpenAPI share one schema-checked
6 * surface.
7 *
8 * Endpoints use `HttpApiEndpoint`; groups are implemented with
9 * `HttpApiBuilder.group`. The exported groups can also form a smaller API.
10 *
11 * | Group | Path | Routes |
12 * | --- | --- | --- |
13 * | {@link Repos} | `/api/v1/repos` | create, get, update, list, delete, fork, import, compact |
14 * | {@link Refs} | `/api/v1/repos/:owner/:repo/ref(s)` | list, get, update, remove |
15 * | {@link Objects} | `/api/v1/repos/:owner/:repo/…` | commit, log, tree, blob, diff, compare, blobRaw, file |
16 * | {@link Pulls} | `/api/v1/repos/:owner/:repo/pulls` | create, list, get, update, merge |
17 * | {@link Protocol} | `/:owner/:repo/…` | infoRefs, uploadPack, receivePack |
18 * | {@link GitHub} | `/api/v3` | the GitHub REST v3 facade |
19 *
20 * No route carries middleware: apply application HttpRouter middleware to
21 * Git.ApiLive when composing the public routes. The engine's own hash route is
22 * {@link InternalApi}, mounted separately. Shared schemas and tagged
23 * errors live in `Api/Schema.ts` and are re-exported flatly from here.
24 */
25 import type * as HttpServerRequest from "effect/http/HttpServerRequest";
26 import { GitHub } from "./Api/GitHub.ts";
27 import { Objects } from "./Api/Objects.ts";
28 import { Protocol } from "./Api/Protocol.ts";
29 import { Pulls } from "./Api/Pulls.ts";
30 import { Refs } from "./Api/Refs.ts";
31 import { Repos } from "./Api/Repos.ts";
32
33 export * from "./Api/Schema.ts";
34 export * from "./Api/GitHub.ts";
35 export * from "./Api/Internal.ts";
36 export * from "./Api/Objects.ts";
37 export * from "./Api/Protocol.ts";
38 export * from "./Api/Pulls.ts";
39 export * from "./Api/Refs.ts";
40 export * from "./Api/Repos.ts";
41
42 /**
43 * The complete git-service API: the REST plane at `/api/v1`, the git wire
44 * protocol at the root, and the GitHub facade at `/api/v3`. Compose client
45 * schemas with `AppApi.addHttpApi(Git.Api)`, or build one from the groups.
46 */
47 export class GitApi extends HttpApi.make("git-service")
48 .add(Repos)
49 .add(Refs)
50 .add(Objects)
51 .add(Pulls)
52 .add(Protocol)
53 .add(GitHub) {}
54
55 /**
56 * Whether a request to one of the engine's routes only reads: the REST
57 * and raw reads, the GitHub facade's `GET`s, the ref advertisement for a
58 * fetch, and `git-upload-pack`. The advertisement for a push
59 * (`info/refs?service=git-receive-pack`) is not a read. A middleware that
60 * lets anonymous callers read public repositories asks this.
61 *
62 * ```typescript
63 * (httpEffect) =>
64 * Effect.gen(function* () {
65 * const request = yield* HttpServerRequest.HttpServerRequest;
66 * if (Git.isRead(request) && (yield* isPublic)) return yield* httpEffect;
67 * // …
68 * })
69 * ```
70 */
71 export const isRead = (request: HttpServerRequest.HttpServerRequest): boolean => {
72 const url = new URL(request.url, "http://localhost");
73 if (request.method === "POST") return url.pathname.endsWith("/git-upload-pack");
74 if (request.method !== "GET" && request.method !== "HEAD") return false;
75 return (
76 !url.pathname.endsWith("/info/refs") || url.searchParams.get("service") !== "git-receive-pack"
77 );
78 };