ferdikoomen / openapi-typescript-codegen

NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification
MIT License
2.8k stars 515 forks source link

NextJS Server Comoponents #2133

Open mstosio opened 2 months ago

mstosio commented 2 months ago

I've got cookie based auth but I cannot make it work on my NextJS Project in server components.

Working examples:

  1. Working example in server component

    
    const getData2 = async () => {
    try {
    
        const response = await fetch(new URL(SAME_URL_AS_IN_LOGOUT_SERVICE), {
            headers: { Cookie: cookies().toString() },
        })
    
        return response.json()
    
    } catch(err){
        console.log(err)
        // console.log("error", err)
    }

} ...... ..... Server component: const data = await getData() // auth works here

2. Working example in client component, looks like cookies are correctly passed here.
```javascript
use client

React.useEffect(() => {
    const getData = async () => {

        const getDataNotWorkingAuth = async () => {
            try {
                return await LogoutService.isAuthorizedUsingGet();
            } catch (err) {
                console.log(err);
            }
        }

        const data = await getDataNotWorkingAuth(); // Auth works here
    }

    getData();
}, []);

Not working example in server component:

OpenAPI.HEADERS = {
    Cookie: cookies().toString()
}

const getDataNotWorkingAuth = async () => {
    try {

        return await LogoutService.isAuthorizedUsingGet()
    } catch(err){
        console.log(err)
        // console.log("error", err)
    }

}

...
...
Server component:
const data =  getDataNotWorkingAuth() //not authenticated

Do you have any idea how to make it work with typescript codegen?

mrlubos commented 2 months ago

@mstosio If you're interested in resolving this, you'll have to migrate to https://github.com/hey-api/openapi-ts and then we can have a look

mstosio commented 2 months ago

@mrlubos I'll try to do it ;)