Skip to content

feat: Adds auto openapi doc generation + swagger UI for API #779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-guests-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperdx/api": patch
---

Adds openapidoc annotations for spec generation and swagger route for development
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ packages/app/.vercel
packages/app/coverage
packages/app/out

# OpenAPI spec
packages/public/openapi.json

# optional npm cache directory
**/.npm

Expand Down
1 change: 1 addition & 0 deletions packages/api/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ PORT=${HYPERDX_API_PORT}
REDIS_URL=redis://localhost:6379
USAGE_STATS_ENABLED=false
NODE_OPTIONS="--max-http-header-size=131072"
ENABLE_SWAGGER=true
6 changes: 5 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@
"@types/semver": "^7.3.12",
"@types/sqlstring": "^2.3.0",
"@types/supertest": "^2.0.12",
"@types/swagger-jsdoc": "^6",
"@types/uuid": "^8.3.4",
"jest": "^28.1.3",
"migrate-mongo": "^11.0.0",
"nodemon": "^2.0.20",
"rimraf": "^4.4.1",
"supertest": "^6.3.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"ts-jest": "^28.0.5",
"ts-node": "^10.8.1",
"tsc-alias": "^1.8.8",
Expand All @@ -98,6 +101,7 @@
"dev:migrate-db-create": "ts-node node_modules/.bin/migrate-mongo create -f migrate-mongo-config.ts",
"dev:migrate-db": "ts-node node_modules/.bin/migrate-mongo up -f migrate-mongo-config.ts",
"dev:migrate-ch-create": "migrate create -ext sql -dir ./migrations/ch -seq",
"dev:migrate-ch": "migrate -database 'clickhouse://localhost:9000?database=default&x-multi-statement=true' -path ./migrations/ch up"
"dev:migrate-ch": "migrate -database 'clickhouse://localhost:9000?database=default&x-multi-statement=true' -path ./migrations/ch up",
"docgen": "ts-node scripts/generate-api-docs.ts"
}
}
12 changes: 12 additions & 0 deletions packages/api/scripts/generate-api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'fs';
import path from 'path';
import swaggerJsdoc from 'swagger-jsdoc';

import { swaggerOptions } from '../src/utils/swagger';

const specs = swaggerJsdoc(swaggerOptions);
const outputPath = path.resolve(__dirname, '../../public/openapi.json');
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, JSON.stringify(specs, null, 2));

console.log(`OpenAPI specification written to ${outputPath}`);
11 changes: 10 additions & 1 deletion packages/api/src/api-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import externalRoutersV2 from './routers/external-api/v2';
import usageStats from './tasks/usageStats';
import { expressLogger } from './utils/logger';
import passport from './utils/passport';
import { setupSwagger } from './utils/swagger';

const app: express.Application = express();

Expand Down Expand Up @@ -102,7 +103,15 @@ app.use('/clickhouse-proxy', isUserAuthenticated, clickhouseProxyRouter);
// ---------------------------------------------------------------------
// ----------------------- External Routers ----------------------------
// ---------------------------------------------------------------------
// API v1
// API v2
// Only initialize Swagger in development or if explicitly enabled
if (
process.env.NODE_ENV !== 'production' ||
process.env.ENABLE_SWAGGER === 'true'
) {
setupSwagger(app);
}

app.use('/api/v2', externalRoutersV2);

// error handling
Expand Down
Loading