Skip to content

Refactor prompt template: Workshop 5

Use this as a starting point. The prompt is more important than the tool. Iterate on the prompt, not the output.


You're a senior TypeScript engineer working in a small SaaS monorepo. You
follow these conventions:

- Functions are small (< 30 lines).
- Side effects (DB writes, notifications) are isolated into separate
  modules.
- Pure data transformations are extracted into functions named after what
  they produce (e.g. `buildCsvRows`, not `processData`).
- Existing test coverage must continue to pass. Do NOT change behavior.

Here is the file to refactor:

[PASTE THE FULL CONTENTS OF apps/api/src/routes/reports.ts HERE]

Refactor this file so the route handler is under 30 lines. Extract pure
helpers into separate functions in the same file. Keep all behavior
identical. Output ONLY the new contents of the file, as a single TypeScript
block. After the code, list the helper functions you extracted in a short
bulleted summary.

If anything is unclear or you need to make a judgment call, state your
assumption before the code.

Iteration tips

If the AI gives you back something that doesn't compile:

  • Add Note: this codebase uses ESM import syntax and runs TypeScript via tsx (node --import tsx), not CommonJS requires. to your prompt.

If the AI invents APIs (e.g., a dayjs import you don't have):

  • Add Use only the packages already imported at the top of the file. Do not introduce new dependencies. Note: reports.ts uses only native Date for date handling. Do not add a date library.

If the AI's refactor breaks an existing test:

  • Run the test, copy the failure message, paste it back into the chat with: This test failed after applying your refactor: [paste]. Fix the refactor to preserve the original behavior.

If the AI changes behavior on purpose ("I improved this to also do X"):

  • Add Do not improve, optimize, or alter behavior. Only restructure.

Capture in your logbook

Record these in your logbook as we go:

  • Number of prompt iterations before output was usable.
  • Whether existing tests passed after applying the refactor.
  • One specific thing the AI got right.
  • One specific thing you had to fix.