
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
aio-service
Advanced tools
``` javascript npm i aio-service ``` # import ``` javascript import AIOService from 'aio-service'; ``` # create instance ``` javascript import myApiFunctions from './my-api-functions';
npm i aio-service
import AIOService from 'aio-service';
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});
id required - string - should set an uniq id.getApiFunctions required - function - returns an object contain api functionstoken optional - string - this token will set on all requestsbaseUrl optional - string - set base urlonCatch optional - function - get catched requests response and should returns an string as error messagegetError optional - function - get each requests response and if is there any error should returns an string as error messageloader optional - html/jsx - set custom loaderyou 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:
Axios Axios dependecy for requests , use this for prevent import itbaseUrl base url (which was defined when the instance was created).each functin take 2 parameters :
parameter is parameter of function that you can call api function by it.appState is 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 :
response return response for error handling.result return 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}
}
}
}
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
})
api required - name of api function that writed in getMyApiFunctions returned objectparameter value that api function will take as parameter (in this example 0)description description of action. use in generate alert messagesonSuccess optional - A function that is called with the result if the request is successful. dont need async and awaitonError optional - A function that is called in case of an error in the requestdef optional - any types - In case of error, this value will be returned as a resultmessage optional - handle alert messages of requestmessage.error set false for prevent show error alert, set string for show custom message as alertmessage.success set true for alert success message automatically. set string for alert custom success message. if not set , success message will not showcache optional - you can set cache for api function result and next time result will read from cache storagecache.name name of cache for isolate cache resultscache.time set a number to set time of caching. for example if you set 24 * 60 * 60 * 1000 , api function result will cache for 24 hoursloading for prevent show loader set loading false. default is true,loadingParent optional - you can set container selector of loading, by default will render in center of screentoken optional - If you have set the token in the instance creation, you dont need to set the token heregetError optional - If you have set the getError in the instance creation, you dont need to set it hereapis.setToken change token of instance by call instance.setToken(token)
apis.setToken(token);
apis.getCache get chached object that is contain of all cached api functions result
let cacheModel = apis.getCache();
apis.setCache change a cached value
// first parameter is cache name and second parameter is value to change
apis.setCache('orders',[]);
apis.removeCache remove a cached value
// parameter is name of cached value to remove
apis.removeCache('orders');
apis.setProperty change properties of instance
apis.setProperty('getState',()=>this.state);
apis.setProperty('loader',<Loading/>);
apis.setProperty('baseUrl','https://apis/v1');
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})
})
FAQs
``` javascript npm i aio-service ``` # import ``` javascript import AIOService from 'aio-service'; ``` # create instance ``` javascript import myApiFunctions from './my-api-functions';
The npm package aio-service receives a total of 0 weekly downloads. As such, aio-service popularity was classified as not popular.
We found that aio-service demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.