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

http.ts

89 lines3.6 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 (
22 cause._tag === "ProjectNotFoundError" ||
23 cause._tag === "ProjectConflictError" ||
24 cause._tag === "ProjectNotEmptyError"
25 ) {
26 return yield* failEnvironmentInvalidRequest("invalid_command");
27 }
28 return yield* failEnvironmentInternal("project_mutation_failed", cause);
29});
30​
31export const projectHttpApiLayer = HttpApiBuilder.group(
32 EnvironmentHttpApi,
33 "projects",
34 Effect.fnUntraced(function* (handlers) {
35 const projects = yield* ProjectService;
36 const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup;
37​
38 return handlers
39 .handle(
40 "snapshot",
41 Effect.fn("environment.projects.snapshot")(function* (args) {
42 yield* annotateEnvironmentRequest(args.endpoint.name);
43 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
44 return yield* projects.snapshot.pipe(
45 Effect.catch((cause) => failEnvironmentInternal("project_snapshot_failed", cause)),
46 );
47 }),
48 )
49 .handle(
50 "mutate",
51 Effect.fn("environment.projects.mutate")(function* (args) {
52 yield* annotateEnvironmentRequest(args.endpoint.name);
53 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
54 const mutation = args.payload;
55 const operation =
56 mutation.type === "project.create"
57 ? projects.create({
58 commandId: mutation.commandId,
59 projectId: mutation.projectId,
60 title: mutation.title,
61 workspaceRoot: mutation.workspaceRoot,
62 ...(mutation.defaultModelSelection === undefined
63 ? {}
64 : { defaultModelSelection: mutation.defaultModelSelection }),
65 ...(mutation.scripts === undefined ? {} : { scripts: mutation.scripts }),
66 })
67 : mutation.type === "project.update"
68 ? projects.update({
69 commandId: mutation.commandId,
70 projectId: mutation.projectId,
71 ...(mutation.title === undefined ? {} : { title: mutation.title }),
72 ...(mutation.workspaceRoot === undefined
73 ? {}
74 : { workspaceRoot: mutation.workspaceRoot }),
75 ...(mutation.defaultModelSelection === undefined
76 ? {}
77 : { defaultModelSelection: mutation.defaultModelSelection }),
78 ...(mutation.scripts === undefined ? {} : { scripts: mutation.scripts }),
79 })
80 : projects.delete({
81 commandId: mutation.commandId,
82 projectId: mutation.projectId,
83 ...(mutation.force === undefined ? {} : { force: mutation.force }),
84 });
85 return yield* startup.enqueueCommand(operation).pipe(Effect.catch(failProjectMutation));
86 }),
87 );
88 }),
89);