cute http
1.0.0
Cute는 동일한 URL의 가져오기 요청에 대해 쿼리 매개변수가 변경되지 않은 경우 캐시된 결과를 먼저 가져옵니다. 검색할 수 없으면 백엔드로 이동하여 요청합니다. 변경하면 이전에 캐시된 결과가 삭제되고 다음 결과가 검색되어 불필요한 데이터가 너무 많이 캐시되는 것을 방지하기 위해 클라이언트가 새로운 결과를 요청합니다.
기본적으로 요청 중 하나에서 오류가 발생하더라도 결과는 다른 요청에 영향을 미치지 않습니다. 물론 이때 사용자는 반환된 결과 데이터를 순회해야 합니다. 그 중 오류가 있을 수 있습니다. 사용자는 여러 요청이 시작될 때 하나의 요청에 오류가 있는 한 모든 요청이 실패하도록 설정할 수도 있습니다.
귀여운 http를 설치하세요
npm i cute-http --save
소개하다
import * as cute from 'cute-http';
const {ONE_ERROR_ABORT_ALL, KEEP_ALL_BEEN_EXECUTED, LOCAL_STORAGE, MEMORY} = cute.const;
localStorage
에서 반환 결과를 얻습니다.memory
에 캐시합니다. 최상위 API, 귀여운 매개변수 구성
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)
물론 특정 코드를 주석 처리하고 그 중 하나의 효과만 확인할 수도 있습니다.
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;
}
더 많은 예제는 /test/test-api.js를 참조하세요.
시뮬레이션 서버 실행
* npm start
테스트 스크립트 실행
* npm test