Malik
Malik
NNuxt
Created by Malik on 3/28/2024 in #❓・help
What is "The Nuxt Way" (TM) to abstract http libs?
Changing your example to what I had before
import type { User } from '~/utils/repository.js';
const app = useNuxtApp();
const userRepo = repository(app.$api);

export const useTestStore = defineStore('test', {
state: () => ({
users: [] as User[],
}),
actions: {
async fetchData() {
this.users = await userRepo.get();
},
},
});
import type { User } from '~/utils/repository.js';
const app = useNuxtApp();
const userRepo = repository(app.$api);

export const useTestStore = defineStore('test', {
state: () => ({
users: [] as User[],
}),
actions: {
async fetchData() {
this.users = await userRepo.get();
},
},
});
Results in what I struggled with
[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at `https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables`.
[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at `https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables`.
Oh my, I should have thought of that. Well, thanks a bunch @manniL / TheAlexLichter, you just solved my case. I am flabbergasted.
12 replies
NNuxt
Created by Malik on 3/28/2024 in #❓・help
What is "The Nuxt Way" (TM) to abstract http libs?
Uh nice!!! I am not sure if I get that correctly, but what solves this is that using a function instead of an object when calling defineStore and inside that function I get access to useNuxtApp(), am I right? Just to understand why this works now 😄
12 replies
NNuxt
Created by Malik on 3/28/2024 in #❓・help
What is "The Nuxt Way" (TM) to abstract http libs?
Thank you @manniL / TheAlexLichter for the super fast support :). I figure that you use a similar approach as I had in mind with my setup, but if I didn't miss it, I see no usage of your plugin from within a pinia store. While I am not necessarily saying that it is generally my preferred approach to make http requests from store actions, I see it quite a lot. My custom http plugin approach, however, was not usable from within a pinia store file. Do you have a suggestion for that? I believe what bites me is the access to the runtime Config and the wrong context I am trying to access it in. EDIT: If the answer is "making http requests in store actions is an anti-pattern" then I could live with that, too 😄
12 replies