config.ts 897 B

1234567891011121314151617181920212223
  1. import { Config, ConfigProvider, Effect, Layer, Schema } from "effect"
  2. import * as Context from "effect/Context"
  3. import { Resource } from "sst/resource"
  4. export class AppConfigValue extends Schema.Class<AppConfigValue>("AppConfigValue")({
  5. stage: Schema.NonEmptyString,
  6. publicUrl: Schema.NonEmptyString,
  7. }) {}
  8. const decodeAppConfigValue = Schema.decodeUnknownSync(AppConfigValue)
  9. const config = Config.all({
  10. stage: Config.succeed(Resource.App.stage),
  11. publicUrl: Config.string("PUBLIC_URL").pipe(Config.withDefault("http://localhost:3000")),
  12. }).pipe(Config.map(decodeAppConfigValue))
  13. export class AppConfig extends Context.Service<AppConfig, AppConfigValue>()("@kirincode/stats/AppConfig") {
  14. static readonly config = config
  15. static readonly layer: Layer.Layer<AppConfig, never, never> = Layer.effect(
  16. AppConfig,
  17. config.parse(ConfigProvider.fromEnv()).pipe(Effect.orDie),
  18. )
  19. }