Bone-api
Ajax tool designed for large project
It requset through the dependency Reqwest (with small modification)
Install
npm install bone-api --save
or
<script type="text/javascript" src="./boneapi.min.js"></script>
Global config
It helps you prevent from repeating the same thing when requesting.
boneapi.config({
base: '',
filterData: function(data, config){
data.config = 'param from filter'
return data
},
filterConfig: function(config){
if(config.method == 'delete')
config.method = 'get'
return config
},
route: function(res){
if(res.code == 0){
return true
} else {
return false
}
},
error: function(){
log('Global error')
}
})
Examples
Base usage
boneapi.get('/testapi', {
params: 'value'
}).success(function(res, xhr){
})
More
boneapi.post('http://www.google.com', {
params: 'value'
}, {
headers: {
test: 'value'
}
}).success(function(res, xhr){
console.log(xhr.getResponseHeader('header-attribute'))
}).error(function(res){
return false
}).always(function(){
})
Authorization
You can do it globally
boneapi.config({
filterConfig: function(config){
if(config.headers){
config.headers.auth = 'your token here'
} else {
config.headers ={
auth: 'your token here'
}
}
return config
}
})
or
boneapi.delete('/testapi', {
params: 'value'
}, {
headers: {
auth: 'token'
}
}).success(function(res, xhr){
})
Send JSON with payload
Data will be parsed
boneapi.post('/testapi', {
params: 'value'
}, {
contentType: 'application/json; charset=utf-8'
}).success(function(res, xhr){
})