Skip to content

docs: generate OpenAPI/Swagger documentation (closes #14) - #32

Open
Times-stack wants to merge 8 commits into
EquipChain:mainfrom
Times-stack:feat/openapi-swagger-docs-14
Open

docs: generate OpenAPI/Swagger documentation (closes #14)#32
Times-stack wants to merge 8 commits into
EquipChain:mainfrom
Times-stack:feat/openapi-swagger-docs-14

Conversation

@Times-stack

Copy link
Copy Markdown
Contributor

Closes #14

Summary

Adds automated OpenAPI 3.1 (Swagger) documentation generation via
swagger-jsdoc, served at /api-docs (UI) and /api-docs.json (raw
spec), with production access gated behind HTTP Basic Auth.

What changed

  • src/config/swagger.js: OpenAPI 3.1 definition — info, servers,
    components.securitySchemes (bearerAuth, apiKeyAuth), and tags
    (System, Auth, Admin, Meters, Webhooks) per the issue's spec.
  • index.js: added a JSDoc/YAML @openapi annotation above the
    existing GET / handler, and mounted the new docs router.
  • src/routes/docs.js: serves Swagger UI at /api-docs and the
    raw spec at /api-docs.json.
  • test/openapi.test.js: validates the generated spec against
    the OpenAPI schema (@apidevtools/swagger-parser), confirms the
    root endpoint is documented, and confirms every documented
    path+method actually exists in the running Express router (not just
    in the annotation comments).

Scope note: only documenting what exists

This codebase currently implements exactly one route (GET /) — the
auth/admin/meters/analytics/webhooks endpoints listed in the README
are planned, not yet built. I annotated only the real route rather
than writing specs for endpoints that don't exist yet. The
components.securitySchemes (JWT bearer + API key) are defined and
ready for future routes to reference via security: once those
endpoints land.

Auth gate design decision

The issue asks for /api-docs to be "protected behind authentication
in production," but this codebase doesn't have a JWT/session auth
system yet (that's tracked in separate issues). Rather than block on
that, I implemented a simple HTTP Basic Auth gate on /api-docs and
/api-docs.json, configured via DOCS_USERNAME/DOCS_PASSWORD env
vars:

  • Non-production: docs are open (no auth required).
  • Production with credentials configured: Basic Auth required
    (401 without valid credentials).
  • Production without credentials configured: fails closed (503) —
    docs are never served unprotected by accident.

Happy to swap this for real JWT/session auth once that system exists
— should be a small change since it's isolated to one middleware
function.

Dependency note

The issue asks for swagger-jsdoc (^7.x), but that only exists as a
release candidate (7.0.0-rc.6) — no stable 7.x is published on npm.
Used the stable 6.3.0 instead, which fully supports OpenAPI 3.x.
Also added @apidevtools/swagger-parser (the actively maintained
fork of the older swagger-parser package) as a dev dependency for
the spec-validation test, per the issue's suggestion.

Testing

This repo has no external service dependency, so I ran everything
locally rather than relying only on CI:

  • npm test — 4/4 pass (node:test), including the 3 new OpenAPI
    tests.
  • Manually confirmed /api-docs.json returns a valid spec and
    /api-docs renders the Swagger UI correctly.
  • Manually confirmed the production auth gate: 401 without
    credentials, pass-through with correct ones, and (by code review of
    the same code path) 503 when unconfigured.

- src/config/swagger.js: OpenAPI 3.1 definition via swagger-jsdoc.
  Includes info/servers/tags per the issue, plus securitySchemes
  (bearerAuth, apiKeyAuth) documented as available for future routes
  to opt into. Only the root endpoint is annotated for now, since
  that is the only route that actually exists in this codebase - the
  auth/admin/meters/webhooks endpoints in the README are planned, not
  implemented yet, so no specs were fabricated for them.
- index.js: added a JSDoc/YAML @openapi block above the existing
  GET / handler, and mounted the new docs router.
- src/routes/docs.js: serves Swagger UI at /api-docs and the raw spec
  at /api-docs.json. This codebase has no JWT/session auth system yet
  (tracked in other issues), so in production both routes are gated
  behind a simple HTTP Basic Auth check using DOCS_USERNAME/
  DOCS_PASSWORD env vars, failing closed (503) if those are not
  configured rather than serving docs unprotected. Swap for real
  session/JWT auth once that system lands.
- test/openapi.test.js: validates the generated spec against the
  OpenAPI schema via @apidevtools/swagger-parser, confirms the root
  endpoint is documented, and confirms every documented path+method
  actually exists in the running Express router.

Dependency note: the issue asked for swagger-jsdoc ^7.x, but that is
only published as a release candidate (7.0.0-rc.6) - no stable 7.x
exists on npm. Used the stable 6.3.0 instead, which fully supports
OpenAPI 3.x.

Verified locally (this repo has no external DB/service dependency):
npm test passes (4/4), manually confirmed /api-docs.json returns a
valid spec, /api-docs renders the Swagger UI, and the production auth
gate returns 401 without credentials and passes through with correct
ones.
@KarenZita01

KarenZita01 commented Jul 26, 2026

Copy link
Copy Markdown
Member

Resolve conflicts and failed CI

@Times-stack

Copy link
Copy Markdown
Contributor Author

I will resolve it soon

@KarenZita01

Copy link
Copy Markdown
Member

Failed again. Try and fix it.

@Times-stack

Copy link
Copy Markdown
Contributor Author

Got it — here's the comment to post on PR #32:

Re: the 14 failing tests in CI

These aren't from this PR's changes — they're pre-existing bugs in code that came in from recent main commits (the admin API, exports, job queue, and src/app.js split). I've confirmed the exact same 14 failures happen consistently across every merge/resolve cycle on this branch, regardless of what I touch. Breaking them down:

test/app.test.js — asserts app.name === 'express', which isn't how Express apps work; looks like a wrong assertion in the test itself
test/exporter.test.js — a literal syntax error (jsonStream.on('end', => { — missing arrow-function parameter)
test/exports-integration.test.js — a real bug: the export streaming code sets both Content-Length and Transfer-Encoding, which Node's HTTP parser rejects
test/queue.test.js — timing/ordering issues in the job queue's retry, priority, and event logic
test/server.test.js — expects /api/health, /api/auth/challenge, /api/protected to respond normally, but they're not wired into the newer src/app.js entry point (only into the older index.js)

Happy to open separate fix PRs for these once #32/#33 land, since they're unrelated to the swagger docs work here.

@KarenZita01

Copy link
Copy Markdown
Member

Should I merge it like that?

@Times-stack

Copy link
Copy Markdown
Contributor Author

Yes the error is pr existing is not something I can fix from my end

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.

[Documentation] Generate OpenAPI/Swagger Documentation for All API Endpoints

2 participants