feat(templates): typed env config utility (#60)#94
Merged
PAMulligan merged 1 commit intoMay 15, 2026
Conversation
Replaces raw process.env access in generated projects with a Zod-validated config module. Missing or malformed environment variables now fail fast at startup with a clear error instead of throwing somewhere deep in a handler. - Adds templates/snippets/node/src/config.ts parsing process.env at module load. JWT_SECRET is required (min 32 chars) in production and defaulted in development/test; LOG_LEVEL defaults to debug in dev, info in prod. - Adds templates/snippets/cloudflare/src/config.ts exporting envSchema and parseConfig(env), since Workers don't expose process.env. Wired into index.ts as a middleware that parses c.env once and stores the typed Config on c.var.config. - Updates health routes and index.ts on both platforms to read from config instead of process.env / c.env. - Adds JWT_SECRET to .env.example and .dev.vars.example with guidance. - Pre-seeds required env vars in shared tests/setup.ts so the Node config module can parse at import time during unit tests. - Adds zod to templates/snippets/package.json so the typecheck-templates CI job resolves it. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #60. Adds a typed, validated environment-config module to the generated project templates so that missing or malformed env vars fail fast at startup instead of crashing in a handler.
src/config.tssnippets for both platforms, exposed viasetup-project.sh:process.envat module load with Zod.JWT_SECRETis required (min 32 chars) in production, defaulted in dev/test;LOG_LEVELdefaults todebugin dev andinfoin prod.envSchema+parseConfig(env)(noprocess.envin Workers).index.tswires it as middleware that parsesc.envonce and stores typedConfigonc.var.config.src/index.tsandsrc/routes/health.tsnow read fromconfiginstead of rawprocess.env/c.env..env.exampleand.dev.vars.exampledocumentJWT_SECRET.tests/setup.tspre-seeds required env vars so the Nodeconfigmodule parses cleanly at import time.zodtotemplates/snippets/package.jsonso thetypecheck-templatesCI job resolves it.Test plan
pnpm typecheckintemplates/snippets/(bothtsconfig.cloudflare.jsonandtsconfig.node.json)bash -n scripts/setup-project.shbash scripts/setup-project.sh test-api --node --dry-runshowsconfig.tscopiedbash scripts/setup-project.sh test-api-cf --cloudflare --dry-runshowsconfig.tscopied.env,pnpm testpasses🤖 Generated with Claude Code