Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add custom fetch composable example #20115

Merged
merged 22 commits into from Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions docs/3.api/1.composables/use-fetch.md
Expand Up @@ -126,6 +126,9 @@ const { data, pending, error, refresh } = await useFetch('/api/auth/login', {
`useFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useFetch`.
::

::LinkExample{link="/docs/examples/other/use-custom-fetch-composable"}
::

:ReadMore{link="/docs/getting-started/data-fetching"}

::LinkExample{link="/docs/examples/composables/use-fetch"}
Expand Down
9 changes: 9 additions & 0 deletions docs/4.examples/8.other/use-custom-fetch-composable.md
@@ -0,0 +1,9 @@
---
toc: false
---

# Use custom fetch composable

This example shows a convenient wrapper for the useFetch composable from nuxt. It allows you to customize the fetch request with default values and user authentication token.

::sandbox{repo="nuxt/nuxt" branch="main" dir="examples/other/use-custom-fetch-composable" file="composables/useCustomFetch.ts"}
16 changes: 16 additions & 0 deletions examples/other/use-custom-fetch-composable/app.vue
@@ -0,0 +1,16 @@

<script setup lang="ts">

const { data } = await useCustomFetch<object>('/beers')

</script>

<template>
<NuxtExampleLayout example="other/use-custom-fetch-composable">
<h1 class="text-xl opacity-50">
Nuxt custom fetch
</h1>

<pre class="text-left text-xs">{{ data }}</pre>
</NuxtExampleLayout>
</template>
@@ -0,0 +1,32 @@
import type { UseFetchOptions } from 'nuxt/app'
import { defu } from 'defu'

export async function useCustomFetch<T> (url: string, options: UseFetchOptions<T> = {}) {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
const userAuth = useCookie('token')
const config = useRuntimeConfig()

const defaults: UseFetchOptions<T> = {
baseURL: config.baseUrl ?? 'https://api.nuxtjs.dev',
// cache request
key: url,

headers: new Headers(
userAuth.value // set user token if connected
? { Authorization: `Bearer ${userAuth.value}` }
: {}
),
danielroe marked this conversation as resolved.
Show resolved Hide resolved

onResponse (__ctx) {
// return new myBusinessResponse(response._data)
},

onResponseError (__ctx) {
// return new myBusinessError(error)
}
}

// for nice deep defaults, please use unjs/defu
const params = defu(defaults, options)

return await useFetch(url, params)
danielroe marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 6 additions & 0 deletions examples/other/use-custom-fetch-composable/nuxt.config.ts
@@ -0,0 +1,6 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
modules: [
'@nuxt/ui'
]
})
13 changes: 13 additions & 0 deletions examples/other/use-custom-fetch-composable/package.json
@@ -0,0 +1,13 @@
{
"name": "example-use-custom-fetch-composable",
"private": true,
"scripts": {
"build": "nuxi build",
"dev": "nuxi dev",
"start": "nuxi preview"
},
"devDependencies": {
"@nuxt/ui": "^0.3.3",
"nuxt": "^3.0.0"
}
}
4 changes: 4 additions & 0 deletions examples/other/use-custom-fetch-composable/tsconfig.json
@@ -0,0 +1,4 @@
{
// https://nuxt.com/docs/guide/concepts/typescript
"extends": "./.nuxt/tsconfig.json"
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.