Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add custom fetch composable example (#20115)
- Loading branch information
Showing
8 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
31 changes: 31 additions & 0 deletions
31
examples/other/use-custom-fetch-composable/composables/useCustomFetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { UseFetchOptions } from 'nuxt/app' | ||
import { defu } from 'defu' | ||
|
||
export function useCustomFetch<T> (url: string, options: UseFetchOptions<T> = {}) { | ||
const userAuth = useCookie('token') | ||
const config = useRuntimeConfig() | ||
|
||
const defaults: UseFetchOptions<T> = { | ||
baseURL: config.baseUrl ?? 'https://api.nuxtjs.dev', | ||
// cache request | ||
key: url, | ||
|
||
// set user token if connected | ||
headers: userAuth.value | ||
? { Authorization: `Bearer ${userAuth.value}` } | ||
: {}, | ||
|
||
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 useFetch(url, params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
export default defineNuxtConfig({ | ||
modules: [ | ||
'@nuxt/ui' | ||
] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
// https://nuxt.com/docs/guide/concepts/typescript | ||
"extends": "./.nuxt/tsconfig.json" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.