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

http.ts

59 lines2.1 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 { normalizeDispatchCommand } from "./Normalizer.ts";
10import {
11 annotateEnvironmentRequest,
12 failEnvironmentInternal,
13 failEnvironmentInvalidRequest,
14 requireEnvironmentScope,
15} from "../auth/http.ts";
16import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts";
17import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";
18​
19export const orchestrationHttpApiLayer = HttpApiBuilder.group(
20 EnvironmentHttpApi,
21 "orchestration",
22 Effect.fnUntraced(function* (handlers) {
23 const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
24 const orchestrationEngine = yield* OrchestrationEngineService;
25​
26 return handlers
27 .handle(
28 "snapshot",
29 Effect.fn("environment.orchestration.snapshot")(function* (args) {
30 yield* annotateEnvironmentRequest(args.endpoint.name);
31 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
32 return yield* projectionSnapshotQuery
33 .getSnapshot()
34 .pipe(
35 Effect.catch((cause) =>
36 failEnvironmentInternal("orchestration_snapshot_failed", cause),
37 ),
38 );
39 }),
40 )
41 .handle(
42 "dispatch",
43 Effect.fn("environment.orchestration.dispatch")(function* (args) {
44 yield* annotateEnvironmentRequest(args.endpoint.name);
45 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
46 const normalizedCommand = yield* normalizeDispatchCommand(args.payload).pipe(
47 Effect.catch(() => failEnvironmentInvalidRequest("invalid_command")),
48 );
49 return yield* orchestrationEngine
50 .dispatch(normalizedCommand)
51 .pipe(
52 Effect.catch((cause) =>
53 failEnvironmentInternal("orchestration_dispatch_failed", cause),
54 ),
55 );
56 }),
57 );
58 }),
59);