63 lines
2.0 KiB
JavaScript
63 lines
2.0 KiB
JavaScript
// Simple direct OpenAPI JSON endpoint
|
|
// This acts as a fallback if the main server endpoint fails
|
|
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
// Set appropriate headers for JSON response
|
|
setHeader(event, 'Content-Type', 'application/json')
|
|
setHeader(event, 'Cache-Control', 'no-cache')
|
|
|
|
// Allow CORS
|
|
setHeader(event, 'Access-Control-Allow-Origin', '*')
|
|
setHeader(event, 'Access-Control-Allow-Methods', 'GET, OPTIONS')
|
|
setHeader(event, 'Access-Control-Allow-Headers', 'Content-Type, Authorization')
|
|
|
|
// Handle OPTIONS request for CORS preflight
|
|
if (event.node.req.method === 'OPTIONS') {
|
|
return {}
|
|
}
|
|
|
|
console.log('Serving direct OpenAPI JSON...')
|
|
|
|
// Read the OpenAPI JSON file directly
|
|
const openApiPath = path.join(process.cwd(), 'docs', 'openapi.json')
|
|
|
|
console.log(`Looking for OpenAPI file at: ${openApiPath}`)
|
|
|
|
if (!fs.existsSync(openApiPath)) {
|
|
console.error(`OpenAPI file not found at: ${openApiPath}`)
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'OpenAPI file not found'
|
|
})
|
|
}
|
|
|
|
// Read and return the raw file content
|
|
const content = fs.readFileSync(openApiPath, 'utf-8')
|
|
|
|
try {
|
|
// Parse to ensure it's valid JSON
|
|
const parsed = JSON.parse(content)
|
|
console.log('Successfully parsed OpenAPI JSON')
|
|
return parsed
|
|
} catch (parseError) {
|
|
console.error(`Failed to parse OpenAPI JSON: ${parseError.message}`)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Invalid JSON format',
|
|
data: { error: parseError.message }
|
|
})
|
|
}
|
|
} catch (error) {
|
|
console.error('Error serving direct OpenAPI JSON:', error)
|
|
|
|
throw createError({
|
|
statusCode: error.statusCode || 500,
|
|
statusMessage: error.message || 'Failed to serve OpenAPI JSON',
|
|
data: { error: error.message }
|
|
})
|
|
}
|
|
})
|