Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
docs: get event before running async function (#8861)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Nov 10, 2022
1 parent 84c26fa commit 5715128
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions docs/content/1.getting-started/6.data-fetching.md
Expand Up @@ -238,9 +238,8 @@ The example below adds the request headers to an isomorphic `$fetch` call to ens

```vue
<script setup>
const { data } = await useFetch('/api/me', {
headers: useRequestHeaders(['cookie'])
})
const headers = useRequestHeaders(['cookie'])
const { data } = await useFetch('/api/me', { headers })
</script>
```

Expand All @@ -262,11 +261,11 @@ Here is a list of common headers that are NOT to be proxied:
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]
export const fetchWithCookie = async (url: string) => {
export const fetchWithCookie = async (event: H3Event, url: string) => {
const res = await $fetch.raw(url)
const cookies = (res.headers.get('set-cookie') || '').split(',')
for (const cookie of cookies) {
appendHeader(useRequestEvent(), 'set-cookie', cookie)
appendHeader(event, 'set-cookie', cookie)
}
return res._data
}
Expand All @@ -275,7 +274,8 @@ export const fetchWithCookie = async (url: string) => {
```vue
<script setup lang="ts">
// This composable will automatically pass cookies to the client
const result = await fetchWithCookie('/api/with-cookie')
const event = useRequestEvent()
const result = await fetchWithCookie(event, '/api/with-cookie')
onMounted(() => console.log(document.cookie))
</script>
```
Expand Down

0 comments on commit 5715128

Please sign in to comment.