Skip to content

Commit

Permalink
docs: use splitSetCookieString from cookie-es
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 21, 2024
1 parent 8aefb55 commit fa29c35
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions docs/1.getting-started/6.data-fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,15 @@ Be very careful before proxying headers to an external API and just include head
If you want to pass on/proxy cookies in the other direction, from an internal request back to the client, you will need to handle this yourself.

```ts [composables/fetch.ts]
import { appendResponseHeader, H3Event, splitCookiesString } from 'h3'
import { appendResponseHeader } from 'h3'
import type { H3Event } from 'h3'
import { splitSetCookieString } from 'cookie-es'

export const fetchWithCookie = async (event: H3Event, url: string) => {
/* Get the response from the server endpoint */
const res = await $fetch.raw(url)
/* Get the cookies from the response */
const cookies = splitCookiesString(res.headers.get('set-cookie') || '')
const cookies = splitSetCookieString(res.headers.get('set-cookie') || '')
/* Attach each cookie to our incoming Request */
for (const cookie of cookies) {
appendResponseHeader(event, 'set-cookie', cookie)
Expand Down

0 comments on commit fa29c35

Please sign in to comment.