Skip to content
Source
Sign inSign up

use-orchestration-command.ts

37 lines1.3 KB

1import type {
2 AtomCommand,
3 AtomCommandOptions,
4 AtomCommandResult,
5} from "@t3tools/client-runtime/state/runtime";
6import {
7 AuthOrchestrationOperateScope,
8 EnvironmentAuthorizationError,
9 type EnvironmentId,
10} from "@t3tools/contracts";
11import * as Cause from "effect/Cause";
12import { AsyncResult } from "effect/reactivity";
13​
14import { readEnvironmentScope } from "./session";
15import { useAtomCommand } from "./use-atom-command";
16​
17const ORCHESTRATION_OPERATE_DENIED_MESSAGE = "This connection cannot change threads or projects.";
18​
19export function useOrchestrationCommand<W extends { readonly environmentId: EnvironmentId }, A, E>(
20 command: AtomCommand<W, A, E>,
21 options?: string | AtomCommandOptions,
22): (value: W) => Promise<AtomCommandResult<A, E | EnvironmentAuthorizationError>> {
23 const run = useAtomCommand(command, options);
24 return async (value: W): Promise<AtomCommandResult<A, E | EnvironmentAuthorizationError>> => {
25 if (!readEnvironmentScope(value.environmentId, AuthOrchestrationOperateScope)) {
26 return AsyncResult.failure(
27 Cause.fail(
28 new EnvironmentAuthorizationError({
29 requiredScope: AuthOrchestrationOperateScope,
30 message: ORCHESTRATION_OPERATE_DENIED_MESSAGE,
31 }),
32 ),
33 );
34 }
35 return run(value);
36 };