summaryrefslogtreecommitdiff
path: root/u/process/env.ts
diff options
context:
space:
mode:
Diffstat (limited to 'u/process/env.ts')
-rw-r--r--u/process/env.ts13
1 files changed, 7 insertions, 6 deletions
diff --git a/u/process/env.ts b/u/process/env.ts
index 1e4fd32..9a55488 100644
--- a/u/process/env.ts
+++ b/u/process/env.ts
@@ -1,10 +1,11 @@
-import { Either, type IEither } from '@emprespresso/pengueno';
+import { IOptional, Either, Optional, type IEither } from '@emprespresso/pengueno';
-export const getRequiredEnv = <V extends string>(name: V): IEither<Error, V> =>
- Either.fromFailable<Error, V | undefined>(() => process.env[name] as V | undefined) // could throw when no permission.
- .flatMap(
- (v) => (v && Either.right(v)) || Either.left(new Error(`environment variable "${name}" is required D:`)),
- );
+export const getEnv = <V extends string>(name: string): IOptional<V> => Optional.from(<V>process.env[name]);
+
+export const getRequiredEnv = <V extends string>(name: string): IEither<Error, V> =>
+ Either.fromFailable(() => getEnv<V>(name).get()).mapLeft(
+ () => new Error(`environment variable "${name}" is required D:`),
+ );
type ObjectFromList<T extends ReadonlyArray<string>, V = string> = {
[K in T extends ReadonlyArray<infer U> ? U : never]: V;