simple-axios-wechat
v0.2.1
Published
小于1kb的简易的axios微信小程序版使用typescript实现
Downloads
31
Maintainers
Readme
simple-axios-wechat
- 小于1kb的简易的axios微信小程序版使用typescript实现
目录
Installing
Package manager
Using npm:
$ npm install simple-axios-wechat
Using yarn:
$ yarn add simple-axios-wechat
直接调用
- (SimpleAxios(config))
import SimpleAxios from 'simple-axios-wechat'
SimpleAxios<{result: any}>(config)
.then(res => { console.log(res) })
.catch(err => { console.error(err) })
method别名
- 与SimpleAxios.create()实例一致
axios.request<{result: any}>(config)
axios.get<{result: any}>(url[, config])
axios.post<{result: any}>(url[, data[, config]])
axios.put<{result: any}>(url[, data[, config]])
axios.delete<{result: any}>(url[, config])
axios.head<{result: any}>(url[, config])
axios.options<{result: any}>(url[, config])
axios.trace<{result: any}>(url[, data[, config]])
axios.connect<{result: any}>(url[, data[, config]])
Example
get请求并定义返回数据类型
import SimpleAxios from 'simple-axios-wechat'
// 默认 get 请求
SimpleAxios<{result: string}>({ url: 'http://example.org/get' })
.then(res => { console.log(res) })
.catch(err => { console.error(err) })
SimpleAxios.get<{result: string}>('http://example.org/get')
.then(res => { console.log(res) })
.catch(err => { console.error(err) })
post请求并定义返回数据类型
SimpleAxios<{result: string}>({ url: 'http://example.org/get', method: 'POST' })
.then(res => { console.log(res) })
.catch(err => { console.error(err) })
SimpleAxios.post<{result: string}>('http://example.org/post', { data: {} })
.then((res) => { console.log(res) })
.catch((err) => { console.error(err) });
全局默认值
- (默认值优先级与axios一致,后设置大于先设置)
import SimpleAxios from 'simple-axios-wechat'
SimpleAxios.defaults.baseURL = 'http://example.org/get'
SimpleAxios.defaults.timeout = 10 * 1000
创建实例
import SimpleAxios from 'simple-axios-wechat'
const simpleAxios = SimpleAxios.create({
baseURL: 'http://example.org/get',
timeout: 10 * 1000
})
拦截器
import SimpleAxios from 'simple-axios-wechat'
const simpleAxios = SimpleAxios.create({
baseURL: 'http://example.org/get'
})
simpleAxios.interceptors.request.use((config) => {
// 干点啥?
return config
}, (err) => {
return Promise.reject(err)
})
simpleAxios.interceptors.response.use((response) => {
// 总得干点啥吧?
return response
}, (err) => {
return Promise.reject(err)
})
获取RequestTask
- 在请求config配置项中通过回调获取RequestTask(比如用来 abort())
import SimpleAxios from 'simple-axios-wechat'
SimpleAxios<{data: any, state: number}>({
url: 'http://example.org/get',
getRequestTask: (task) => { task.abort() }
})
.catch(err => { console.error(err) })
自定义请求方法
- (要求返回一个Promise)
import SimpleAxios from 'simple-axios-wechat'
const simpleAxios = SimpleAxios.create({
baseURL: 'http://example.org/get',
adapter: function adapter(config) {
return new Promise((resolve, reject) => {
const RequestTask = wx.request({
...config,
success: (res) => { resolve(res) },
fail: (err) => { reject(err) }
})
// ! ** 自定义adapter需要通过回调暴露RequestTask **
config.getRequestTask && config.getRequestTask(RequestTask)
})
}
})