aio-service v6.2.0
aio-service is a javascript dependency for handle all apis and server requests in your application
aio-service is an useful class for develop all api requests in one place in one ore more js files.
In this way, you write all functions related to requests in one file as methods of an object. And throughout the program you call them with the desired features and properties.
You can divide the app requests into several parts in separate files and finally introduce them to the app through one file.
Easily and just by setting a variable, you can save the answers to the requests for a certain period of time so that the answers are read from the memory for the next time and do not request the server again.
This module automatically displays page loaders and message boxes, and you can easily display existing errors.
Through this module, you can specify how to read server errors by configuring it once, and during the application, errors and server messages will be displayed automatically.
You don't need to use catch, this is done by the module itself with the definition you make for it.
You can manage the frontend and backend synchronization operations in one file so that the backend developer alone can do this from the fake data you define.
install
npm i aio-serviceimport
import AIOService from 'aio-service'; create instance
import myApiFunctions from './my-api-functions';
let apis = new AIOService({
id:'my app',
getApiFunctions:myApiFunctions,
token:'......',
baseUrl:'https://my-route/api/v1',
onCatch:(error)=>{
if(error.response){
return error.response.data.Message
}
},
getError:(response)=>{
if(!response.data.isSuccess){return response.data.message}
},
loader:<Loading/>,
})
this.setState({apis});idrequired - string - should set an uniq id.getApiFunctionsrequired - function - returns an object contain api functionstokenoptional - string - this token will set on all requestsbaseUrloptional - string - set base urlonCatchoptional - function - get catched requests response and should returns an string as error messagegetErroroptional - function - get each requests response and if is there any error should returns an string as error messageloaderoptional - html/jsx - set custom loader
getApiFunctions property
you should write a function as getApiFunctions that returns an object contain api functions.
in your project you can write all requests in this function and you can call them in all over your app.
getApiFunctions Provides an object as an input parameter contain below properties:
AxiosAxios dependecy for requests , use this for prevent import itbaseUrlbase url (which was defined when the instance was created).each functin take 2 parameters :
parameteris parameter of function that you can call api function by it.appStateis return result of getState function. this is for read app information in all api functions for prevent send anymore thing to these functions as parameter.each function should returns an object contain :
responsereturn response for error handling.resultreturn result of request to your app.!NOTE result can have any types exept strings. if result type be an string that means you returns an error and this string will show to user as alert. if you want to return an string result you should put it in an object and send it as result.
function myApiFunctions({Axios,baseUrl}){
return {
async getUserInfo(parameter,appState){
let url = `${baseUrl}/GetUserInfo`;
let response = await Axios.get(url);
let result = response.data.UserInfo;
return {response,result}
},
async getMyOrders(parameter,appState){
let url = `${baseUrl}/GetOrders`;
let body = {
userId:appState.userInfo.id,
orderType:parameter
}
let response = await Axios.post(url,body);
let result = response.data.data;
return {response,result}
}
}
}1 - instance methods(request)
apis.request in all over of your app wherever you have access to instance of AIOService class, you can call api functions by call instance.request method
api-functions.js
const apis = new AIOService({...});
export default apis;app.js
let {apis} = useContext(...)
let res = await apis.request({
api:'getMyOrders',
parameter:0,
description:'get user info',
onSuccess:(result)=>{
//for example this.setState({users:result})
},
onError:()=>{//optional
//for example this.close()
},
def:[],
message:{
error:'my custom error message',
success:true
},
cache:{
name:'cache name',
time:24 * 60 * 60 * 1000
},
loading:false,
loadingParent:'.my-box',
token:'....',
getError:(response)=>!response.data.isSuccess?response.data.message
})apirequired - name of api function that writed in getMyApiFunctions returned objectparametervalue that api function will take as parameter (in this example 0)descriptiondescription of action. use in generate alert messagesonSuccessoptional - A function that is called with the result if the request is successful. dont need async and awaitonErroroptional - A function that is called in case of an error in the requestdefoptional - any types - In case of error, this value will be returned as a resultmessageoptional - handle alert messages of requestmessage.errorset false for prevent show error alert, set string for show custom message as alertmessage.successset true for alert success message automatically. set string for alert custom success message. if not set , success message will not showcacheoptional - you can set cache for api function result and next time result will read from cache storagecache.namename of cache for isolate cache resultscache.timeset a number to set time of caching. for example if you set 24 60 60 * 1000 , api function result will cache for 24 hoursloadingfor prevent show loader set loading false. default is true,loadingParentoptional - you can set container selector of loading, by default will render in center of screentokenoptional - If you have set the token in the instance creation, you dont need to set the token heregetErroroptional - If you have set the getError in the instance creation, you dont need to set it here
2 - instance methods ( setToken )
apis.setToken change token of instance by call instance.setToken(token)
apis.setToken(token);3 - instance methods ( getCache )
apis.getCache get chached object that is contain of all cached api functions result
let cacheModel = apis.getCache();4 - instance methods ( setCache )
apis.setCache change a cached value
// first parameter is cache name and second parameter is value to change
apis.setCache('orders',[]);5 - instance methods ( removeCache )
apis.removeCache remove a cached value
// parameter is name of cached value to remove
apis.removeCache('orders');6 - instance methods ( setProperty )
apis.setProperty change properties of instance
apis.setProperty('getState',()=>this.state);
apis.setProperty('loader',<Loading/>);
apis.setProperty('baseUrl','https://apis/v1');manage api functions
get-api-functions.js
import userApiFunctions from './user-api-functions';
import orderApiFunctions from './order-api-functions';
export default function getApiFunctions({Axios,baseUrl}){
return {
user:userApiFunctions({Axios,baseUrl}),
orders:orderApiFunctions({Axios,baseUrl})
}
}user-api-functions.js
export default function userApiFunctions({Axios,baseUrl}){
return {
async getUserInfo(parameter,appState){
let url = `${baseUrl}/GetUserInfo`;
let response = await Axios.get(url);
let result = response.data.UserInfo;
return {response,result}
},
....
}
}order-api-functions.js
export default function orderApiFunctions({Axios,baseUrl}){
return {
async getMyOrders(parameter,appState){
let url = `${baseUrl}/GetOrders`;
let body = {
userId:appState.userInfo.id,
orderType:parameter
}
let response = await Axios.post(url,body);
let result = response.data.data;
return {response,result}
},
....
}
}call nested api functions
let res = await apis.request({
api:'orders.getMyOrders',
parameter:0,
description:'get user info',
def:[],
onSuccess:(orders)=>this.setState({orders})
})