All Projects → kuitos → Axios Extensions

kuitos / Axios Extensions

Licence: mit
🍱 axios extensions lib, including throttle, cache, retry features etc...

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Axios Extensions

Use Axios Request
Data fetching is easy with React Hooks for axios!
Stars: ✭ 38 (-93.6%)
Mutual labels:  cache, axios
Gf
GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
Stars: ✭ 6,501 (+994.44%)
Mutual labels:  cache
Pixel Web
一个 Vue 微博客户端
Stars: ✭ 500 (-15.82%)
Mutual labels:  axios
Go Cache
An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.
Stars: ✭ 5,676 (+855.56%)
Mutual labels:  cache
Axios Auth Refresh
Library that helps you implement automatic refresh of authorization via axios interceptors. You can easily intercept the original request when it fails, refresh the authorization and continue with the original request, without user even noticing.
Stars: ✭ 502 (-15.49%)
Mutual labels:  axios
Flask Vuejs Madblog
基于 Flask 和 Vue.js 前后端分离的微型博客项目,支持多用户、Markdown文章(喜欢/收藏文章)、粉丝关注、用户评论(点赞)、动态通知、站内私信、黑名单、邮件支持、管理后台、权限管理、RQ任务队列、Elasticsearch全文搜索、Linux VPS部署、Docker容器部署等
Stars: ✭ 541 (-8.92%)
Mutual labels:  axios
Vue Music Webapp
🎵💞🐔基于Vue 全家桶 (2.x)制作的移动端音乐 WebApp 。媲美原生、项目完整、功能完备、UI美观、交互一流。
Stars: ✭ 499 (-15.99%)
Mutual labels:  axios
Flask Caching
A caching extension for Flask
Stars: ✭ 582 (-2.02%)
Mutual labels:  cache
Vue Seed
a boilerplate for large vue project with ElementUI 2.x
Stars: ✭ 555 (-6.57%)
Mutual labels:  axios
Laravel Eloquent Query Cache
Adding cache on your Laravel Eloquent queries' results is now a breeze.
Stars: ✭ 529 (-10.94%)
Mutual labels:  cache
Cached
Rust cache structures and easy function memoization
Stars: ✭ 530 (-10.77%)
Mutual labels:  cache
Androidvideocache
Cache support for any video player with help of single line
Stars: ✭ 4,849 (+716.33%)
Mutual labels:  cache
Sdwebimage
Asynchronous image downloader with cache support as a UIImageView category
Stars: ✭ 23,928 (+3928.28%)
Mutual labels:  cache
Vue Music
cloud-music(网易云音乐)
Stars: ✭ 500 (-15.82%)
Mutual labels:  axios
Valuestore
Easily store some values
Stars: ✭ 560 (-5.72%)
Mutual labels:  cache
Blog.admin
✨ 基于vue 的管理后台,配合Blog.Core与Blog.Vue等多个项目使用
Stars: ✭ 500 (-15.82%)
Mutual labels:  axios
Bigcache
Efficient cache for gigabytes of data written in Go.
Stars: ✭ 5,304 (+792.93%)
Mutual labels:  cache
React Esi
React ESI: Blazing-fast Server-Side Rendering for React and Next.js
Stars: ✭ 537 (-9.6%)
Mutual labels:  cache
Vue Blog
A single-user blog built with vue2, koa2 and mongodb which supports Server-Side Rendering
Stars: ✭ 586 (-1.35%)
Mutual labels:  axios
Carlos
A simple but flexible cache
Stars: ✭ 564 (-5.05%)
Mutual labels:  cache

axios-extensions

npm version coverage npm downloads Build Status

A non-invasive, simple, reliable collection of axios extension

Extension List

v3.x has a lot of api changes, if you are looking for v2.x doc, see here

Not working with axios v0.19.0 as its custom config bug, See https://github.com/axios/axios/pull/2207.

Installing

npm i axios-extensions -S

or

yarn add axios-extensions

or

// exposed as window['axios-extensions']
<script src="https://unpkg.com/axios-extensions/dist/axios-extensions.min.js"></script>

Usage

import axios from 'axios';
import { cacheAdapterEnhancer, throttleAdapterEnhancer } from 'axios-extensions';

// enhance the original axios adapter with throttle and cache enhancer 
const http = axios.create({
	baseURL: '/',
	headers: { 'Cache-Control': 'no-cache' },
	adapter: throttleAdapterEnhancer(cacheAdapterEnhancer(axios.defaults.adapter))
});

Enable Logging

It is highly recommended to enable the request logging recorder in development environment(disabled by default).

browser (webpack)

new webpack.DefinePlugin({
  'process.env.LOGGER_LEVEL': JSON.stringify('info')
})

node

// package.json
"scripts": {
	"start": "cross-env LOGGER_LEVEL=info node server.js"
}

API

cacheAdapterEnhancer

Makes axios cacheable

cacheAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter

Where adapter is an axios adapter which following the axios adapter standard, options is an optional that configuring caching:

