0.2.2 • Published 2 years ago
cookie-fetch v0.2.2
cookie-fetch
安装
使用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缓存 |