Proper example of using apisauce with mobx
Hey all,
Is there anywhere proper example of apisauce implementation with mobx? I see you can put all API calls in to services/api/api.ts but that doesn't seem right. I'd like to have in every store let's say just different base url based on store I'm on (eg. /user, /posts, /comments). Should I call in every store like it's on example screen:
const api = new Api()
api.setup()
or is there better implementation? Thanks
I don't have an example for you, but I would expect the API setup would go in the RootStore. @skellock or @robinheinze have any examples or suggestions?
I like to put the API in the environment of the RootStore. That way it is available to every store that attaches to it.
Chances are your API has one base URL. What I like to do is make type-safe wrappers for the endpoints as methods on an API service.
These methods become the functions which get called from your async actions in your store.
If you’re looking to fast track it without much structure, I’d consider skipping apisauce entirely and dropping fetch calls in your actions.
It all depends on what your building and your goals.
@skellock Can I ask you how do you access API from enviroment-variabbles after you setup the store?
@mkchx I called getEnv(self).api.getRepos() inside action of some store.
@questionablequestion Thank you!