Skip to content

Repository files navigation

Simply API Client

A powerful Javascript API client built on top of axios, designed for modern React and Node.js applications.

Features

  • 🚀 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

Installation

npm install @simplify9/simplyapiclient axios

Note: You need to install axios separately as it's a peer dependency.

Quick Start

1. Setup (Required)

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);

2. Basic Usage

import{ClientFactory}from'@simplify9/simplyapiclient';// Create a client instanceconstapiClient=ClientFactory();// Make API callsconstresponse=awaitapiClient.SimplyGetAsync('/users');console.log(response.data);

3. With Configuration

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');

Complete Setup Guide

For React Applications

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;

For Node.js Applications

constaxios=require('axios');const{ setAxiosInstance, ClientFactory }=require('@simplify9/simplyapiclient');// SetupsetAxiosInstance(axios);// Use the clientconstclient=ClientFactory();

API Reference

Client Methods

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?);

Configuration Functions

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;});

Response Format

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}

Advanced Usage

Custom Client Configuration

import{ClientFactory}from'@simplify9/simplyapiclient';constcustomConfig={baseUrl: 'https://custom-api.com',authType: 'bearer',getBearer: ()=>getCustomToken(),onAuthFail: ()=>handleAuthFailure(),refreshAuth: ()=>refreshCustomToken()};constclient=ClientFactory(customConfig);

Error Handling

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);}

Migration Guide

From Version 2.1.x to 2.2.x

  1. Install axios if not already installed:

    npm install axios
  2. Add axios setup to your app:

    importaxiosfrom'axios';import{setAxiosInstance}from'@simplify9/simplyapiclient';setAxiosInstance(axios);
  3. Update axios version if experiencing build issues:

    npm install axios@1.6.8

Troubleshooting

"Axios not found" Error

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.

Webpack Build Errors

If you see webpack errors related to axios modules:

  1. Downgrade axios to a stable version:

    npm install axios@1.6.8
  2. Make sure you're using the latest version of this package:

    npm install @simplify9/simplyapiclient@latest

Requirements

  • axios: >=0.21.1 <2.0.0
  • Node.js: >=12.0.0
  • TypeScript: >=3.8 (if using TypeScript)

License

MIT

Contributing

Issues and pull requests are welcome on GitHub.

About

JavaScript API client built on axios with JWT and refresh token handling for React and Node.js apps

Topics

Resources

Code of conduct

Contributing

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages