49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
// This is a direct endpoint to serve the OpenAPI file
|
|
// Simpler than the dynamic route to avoid any issues
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
// Set appropriate headers
|
|
setHeader(event, 'Content-Type', 'application/json')
|
|
setHeader(event, 'Cache-Control', 'no-cache')
|
|
setHeader(event, 'Access-Control-Allow-Origin', '*')
|
|
|
|
console.log('Direct OpenAPI file endpoint called')
|
|
|
|
// Hard-coded path to the openapi.json file
|
|
const filePath = path.join(process.cwd(), 'docs', 'openapi.json')
|
|
|
|
// Check if file exists
|
|
if (!fs.existsSync(filePath)) {
|
|
console.error(`OpenAPI file not found at ${filePath}`)
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'OpenAPI file not found'
|
|
})
|
|
}
|
|
|
|
// Read file content
|
|
const content = fs.readFileSync(filePath, 'utf-8')
|
|
|
|
// Parse JSON to validate it
|
|
try {
|
|
const parsed = JSON.parse(content)
|
|
return parsed
|
|
} catch (error) {
|
|
console.error(`Failed to parse OpenAPI JSON: ${error}`)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Invalid OpenAPI JSON format'
|
|
})
|
|
}
|
|
} catch (error) {
|
|
console.error('Error serving OpenAPI file:', error)
|
|
throw createError({
|
|
statusCode: error.statusCode || 500,
|
|
statusMessage: error.message || 'Failed to serve OpenAPI file'
|
|
})
|
|
}
|
|
})
|