// server/middleware/directus-proxy.js
import { createProxyMiddleware } from 'http-proxy-middleware'

export default defineEventHandler((event) => {
  const proxy = createProxyMiddleware({
    target: 'http://localhost:8055', // Your Directus URL
    changeOrigin: true,
    pathRewrite: {
      '^/api/directus': '', // Remove `/api/directus` when forwarding to Directus
    },
    onProxyReq(proxyReq) {
      // Add auth headers if needed (e.g., static token)
      proxyReq.setHeader('Authorization', 'Bearer YOUR_STATIC_TOKEN')
    },
  })

  // Forward the request
  return new Promise((resolve, reject) => {
    proxy(event.node.req, event.node.res, (err) => {
      if (err) reject(err)
      else resolve()
    })
  })
})