|
1 | | -export * as Env from "./env" |
| 1 | +import { Context, Effect, Layer } from "effect" |
| 2 | +import { InstanceState } from "@/effect" |
| 3 | + |
| 4 | +type State = Record<string, string | undefined> |
| 5 | + |
| 6 | +export interface Interface { |
| 7 | + readonly get: (key: string) => Effect.Effect<string | undefined> |
| 8 | + readonly all: () => Effect.Effect<State> |
| 9 | + readonly set: (key: string, value: string) => Effect.Effect<void> |
| 10 | + readonly remove: (key: string) => Effect.Effect<void> |
| 11 | +} |
| 12 | + |
| 13 | +export class Service extends Context.Service<Service, Interface>()("@opencode/Env") {} |
| 14 | + |
| 15 | +export const layer = Layer.effect( |
| 16 | + Service, |
| 17 | + Effect.gen(function* () { |
| 18 | + const state = yield* InstanceState.make<State>(Effect.fn("Env.state")(() => Effect.succeed({ ...process.env }))) |
| 19 | + |
| 20 | + const get = Effect.fn("Env.get")((key: string) => InstanceState.use(state, (env) => env[key])) |
| 21 | + const all = Effect.fn("Env.all")(() => InstanceState.get(state)) |
| 22 | + const set = Effect.fn("Env.set")(function* (key: string, value: string) { |
| 23 | + const env = yield* InstanceState.get(state) |
| 24 | + env[key] = value |
| 25 | + }) |
| 26 | + const remove = Effect.fn("Env.remove")(function* (key: string) { |
| 27 | + const env = yield* InstanceState.get(state) |
| 28 | + delete env[key] |
| 29 | + }) |
| 30 | + |
| 31 | + return Service.of({ get, all, set, remove }) |
| 32 | + }), |
| 33 | +) |
| 34 | + |
| 35 | +export const defaultLayer = layer |
| 36 | + |
| 37 | +export * as Env from "." |
0 commit comments