1import * as Region from "@distilled.cloud/aws/Region";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { AWSEnvironment } from "./Environment.ts";
5
6export { AWS_REGION, type RegionID } from "./Environment.ts";
7export { Region } from "@distilled.cloud/aws/Region";
8
9declare module "@distilled.cloud/aws/Region" {
10 interface Region {
11 readonly kind: "Environment";
12 }
13}
14
15export const of = (region: string) =>
16 Layer.succeed(Region.Region, Effect.succeed(region));
17
18export const fromEnvOrElse = (region: string) =>
19 Layer.succeed(
20 Region.Region,
21 Effect.succeed(process.env.AWS_REGION ?? region),
22 );
23
24// Deferred with `Effect.suspend` so it does not dereference `AWSEnvironment`
25// during module evaluation. `Region.ts` and `Environment.ts` are part of an
26// import cycle (AuthProvider → Region → Environment → AuthProvider); touching
27// `AWSEnvironment` eagerly at top level hits a temporal-dead-zone error when
28// this module is evaluated mid-cycle.
29export const CurrentRegion = Effect.suspend(() =>
30 AWSEnvironment.use((env) =>
31 Effect.flatMap(env, ({ region }) => Effect.succeed(region)),
32 ),
33);
34
35/**
36 * Derive the AWS region from the surrounding {@link AWSEnvironment}.
37 */
38export const fromEnvironment = Layer.effect(
39 Region.Region,
40 Effect.gen(function* () {
41 const env = yield* AWSEnvironment;
42 return Effect.map(env, (env) => env.region);
43 }),
44);