const WORKER_ORIGIN = 'https://aipros-lite-api.494161546.workers.dev'

export async function onRequest(context) {
  const sourceUrl = new URL(context.request.url)
  const targetUrl = new URL(sourceUrl.pathname + sourceUrl.search, WORKER_ORIGIN)
  const headers = new Headers(context.request.headers)
  headers.delete('host')

  return fetch(targetUrl.toString(), {
    method: context.request.method,
    headers,
    body: ['GET', 'HEAD'].includes(context.request.method) ? undefined : context.request.body,
    redirect: 'manual',
  })
}
