1.0.0 • Published 5 months ago

dynamic-api-wrapper v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

šŸš€ Dynamic API Wrapper

npm version License: MIT

A powerful, flexible, and easy-to-use API wrapper for any REST API.
Simplify API integrations in Node.js with built-in authentication, error handling, and rate limiting.


🌟 Features

āœ… Supports GET, POST, PUT, DELETE, PATCH requests
āœ… Works with any REST API
āœ… Supports API Key & OAuth authentication
āœ… Automatic retries on failures & rate limits
āœ… Environment variable support (for API keys)
āœ… Modular & Extensible
āœ… TypeScript support for better DX


šŸ“Œ When to Use

  • Connecting to external REST APIs in a Node.js project
  • Building microservices that require multiple API calls
  • Simplifying API integration for internal and external services
  • Handling authentication, retries, and error handling automatically

šŸ› ļø How This API Wrapper Makes Your Work Easier

Before using this wrapper, you might be making raw API calls manually using axios or fetch, leading to:
āŒ Repeated code for handling API requests
āŒ Hardcoded authentication headers everywhere
āŒ No error handling for rate limits or failed requests
āŒ Difficult debugging & maintenance

āœ… How This Wrapper Helps

šŸ”¹ One-time setup → Initialize the client once and reuse it
šŸ”¹ Automatic authentication → No need to manually add headers
šŸ”¹ Built-in error handling → Automatically retries rate-limited requests
šŸ”¹ Less code, more efficiency → Clean, readable API calls
šŸ”¹ Consistent API design → Same method for any REST API

šŸ“Œ Example Before vs. After Using This Wrapper

āŒ Without This Wrapper (Traditional Approach)

const axios = require("axios");

async function getUser(userId) {
  try {
    const response = await axios.get(`https://api.example.com/users/${userId}`, {
      headers: { Authorization: `Bearer ${process.env.API_KEY}` }
    });
    return response.data;
  } catch (error) {
    console.error("API Error:", error.message);
  }
}

getUser("12345");

āœ… With This Wrapper

const DynamicAPIWrapper = require("dynamic-api-wrapper");

const api = new DynamicAPIWrapper({
  baseURL: "https://api.example.com",
  apiKey: process.env.API_KEY,
});

async function getUser() {
  try {
    const user = await api.get("/users/12345");
    console.log(user);
  } catch (error) {
    console.error("Error:", error.message);
  }
}

getUser();

šŸŽÆ What's Different?

āœ… No need to manually handle headers
āœ… No need to handle errors manually
āœ… Reusable client for multiple API calls
āœ… Cleaner, more readable code


šŸ“¦ Installation

Install via NPM:

npm install dynamic-api-wrapper

Or using Yarn:

yarn add dynamic-api-wrapper

šŸš€ Quick Start

1ļøāƒ£ Import & Initialize

const DynamicAPIWrapper = require("dynamic-api-wrapper");

const api = new DynamicAPIWrapper({
  baseURL: "https://api.example.com",
  apiKey: process.env.API_KEY, // API Key from .env
});

2ļøāƒ£ Make API Requests

āœ… GET Request

async function fetchUser() {
  try {
    const user = await api.get("/users/12345");
    console.log(user);
  } catch (error) {
    console.error("Error:", error.message);
  }
}
fetchUser();

āœ… POST Request

api.post("/users", { name: "John Doe" })
  .then(response => console.log(response))
  .catch(error => console.error(error));

āœ… OAuth Support

const api = new DynamicAPIWrapper({
  baseURL: "https://api.example.com",
  token: process.env.OAUTH_TOKEN,
  authType: "oauth",
});

āš™ļø How It Works

  1. Create an API client instance with baseURL and authentication method.
  2. Make API calls using get(), post(), put(), or delete().
  3. Handles rate limits, errors, and retries automatically.

āœ… Uses Axios for requests
āœ… Supports OAuth and API Key-based authentication
āœ… Retries 429 (Rate Limit Exceeded) errors automatically


šŸ› ļø Configuration

Using .env for API Keys

Create a .env file and store your API credentials securely:

API_KEY=your_api_key_here
API_BASE_URL=https://api.example.com

Then, use it in your code:

require("dotenv").config();

šŸ’” Advanced Usage

Custom Headers

api.get("/data", {}, { "Custom-Header": "value" });

Sending Query Parameters

api.get("/search", { query: "test", limit: 10 });

Handling Errors

try {
  const response = await api.get("/users/12345");
} catch (error) {
  console.error("API Error:", error.message);
}

šŸ“ License

This project is licensed under the MIT License.
Read More


šŸ‘Øā€šŸ’» Author

Ankit – Full Stack Developer
šŸ”— LinkedIn
šŸ”— GitHub
šŸ”— NPM Profile

ā˜• Support My Work:
Buy Me A Coffee


šŸ’¬ Contributing

Contributions are welcome!
Feel free to submit issues and pull requests on GitHub.

Happy coding! šŸš€

1.0.0

5 months ago