x-control-panel/api-sandbox/app/vite.config.js

96 lines
2.5 KiB
JavaScript

import { defineConfig } from 'vite'
import { resolve } from 'path'
import copy from 'rollup-plugin-copy'
export default defineConfig({
root: '.',
build: {
outDir: '../src/main/resources/htdocs/api-sandbox',
rollupOptions: {
plugins: [
copy({
targets: [
{
src: resolve(__dirname, '../../api.spec.json'),
dest: 'dist'
}
],
hook: 'writeBundle'
})
]
}
},
resolve: {
alias: {
'@': resolve(__dirname, './')
}
},
server: {
port: 3000,
fs: {
allow: ['..', '../..']
},
historyApiFallback: false
},
publicDir: false,
configureServer(server) {
server.middlewares.use('/', (req, res, next) => {
if (req.url === '/api.spec.json' && req.method === 'GET') {
const fs = require('fs')
const path = require('path')
const apiSpecPath = path.resolve(__dirname, './api.spec.json')
console.log('Serving api.spec.json from:', apiSpecPath)
try {
const content = fs.readFileSync(apiSpecPath, 'utf-8')
res.setHeader('Content-Type', 'application/json')
res.setHeader('Cache-Control', 'no-cache')
res.end(content)
} catch (error) {
console.error('Error reading api.spec.json:', error)
res.statusCode = 404
res.setHeader('Content-Type', 'text/plain')
res.end('File not found')
}
} else {
next()
}
})
// Mock API endpoint for testing
server.middlewares.use('/api', (req, res, next) => {
if (req.method === 'POST') {
let body = ''
req.on('data', chunk => {
body += chunk.toString()
})
req.on('end', () => {
try {
const requestData = JSON.parse(body)
const response = {
jsonrpc: '2.0',
result: {
success: true,
message: `Method ${requestData.method} called with params: ${JSON.stringify(requestData.params)}`,
timestamp: new Date().toISOString()
},
id: requestData.id
}
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(response))
} catch (error) {
res.statusCode = 400
res.end(JSON.stringify({
jsonrpc: '2.0',
error: { code: -32700, message: 'Parse error' },
id: null
}))
}
})
} else {
next()
}
})
}
})