Skip to content

Commit 1b049a2

Browse files
committed
feat: implement vue-query module
1 parent 1accf36 commit 1b049a2

File tree

24 files changed

+6436
-885
lines changed

24 files changed

+6436
-885
lines changed

README.md

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,117 @@
1-
# ⚗️ My Module Nuxt
1+
# ⚗️ Vue Query Nuxt
22

3-
[![CI](https://github.com/Hebilicious/authjs-nuxt/actions/workflows/ci.yaml/badge.svg)](https://github.com/Hebilicious/authjs-nuxt/actions/workflows/ci.yaml)
4-
[![npm version](https://badge.fury.io/js/@hebilicious%2Fauthjs-nuxt.svg)](https://badge.fury.io/js/@hebilicious%2Fauthjs-nuxt)
3+
[![CI](https://github.com/Hebilicious/vue-query-nuxt/actions/workflows/ci.yaml/badge.svg)](https://github.com/Hebilicious/vue-query-nuxt/actions/workflows/ci.yaml)
4+
[![npm version](https://badge.fury.io/js/@hebilicious%2Fvue-query-nuxt.svg)](https://badge.fury.io/js/@hebilicious%2Fvue-query-nuxt)
55
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
66

7-
🚀 Welcome to __Hebilicious Nuxt Module Starter Template__!
7+
🚀 Welcome to __Vue Query Nuxt__!
88

9+
This Nuxt Module automatically installs and configure Vue Query for your Nuxt application.
10+
It has 0 config out-of-the box and extremely lightweight.
911

1012
## ⚠️ Disclaimer
1113

12-
_🧪 This module is really unstable and is not recommended for production use. It is intended for those who want to experiment with the edge._
14+
_🧪 This module is in active development._
1315

16+
Refer to the [Vue Query documentation](https://tanstack.com/query/latest/docs/vue/quick-start) for more information about Vue Query.
1417

1518
## 📦 Installation
1619

1720

21+
1. Use npm, pnpm or yarn to install the dependencies.
22+
23+
```bash
24+
npm i @hebilicious/vue-query-nuxt @tanstack/vue-query
25+
```
26+
1827
```bash
19-
npm i
28+
pnpm i @hebilicious/vue-query-nuxt @tanstack/vue-query
29+
```
30+
31+
```bash
32+
yarn i @hebilicious/vue-query-nuxt @tanstack/vue-query
33+
```
34+
35+
2. Add the modules to your Nuxt modules
36+
37+
```ts
38+
// In nuxt.config.ts
39+
export default defineNuxtConfig({
40+
modules: ["@hebilicious/vue-query-nuxt"],
41+
// These are the default values, you do not need to specify them.
42+
// Refer to the vue-query docs for more information.
43+
vueQuery: {
44+
stateKey: "vue-query-nuxt",
45+
queryClientOptions: {
46+
defaultOptions: { queries: { staleTime: 5000 } } // default
47+
},
48+
vueQueryPluginOptions: {}
49+
}
50+
})
51+
```
52+
53+
3. Use right away
54+
55+
```html
56+
<script setup>
57+
import { useQueryClient, useQuery, useMutation } from '@tanstack/vue-query'
58+
59+
// Access QueryClient instance
60+
const queryClient = useQueryClient()
61+
62+
// Define a fetching function
63+
const getTodos = () => $fetch("/api/todos")
64+
65+
// Query
66+
const { isLoading, isError, data, error } = useQuery({
67+
queryKey: ['todos'],
68+
queryFn: getTodos,
69+
})
70+
71+
// Mutation
72+
const mutation = useMutation({
73+
mutationFn: postTodo,
74+
onSuccess: () => {
75+
// Invalidate and refetch
76+
queryClient.invalidateQueries({ queryKey: ['todos'] })
77+
},
78+
})
79+
80+
function onButtonClick() {
81+
mutation.mutate({
82+
id: Date.now(),
83+
title: 'Do Laundry',
84+
})
85+
}
86+
</script>
87+
88+
<template>
89+
<span v-if="isLoading">Loading...</span>
90+
<span v-else-if="isError">Error: {{ error.message }}</span>
91+
<!-- We can assume by this point that `isSuccess === true` -->
92+
<ul v-else>
93+
<li v-for="todo in data" :key="todo.id">{{ todo.title }}</li>
94+
</ul>
95+
<button @click="onButtonClick">Add Todo</button>
96+
</template>
97+
```
98+
99+
4. Advanced configuration
100+
101+
Create a `vue-query.config.ts` file at the root of your project.
102+
103+
```ts
104+
// vue-query.config.ts
105+
import { library } from "@example/libray"
106+
107+
export default defineVueQueryPluginCallback((vueQueryOptions) => {
108+
console.log(vueQueryOptions) // You can access the queryClient here
109+
return { provide: { library, test: console } }
110+
})
20111
```
21112

113+
This callback will be run *directly* after the Vue Query plugin is installed, so you can use it to provide something here.
114+
This can be useful if you want to configure something that needs the queryClient or you want to provide a library.
22115

23116
## 📦 Contributing
24117

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "@hebilicious/authjs-nuxt",
2+
"name": "@hebilicious/vue-query-nuxt",
33
"type": "module",
4-
"version": "0.1.0-beta.4",
4+
"version": "0.0.1",
55
"private": true,
66
"packageManager": "pnpm@8.6.0",
77
"scripts": {
88
"build": "rimraf packages/*/dist && pnpm -r --filter=./packages/* run build",
9-
"nx:build": "nx run-many --target=build --p my-module",
9+
"nx:build": "nx run-many --target=build --p vue-query-nuxt",
1010
"lint": "eslint --cache .",
1111
"lint:fix": "nr lint --fix",
1212
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
@@ -23,7 +23,7 @@
2323
"eslint": "8.41.0",
2424
"esno": "^0.16.3",
2525
"lint-staged": "^13.2.2",
26-
"nx": "^16.2.2",
26+
"nx": "^16.3.0",
2727
"pnpm": "8.6.0",
2828
"prettier": "^2.8.8",
2929
"rimraf": "^5.0.1",
@@ -32,7 +32,7 @@
3232
"tsup": "^6.7.0",
3333
"typescript": "^5.0.4",
3434
"unbuild": "^1.2.1",
35-
"vitest": "^0.31.2"
35+
"vitest": "^0.31.3"
3636
},
3737
"simple-git-hooks": {
3838
"pre-commit": "npx lint-staged"

packages/my-module/build.config.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/my-module/src/module.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

packages/my-module/src/runtime/client.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/my-module/src/runtime/composables/useSomething.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/my-module/src/runtime/plugin.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/my-module/src/runtime/server.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/vue-query-nuxt/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shamefully-hoist=true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineBuildConfig } from "unbuild"
2+
3+
export default defineBuildConfig({
4+
entries: ["src/module"],
5+
externals: ["@tanstack/vue-query"]
6+
})

0 commit comments

Comments
 (0)