Skip to content

feat(templates): typed env config utility (#60)#94

Merged
PAMulligan merged 1 commit into
mainfrom
60-add-environment-specific-configuration-template
May 15, 2026
Merged

feat(templates): typed env config utility (#60)#94
PAMulligan merged 1 commit into
mainfrom
60-add-environment-specific-configuration-template

Conversation

@PAMulligan
Copy link
Copy Markdown
Contributor

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.

  • New src/config.ts snippets for both platforms, exposed via setup-project.sh:
    • Node: parses process.env at module load with Zod. JWT_SECRET is required (min 32 chars) in production, defaulted in dev/test; LOG_LEVEL defaults to debug in dev and info in prod.
    • Cloudflare Workers: exports envSchema + parseConfig(env) (no process.env in Workers). index.ts wires it as middleware that parses c.env once and stores typed Config on c.var.config.
  • src/index.ts and src/routes/health.ts now read from config instead of raw process.env / c.env.
  • .env.example and .dev.vars.example document JWT_SECRET.
  • Shared tests/setup.ts pre-seeds required env vars so the Node config module parses cleanly at import time.
  • Adds zod to templates/snippets/package.json so the typecheck-templates CI job resolves it.

Test plan

  • pnpm typecheck in templates/snippets/ (both tsconfig.cloudflare.json and tsconfig.node.json)
  • bash -n scripts/setup-project.sh
  • bash scripts/setup-project.sh test-api --node --dry-run shows config.ts copied
  • bash scripts/setup-project.sh test-api-cf --cloudflare --dry-run shows config.ts copied
  • CI green
  • Smoke: generate a fresh project, set .env, pnpm test passes

🤖 Generated with Claude Code

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>
@PAMulligan PAMulligan linked an issue May 15, 2026 that may be closed by this pull request
6 tasks
@github-actions github-actions Bot added area: scripts Automation scripts area: templates Starter templates labels May 15, 2026
@PAMulligan PAMulligan merged commit 190f1a1 into main May 15, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: scripts Automation scripts area: templates Starter templates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add environment-specific configuration template

1 participant