A set of Vue 3 composables for simplified HTTP requests (GET, POST, PUT, PATCH, DELETE) using @tanstack/vue-query and Axios. Effortlessly fetch and mutate data with built-in caching, loading states, error handling, and TypeScript support. Perfect for Vue 3 and Nuxt 3 projects.
- π Automatic caching and refetching via
@tanstack/vue-query
- π‘ Configurable Axios instance for flexible API requests
- βοΈ Query parameter support for GET requests
- π TypeScript-friendly with fully typed composables
- π± Compatible with Vue 3 and Nuxt 3 projects
- π Simple API for both queries (
useGetQuery
) and mutations (useSend
) - π οΈ Error handling and loading states out of the box
Install the package via npm, Yarn, or pnpm:
npm install vue-query-request-utils
# or
yarn add vue-query-request-utils
# or
pnpm add vue-query-request-utils
Ensure the following dependencies are installed in your project:
npm install @tanstack/vue-query axios vue
Create an Axios instance to use with the composables:
// api.ts
import axios from 'axios';
export const API = axios.create({
baseURL: 'https://api.example.com',
});
Fetch data with caching and loading states:
<script setup>
import { useGetQuery } from 'vue-query-request-utils';
import { API } from './api';
const { data, isLoading, error, refetch } = useGet({
API,
apiUrl: '/users/', // Added an extra "/" at the end
queryKey: ['users'],
paramRef: { query: { page: 1, limit: 10 } }, // The url will be: /users/?page=1&limit=10
options: { enabled: true },
});
</script>
<template>
<div>
<button @click="refetch">Refresh</button>
<div v-if="isLoading">Loading...</div>
<div v-else-if="error">{{ error.message }}</div>
<ul v-else>
<li v-for="user in data" :key="user.id">{{ user.name }}</li>
</ul>
</div>
</template>
paramRef: { page: 1, active: true }
// β /users?page=1&active=true
paramRef: [123]
// β /users/123
paramRef: {
path: [123],
query: { active: true, page: 1 }
}
// β /users/123?active=true&page=1
All of the above support ref or computed values.
useGet({
API: customAxios,
url: "/example",
...
})
Perform mutations to create, update, or delete data:
<script setup>
import { useSend } from 'vue-query-request-utils';
import { API } from './api'; // Custom Axios Instance
const { mutate, isLoading: isMutating, isSuccess, error } = useSend({
API,
method: 'post',
url: '/users',
mutationKey: ['createUser'],
options: {
onSuccess: () => console.log('User created!'),
onError: (err) => console.error('Error:', err),
},
});
const createUser = () => {
mutate({ name: 'John Doe', email: 'john@example.com' });
};
</script>
<template>
<div>
<button :disabled="isMutating" @click="createUser">
{{ isMutating ? 'Creating...' : 'Create User' }}
</button>
<div v-if="isSuccess">User created successfully!</div>
<div v-if="error">{{ error.message }}</div>
</div>
</template>
The composables work seamlessly with Nuxt 3. Use them in your setup scripts or provide the Axios instance via a Nuxt plugin:
// plugins/api.ts
import { defineNuxtPlugin } from '#app';
import axios from 'axios';
import { provideApi } from 'vue-query-request-utils'
export default defineNuxtPlugin(() => {
const API = axios.create({ baseURL: 'https://api.example.com' });
nuxtApp.vueApp.use(provideApi(api));
});
<script setup>
import { useGet } from 'vue-query-request-utils';
const { data, isLoading } = useGet({
apiUrl: '/users',
queryKey: ['users'],
});
</script>
A composable for GET requests with @tanstack/vue-query.
-
url: API endpoint (e.g., /users).
-
queryKey: Unique key for caching (e.g., ['users'] or a Vue Ref).
-
API?: Axios instance for making requests.
-
paramRef?: Optional query parameters (e.g., { page: 1 }).
-
options?: Additional useQuery options (e.g., { enabled: false }).
-
Returns: UseQueryResult with properties like:
-
data: Fetched data.
-
isLoading: Loading state.
-
error: Error details, if any.
-
refetch: Function to refetch data.
A composable for POST, PUT, PATCH, or DELETE requests.
-
method: HTTP method (post, put, patch, delete).
-
url: API endpoint (e.g., /users).
-
API?: Axios instance for making requests.
-
requestConfig?: Optional Axios request config (e.g., { headers: {} }).
-
mutationKey?: Unique key for the mutation (e.g., ['createUser']).
-
options?: Additional useMutation options (e.g., { onSuccess }).
-
Returns: UseMutationResult with properties like:
-
mutate: Function to trigger the mutation.
-
isLoading: Mutation in progress.
-
isSuccess: Mutation success state.
-
data: Response data.
-
error: Error details, if any.