{{ post.title }}
{{ post.excerpt }}
Fast, type-safe @tanstack/vue-query client to work with your OpenAPI schema.
npm install openapi-vue-query
A type-safe and lightweight (1kb) wrapper around @tanstack/vue-query for consuming OpenAPI schemas in Vue.js applications.
Built on top of openapi-fetch and openapi-typescript, providing:
- ✅ 100% type-safe - No typos in URLs, params, request bodies, or responses
- ✅ Zero runtime overhead - Types are generated at build time
- ✅ Automatic type inference - No manual typing of your API
- ✅ Eliminates any types that hide bugs
- ✅ Eliminates as type overrides that can hide bugs
- ✅ Vue 3 Composition API support with full reactivity
- ✅ All @tanstack/vue-query features - Caching, background updates, optimistic updates, etc.
``bash`
npm i openapi-vue-query openapi-fetch
npm i -D openapi-typescript typescript
Generate TypeScript types from your OpenAPI schema:
`bash`
npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts
`vue
{{ data?.content }}
Loading...
Error: {{ error.message }}
{{ data?.title }}
`
For fetching data. Wraps @tanstack/vue-query's useQuery.
`vue`
For creating, updating, or deleting data. Wraps @tanstack/vue-query's useMutation.
`vue
`
For paginated data. Wraps @tanstack/vue-query's useInfiniteQuery.
`vue
{{ post.excerpt }} v-if="hasNextPage"
{{ post.title }}
@click="fetchNextPage"
:disabled="isFetchingNextPage"
>
{{ isFetchingNextPage ? "Loading..." : "Load More" }}
`
For creating reusable query configurations.
`ts
// composables/usePostQuery.ts
export function usePostQuery(postId: Ref
return $api.queryOptions("get", "/posts/{id}", {
params: { path: { id: postId } },
});
}
// In component
const postQuery = usePostQuery(postId);
const { data } = useQuery(postQuery);
`
Create reusable API composables:
`ts
// composables/useAuth.ts
export function useAuth() {
const login = $api.useMutation("post", "/auth/login");
const logout = $api.useMutation("post", "/auth/logout");
const { data: user } = $api.useQuery("get", "/auth/me", undefined, {
enabled: computed(() => !!getToken()),
});
return {
user: readonly(user),
login: login.mutate,
logout: logout.mutate,
isLoading: computed(() => login.isPending || logout.isPending),
};
}
`
`vue`
`ts
import createFetchClient from "openapi-fetch";
import createClient from "openapi-vue-query";
const fetchClient = createFetchClient
baseUrl: "https://api.example.com/v1",
headers: {
"User-Agent": "MyApp/1.0",
},
});
// Add auth middleware
fetchClient.use({
onRequest({ request }) {
const token = getToken();
if (token) {
request.headers.set("Authorization", Bearer ${token});
}
},
onResponse({ response }) {
if (response.status === 401) {
// Handle unauthorized
logout();
}
},
});
export const $api = createClient(fetchClient);
`
openapi-vue-query is fully compatible with @tanstack/vue-query. You can use all vue-query features:
`vue``
- Basic CRUD Operations
- Infinite Scroll
- Form Mutations
- Complete Example
- openapi-fetch - The underlying fetch client
- openapi-typescript - TypeScript code generator
- @tanstack/vue-query - The query library being wrapped
For detailed documentation and examples, visit: https://openapi-ts-vue.dev/openapi-vue-query/
Found a bug or have a feature request? Please open an issue on GitHub.
MIT License. See LICENSE for details.