Param Type Default value Description
enabledByDefault boolean true Enables cache for all requests without explicit definition in request config (e.g. cache: true)
cacheFlag string 'cache' Configures key (flag) for explicit definition of cache usage in axios request
defaultCache CacheLike
new LRUCache({ maxAge: FIVE_MINUTES, max: 100 })
a CacheLike instance that will be used for storing requests by default, except you define a custom Cache with your request config

cacheAdapterEnhancer enhances the given adapter and returns a new cacheable adapter back, so you can compose it with any other enhancers, e.g. throttleAdapterEnhancer.

basic usage

import axios from 'axios';
import { cacheAdapterEnhancer } from 'axios-extensions';

const http = axios.create({
	baseURL: '/',
	headers: { 'Cache-Control': 'no-cache' },
	// cache will be enabled by default
	adapter: cacheAdapterEnhancer(axios.defaults.adapter)
});

http.get('/users'); // make real http request
http.get('/users'); // use the response from the cache of previous request, without real http request made
http.get('/users', { cache: false }); // disable cache manually and the the real http request invoked

custom cache flag

const http = axios.create({
	baseURL: '/',
	headers: { 'Cache-Control': 'no-cache' },
	// disable the default cache and set the cache flag
	adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false, cacheFlag: 'useCache'})
});

http.get('/users'); // default cache was disabled and then the real http request invoked 
http.get('/users', { useCache: true }); // make the request cacheable(real http request made due to first request invoke)
http.get('/users', { useCache: true }); // use the response cache from previous request
custom cache typing

Note that if you are using custom cache flag and typescript, you may need to add the typing declaration like below:

import { ICacheLike } from 'axios-extensions';
declare module 'axios' {
  interface AxiosRequestConfig {
    // if your cacheFlag was setting to 'useCache'
    useCache?: boolean | ICacheLike<any>;
  }
}

more advanced

Besides configuring the request through the cacheAdapterEnhancer, we can enjoy more advanced features via configuring every individual request.

import axios from 'axios';
import { cacheAdapterEnhancer, Cache } from 'axios-extensions';

const http = axios.create({
	baseURL: '/',
	headers: { 'Cache-Control': 'no-cache' },
	// disable the default cache
	adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false })
});

http.get('/users', { cache: true }); // make the request cacheable(real http request made due to first request invoke)

// define a cache manually
const cacheA = new Cache();
// or a cache-like instance
const cacheB = { get() {/*...*/}, set() {/*...*/}, del() {/*...*/} };

// two actual request will be made due to the different cache 
http.get('/users', { cache: cacheA });
http.get('/users', { cache: cacheB });

// a actual request made and cached due to force update configured
http.get('/users', { cache: cacheA, forceUpdate: true });

Note: If you are using typescript, do not forget to enable "esModuleInterop": true and "allowSyntheticDefaultImports": true for better development experience.

throttleAdapterEnhancer

Throttle GET requests most once per threshold milliseconds

throttleAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter

Where adapter is an axios adapter which following the axios adapter standard, options is an optional object that configuring throttling:

Param Type Default value Description
threshold number 1000 The number of milliseconds to throttle request invocations to
cache CacheLike
new LRUCache({ max: 10 })
CacheLike instance that will be used for storing throttled requests

Basically we recommend using the throttleAdapterEnhancer with cacheAdapterEnhancer together for the maximum caching benefits. Note that POST and other methods besides GET are not affected.

throttleAdapterEnhancer(cacheAdapterEnhancer(axios.defaults.adapter))

Check David Corbacho's article to learn more details about throttle and how it differs from debounce.

basic usage

import axios from 'axios';
import { throttleAdapterEnhancer } from 'axios-extensions';

const http = axios.create({
	baseURL: '/',
	headers: { 'Cache-Control': 'no-cache' },
	adapter: throttleAdapterEnhancer(axios.defaults.adapter, { threshold: 2 * 1000 })
});

http.get('/users'); // make real http request
http.get('/users'); // responsed from the cache
http.get('/users'); // responsed from the cache

setTimeout(() => {
	http.get('/users'); // after 2s, the real request makes again
}, 2 * 1000);

retryAdapterEnhancer

Retry the failed request with special times

retryAdapterEnhancer(adapter: AxiosAdapter, options: Options): AxiosAdapter

Where adapter is an axios adapter which following the axios adapter standard, options is an optional that configuring caching: | Param | Type | Default value | Description | | ---------------- | ---------------------------------------- | ------------------------------------------------------------ | ---- | | times | number | 2 | Set the retry times for failed request globally. |

basic usage

import axios from 'axios';
import { retryAdapterEnhancer } from 'axios-extensions';

const http = axios.create({
	baseURL: '/',
	headers: { 'Cache-Control': 'no-cache' },
	adapter: retryAdapterEnhancer(axios.defaults.adapter)
});

// this request will retry two times if it failed
http.get('/users');

// you could also set the retry times for a special request
http.get('/special', { retryTimes: 3 });
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].