Environment and health
@exo/kit/env#
Exports: defineEnv, str, num, bool, url, enumOf, renderEnvExample, EnvError. Peer: zod 3.25 or newer, or 4.
You describe every variable once. The environment is read at boot, validated and typed from then on.
import { defineEnv, str, num, bool, url, renderEnvExample } from '@exo/kit/env';
export const schema = {
DATABASE_URL: url({
optional: true,
protocols: ['postgresql', 'postgres'],
describe: 'Postgres. Empty means the product runs without one.',
example: 'postgresql://app:<password>@postgres:5432/app',
}),
SESSION_SECRET: str({ min: 32, secret: true, describe: 'A long random string.' }),
SECURE_COOKIE: bool({ default: true, describe: 'Off only for http://localhost.' }),
PORT: num({ default: 3000, omitExample: true }),
};
export const env = defineEnv(schema, process.env);
- A bad value throws
EnvErrorlisting every bad variable by name and never by value, because such errors get pasted into chats. renderEnvExample(schema, { header })writes an.env.examplefrom the same schema, so the example is never a second copy of the truth.- A rule that spans fields, such as "set both halves of a key pair or neither", goes in the
refineoption. Its problems join the same error.
@exo/kit/health#
Exports: createHealth. No dependencies.
createHealth returns live() and ready(). Both return a Web Response with { status, version, checks }. live() checks nothing. ready() runs your checks and answers an error status only when a check listed in required fails.
import { createHealth } from '@exo/kit/health';
export const health = createHealth({
version: process.env.APP_VERSION ?? 'dev',
checks: {
db: async () => { await db`select 1`; return 'ok'; },
redis: async () => (await redis.ping()) === 'PONG',
search: async () => ((await searchUp()) ? 'ok' : 'fail'),
},
required: ['db', 'redis'],
reportError: telemetry.reportError,
});
// Fastify
app.get('/health/ready', async (_req, reply) => {
const res = await health.ready();
return reply.code(res.status).send(await res.json());
});
A check may return a boolean or a state. A check that is not in required reports itself without turning the product red. In Next.js, use export const GET = () => health.ready();.