Skip to content

refactor: split server.ts into routed modules - #36

Merged
debba merged 1 commit into
mainfrom
refactor/server-routes
Sep 3, 2026
Merged

refactor: split server.ts into routed modules#36
debba merged 1 commit into
mainfrom
refactor/server-routes

Conversation

@debba

@debba debba commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Summary

src/server.ts was a 1475-line file mixing bootstrap, GraphQL documents, HTML scraping and a long chain of url.startsWith(...) checks. This PR splits it up and puts routing on find-my-way, the router used by Fastify, while keeping Node's native http request/response model.

File Role
src/server.ts bootstrap only (16 lines)
src/server/app.ts builds the request handler: app routes, static files, API router, SPA fallback
src/server/router.ts typed wrapper over find-my-way (RouteContext, 405 handling)
src/server/spa.ts index.html serving and client-route detection
src/server/routes/* one module per domain: auth, accounts, dashboard, repository, mentions, projects, notifications
src/server/graphql/* GraphQL documents, separated from HTTP handlers
src/utils/dependents.ts dependents HTML parser as pure logic, with tests

Repeated code is centralised: parseJsonBody, sendPayload, sendRedirect in http.ts; requireRepo / sendError in routes/shared.ts.

Behaviour changes

  • Routes match by method and exact path instead of prefix. The frontend already uses exact paths for every endpoint, so no client change is needed.
  • A wrong HTTP verb answers 405 with an Allow header and the same JSON body as before; per-handler req.method checks are gone.
  • An unknown /api/* path answers a JSON 404 instead of index.html.
  • Fixes the pre-existing type error in the GitLab OAuth callback handler.

Build

find-my-way is CommonJS and the ESM bundle produced by esbuild failed at startup with Dynamic require of "node:assert" is not supported. The build script now adds the standard createRequire banner.

Verification

  • npm run typecheck: no errors in the new code (the 5 pre-existing errors in views, openaiDigest.ts and colors.test.ts are untouched).
  • npm test: 23 files, 116 tests passing, including 8 new ones for the router and the dependents parser.
  • Smoke-tested both node dist/server.js and tsx src/server.ts: SPA routes, static files, 401 without login, 400, 404 and 405 all respond as expected.

Replace the 1400-line prefix-matching dispatcher in src/server.ts with a
find-my-way router and one route module per domain (auth, accounts,
dashboard, repository, mentions, projects, notifications). GraphQL
documents move to src/server/graphql, the dependents HTML parser becomes
pure logic in src/utils with unit tests, and repeated body/repo/redirect
handling is centralised in http.ts and routes/shared.ts.

Routes now match by method and exact path: a wrong verb answers 405 with
an Allow header and an unknown /api/* path answers a JSON 404 instead of
index.html. The esbuild bundle gets a createRequire banner so the
CommonJS router loads under ESM.
@debba
debba force-pushed the refactor/server-routes branch from 8f3774d to b693f20 Compare September 3, 2026 14:04
@debba
debba merged commit 8c0b742 into main Sep 3, 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.

1 participant