@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
mdx 152 lines 3.71 KB
Raw
1 ---
2 title: "Part 5: Add your application's API"
3 description: Add a typed /me endpoint beside Git and use the caller already supplied by your authentication middleware.
4 ---
5
6 import PackageManagers from "../../../../components/PackageManagers.astro";
7
8 Your Git host already identifies callers. Add `/api/v1/me` to return the current
9 user. This part introduces an application API as a sibling of the existing Git
10 routes; it uses the same Worker, URL, and authentication.
11
12 ## Define the response and error
13
14 Create `src/api.ts`:
15
16 ```typescript
17 // src/api.ts
18 import * as Schema from "effect/Schema";
19
20 const User = Schema.Struct({ id: Schema.String });
21
22 class Unauthorized extends Schema.TaggedError<Unauthorized>()(
23 "Unauthorized",
24 {},
25 { httpApiStatus: 401 },
26 ) {}
27 ```
28
29 The endpoint returns the caller's ID. Its error schema declares that a missing
30 caller is an HTTP `401`.
31
32 ## Define the endpoint
33
34 Add the API imports:
35
36 ```diff lang="typescript"
37 // src/api.ts
38 import * as Schema from "effect/Schema";
39 +import * as HttpApi from "effect/http-api/HttpApi";
40 +import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint";
41 +import * as HttpApiGroup from "effect/http-api/HttpApiGroup";
42 ```
43
44 Append the endpoint and its group:
45
46 ```typescript
47 // src/api.ts (append)
48 const Me = HttpApiEndpoint.get("me", "/api/v1/me", {
49 success: User,
50 error: Unauthorized,
51 });
52
53 class AppRoutes extends HttpApiGroup.make("app").add(Me) {}
54 export class AppApi extends HttpApi.make("app").add(AppRoutes) {}
55 ```
56
57 `AppRoutes` is your application's group. `AppApi` describes that group, including
58 the path and response schema of `/api/v1/me`.
59
60 ## Implement the endpoint
61
62 Add these imports:
63
64 ```diff lang="typescript"
65 // src/api.ts
66 import * as Schema from "effect/Schema";
67 +import * as Effect from "effect/Effect";
68 +import * as Layer from "effect/Layer";
69 +import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
70 +import { Session } from "./session.ts";
71 ```
72
73 Append the implementation:
74
75 ```typescript
76 // src/api.ts (append)
77 const MeLive = HttpApiBuilder.group(AppApi, "app", (h) =>
78 h.handle("me", () =>
79 Effect.gen(function* () {
80 const { user } = yield* Session;
81 if (user === null) return yield* new Unauthorized();
82 return user;
83 }),
84 ),
85 );
86
87 export const AppApiLive = HttpApiBuilder.layer(AppApi).pipe(
88 Layer.provide(MeLive),
89 );
90 ```
91
92 The handler reads `Session`, which the middleware from Part 4 supplies.
93 `MeLive` implements your group; `AppApiLive` registers its HTTP routes.
94
95 ## Compose your routes with Git
96
97 Import the new route layer in `src/git.ts`:
98
99 ```diff lang="typescript"
100 // src/git.ts
101 import { Authentication } from "./middleware.ts";
102 +import { AppApiLive } from "./api.ts";
103 ```
104
105 Merge it beside Git before applying the middleware:
106
107 ```diff lang="typescript"
108 // src/git.ts
109 -const PublicRoutes = Git.ApiLive.pipe(
110 +const PublicRoutes = Layer.mergeAll(AppApiLive, Git.ApiLive).pipe(
111 Layer.provide(Authentication.layer),
112 );
113 ```
114
115 Both sets of routes now run with the same access policy and request session.
116 Your application's API definition and handlers stay in `src/api.ts`.
117
118 ## Deploy the application endpoint
119
120 <PackageManagers run="alchemy deploy" />
121
122 No new storage resource is needed for this change.
123
124 ## Call your endpoint
125
126 Use the session cookie from Part 4:
127
128 ```sh
129 curl --fail-with-body -b dana.cookies "$HOST/api/v1/me"
130 ```
131
132 Expect `{"id":"..."}` with the same ID as `$OWNER`. The API key also identifies
133 Dana:
134
135 ```sh
136 curl --fail-with-body -u "x:$KEY" "$HOST/api/v1/me"
137 ```
138
139 Without either credential, `/me` returns `401`:
140
141 ```sh
142 curl -i "$HOST/api/v1/me"
143 ```
144
145 Verify that Git still works on the same host:
146
147 ```sh
148 git -c credential.helper= -C work fetch origin
149 ```
150
151 Enter Dana's API key when prompted. Continue to
152 [Part 6: Protect a branch](/git/tutorial/part-6).