1import * as Http from "@/Http/index.ts";
2import * as Layer from "effect/Layer";
3/** Standard Effect HTTP group registration with an application user handler. */
4import { GitApi, GroupsLive, InternalApiLive, Handlers } from "@/Git/index.ts";
5import * as Effect from "effect/Effect";
6import * as Option from "effect/Option";
7import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
8import * as HttpServerResponse from "effect/http/HttpServerResponse";
9import { TestCaller, TestAuthLive } from "./test-auth.ts";
10
11export const GitHubLive = HttpApiBuilder.group(GitApi, "github", (h) =>
12 Effect.map(Handlers, (git) =>
13 h.handleAll({
14 ...git.github,
15 user: () =>
16 Effect.gen(function* () {
17 const caller = yield* Effect.serviceOption(TestCaller);
18 const user = Option.isSome(caller) ? caller.value.user : null;
19 return user === null
20 ? HttpServerResponse.jsonUnsafe(
21 { message: "Requires authentication" },
22 { status: 401 },
23 )
24 : HttpServerResponse.jsonUnsafe({
25 login: user.name,
26 id: 1,
27 type: "User",
28 });
29 }),
30 }),
31 ),
32);
33
34/** The test application's router: public Git routes plus the internal hash route. */
35export const TestRoutes = Layer.mergeAll(
36 HttpApiBuilder.layer(GitApi).pipe(
37 Layer.provide(Layer.mergeAll(GroupsLive, GitHubLive)),
38 Layer.provide(TestAuthLive),
39 ),
40 InternalApiLive,
41).pipe(Layer.provide(Http.Platform));