1import * as DistilledRegion from "@distilled.cloud/gcp/Region";
2import * as Effect from "effect/Effect";
3import type * as Layer from "effect/Layer";
4import { GcpEnvironment } from "./Environment.ts";
5
6export type {
7 RegionalEndpointMode,
8 RegionName,
9} from "@distilled.cloud/gcp/Region";
10
11/**
12 * Override the default GCP region — the region regional resources land in
13 * when created without an explicit `location` / `region`.
14 *
15 * The region normally comes with the credentials (the profile's
16 * `region`), else `us-central1`. Provide `GCP.Region(...)` on the
17 * providers layer to override that for a stack. There is no environment
18 * variable: Google's SDKs don't define one, and deployed GCP runtimes
19 * read their region from the metadata server. A resource that should live elsewhere takes
20 * an explicit `location`; a recorded location always wins, so changing
21 * the region never moves a deployed resource.
22 *
23 * ### Choosing a region
24 * **Example:** Deploy a stack to europe-west1
25 * ```typescript
26 * export default Alchemy.Stack(
27 * "App",
28 * {
29 * providers: GCP.providers().pipe(
30 * Layer.provideMerge(GCP.Region("europe-west1")),
31 * ),
32 * state: Alchemy.localState(),
33 * },
34 * program,
35 * );
36 * ```
37 */
38export const Region = (
39 region: DistilledRegion.RegionName,
40): Layer.Layer<DistilledRegion.Region> => DistilledRegion.of(region);
41
42/**
43 * Route requests to Google's regional endpoints
44 * (`{service}.{region}.rep.googleapis.com`):
45 *
46 * - `"required"` (default): only where the global endpoint rejects
47 * regional resources (Secret Manager, Parameter Manager).
48 * - `"prefer"`: every request for a regional resource whose service
49 * publishes a regional endpoint, so traffic terminates in-region.
50 * - `"never"`: always the global endpoint.
51 *
52 * ### Data residency
53 * **Example:** Keep every regional request in-region
54 * ```typescript
55 * GCP.providers().pipe(
56 * Layer.provideMerge(GCP.Region("europe-west1")),
57 * Layer.provideMerge(GCP.RegionalEndpoints("prefer")),
58 * );
59 * ```
60 */
61export const RegionalEndpoints = (
62 mode: DistilledRegion.RegionalEndpointMode,
63): Layer.Layer<DistilledRegion.RegionalEndpoints> =>
64 DistilledRegion.regionalEndpoints(mode);
65
66/** The effective default region in the current scope. */
67export const currentRegion = Effect.suspend(() =>
68 GcpEnvironment.use((env) => Effect.map(env, ({ region }) => region)),
69);