Skip to content

docs: Add Nuxt 3 example for openapi-fetch #2331

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/openapi-fetch/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ _Note: if you’re using Svelte without SvelteKit, the root example in `src/rout

[View a code example in GitHub](https://github.com/openapi-ts/openapi-typescript/tree/main/packages/openapi-fetch/examples/vue-3)

## Nuxt 3

[Nuxt 3](https://nuxtjs.org/) is a popular SSR framework for Vue 3. By combining Nuxt's built-in [useAsyncData](https://nuxt.com/docs/api/composables/use-async-data) composable with openapi-fetch, you can easily implement type-safe API communication with server-side rendering. This example demonstrates how to fetch data during SSR and enable client-side refetching.

[View a code example in GitHub](https://github.com/openapi-ts/openapi-typescript/tree/main/packages/openapi-fetch/examples/nuxt-3)

---

Additional examples are always welcome! Please [open a PR](https://github.com/openapi-ts/openapi-typescript/pulls) with your examples.
24 changes: 24 additions & 0 deletions packages/openapi-fetch/examples/nuxt-3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Nuxt dev/build outputs
.output
.data
.nuxt
.nitro
.cache
dist

# Node dependencies
node_modules

# Logs
logs
*.log

# Misc
.DS_Store
.fleet
.idea

# Local env files
.env
.env.*
!.env.example
168 changes: 168 additions & 0 deletions packages/openapi-fetch/examples/nuxt-3/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<script setup>
import { useAsyncData } from '#app';
import client from "@/lib/api";

// ssr fetching example
const { data: catFact, error, pending, refresh } = useAsyncData(
'catFact',
async () => {
const { data, error } = await client.GET("/fact", {
params: {
query: {
max_length: 140,
},
},
});

if (error) {
throw error;
}
return data;
}
);

const isRefetching = ref(false);

async function fetchNewFact() {
isRefetching.value = true;
await refresh();
isRefetching.value = false;
}
</script>

<template>
<div class="cat-fact-container">
<h1 class="title">Cat Facts</h1>

<div v-if="pending || isRefetching" class="loading">
<span class="loader"></span> Loading...
</div>

<div v-else-if="error" class="error">
<p>{{ error.message }}</p>
<p class="error-code">Error code: {{ error.code }}</p>
</div>

<div v-else-if="catFact" class="fact-card">
<p class="fact">{{ catFact.fact }}</p>
<span class="fact-length">Character count: {{ catFact.length }}</span>
</div>

<button @click="fetchNewFact" class="fetch-button" :disabled="pending || isRefetching">
{{ (pending || isRefetching) ? 'Fetching...' : 'Get New Cat Fact' }}
</button>
</div>
</template>

<style>
:root {
--color-primary: #00DC82;
--color-primary-dark: #00C476;
--color-primary-light: #94E3C9;
--color-secondary: #F3F4F6;
--color-text: #374151;
--color-text-light: #666666;
--color-error: #EF4444;
--color-error-bg: #FEE2E2;
--color-background: #F9FAFB;
}
</style>

<style scoped>
.cat-fact-container {
max-width: 600px;
margin: 0 auto;
padding: 2rem;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
color: var(--color-text);
}

.title {
font-size: 2rem;
margin-bottom: 2rem;
color: var(--color-primary);
text-align: center;
}

.loading {
display: flex;
align-items: center;
justify-content: center;
margin: 2rem 0;
color: var(--color-text-light);
}

.loader {
display: inline-block;
width: 1rem;
height: 1rem;
margin-right: 0.5rem;
border: 2px solid var(--color-primary);
border-radius: 50%;
border-top-color: transparent;
animation: spin 1s linear infinite;
}

@keyframes spin {
to {
transform: rotate(360deg);
}
}

.error {
background-color: var(--color-error-bg);
color: var(--color-error);
padding: 1rem;
border-radius: 0.5rem;
margin: 2rem 0;
}

.error-code {
font-size: 0.875rem;
opacity: 0.8;
}

.fact-card {
background-color: var(--color-secondary);
padding: 1.5rem;
border-radius: 0.5rem;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
margin: 2rem 0;
}

.fact {
font-size: 1.25rem;
line-height: 1.6;
margin-bottom: 1rem;
}

.fact-length {
display: block;
text-align: right;
font-size: 0.875rem;
color: var(--color-text-light);
}

.fetch-button {
display: block;
width: 100%;
padding: 0.75rem 1rem;
background-color: var(--color-primary);
color: white;
border: none;
border-radius: 0.5rem;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: background-color 0.2s;
}

.fetch-button:hover {
background-color: var(--color-primary-dark);
}

.fetch-button:disabled {
background-color: var(--color-primary-light);
cursor: not-allowed;
}
</style>
12 changes: 12 additions & 0 deletions packages/openapi-fetch/examples/nuxt-3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# openapi-fetch + Nuxt3

Example of using openapi-fetch with [Nuxt3](https://nuxt.com/).

## Setup

```sh
pnpm i
pnpm run dev
```

You’ll see the server running at `localhost:3000`
6 changes: 6 additions & 0 deletions packages/openapi-fetch/examples/nuxt-3/lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { paths } from "@/lib/api/v1";
import createClient from "openapi-fetch";

const client = createClient<paths>({ baseUrl: "https://catfact.ninja/" });

export default client;
Loading