docs: use headers.getSetCookie

This commit is contained in:
Daniel Roe 2024-07-21 21:28:20 +02:00
parent 29f95ae0d4
commit 45c6df9a4c
No known key found for this signature in database
GPG Key ID: CBC814C393D93268

View File

@ -468,13 +468,12 @@ Be very careful before proxying headers to an external API and just include head
```ts [composables/fetch.ts] ```ts [composables/fetch.ts]
import { appendResponseHeader } from 'h3' import { appendResponseHeader } from 'h3'
import type { H3Event } from 'h3' import type { H3Event } from 'h3'
import { splitSetCookieString } from 'cookie-es'
export const fetchWithCookie = async (event: H3Event, url: string) => { export const fetchWithCookie = async (event: H3Event, url: string) => {
/* Get the response from the server endpoint */ /* Get the response from the server endpoint */
const res = await $fetch.raw(url) const res = await $fetch.raw(url)
/* Get the cookies from the response */ /* Get the cookies from the response */
const cookies = splitSetCookieString(res.headers.get('set-cookie') || '') const cookies = res.headers.getSetCookie()
/* Attach each cookie to our incoming Request */ /* Attach each cookie to our incoming Request */
for (const cookie of cookies) { for (const cookie of cookies) {
appendResponseHeader(event, 'set-cookie', cookie) appendResponseHeader(event, 'set-cookie', cookie)