import { Config, Schema as S } from "effect";
const allowedHostnames = S.Literal("localhost", "127.0.0.1", "0.0.0.0");
type Hostname = typeof allowedHostnames.Type;
const allowedPorts = S.NumberFromString.pipe(S.between(1, 65535));
type Port = typeof allowedPorts.Type;
const appConfig = Config.all({
host: envVar<Hostname>("HOST", allowedHostnames as S.Schema<Hostname, string>), // how to avoid casting?
port: envVar<Port>("PORT", allowedPorts), // this works
});
function envVar<A>(
name: string,
schema: S.Schema<A, string>
): Config.Config<A> {
return S.Config(name, schema);
}
const program = Effect.gen(function* () {
const app = yield* appConfig
console.log(`ok: ${app.host}`)
console.log(`ok: ${app.port}`)
})
Effect.runSync(program)
import { Config, Schema as S } from "effect";
const allowedHostnames = S.Literal("localhost", "127.0.0.1", "0.0.0.0");
type Hostname = typeof allowedHostnames.Type;
const allowedPorts = S.NumberFromString.pipe(S.between(1, 65535));
type Port = typeof allowedPorts.Type;
const appConfig = Config.all({
host: envVar<Hostname>("HOST", allowedHostnames as S.Schema<Hostname, string>), // how to avoid casting?
port: envVar<Port>("PORT", allowedPorts), // this works
});
function envVar<A>(
name: string,
schema: S.Schema<A, string>
): Config.Config<A> {
return S.Config(name, schema);
}
const program = Effect.gen(function* () {
const app = yield* appConfig
console.log(`ok: ${app.host}`)
console.log(`ok: ${app.port}`)
})
Effect.runSync(program)