Install
npm install common-cores
yarn add common-cores
pnpm add common-cores
Description
使用typescript的通用api仓库,目前只有dayjs、http和其常用的方法,http是对fetch进行封装的一个请求库,。后续会添加更多的api。
Usage dayjs
import { dayjs } from 'common-cores';
<script>
dayjs().add(1,'day').format('YYYY-MM-DD');
dayjs('2020-01-01').subtract(10,'month').format('YYYY-MM-DD');
dayjs('2023/03/10 10:10:10').format('YYYY-MM-DD HH:mm:ss');
dayjs().getYear();
dayjs().getMonth();
dayjs().valueOf();
dayjs().compare('2020-01-01');
</script>
Usage http
import { http } from 'common-cores';
<script>
<!-- 接口请求 -->
http('user/page',{ method: 'post',data: { page: 1, size: 10 }}).then(res=>res);
http('user/detail/1').then(res=>res).catch(err=>err);
http('user/delete',{ method: 'delete',data:[1,2,3] }).then(res=>res).catch(err=>err);
<!-- 创建实例,实例会有相应和请求拦截器 -->
const httpInstance = http.create({
baseURL: 'api/',
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
});
<!-- 请求拦截器 -->
httpInstance.rerequestInterceptor(config=>{
config.headers['Authorization'] = 'token';
return config;
});
<!-- 响应拦截器 -->
httpInstance.rereponseInterceptor(response=>{
return response;
},error=>{
})
<!-- 接口请求 -->
httpInstance('user/page',{ method: 'post',data: { page: 1, size: 10 }}).then(res=>res);
httpInstance('user/detail/1').then(res=>res).catch(err=>err);
httpInstance('user/delete',{ method: 'delete',data:[1,2,3] }).then(res=>res).catch(err=>err);
</script>