update swagger for api
This commit is contained in:
+14
-8
@@ -2,6 +2,8 @@ import express from 'express'
|
||||
import cors from 'cors'
|
||||
import helmet from 'helmet'
|
||||
import morgan from 'morgan'
|
||||
import swaggerUi from 'swagger-ui-express'
|
||||
import { openApiDocument } from './swagger/openapi'
|
||||
import { getLegacyStorageRoots, getStorageRoot } from './lib/storage'
|
||||
import { authLimiter, apiLimiter, publicLimiter, adminLimiter } from './middleware/rateLimiter'
|
||||
|
||||
@@ -100,6 +102,10 @@ export function createApp() {
|
||||
app.use('/storage', express.static(legacyRoot))
|
||||
}
|
||||
|
||||
// Swagger UI — mounted before helmet so its assets are not blocked by CSP
|
||||
app.use('/docs', swaggerUi.serve, swaggerUi.setup(openApiDocument, { customSiteTitle: 'RentalDriveGo API Docs' }))
|
||||
app.get('/api/v1/openapi.json', (_req, res) => res.json(openApiDocument))
|
||||
|
||||
// Webhook must use raw body BEFORE express.json()
|
||||
app.use('/api/v1/webhooks', express.raw({ type: 'application/json' }), webhookRouter)
|
||||
|
||||
@@ -141,14 +147,14 @@ export function createApp() {
|
||||
})
|
||||
|
||||
app.get(`${v1}/docs`, (_req, res) => {
|
||||
res.json({ name: 'rentaldrivego-api', version: '1.0.0', baseUrl: v1, docsUrl: '/docs', routes: routeDocs })
|
||||
})
|
||||
|
||||
app.get('/docs', (_req, res) => {
|
||||
const rows = routeDocs
|
||||
.map((r) => `<tr><td>${r.method}</td><td><code>${r.path}</code></td><td>${r.description}</td></tr>`)
|
||||
.join('')
|
||||
res.type('html').send(`<!doctype html><html lang="en"><head><meta charset="utf-8"/><title>RentalDriveGo API Docs</title></head><body><h1>RentalDriveGo API</h1><table><thead><tr><th>Method</th><th>Path</th><th>Description</th></tr></thead><tbody>${rows}</tbody></table></body></html>`)
|
||||
res.json({
|
||||
name: 'rentaldrivego-api',
|
||||
version: '1.0.0',
|
||||
baseUrl: v1,
|
||||
routes: routeDocs,
|
||||
swaggerUi: '/docs',
|
||||
openApi: '/api/v1/openapi.json',
|
||||
})
|
||||
})
|
||||
|
||||
// ─── Error handler ──────────────────────────────────────────
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -17,5 +17,14 @@ describe('GET /health', () => {
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.body.name).toBe('rentaldrivego-api')
|
||||
expect(Array.isArray(res.body.routes)).toBe(true)
|
||||
expect(res.body.swaggerUi).toBe('/docs')
|
||||
expect(res.body.openApi).toBe('/api/v1/openapi.json')
|
||||
})
|
||||
|
||||
it('GET /api/v1/openapi.json returns the OpenAPI document', async () => {
|
||||
const res = await request(app).get('/api/v1/openapi.json')
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.body.openapi).toBe('3.0.3')
|
||||
expect(res.body.info?.title).toBe('RentalDriveGo API')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user