@devboostsolution/api-client
v1.0.6
Published
This is an axios extension that makes it easier for developers to call the API through the application
Downloads
8
Readme
API-CLIENT
This is an axios extension that makes it easier for developers to call the API through the application
Fetures
- Support standards request:
GET
,POST
,PATCH
,DELETE
,PUT
. - Allow customize Axios Instance and Error Handling function.
- TypeScript Compatibility.
- Allow extend and modify API wrapper client by implement interfaces and extends base interceptor.
Installation
Npm:
npm install @devboostsolution/api-client
Yarn:
yarn install @devboostsolution/api-client
Usage example
Initilze API client
/**
* Step:
* 1. Init axios instance
* 2. Define errorHandle function
* 3. Init axios interceptor
* 4. Init axios api client
*/
const axiosInstance = axios.create({
baseURL: process.env.BASE_URL,
timeout: 1000,
headers: {
'Content-Type': 'application/json;charset=utf-8',
},
});
onst errorHandler = (error: any) => {
if (error.response) {
console.log(`ErrorHandler is called`);
}
if (error.response.data) {
return error.response.data;
}
return Promise.reject(error);
};
// Mode: development, production, test
const axiosInterceptor = new AxiosInterceptor(
axiosInstance,
errorHandler,
'development',
);
const apiClient = new AxiosApiClient(axiosInterceptor);
const response = await apiClient.get({ url: '/api/category' });
Development setup
Clone the repository
make install
npm test
Meta
Distributed under the MOT license. See LICENSE
for more information.
https://github.com/DevBoostSolutions/api-client
Contributing
Fork it (https://github.com/DevBoostSolutions/api-client/fork)
Create your feature branch (
git checkout -b feature/fooBar
)Commit your changes (
git commit -am 'Add some fooBar'
)Push to the branch (
git push origin feature/fooBar
)Create a new Pull Request