Skip to content

feat: v0.2 — template-driven real handlers in generateRoutes() #3

@stackbilt-admin

Description

@stackbilt-admin

Problem

generateRoutes() currently emits stub handlers:

lines.push(`  // TODO: implement ${routeName} handler`);
lines.push(`  return c.json({ status: 'ok' });`);

Specs pass but handlers are hollow. No DB queries, no response shaping, no error handling. This blocks FoodFiles domain contracts (Stackbilt-dev/foodfilesapi_v2#23) from producing usable code.

Decided Approach: Handlebars-style string templates

Per prior architecture decision:

  1. Template files under src/generators/templates/ with placeholders ({{contractName}}, {{inputSchema}}, {{refImports}}, etc.)
  2. Zod walker builds a context object, feeds it to templates
  3. Generated handler bodies include:
    • req.body validation against Zod schema (already done)
    • DB queries via surfaces.db helpers (db.prepare().bind().run() for D1)
    • Standard success/error response shaping (try/catch with errorResponse() envelope)
    • State transition guards from contract.states
    • Authority middleware from contract.authority (already done)
  4. Prettier pass on output

Why Templates (Not AST)

  • Speed: instant iteration on contract definitions
  • Auditability: deterministic, diffable output
  • Low risk: no complex AST transforms — aligns with speed + rigor

AST-based generation (ts-morph) is v0.3, gated on template coverage ≥ 90%.

Scope

Must have (v0.2)

  • Route template: CRUD handler bodies (create, read, update, delete, list)
  • Route template: state transition operations (e.g., publish, archive)
  • DB query generation from surfaces.db.table + schema columns
  • ref() → JOIN clause generation
  • Error handling: try/catch with structured error response
  • Test harness: generated code compiles, zero TODO markers

Also fix

  • generateSQL() — validate against FoodFiles migration 001 (recipe/org tables)
  • generateSDK() — fix syntax bug (unmatched paren line 72)
  • generateTests() — emit integration tests that hit handlers, not just schema validation

Validation Target

Write RecipeContract (state machine: draft→published→archived, refs to Organization + User, invariant: published requires ingredients). Run all generators. Diff SQL against FoodFiles 001_fresh_multitenant_schema.sql.

Roadmap

Version Approach Status
v0.1 Stub handlers Done
v0.2 Template-driven real handlers This issue
v0.3 AST-based generation (ts-morph) Planned
v0.4 CI integration + no-TODO lint rule Planned

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions