@smitejs/env
Declarative environment variables for Smite: provider, zod validation, collect-mode manifest.
yarn add @smitejs/env8 documented symbols · version 2.0.0-SNAPSHOT.0 · source
Providers
typeEnvProvider
type EnvProvider = (key: string) => Promise<unknown>;Source of raw environment values: (key) => Promise<raw>.
Registration
interfaceEnvBuilder
interface EnvBuilder<Specs extends EnvSpecs> {
readonly withProvider: (provider: EnvProvider, options?: EnvProviderOptions) => EnvInstance<Specs>;
}Builder returned by register; attach a provider to get an instance.
typeEnvSpec
type EnvSpec<Value = unknown> = {
readonly key: string;
readonly validation: z.ZodType<Value>;
};A declared variable: the raw provider key plus its zod validation.
typeEnvSpecs
type EnvSpecs = Readonly<Record<string, EnvSpec>>;A map of declared name to its spec.
functionregister
function register<Specs extends EnvSpecs>(entries: Specs): EnvBuilder<Specs>;Declares a set of env variables and returns a builder that becomes a scoped instance once a provider is attached. Fails on duplicate names. In collect mode each variable is also registered as an env.var node.
const instance = env
.register({
databaseUrl: { key: "DATABASE_URL", validation: z.string() },
})
.withProvider(from({ DATABASE_URL: "postgres://x" }));
const url = await instance.databaseUrl;"postgres://x"const instance = env
.register({
port: { key: "PORT", validation: z.coerce.number().default(3000) },
})
.withProvider(async () => undefined);
const port = await instance.port;3000const instance = env
.register({
debug: { key: "DEBUG", validation: z.string().optional() },
})
.withProvider(async () => undefined);
const debug = await instance.debug;Resolution
typeEnvInstance
type EnvInstance<Specs extends EnvSpecs> = {
readonly [Name in keyof Specs]: Promise<z.infer<Specs[Name]["validation"]>>;
};The resolved instance: one lazily-resolved property per declared name.
typeEnvProviderOptions
type EnvProviderOptions = {
readonly cache?: boolean;
};Per-instance options passed to EnvBuilder.withProvider.
Surface
constenv
const env: {
register: typeof register;
};The env factory: declare variables, attach a provider, read values.
const instance = env
.register({
databaseUrl: { key: "DATABASE_URL", validation: z.string() },
})
.withProvider(from({ DATABASE_URL: "postgres://x" }));
const url = await instance.databaseUrl;"postgres://x"