![LICENSE](https://camo.githubusercontent.com/118147d0e46823e9ecdbcedb7ca87bdb9c432d73a0a1bcfba7630e275e96c79f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542532303939362d626c75652e737667)
- A vue plug-in integrated with axios. Build the API using chain programming and return the request instance as a Promise. A nice simplification of how apis are built, and how they are referenced.
- 一个集成了axios的vue插件。使用链式编程方式构建api,并以Promise返回请求实例。很好地简化了api的构建方式,和引用方式(通过this.$apis.apiName进行引用)。
![Chrome](https://camo.githubusercontent.com/c23f02f6604b0b5fee3615832d7f237055892cdda9f8a2c46e62131cf7e7cd12/68747470733a2f2f7261772e6769746875622e636f6d2f616c7272612f62726f777365722d6c6f676f732f6d61737465722f7372632f6368726f6d652f6368726f6d655f34387834382e706e67) |
![Firefox](https://camo.githubusercontent.com/234a8814dfe9d689612fe6f899acbba9b1385af208cd693bc3b08cfc931ed954/68747470733a2f2f7261772e6769746875622e636f6d2f616c7272612f62726f777365722d6c6f676f732f6d61737465722f7372632f66697265666f782f66697265666f785f34387834382e706e67) |
![Safari](https://camo.githubusercontent.com/40dd86d718fa616895f201e601dfe0d1988c01c5a57c27fe6f104999eef377a2/68747470733a2f2f7261772e6769746875622e636f6d2f616c7272612f62726f777365722d6c6f676f732f6d61737465722f7372632f7361666172692f7361666172695f34387834382e706e67) |
![Opera](https://camo.githubusercontent.com/e13076052cc3a8e0e2d3532a7e63bb4b0971cc796768e85f11897b76654f3216/68747470733a2f2f7261772e6769746875622e636f6d2f616c7272612f62726f777365722d6c6f676f732f6d61737465722f7372632f6f706572612f6f706572615f34387834382e706e67) |
![Edge](https://camo.githubusercontent.com/490746383ccfcbcadcb2d362870f855731dbe73c3230b171d31f5b090406816e/68747470733a2f2f7261772e6769746875622e636f6d2f616c7272612f62726f777365722d6c6f676f732f6d61737465722f7372632f656467652f656467655f34387834382e706e67) |
![IE](https://camo.githubusercontent.com/d51c4abaf7ee3294cf830f92706910b44e3366852800e5e2087dd462905e4393/68747470733a2f2f7261772e6769746875622e636f6d2f616c7272612f62726f777365722d6c6f676f732f6d61737465722f7372632f617263686976652f696e7465726e65742d6578706c6f7265725f392d31312f696e7465726e65742d6578706c6f7265725f392d31315f34387834382e706e67) |
Latest ✔ |
Latest ✔ |
Latest ✔ |
Latest ✔ |
Latest ✔ |
11 ✔ |
![Browser Matrix](https://camo.githubusercontent.com/47daeb0862ede10a83498a66521f61de63e6c696f9c55967e168d47a1a11c0b2/68747470733a2f2f73617563656c6162732e636f6d2f6f70656e5f73617563652f6275696c645f6d61747269782f6178696f732e737667)
npm install vue-apis
// or
yarn add vue-apis
import Vue from 'vue'
import VueApis from 'vue-apis'
Vue.use(VueApis, options)
function |
example |
argument |
description |
setUrl |
setUrl('https://baidu.com') |
(url: String) |
set request url address |
setData |
setData({ a: 1}) |
(data: Object) |
set post body object |
setParams |
setParams({ t: Date.now() }) |
(params: Object) |
set request url query |
setHeaders |
setHeaders({ 'content-type': 'application/json' }) |
(headers: Object) |
set request headers |
setTimeout |
setTimeout(10000) |
(timeout: Number) |
set request timeout |
setCustomOptions |
setCustomOptions({ responseType: 'stream' }) |
(options: Object, clear: boolean) |
set custom options |
option key |
type |
default value |
description |
apis |
object |
{} |
api set |
showLoading |
function |
undefined |
show loading layout function |
hideLoading |
function |
undefined |
hide loading layout function |
interceptors |
InterceptorObject |
undefined |
You can intercept requests or responses before they are handled by then or catch. |
field |
type |
description |
request |
RequestObject / Function |
When the instance is 'Function', it is a 'then' callback to the interceptor |
response |
ResponseObject / Function |
When the instance is 'Function', it is a 'then' callback to the interceptor |
Function |
e.g. |
then |
(config) => { return config; } |
catch |
(error) => { return Promise.reject(error); } |
Function |
e.g. |
then |
(response) => { return response; } |
catch |
(error) => { return Promise.reject(error); } |
import Vue from 'vue'
import VueApis from 'vue-apis'
import Api from './api'
Vue.use(VueApis, {
apis: Api,
showLoading: () => {
console.log('showLoading')
},
hideLoading: () => {
console.log('hideLoading')
},
interceptors: {
request: {
then(config) {
// Do something before request is sent
return config;
},
catch(error) {
// Do something with request error
return Promise.reject(error);
}
},
response: {
then(response) {
// Do something with response data
return response;
},
catch(error) {
// Do something with response error
return Promise.reject(error);
}
}
}
})
import { ApiOptions, ApiMethod } from 'vue-apis'
const $api = {
readme () {
return new ApiOptions()
.setUrl(`https://raw.githubusercontent.com/Chans-Open-Source/vue-apis/master/README.md`)
.setMethod(ApiMethod.GET)
.setParams({
timestamp: Date.now()
})
.setHeaders({
Authorization: `Bearer ${Date.now()}`
})
.request()
}
}
export default $api
<template>
<div v-html="readme"></div>
</template>
<script>
export default {
data () {
return {
readme: ''
}
},
async created () {
const res = await this.$apis.readme()
this.readme = res
}
}
</script>
const $api = {
formDataRequest (formData) {
return new ApiOptions()
.setUrl(URL)
.setMethod(ApiMethod.POST)
.setData(formData)
.request()
}
}
const formData = new window.FormData()
formData.append(key, value)
// Request
this.$apis.formDataRequest(formData)
const $api = {
formDataRequest (formData) {
return new ApiOptions()
.setUrl(URL)
.setMethod(ApiMethod.POST)
.setData(formData)
.setCustomOptions({
url: `https://baidu.com`, // invalid
data: {}, // invalid
params: {}, // invalid
headers: {}, // invalid
method: {}, // invalid
responseType: 'stream' // valid
}, /* is clear all custom options at first */ false)
.request()
}
}