/** * @module EnvAccessor */ import { type StandardSchemaV1 } from "@standard-schema/spec"; import { type BaseEnvConfig, type IEnvAccessor, type RawEnvConfig } from "../../env-accessor/contracts/_module.js"; import { type UndefinedToNull, type OneOrMore, type AsyncLazyable } from "../../utilities/_module.js"; /** * Settings for configuring an {@link EnvAccessor | `EnvAccessor`} instance. * * @template TEnvConfig The environment config type. */ export type EnvAccessorSettings = { /** * The schema used to validate and type the environment config. */ schema: StandardSchemaV1>, TEnvConfig>; /** * One or more sources (sync/async/lazy) providing raw environment config values. */ sources: OneOrMore>; }; /** * `EnvAccessor` provides type-safe access to environment variables, with optional schema validation. * * It supports multiple sources (sync/async/lazy), schema validation, and convenient access patterns. * * @template TEnvConfig The environment config type. * @group Implementations */ export declare class EnvAccessor implements IEnvAccessor { private envConfig; private readonly schema; private readonly sources; /** * @example * ```ts * import { EnvAccessor } from "@daiso-tech/core/env-accessor"; * import { z } from "zod"; * import { SecretsManagerClient, GetSecretValueCommand } from "@aws-sdk/client-secrets-manager"; * * * // Combine AWS Secrets Manager and process.env as sources * // Note: The order matters—later sources override previous ones for overlapping keys. * const secretsManager = new SecretsManagerClient({ region: "us-east-1" }); * const sources = [ * process.env, * async () => { * const secret = await secretsManager.send( * new GetSecretValueCommand({ SecretId: "my-app/env" }) * ); * return JSON.parse(secret.SecretString ?? "{}"); * }, * ]; * * // Define a schema for your environment variables * const schema = z.object({ * NODE_ENV: z.string(), * PORT: z.string().default("3000").pipe(z.coerce.number()), * }); * * // Initialize the accessor * const accessor = new EnvAccessor({ schema, sources }); * await accessor.init(); * * // Retrieve a value * const port = accessor.get("PORT"); * ``` */ constructor(settings: EnvAccessorSettings); /** * Initialize the EnvAccessor by resolving all sources and validating the merged config. * * @returns Promise that resolves when initialization is complete. */ init(): Promise; get(field: TField): UndefinedToNull; getOr(field: TField, defaultValue: NonNullable): NonNullable; }