Skip to content

feat: OpenApiDocument is now a class with built-in file watching and EventTarget#1826

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/convert-openapi-document-to-class
Draft

feat: OpenApiDocument is now a class with built-in file watching and EventTarget#1826
Copilot wants to merge 3 commits intomainfrom
copilot/convert-openapi-document-to-class

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 11, 2026

Summary

OpenApiDocument was a plain interface — loading and watching the spec file were handled by separate loadOpenApiDocument and OpenApiWatcher helpers. This PR collapses all three responsibilities into a single class.

src/server/openapi-document.ts — new class:

  • Extends EventTarget; dispatches "reload" whenever the file changes on disk
  • new OpenApiDocument(source) — captures source path
  • await doc.load() — reads + dereferences the spec, populates paths, basePath, produces
  • await doc.watch() / await doc.stopWatching() — file-system watcher (no-op for "_" and HTTP sources)

src/server/load-openapi-document.ts — now just creates an instance and calls load().

src/server/dispatcher.ts — retains the old OpenApiDocument interface for backward-compat; class instances satisfy it structurally so existing tests and consumers pass plain objects unchanged.

src/app.tsOpenApiWatcher removed; replaced by openApiDocument?.watch() / openApiDocument?.stopWatching().

const doc = new OpenApiDocument("/path/to/openapi.json");
await doc.load();
await doc.watch(); // starts watching; emits "reload" on changes
doc.addEventListener("reload", () => { /* react to live spec updates */ });
await doc.stopWatching();
Original Prompt

OpenAPIDocument should be a class

It should know the location of its source file. It should read the file and initialize itself. It should watch for changes (instead of a separate OpenAPIWatcher). It should extend EventTarget so it can notify others when it has been reloaded (those that formerly relied on OpenAPIWatcher).

Manual acceptance tests

  • Edit the OpenAPI spec file while the server is running — routes reflect the new spec without a restart
  • loadOpenApiDocument() returns an instance with paths, basePath, and produces populated correctly
  • Stopping the server cleanly closes the watcher (no dangling process or unhandled-rejection warnings)
  • openApiPath: "_" starts the server with no file watcher active
  • Routes that existed before a live reload continue to respond correctly (regression)

Tasks

  • Created src/server/openapi-document.ts: OpenApiDocument extends EventTarget with load(), watch(), stopWatching()
  • Simplified src/server/load-openapi-document.ts to delegate to the new class
  • Removed OpenApiWatcher usage from src/app.ts; wired openApiDocument.watch() / stopWatching() in its place
  • Kept the backward-compatible OpenApiDocument interface in dispatcher.ts so plain-object test fixtures continue to compile
  • Added test/server/openapi-document.test.ts with 8 tests (EventTarget, load, error handling, reload event, path update after reload, no-op watch paths)
  • Added minor changeset

Copilot AI linked an issue Apr 11, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Convert OpenAPIDocument to a class for improved functionality feat: OpenApiDocument is now a class with built-in file watching and EventTarget Apr 11, 2026
Copilot AI requested a review from pmcelhaney April 11, 2026 02:43
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.

OpenAPIDocument should be a class

2 participants