-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
602c4d1
commit a5ffc92
Showing
4 changed files
with
146 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,88 @@ | ||
// todo | ||
// 1. 请求接口的时候先查找缓存 如果缓存存在并且没有失效 则使用缓存 如果失效继续请求接口 - 缓存失效时间可以配置在new 实例的时候设置 | ||
// 2. 默认使用缓存 在new实例的时候可以强制关闭缓存 也可以在单个接口中开启和关闭缓存 | ||
// 1. 请求接口的时候先查找缓存 如果缓存存在并且没有失效 则使用缓存 如果失效继续请求接口 | ||
// 2. 默认关闭缓存 调用插件的时候可以控制开启关闭缓存 也可以在单个接口中开启和关闭缓存 | ||
// 3. 单个请求可以强制使用缓存或丢弃缓存 ignoreCache | ||
// 4. store 可以为变量也可以为localstorage redis 等 | ||
|
||
import { HttpService, generateHashKey } from './util'; | ||
import { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import localStore from '@/assets/script/storage'; | ||
|
||
// 拦截器里后use的函数先执行 是栈 | ||
/** | ||
* @param httpClient | ||
* @param param1 ttl 缓存失效时间 单位 ms | ||
* @param store 将store分离出去 可以选择 | ||
* 变量缓存(刷新浏览器即丢失)- 默认使用 | ||
* localstorage|sessionstorage 本地缓存 | ||
* @param exclude todo | ||
* { | ||
* paths: [], // url 路径 | ||
* methods: [], // 请求方式 | ||
* filter: () => boolean // 为true的时候就不开启 | ||
* } | ||
*/ | ||
export function axiosCache( | ||
httpClient: HttpService, | ||
{ | ||
ttl, | ||
store = new Store(), | ||
exclude = {} | ||
}: { | ||
ttl: number; | ||
store?: Store | typeof localStore; | ||
exclude?: { paths?: string[]; methods?: Array<string>; filter?: () => boolean }; | ||
} | ||
) { | ||
httpClient.service.interceptors.request.use((config: AxiosRequestConfig) => { | ||
const { paths, methods, filter } = exclude; | ||
if (paths && paths.includes(config.url || '')) return config; | ||
if (methods && methods.includes(config.method as string)) return config; | ||
if (filter && filter()) return config; | ||
|
||
const hash = generateHashKey(config); | ||
if (store.has(hash)) { | ||
const { expire, data } = store.get(hash)!; | ||
if (expire > Date.now()) { | ||
// 未过期 必须包装成 {data:data} => response | ||
// 如果reslove还需要执行下一个request拦截器 不行 | ||
// 如果是reject axios会读取cancelToken 报错 | ||
// 所以统一用canceltoken来取消请求 | ||
return Promise.reject({ data }); | ||
} | ||
} | ||
|
||
// @ts-ignore | ||
config.__cacheHash__ = hash; | ||
return config; | ||
}); | ||
|
||
httpClient.service.interceptors.response.use((response: AxiosResponse) => { | ||
// 将data保存在store里 | ||
if (response.status === 200) { | ||
// @ts-ignore | ||
const cacheHash = response.config.__cacheHash__; | ||
store.set(cacheHash, { expire: Date.now() + ttl, data: response.data }); | ||
} | ||
|
||
return response; | ||
}); | ||
} | ||
|
||
export class Store { | ||
store: Map<string, { data: any; expire: number }>; | ||
constructor() { | ||
this.store = new Map(); | ||
} | ||
|
||
get(key: string) { | ||
return this.store.get(key); | ||
} | ||
|
||
has(key: string) { | ||
return this.store.has(key); | ||
} | ||
|
||
set(key: string, d: { data: any; expire: number }) { | ||
return this.store.set(key, d); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
// 1. 重新请求接口 可以在new HttpService() 之后使用 | ||
// const httpClinet = new HttpService() | ||
// axiosRetry(httpClinet, {times: 2, enable: true}) | ||
// axiosRetry(httpClinet, {times: 2 }) | ||
// 2. todo - 在单个接口请求中开启retry并指定retry次数 post 和 get 方法多传一个参数 | ||
|
||
// todo need to test!!! | ||
import { HttpService } from './util'; | ||
import { AxiosResponse, AxiosError } from 'axios'; | ||
export function axiosRetry( | ||
httpClient: HttpService, | ||
{ times = 2, enable = false }: { times: number; enable: boolean } | ||
) { | ||
if (enable) { | ||
let retryTimes = 0; | ||
httpClient.service.interceptors.response.use( | ||
(response: AxiosResponse) => { | ||
retryTimes = 0; | ||
return response; | ||
}, | ||
(error: AxiosError) => { | ||
if (error.response) { | ||
if (retryTimes < times) { | ||
retryTimes++; | ||
const resp = error.response; | ||
const method = resp.config.method; | ||
const url = resp.config.url; | ||
let params, fn; | ||
if (method === 'get') { | ||
params = resp.config.params || {}; | ||
fn = httpClient.get; | ||
} else if (method === 'post') { | ||
params = resp.config.data || {}; | ||
fn = httpClient.post; | ||
} | ||
|
||
if (url && params && fn) { | ||
fn(url, params); | ||
} | ||
} | ||
/** | ||
* | ||
* @param httpClient axios 实例 | ||
* @param param1 times: 重复请求次数 | ||
*/ | ||
export function axiosRetry(httpClient: HttpService, { times = 2 }: { times: number }) { | ||
let retryTimes = 0; | ||
httpClient.service.interceptors.response.use( | ||
(response: AxiosResponse) => { | ||
retryTimes = 0; | ||
return response; | ||
}, | ||
(error: AxiosError) => { | ||
if (retryTimes < times) { | ||
retryTimes++; | ||
const { method, url } = error.config; | ||
let params, fn; | ||
if (method === 'get') { | ||
params = error.config.params || {}; | ||
fn = httpClient.get; | ||
} else if (method === 'post') { | ||
params = error.config.data || {}; | ||
fn = httpClient.post; | ||
} | ||
|
||
if (url && params && fn) { | ||
fn(url, params); | ||
} | ||
} | ||
); | ||
} | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters