summaryrefslogtreecommitdiff
path: root/u/process/env.ts
blob: 65f4e631bf94b0d2553bdad36f4271f161628c49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { Either, type IEither } from "@emprespresso/pengueno";

export const getRequiredEnv = (name: string): IEither<Error, string> =>
  Either
    .fromFailable<Error, string | undefined>(() => Deno.env.get(name)) // could throw when no permission.
    .flatMap((v) =>
      (v &&
        Either.right(v)) ||
      Either.left(
        new Error(`environment variable "${name}" is required D:`),
      )
    );

export const getRequiredEnvVars = (vars: Array<string>) =>
  vars
    .map((envVar) =>
      [envVar, getRequiredEnv(envVar)] as [string, IEither<Error, string>]
    )
    .reduce((acc, x: [string, IEither<Error, string>]) => {
      const [envVar, eitherVal] = x;
      return acc.flatMap((args) => {
        return eitherVal.mapRight((envValue) => ({
          ...args,
          [envVar]: envValue,
        }));
      });
    }, Either.right<Error, Record<string, string>>({}));