Skip to content

Commit

Permalink
fix: enforce plugin to run first
Browse files Browse the repository at this point in the history
  • Loading branch information
enkot committed Dec 15, 2023
1 parent ee074eb commit 0e47d64
Show file tree
Hide file tree
Showing 12 changed files with 2,447 additions and 1,936 deletions.
31 changes: 17 additions & 14 deletions docs/content/4.advanced/1.custom-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@ export default defineNuxtConfig({

For example if you need to add logging on each request:
```ts
export default defineNuxtPlugin(() => {
const { public: { openFetch: clients }} = useRuntimeConfig()
export default defineNuxtPlugin({
enforce: 'pre', // so our clients will be ready for use in other plugins, Pinia stores etc.
setup() {
const { public: { openFetch: clients }} = useRuntimeConfig()

return {
provide: Object.fromEntries(Object.entries(clients).map(([name, client]) => [
`${name}Fetch`,
createOpenFetch((options) => ({
...clients.pets,
...options,
onRequest(ctx) {
console.log('My logging', ctx.request)
return options.onRequest?.(ctx)
}
}))
]))
return {
provide: Object.fromEntries(Object.entries(clients).map(([name, client]) => [
`${name}Fetch`,
createOpenFetch((options) => ({
...clients.pets,
...options,
onRequest(ctx) {
console.log('My logging', ctx.request)
return options.onRequest?.(ctx)
}
}))
]))
}
}
})
```
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,24 @@
"test:watch": "vitest watch"
},
"dependencies": {
"@nuxt/kit": "^3.7.0",
"@nuxt/kit": "^3.8.2",
"defu": "^6.1.2",
"openapi-typescript": "^6.7.0",
"openapi-typescript": "^6.7.3",
"openapi-typescript-helpers": "^0.0.4",
"scule": "^1.0.0"
},
"devDependencies": {
"@nuxt/devtools": "latest",
"@nuxt/eslint-config": "^0.1.1",
"@nuxt/module-builder": "^0.5.1",
"@nuxt/schema": "^3.7.0",
"@nuxt/test-utils": "^3.7.0",
"@nuxt/schema": "^3.8.2",
"@nuxt/test-utils": "^3.9.0-alpha.1",
"@types/node": "^18.17.3",
"changelogen": "^0.5.4",
"eslint": "^8.46.0",
"h3": "^1.8.1",
"listhen": "^1.4.4",
"msw": "^1.2.5",
"nuxt": "^3.8.1",
"nuxt": "^3.8.2",
"ofetch": "^1.3.3",
"vitest": "^0.34.1"
}
Expand Down
7 changes: 5 additions & 2 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<script setup lang="ts">
import { usePetsFetch } from "#imports";
const { execute, data } = await usePetsFetch("/pet/{petId}", {
path: {
petId: 12,
petId: 4,
},
immediate: false,
});
</script>

<template>
<button @click="() => execute()">execute</button>
<button @click="() => execute()">
execute
</button>
</template>
5 changes: 4 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export default defineNuxtConfig({
modules: ['../src/module'],
modules: [
'../src/module',
'@pinia/nuxt',
],
extends: ['./pets'],
devtools: { enabled: false },
openFetch: {
Expand Down
6 changes: 5 additions & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"generate": "nuxi generate"
},
"devDependencies": {
"nuxt": "^3.8.1"
"nuxt": "^3.8.2"
},
"dependencies": {
"@pinia/nuxt": "^0.5.1",
"pinia": "^2.1.7"
}
}
10 changes: 10 additions & 0 deletions playground/plugins/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineNuxtPlugin, useAuth } from "#imports"

export default defineNuxtPlugin(async () => {
const auth = useAuth()
await auth.init()

return {
provide: { auth },
}
})
30 changes: 17 additions & 13 deletions playground/plugins/openFetch.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { createOpenFetch } from "#imports"
import { defineNuxtPlugin, useRuntimeConfig } from '#app'
export default defineNuxtPlugin(() => {
const { public: { openFetch: clients } } = useRuntimeConfig()
import { createOpenFetch } from "#imports"

return {
provide: {
petsFetch: createOpenFetch((options) => ({
...clients.pets,
...options,
onRequest(ctx) {
console.log('My logging', ctx.request)
return options.onRequest?.(ctx)
}
}))
export default defineNuxtPlugin({
enforce: 'pre',
setup() {
const { public: { openFetch: clients } } = useRuntimeConfig()

return {
provide: {
petsFetch: createOpenFetch((options) => ({
...clients.pets,
...options,
onRequest(ctx) {
console.log('My logging', ctx.request)
return options.onRequest?.(ctx)
}
}))
}
}
}
})
Loading

0 comments on commit 0e47d64

Please sign in to comment.