docs: generate OpenAPI/Swagger documentation (closes #14) - #32
docs: generate OpenAPI/Swagger documentation (closes #14)#32Times-stack wants to merge 8 commits into
Conversation
- 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.
|
Resolve conflicts and failed CI |
|
I will resolve it soon |
|
Failed again. Try and fix it. |
|
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 Happy to open separate fix PRs for these once #32/#33 land, since they're unrelated to the swagger docs work here. |
|
Should I merge it like that? |
|
Yes the error is pr existing is not something I can fix from my end |
Closes #14
Summary
Adds automated OpenAPI 3.1 (Swagger) documentation generation via
swagger-jsdoc, served at
/api-docs(UI) and/api-docs.json(rawspec), 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@openapiannotation above theexisting
GET /handler, and mounted the new docs router.src/routes/docs.js: serves Swagger UI at/api-docsand theraw spec at
/api-docs.json.test/openapi.test.js: validates the generated spec againstthe OpenAPI schema (
@apidevtools/swagger-parser), confirms theroot 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 /) — theauth/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 andready for future routes to reference via
security:once thoseendpoints land.
Auth gate design decision
The issue asks for
/api-docsto be "protected behind authenticationin 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-docsand/api-docs.json, configured viaDOCS_USERNAME/DOCS_PASSWORDenvvars:
(401 without valid credentials).
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 arelease candidate (
7.0.0-rc.6) — no stable 7.x is published on npm.Used the stable
6.3.0instead, which fully supports OpenAPI 3.x.Also added
@apidevtools/swagger-parser(the actively maintainedfork of the older
swagger-parserpackage) as a dev dependency forthe 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 OpenAPItests.
/api-docs.jsonreturns a valid spec and/api-docsrenders the Swagger UI correctly.credentials, pass-through with correct ones, and (by code review of
the same code path) 503 when unconfigured.