unjs / h3

⚡️ Minimal H(TTP) framework built for high performance and portability
https://h3.unjs.io/
MIT License
3.51k stars 204 forks source link

`proxyRequest` does not use new req headers set `event.headers.set()` #763

Open lammerfalcon opened 3 months ago

lammerfalcon commented 3 months ago

Environment

Nitro v2.9.6     
Node v20.12.2

Basic nuxt 3 project

Reproduction

https://stackblitz.com/edit/github-c51gfm?file=app.vue,server%2Fapi%2Fauth%2Flogin.post.ts,server%2Fapi%2F%5B...%5D.ts

Describe the bug

import {joinURL} from "ufo";

export default defineEventHandler((event) => {
    const authCookie = getCookie(event, 'authorization')
    const {proxyUrl} = useRuntimeConfig()
    const path = event.path.replace(/^\/api\//, '')
    const target = joinURL(proxyUrl, path)
    event.headers.set('authorization', `Bearer ${authCookie}`) // does not work
    Object.assign(event.node.req.headers, { Authorization: 'Bearer ' + authCookie}) // works
    console.log(event.headers)
    return proxyRequest(event, target, {
        headers:{
            authorization: `Bearer ${authCookie}` // works
        }
    })
})

event.headers.set() does not apply to proxyRequest, but other variants works well

Additional context

No response

Logs

No response

lammerfalcon commented 3 months ago

Any info?(

pi0 commented 3 months ago

Hi, sorry for the late reply. Two-way sync is not possible today, unfortunately between event.node and event.node.req.headers

I would suggest to use ...event.node.req.headers in proxy request.

I will move this to more relevant place (h3)

pi0 commented 1 month ago

Hi. With upcoming v2, two way sync will be possible (https://github.com/unjs/h3/pull/829)