Add Workers-Safe Env Configuration
Validated config that works on Cloudflare Workers, read lazily so deploys never fail on missing secrets.
Install
npm i @dapplelight/signet-server zodDefine the schema
import { createEnv } from "@dapplelight/signet-server/env"
import { z } from "zod"
export const env = createEnv(
z.object({ DATABASE_URL: z.string().url(), SENTRY_DSN: z.string().url().optional() }),
{ optionalFeatures: [["SENTRY_DSN", "error reporting"]] },
)Validation runs on first property access, not at import.
Hand over the bindings first
import { initRuntimeEnv } from "@dapplelight/signet-server/env"
import { app } from "./app"
export default {
fetch(req: Request, bindings: Record<string, unknown>, ctx: ExecutionContext) {
initRuntimeEnv(bindings)
return app.fetch(req, bindings, ctx)
},
}Read it in a handler
import { env } from "../env"
import { connect } from "./connect"
const db = connect(env.DATABASE_URL)Never read env at module scope: Workers pass secrets per request.
Next
Add a Database
A scoped, soft-deleting, optionally encrypted database layer on D1, SQLite or Postgres, with money, an outbox and idempotent writes.
Add the Error Envelope and Request Validation
Every API error has the same JSON shape, validation failures carry field errors, and production hides internals.