···11<script setup lang="ts">
22-import { getPetById, type Pet } from '~/client';
33-44-// START
55-const baseUrl = 'https://petstore3.swagger.io/api/v3';
66-const finalUrl = `${baseUrl}/pet/8`;
22+import { getPetById } from '~/client';
7388-// $fetch
99-// During SSR data is fetched twice, once on the server and once on the client.
1010-const dollarFetch = await getPetById({
1111- composable: '$fetch',
44+/**
55+ * useAsyncData
66+ *
77+ * During SSR data is fetched only on the server side and transferred to the
88+ * client.
99+ *
1010+ * This will NOT forward anything.
1111+ * Result: { cookies: {} }
1212+ */
1313+const asyncData = await getPetById({
1414+ asyncDataOptions: {},
1515+ composable: 'useAsyncData',
1616+ key: 'item',
1717+ path: {
1818+ petId: 8,
1919+ },
1220});
1313-const dollarFetchNuxt = await $fetch<Pet>(finalUrl);
14211515-// useAsyncData
1616-// During SSR data is fetched only on the server side and transferred to the client.
1717-const asyncData = await getPetById({
2222+/**
2323+ * useAsyncData + useRequestFetch
2424+ *
2525+ * This will forward the user's headers to the event handler.
2626+ * Result: { cookies: { foo: 'bar' } }
2727+ */
2828+const requestFetch = useRequestFetch();
2929+const asyncDataWithRequestFetch = await getPetById({
3030+ $fetch: requestFetch,
1831 composable: 'useAsyncData',
1919- key: 'item',
2020- asyncDataOptions: {},
3232+ path: {
3333+ petId: 8,
3434+ },
2135});
2222-const asyncDataNuxt = await useAsyncData<Pet>(() => $fetch(finalUrl));
2323-const asyncDataWithKeyNuxt = await useAsyncData<Pet>('item', () =>
2424- $fetch(finalUrl),
2525-);
26362727-// useFetch
2828-// You can also useFetch as shortcut of useAsyncData + $fetch
3737+/**
3838+ * useFetch
3939+ *
4040+ * You can also useFetch as shortcut of useAsyncData + $fetch.
4141+ */
2942const fetch = await getPetById({
3043 composable: 'useFetch',
3131- fetchOptions: {},
4444+ path: {
4545+ petId: 8,
4646+ },
3247});
3333-const fetchNuxt = await useFetch<Pet>(finalUrl);
34483535-// useLazyAsyncData
3636-/* Navigation will occur before fetching is complete.
3737- Handle 'pending' and 'error' states directly within your component's template
3838-*/
4949+/**
5050+ * useLazyAsyncData
5151+ *
5252+ * Navigation will occur before fetching is complete. Handle 'pending' and
5353+ * 'error' states directly within your component's template.
5454+ */
3955const lazyAsyncData = await getPetById({
4056 composable: 'useLazyAsyncData',
4157 key: 'count',
5858+ path: {
5959+ petId: 8,
6060+ },
4261});
4343-const lazyAsyncDataNuxt = await useLazyAsyncData<Pet>('count', () =>
4444- $fetch(finalUrl),
4545-);
4646-watch(lazyAsyncDataNuxt.data, (newCount) => {
4747- // Because count might start out null, you won't have access
6262+watch(lazyAsyncData.data, (newPet) => {
6363+ // Because pet might start out null, you won't have access
4864 // to its contents immediately, but you can watch it.
6565+ if (newPet) {
6666+ console.log(newPet.name);
6767+ }
4968});
50695151-// useLazyFetch
5252-/* Navigation will occur before fetching is complete.
5353- * Handle 'pending' and 'error' states directly within your component's template
7070+/**
7171+ * useLazyFetch
7272+ *
7373+ * Navigation will occur before fetching is complete. Handle 'pending' and
7474+ * 'error' states directly within your component's template.
5475 */
5576const lazyFetch = await getPetById({
5677 composable: 'useLazyFetch',
7878+ path: {
7979+ petId: 8,
8080+ },
5781});
5858-const lazyFetchNuxt = await useLazyFetch<Pet>(finalUrl);
5959-watch(lazyFetchNuxt.data, (newPosts) => {
6060- // Because posts might start out null, you won't have access
8282+watch(lazyFetch.data, (newPet) => {
8383+ // Because pet might start out null, you won't have access
6184 // to its contents immediately, but you can watch it.
8585+ if (newPet) {
8686+ console.log(newPet.name);
8787+ }
6288});
63896464-// useRequestFetch
6565-// This will forward the user's headers to the `/api/foo` event handler
6666-// Result: { cookies: { foo: 'bar' } }
6767-const requestFetch = useRequestFetch();
6868-const asyncDataFinal = await getPetById({
6969- composable: 'useAsyncData',
7070- requestFetch,
7171-});
7272-const asyncData2 = await useAsyncData<Pet>(() => requestFetch(finalUrl));
7373-// This will NOT forward anything
7474-// Result: { cookies: {} }
7575-const asyncData3 = await useAsyncData<Pet>(() => $fetch(finalUrl));
7676-7777-// END
7878-7979-async function handleClick() {
9090+async function handleFetch() {
8091 const result = await getPetById({
8192 composable: '$fetch',
8282- // @ts-expect-error
8393 path: {
8494 petId: 8,
8595 },
8696 });
8787- console.warn(result);
9797+ console.log(result);
8898}
8999</script>
9010091101<template>
102102+ <h1>Get Random Pet Nuxt APIs</h1>
92103 <div>
9393- <button @click="handleClick">Get Random Pet</button>
104104+ <button @click="handleFetch" type="button">$fetch</button>
94105 </div>
95106</template>