1import * as Config from "effect/Config";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Interaction from "../Interaction.ts";
5import { AuthError } from "./AuthProvider.ts";
6
7export const getEnv = (key: string) =>
8 Config.option(Config.String(key)).pipe(
9 Effect.map(Option.getOrUndefined),
10 Effect.mapError(
11 (cause) =>
12 new AuthError({
13 message: `Could not read optional env: ${key}`,
14 cause,
15 }),
16 ),
17 );
18
19export const getEnvRequired = (key: string) =>
20 Config.String(key).pipe(
21 Effect.mapError(
22 (cause) =>
23 new AuthError({ message: `Missing required env: ${key}`, cause }),
24 ),
25 );
26
27export const getEnvRedacted = (key: string) =>
28 Config.option(Config.Redacted(key)).pipe(
29 Effect.map(Option.getOrUndefined),
30 Effect.mapError(
31 (cause) =>
32 new AuthError({
33 message: `Could not read optional env: ${key}`,
34 cause,
35 }),
36 ),
37 );
38
39export const getEnvRedactedRequired = (key: string) =>
40 Config.Redacted(key).pipe(
41 Effect.mapError(
42 (cause) =>
43 new AuthError({ message: `Missing required env: ${key}`, cause }),
44 ),
45 );
46
47export const mapPromptCancellation = <A, R>(
48 self: Effect.Effect<A, Interaction.InteractionError, R>,
49) =>
50 self.pipe(
51 Effect.mapError(
52 (cause) =>
53 new AuthError({
54 message:
55 cause._tag === "TerminalCancelled"
56 ? "User cancelled prompt"
57 : cause.message,
58 cause,
59 }),
60 ),
61 );