@hebilicious/vue-query-nuxt
@hebilicious/vue-query-nuxt
@tanstack/vue-query 用の、設定不要な軽量Nuxtモジュール。
⚗️ Vue Query Nuxt
🚀 Vue Query Nuxt へようこそ!
このNuxtモジュールは、お使いのNuxtアプリケーションにVue Queryを自動的にインストールして設定します。0設定で非常に軽量です。
機能
- 0設定ですぐに使える
- すべての設定オプションが利用可能
- Vue Queryコンポーザブルの自動インポート
Vue Queryの詳細については、Vue Queryドキュメントを参照してください。
📦 使用方法
1. npm、pnpm、またはyarnを使用して依存関係をインストールします。
# npm
npm i @hebilicious/vue-query-nuxt @tanstack/vue-query
# pnpm
pnpm i @hebilicious/vue-query-nuxt @tanstack/vue-query
# yarn
yarn add @hebilicious/vue-query-nuxt @tanstack/vue-query
2. モジュールをNuxtモジュールに追加します。
nuxt.config.ts にて
export default defineNuxtConfig({
modules: ["@hebilicious/vue-query-nuxt"]
})
3. すぐに使用
Vueコンポーネントにて
<script setup lang="ts">
// Access QueryClient instance
const queryClient = useQueryClient()
// Query
const { isLoading, isError, data, error } = useQuery({
queryKey: ['todos'],
queryFn: () => $fetch("/api/todos"), // Use $fetch with your api routes to get typesafety
})
// Mutation
const { mutate } = useMutation({
mutationFn: (newTodo) => $fetch("/api/todos", { method: "POST", body: newTodo })
onSuccess: () => {
// Invalidate and refetch
queryClient.invalidateQueries({ queryKey: ['todos'] })
},
})
function onButtonClick() {
mutate({
id: Date.now(),
title: 'Do Laundry',
})
}
</script>
<template>
<span v-if="isLoading">Loading...</span>
<span v-else-if="isError">Error: {{ error.message }}</span>
<!-- We can assume by this point that `isSuccess === true` -->
<ul v-else>
<li v-for="todo in data" :key="todo.id">{{ todo.title }}</li>
</ul>
<button @click="onButtonClick">Add Todo</button>
</template>
4. 高度な設定
nuxt.config.tsファイル内のvueQueryキーの下にオプションを指定できます。すべて型付けされています。
nuxt.config.ts にて
export default defineNuxtConfig({
modules: ["@hebilicious/vue-query-nuxt"],
vueQuery: {
// useState key used by nuxt for the vue query state.
stateKey: "vue-query-nuxt", // default
// If you only want to import some functions, specify them here.
// You can pass false or an empty array to disable this feature.
// default: ["useQuery", "useQueries", "useInfiniteQuery", "useMutation", "useIsFetching", "useIsMutating", "useQueryClient"]
autoImports: ["useQuery"],
// Pass the vue query client options here ...
queryClientOptions: {
defaultOptions: { queries: { staleTime: 5000 } } // default
},
// Pass the vue query plugin options here ....
vueQueryPluginOptions: {}
}
})
Vue Queryをインストールするプラグインを変更する必要がある場合は、プロジェクトのルートにvue-query.config.tsファイルを作成できます。
vue-query.config.ts にて
import { library } from "@example/libray"
export default defineVueQueryPluginHook(({ queryClient, nuxt }) => {
console.log(queryClient, nuxt) // You can access the queryClient here
return {
pluginReturn: { provide: { library, test: console } }, // nuxt plugin return value
vueQueryPluginOptions: { queryClient } // You can pass dynamic options
}
})
このフックはモジュールによってインストールされたNuxtプラグイン内で実行されるため、何かをprovideしたり、Vue Queryオプションを置き換えたりするために使用できます。これは、queryClientがインストールされているときにカスタムロジックを実行する必要がある場合に役立ちます。
📦 貢献
貢献、イシュー、機能リクエストを歓迎します!
- このリポジトリをフォークする
nodeとpnpmをインストールしてください。corepack enable && corepack prepare pnpm@latest --activateを使用すると、pnpmを簡単にインストールできます。- モノレポのルートで
pnpm iを実行します。 - 変更を加え、従来のコミットに従う。
- PRをオープンする 🚀🚀🚀