dynamic-api-wrapper v1.0.0
š Dynamic API Wrapper
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
- Create an API client instance with
baseURL
and authentication method. - Make API calls using
get()
,post()
,put()
, ordelete()
. - 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
š¬ Contributing
Contributions are welcome!
Feel free to submit issues and pull requests on GitHub.
Happy coding! š
5 months ago