Skip to content

Refactor: pass adminApiMiddleware into createKoaApp(); rename koaMiddleware to routesMiddleware#1849

Merged
pmcelhaney merged 4 commits intomainfrom
copilot/fix-admin-api-middleware-in-create-koa-app
Apr 14, 2026
Merged

Refactor: pass adminApiMiddleware into createKoaApp(); rename koaMiddleware to routesMiddleware#1849
pmcelhaney merged 4 commits intomainfrom
copilot/fix-admin-api-middleware-in-create-koa-app

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 13, 2026

Summary

adminApiMiddleware was being constructed inside createKoaApp(), coupling middleware lifecycle to the factory and making parallel server setups awkward. This moves construction to the caller and renames the ambiguous koaMiddleware to routesMiddleware throughout. The registry parameter has also been removed from createKoaApp() as it was only used for a debug log line that has been dropped.

Before:

// createKoaApp internally constructed adminApiMiddleware
const koaApp = createKoaApp(registry, middleware, config, contextRegistry);
// returned: { koaMiddleware: middleware, ... }

After:

const adminMiddleware = config.startAdminApi
  ? adminApiMiddleware(registry, contextRegistry, config)
  : undefined;
const koaApp = createKoaApp(middleware, config, adminMiddleware);
// returned: { routesMiddleware: middleware, ... }
Original Prompt

adminApiMiddleware should not be constructed inside createKoaApp(). That's making the parallel servers implementation difficult.

Also change the name of koaMiddleware to routesMiddleware.

Manual acceptance tests

  • Starting with startAdminApi: true still serves admin API routes at /_counterfact/api/health
  • Starting with startAdminApi: false results in no admin API routes being mounted
  • The routesMiddleware property returned from counterfact() is a callable Koa middleware function
  • Existing API routes (e.g. GET /pets) respond correctly after the refactor
  • Swagger UI at /counterfact/swagger still loads correctly

Tasks

  • Renamed koaMiddlewareroutesMiddleware in src/server/koa-middleware.ts (export) and all callsites
  • Removed ContextRegistry param from createKoaApp(); replaced with optional adminApiMiddleware?: Koa.Middleware
  • Removed registry param from createKoaApp() and dropped the debug("routes", registry.routes) line that was its only use
  • Moved adminApiMiddleware construction + startAdminApi guard into src/app.ts (the caller)
  • Updated returned object property koaMiddlewareroutesMiddleware in counterfact()
  • Updated docs, tests, and added changeset

Copilot AI changed the title [WIP] Fix adminApiMiddleware construction in createKoaApp Refactor: pass adminApiMiddleware into createKoaApp(); rename koaMiddleware to routesMiddleware Apr 14, 2026
Copilot AI requested a review from pmcelhaney April 14, 2026 00:02
Comment thread src/server/create-koa-app.ts Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line and remove registry from the arguments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 3b56164. Removed debug("routes", registry.routes) and the registry parameter from createKoaApp().

@pmcelhaney pmcelhaney marked this pull request as ready for review April 14, 2026 00:09
@pmcelhaney pmcelhaney enabled auto-merge April 14, 2026 00:09
@pmcelhaney pmcelhaney disabled auto-merge April 14, 2026 00:13
Copilot AI requested a review from pmcelhaney April 14, 2026 00:20
@pmcelhaney pmcelhaney enabled auto-merge April 14, 2026 01:00
@pmcelhaney pmcelhaney added this pull request to the merge queue Apr 14, 2026
Merged via the queue into main with commit 72c11d2 Apr 14, 2026
8 of 9 checks passed
@pmcelhaney pmcelhaney deleted the copilot/fix-admin-api-middleware-in-create-koa-app branch April 14, 2026 01:10
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.

adminApiMiddleware should be passed in to createKoaApp()

2 participants