cookie-fetch

0.2.2 • Public • Published

cookie-fetch

npm npm

安装

使用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的结果为ObjectkeyresultisCache。其中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缓存

Readme

Keywords

Package Sidebar

Install

npm i cookie-fetch

Weekly Downloads

17

Version

0.2.2

License

MIT

Unpacked Size

342 kB

Total Files

53

Last publish

Collaborators

  • ljunb