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

docs: get event before running async function #8861

Merged
merged 4 commits into from Nov 10, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 (url: string, event: H3Event) => {
const res = await $fetch.raw(url)
pi0 marked this conversation as resolved.
Show resolved Hide resolved
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('/api/with-cookie', event)
onMounted(() => console.log(document.cookie))
</script>
```
Expand Down