Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

http.ts

84 lines3.4 KB

1import {
2 AuthOrchestrationOperateScope,
3 AuthOrchestrationReadScope,
4 EnvironmentHttpApi,
5} from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
8​
9import {
10 annotateEnvironmentRequest,
11 failEnvironmentInternal,
12 failEnvironmentInvalidRequest,
13 requireEnvironmentScope,
14} from "../auth/http.ts";
15import * as ServerRuntimeStartup from "../serverRuntimeStartup.ts";
16import { ProjectService, type ProjectServiceError } from "./ProjectService.ts";
17​
18export const failProjectMutation = Effect.fn("environment.projects.failMutation")(function* (
19 cause: ProjectServiceError | ServerRuntimeStartup.ServerRuntimeStartupError,
20) {
21 if (cause._tag === "ProjectNotFoundError" || cause._tag === "ProjectConflictError") {
22 return yield* failEnvironmentInvalidRequest("invalid_command");
23 }
24 return yield* failEnvironmentInternal("project_mutation_failed", cause);
25});
26​
27export const projectHttpApiLayer = HttpApiBuilder.group(
28 EnvironmentHttpApi,
29 "projects",
30 Effect.fnUntraced(function* (handlers) {
31 const projects = yield* ProjectService;
32 const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup;
33​
34 return handlers
35 .handle(
36 "snapshot",
37 Effect.fn("environment.projects.snapshot")(function* (args) {
38 yield* annotateEnvironmentRequest(args.endpoint.name);
39 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
40 return yield* projects.snapshot.pipe(
41 Effect.catch((cause) => failEnvironmentInternal("project_snapshot_failed", cause)),
42 );
43 }),
44 )
45 .handle(
46 "mutate",
47 Effect.fn("environment.projects.mutate")(function* (args) {
48 yield* annotateEnvironmentRequest(args.endpoint.name);
49 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
50 const mutation = args.payload;
51 const operation =
52 mutation.type === "project.create"
53 ? projects.create({
54 commandId: mutation.commandId,
55 projectId: mutation.projectId,
56 title: mutation.title,
57 workspaceRoot: mutation.workspaceRoot,
58 ...(mutation.defaultModelSelection === undefined
59 ? {}
60 : { defaultModelSelection: mutation.defaultModelSelection }),
61 ...(mutation.scripts === undefined ? {} : { scripts: mutation.scripts }),
62 })
63 : mutation.type === "project.update"
64 ? projects.update({
65 commandId: mutation.commandId,
66 projectId: mutation.projectId,
67 ...(mutation.title === undefined ? {} : { title: mutation.title }),
68 ...(mutation.workspaceRoot === undefined
69 ? {}
70 : { workspaceRoot: mutation.workspaceRoot }),
71 ...(mutation.defaultModelSelection === undefined
72 ? {}
73 : { defaultModelSelection: mutation.defaultModelSelection }),
74 ...(mutation.scripts === undefined ? {} : { scripts: mutation.scripts }),
75 })
76 : projects.delete({
77 commandId: mutation.commandId,
78 projectId: mutation.projectId,
79 });
80 return yield* startup.enqueueCommand(operation).pipe(Effect.catch(failProjectMutation));
81 }),
82 );
83 }),
84);