使用npm
:
npm install cookie-fetch --save
用yarn
:
yarn add cookie-fetch
import CFetch, { CachePolicy } from 'cookie-fetch';
...
async componentDidMount() {
const url = 'http://food.boohee.com/fb/v1/categories/list';
const options = {cache: CachePolicy.NetworkFirst, timeout: 0.1};
try {
const {result, isCache} = await CFetch.get(url, options);
alert(`response: ${result} \n isCache: ${isCache}`)
} catch (error) {
alert(`error: ${error}`)
}
}
resolve
的结果为Object
,key
为result
、isCache
。其中result
为序列化后的结果,isCache
代表是否从缓存读取的结果。
Name | Description |
---|---|
NetworkFirst | 优先请求网络,请求失败则读取缓存,如无缓存,返回null
|
ForceNetwork | 强制请求网络,请求失败返回null
|
ForceCache | 强制读取缓存,读取失败返回null
|
Name | Description |
---|---|
get |
GET 请求方法,使用方式与fetch 一致,支持在options 中设置timeout
|
post |
POST 请求方法,使用方式与fetch 一致,支持在options 中设置timeout
|
clearHTTPCacheWithURL | 基于某个请求URL 清除HTTP 缓存,接受的参数与请求的URL 需匹配一致,包括query 部分 |
clearAllHTTPCache | 清除所有的HTTP 缓存 |