kuitos / axios-extensions

🍱 axios extensions lib, including throttle, cache, retry features etc...
MIT License
831 stars 49 forks source link

Pass arguments to adapters as object #18

Closed probil closed 6 years ago

probil commented 6 years ago

It might be better to pass arguments to adapters as an object. I mean, instead of:

cacheAdapterEnhancer(adapter, false, 'cache', new LRUCache({ maxAge: FIVE_MINUTES }))

use

const options = { cacheEnabledByDefault: false, enableCacheFlag: 'cache', defaultCache: new LRUCache({ maxAge: FIVE_MINUTES }) };
cacheAdapterEnhancer(adapter, options)

Advantages:

1) easier to use default values of given params For example, currently if I want to pass only custom cache I have to define all other arguments as undefined. And it looks weird:

const cache =  new LRUCache({ maxAge: 1000 * 60 * 10 })
cacheAdapterEnhancer(axios.defaults.adapter, undefined, undefined, cache);
// compare with
cacheAdapterEnhancer(axios.defaults.adapter, { defaultCache: cache });

2) more explicit (self-descriptive) arguments

cacheAdapterEnhancer(axios.defaults.adapter, false); // <-- what's false here?
// compare with
cacheAdapterEnhancer(axios.defaults.adapter, { cacheEnabledByDefault: false } );

It's hard to recognize what is false without checking the docs. It is not good developer experience

What do you think about that @kuitos ?

kuitos commented 6 years ago

Yes a object definition may be better in current scenario, thanks for your advice! And, would you like to create a pr to improve it? 😀

probil commented 6 years ago

:thinking: Probably we should update some adapter options key e.g. enabledByDefault looks better for me than cacheEnabledByDefault

But it seems like it should be solved as separated issue

kuitos commented 6 years ago

argument changed and released v3.0.0

probil commented 6 years ago

Awesome! :tada: