Environment variables
Declaring which variables a scope needs and how they validate.
package @smitejs/env
env.register(specs) is the declaration step: a map of name to a { key, validation } pair. This is where you answer "what does this scope require, and what shape is acceptable?" — exactly once, in one place. It returns a builder you finish with withProvider.
The spec is the contract
Each entry has two parts:
key— the raw provider key, i.e. theprocess.envvariable name the provider is asked for ("DATABASE_URL").validation— the zod schema driving everything: type coercion, optionality, defaults, and error messages.
The declared name is the property you read on the instance (databaseUrl), and the zod schema is also the type source — await appEnv.databaseUrl has the inferred type.
ts
const instance = env
.register({
databaseUrl: { key: "DATABASE_URL", validation: z.string() },
})
.withProvider(from({ DATABASE_URL: "postgres://x" }));
const url = await instance.databaseUrl;ts
"postgres://x"tested in packages/env/src/index.test.ts:128
Required, optional, defaulted
z.string()— the variable must be present and a string.z.string().optional()— a missing variable resolves toundefined.z.coerce.number().default(3000)— missing variables fall back to the default; strings like"8080"are coerced to numbers.
ts
const instance = env
.register({
port: { key: "PORT", validation: z.coerce.number().default(3000) },
})
.withProvider(async () => undefined);
const port = await instance.port;ts
3000tested in packages/env/src/index.test.ts:141
Build-time manifest
Each declared name is also recorded in a build-time manifest. The future CLI uses it to scaffold .env.example files and to fail the build on missing required variables at compile time instead of at boot.