Skip to content

refactor: decompose createHandler in fastify.ts into focused helper functions - #18

Draft
mike-joel with Copilot wants to merge 3 commits into
mar2026-run/WEBINY-3/copilot_web+claude_opus_4.6from
copilot/refactor-fastify-code
Draft

refactor: decompose createHandler in fastify.ts into focused helper functions#18
mike-joel with Copilot wants to merge 3 commits into
mar2026-run/WEBINY-3/copilot_web+claude_opus_4.6from
copilot/refactor-fastify-code

Conversation

Copilot AI commented Mar 6, 2026

Copy link
Copy Markdown

createHandler was a ~480-line monolithic function with significant duplication across route registration, error response serialization, and plugin execution patterns.

Structural decomposition

  • Extract 14 focused module-level functions (registerPreHandler, registerErrorHandler, createRouteHelpers, createContext, etc.) — createHandler is now ~40 lines that read as a sequential recipe
  • All helpers take explicit parameters (no closure captures over createHandler locals), improving testability

DRY patterns

  • Route helpers: 8 identical throwOnDefinedRoute(METHOD, path, options); app.method(path, handler) lambdas → single route() factory
  • Error responses: 5 duplicate JSON.stringify({ message, code, data })toErrorResponseBody()
  • Plugin execution: Repeated try/catch-log-rethrow loop → generic runPlugins<T>()
  • Headers constant: 4 inline { "Cache-Control": "no-store" } objects → NO_STORE_HEADERS
  • Route init: Hand-typed 18-key object literal → ALL_HTTP_METHODS array + createDefinedRoutes()

Control flow cleanup

  • throwOnDefinedRoute: replaced confusing if/else if/else if with early-return guards
// Before: confusing negated condition chains
} else if (definedRoutes[type].includes(path) === false) {
    return;
} else if (options?.override === true) {
    return;
}

// After: single clear guard
if (!definedRoutes[type].includes(path) || options?.override === true) {
    return;
}

All 13 existing tests pass unchanged.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • download.cypress.io
    • Triggering command: /opt/hostedtoolcache/node/24.14.0/x64/bin/node /opt/hostedtoolcache/node/24.14.0/x64/bin/node dist/index.js --exec install (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits March 6, 2026 13:49
…ions

Co-authored-by: mike-joel <23259716+mike-joel@users.noreply.github.com>
…ing comments

Co-authored-by: mike-joel <23259716+mike-joel@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor fastify.ts for improved code quality refactor: decompose createHandler in fastify.ts into focused helper functions Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants