A powerful Javascript API client built on top of axios, designed for modern React and Node.js applications.
- 🚀 Built on axios with full backward compatibility
- 🔒 JWT authentication and refresh token handling
- 🌐 Works in both browser and Node.js environments
- 📦 Zero bundling conflicts with dependency injection
- 🎯 TypeScript support with full type definitions
- ✨ Simple and intuitive API
npm install @simplify9/simplyapiclient axiosNote: You need to install axios separately as it's a peer dependency.
Before using the client, you need to provide your axios instance:
importaxiosfrom'axios';import{setAxiosInstance}from'@simplify9/simplyapiclient';// Set this ONCE at your application startupsetAxiosInstance(axios);import{ClientFactory}from'@simplify9/simplyapiclient';// Create a client instanceconstapiClient=ClientFactory();// Make API callsconstresponse=awaitapiClient.SimplyGetAsync('/users');console.log(response.data);import{ClientFactory,SetBaseUrl,SetAuthType,SetGetBearer}from'@simplify9/simplyapiclient';// Configure the clientSetBaseUrl('https://api.example.com');SetAuthType('bearer');SetGetBearer(()=>localStorage.getItem('auth_token'));// Create client with configurationconstapiClient=ClientFactory();// All requests will now use the base URL and auth tokenconstresponse=awaitapiClient.SimplyGetAsync('/protected-endpoint');Add this to your main App.js or index.js:
importReactfrom'react';importaxiosfrom'axios';import{setAxiosInstance,SetBaseUrl}from'@simplify9/simplyapiclient';// Setup axios instance (do this once at app startup)setAxiosInstance(axios);SetBaseUrl(process.env.REACT_APP_API_URL||'https://api.example.com');functionApp(){// Your app componentsreturn<div>Your App</div>;}exportdefaultApp;constaxios=require('axios');const{ setAxiosInstance, ClientFactory }=require('@simplify9/simplyapiclient');// SetupsetAxiosInstance(axios);// Use the clientconstclient=ClientFactory();constclient=ClientFactory(config?);// GET requestsawaitclient.SimplyGetAsync(url,options?);// POST requests awaitclient.SimplyPostAsync(url,data?,options?);// PUT requestsawaitclient.SimplyPutAsync(url,data?,options?);// DELETE requestsawaitclient.SimplyDeleteAsync(url,options?);import{SetBaseUrl,SetAuthType,SetGetBearer,SetOnAuthFail,SetRefreshAuth}from'@simplify9/simplyapiclient';// Set base URL for all requestsSetBaseUrl('https://api.example.com');// Set authentication typeSetAuthType('bearer');// or 'basic', etc.// Set function to get auth tokenSetGetBearer(()=>localStorage.getItem('token'));// Set auth failure handlerSetOnAuthFail(()=>{// Handle auth failure (redirect to login, etc.)window.location.href='/login';});// Set token refresh functionSetRefreshAuth(async()=>{// Refresh token logicconstnewToken=awaitrefreshToken();localStorage.setItem('token',newToken);returnnewToken;});All API calls return a standardized response:
{status: 200,// HTTP status codesucceeded: true,// true if status 200-299data: {...},// Response dataerror: null// Error message if any}import{ClientFactory}from'@simplify9/simplyapiclient';constcustomConfig={baseUrl: 'https://custom-api.com',authType: 'bearer',getBearer: ()=>getCustomToken(),onAuthFail: ()=>handleAuthFailure(),refreshAuth: ()=>refreshCustomToken()};constclient=ClientFactory(customConfig);try{constresponse=awaitclient.SimplyGetAsync('/data');if(response.succeeded){console.log('Success:',response.data);}else{console.log('API Error:',response.error);}}catch(error){console.log('Network Error:',error.message);}Install axios if not already installed:
npm install axios
Add axios setup to your app:
importaxiosfrom'axios';import{setAxiosInstance}from'@simplify9/simplyapiclient';setAxiosInstance(axios);
Update axios version if experiencing build issues:
npm install axios@1.6.8
If you get this error:
Axios not found! Please use one of these solutions...
Solution: Make sure you call setAxiosInstance(axios) before creating any clients.
If you see webpack errors related to axios modules:
Downgrade axios to a stable version:
npm install axios@1.6.8
Make sure you're using the latest version of this package:
npm install @simplify9/simplyapiclient@latest
- axios:
>=0.21.1 <2.0.0 - Node.js:
>=12.0.0 - TypeScript:
>=3.8(if using TypeScript)
MIT
Issues and pull requests are welcome on GitHub.