1.4.6 • Published 2 years ago

@shitou/tea-plus v1.4.6

Weekly downloads
17
License
ISC
Repository
-
Last release
2 years ago

teaPlus 使用文档

安装

npm i @shitou/tea-plus --save-dev

使用

引用

import teaPlus from '@shitou/tea-plus'

使用api

  • GET方式
teaPlus
    .get(URL,config)
    .then(res => {
     		 console.log(res)
    })
    .catch(e => {
				console.log(e)
    })

teaPlus
	.delete(URL,config)

teaPlus
	.head(URL,config)

teaPlus
	.options(URL,config)
  • PLOS方式
teaPlus
    .post(URL,config)
    .then(res => {
     		 console.log(res)
    })
    .catch(e => {
				console.log(e)
    })

teaPlus
	.put(URL,config)

teaPlus
	.patch(URL,config)

Config配置参数

参数名字类型默认值参数描述
paramsObjectnull只给get方式请求传递参数试用可以不传
dataObjectnull只给post方式请求传递参数试用可以不传
timeoutNumber0接口超时时间配置,以毫秒(ms)为单位
withCredentialsBooleanFalse是否开启ajax跨域请求
headersObject{}配置请求头文件
cacheBooleantrue为false时自动在请求链接后面加入时间戳凡防止缓存
markBooleanfalse为true时会自动配合 mark 打点 计算接口响应时间

返回参数详解

参数类型默认值参数描述
resObjectnull请求成功返回参数
res.dataObjectnull接口返回的内容
res.statusNumbernull接口返回的状态码
res.statusTextString空字符串接口返回的与状态码相对应的状态消息
res.headersObjectnull接口返回的的头信息
res.configObjectObject用户的config配置信息
res.requestObjectObjectXMLHttpRequest 对象

错误参数详解

参数类型默认参数描述
eString空字符串错误信息内容
e.messageString空字符串错误信息内容
e.configObjectObject用户的config配置信息
e.codeNumbernull接口返回的状态码
e.requestObjectnullXMLHttpRequest 对象
e.responseObjectnullXMLHttpRequest 对象
e.isTeaPlusErrorBooleanfalse是否是 timeout 网络异常 返回码不正常的 teaPlus内部错误的情况判断

其他使用方法

  • teaPlus.all:等同于promise.all

  • teaPlus.create:创建静态类

  • teaPlus.spread:等同于promise.spread分别获取 promise.all的结果

拦截器

// 添加一个请求拦截器
teaPlus
    .interceptors
    .request
    .use(function (config) {
      
        // 在发送请求之前可以做一些事情
        return config;
    }, function (error) {
        console.log(error)
        // 处理请求错误
        return Promise.reject(error);
    });

// 添加一个响应拦截器
teaPlus
    .interceptors
    .response
    .use(function (response) {
        console.log(222)
        // 处理响应数据
        return response;
    }, function (error) {
        // 处理响应错误
        return Promise.reject(error);
    });

兼容处理

因为低版本的浏览器不支持promise,本库的链式调用是基于原生promise,本库推荐使用es6-promise所兼容

安装地址:https://www.npmjs.com/package/es6-promise

可以用
require('es6-promise').polyfill();
或者
require('es6-promise/auto');
 

或者引用

<!-- Automatically provides/replaces `Promise` if missing or broken. -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script> 
 
<!-- Minified version of `es6-promise-auto` below. -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script> 
1.4.6

2 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.3.4

3 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago