Cute ha optimizado el caché para una solicitud de obtención de la misma URL, si los parámetros de la consulta permanecen sin cambios, el resultado almacenado en caché se obtendrá primero. Si no se puede recuperar, irá al backend para solicitarlo. cambio, el resultado almacenado en caché anterior se eliminará y se recuperará el siguiente. El cliente solicita nuevos resultados para evitar que se almacenen en caché demasiados datos inútiles.
De forma predeterminada, el resultado se devuelve solo después de que se ejecuta la solicitud. Incluso si ocurre un error en una de las solicitudes, no afectará a otras solicitudes. Por supuesto, el usuario debe recorrer los datos del resultado devuelto en este momento, porque uno. de ellos puede ser un error. Los usuarios también pueden configurarlo para que cuando se inicien varias solicitudes, siempre que haya un error en una solicitud, todas fallen.
Instalar lindo-http
npm i cute-http --save
introducir
import * as cute from 'cute-http';
const {ONE_ERROR_ABORT_ALL, KEEP_ALL_BEEN_EXECUTED, LOCAL_STORAGE, MEMORY} = cute.const;
localStorage
memory
API de nivel superior, configura parámetros para lindo
cute setConfig({
retryCount: number,//重试次数
timeout: 1900,//超时时间(毫秒)
debug:true,//打开debug模式
// cacheType: MEMORY, // 默认无,如果开启了缓存,cute只针对get请求做缓存,如需穿透缓存,可以对query参数加随机值,或者调用cute.get时,配置第二个参数{cacheType:null},表示针对这一次调用不读取缓存值
// failStrategy: ONE_ERROR_ABORT_ALL, //不设置的话,cute默认采用KEEP_ALL_BEEN_EXECUTED
})
/**
* 发起多个post请求
* @param {Array<{type:'post', url:string, data:object} | {type:'get', url:string} | {type:'jsonp', url:string} >} items
* @param {{failStrategy?:number, retryCount?:number, callbackParamName?:string, [otherAxiosConfigKey]:any}} extendedAxiosConfig
* otherAxiosConfigKey @see https://github.com/axios/axios
*/
cute.multi(urls, extendedAxiosConfig)
// data 会被自动stringify拼接到url末尾
cute.get(url:string, data:string|object, extendedAxiosConfig:ExtendedAxiosConfig)
cute.multiGet(urls:string[] | {url:string, data:string|object}[], extendedAxiosConfig:ExtendedAxiosConfig)
cute.post(url:string, data:object, extendedAxiosConfig:ExtendedAxiosConfig)
cute.multiPost({url:string, data:object}[], extendedAxiosConfig:ExtendedAxiosConfig)
// data 会被自动stringify拼接到url末尾
cute.jsonp(url:string, data:string|object, extendedAxiosConfig:ExtendedAxiosConfig)
cute.multiJsonp(urls:string[] | {url:string, data:string|object}[], extendedAxiosConfig:ExtendedAxiosConfig)
Por supuesto, también puedes comentar ciertos códigos y solo ver el efecto de uno de ellos.
const cute = require('../index');
const {ONE_ERROR_ABORT_ALL, KEEP_ALL_BEEN_EXECUTED, MEMORY} = cute.const;
const axios = cute.axios;//这个是axios模块的引用
cute.setConfig({
retryCount: 3,//设置重试次数
timeout: 1900,//设置超时时间
debug: true,
dataVerifyRule: {//设置通用的响应数据校验规则,只支持校验json对象的第一层key的值和类型的校验
data: 'object',
code: 'number',
message: 'string',
},
pathDataVerifyRule:{//对某些请求设置独立的数据校验规则
'/staff/foo':{
reply:'object',
msg:'string',
}
}
cacheType: MEMORY, // 值为 'memory' | 'localStorage' 默认无
failStrategy: KEEP_ALL_BEEN_EXECUTED, //不设置的话,cute默认采用ONE_ERROR_ABORT_ALL
})
const updateBook = 'http://localhost:8888/update-book';
const getBooksByUid = uid => `http://localhost:8888/get-books?uid=${uid}`;
async function main(){
const result1 = await cute.get(getBooksByUid(1));
const books = result.result1;
const result2 = await cute.multiGet([getBooksByUid(1), getBooksByUid(2)]);
const [reply1, reply2] = result2;
const result3 = await cute.post(updateBook, {id:1, name:'zk'});
const updateReply = result.result3;
const result4 = await cute.multiPost([{url:updateBook, body:{id:1, name:'zk'}}, {url:updateBook, body:{id:2, name:'wow'}}]);
const [updateReply1, updateReply2] = result4;
}
Consulte /test/test-api.js para obtener más ejemplos.
Ejecute el servidor de simulación
* npm start
Ejecutar script de prueba
* npm test