ignite icon indicating copy to clipboard operation
ignite copied to clipboard

Proper example of using apisauce with mobx

Open questionablequestion opened this issue 8 years ago • 5 comments

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

questionablequestion avatar May 20 '18 10:05 questionablequestion

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?

kevinvangelder avatar May 22 '18 01:05 kevinvangelder

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 avatar May 22 '18 11:05 skellock

@skellock Can I ask you how do you access API from enviroment-variabbles after you setup the store?

chef-sudo avatar Jun 13 '18 09:06 chef-sudo

@mkchx I called getEnv(self).api.getRepos() inside action of some store.

questionablequestion avatar Jun 13 '18 10:06 questionablequestion

@questionablequestion Thank you!

chef-sudo avatar Jun 13 '18 10:06 chef-sudo