npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-flex

v1.0.2

Published

A powerful, easy-to-use library for seamless API integration with built-in retries, caching, and error handling.

Downloads

222

Readme

Api-Flex

npm npm GitHub issues Build Status License

api-flex is a modern, powerful, and flexible library for handling API requests. It combines the simplicity of fetch with the power of axios, while introducing advanced features such as automatic retries, token management, centralized error handling, and in-memory response caching.

🚀 Key Features

  • Unified API: Combines the best of both fetch and axios.
  • Automatic Retries: Configurable retry mechanism for failed requests.
  • Token Management: Easily manage authorization tokens.
  • Centralized Error Handling: Built-in error logging and handling.
  • Response Caching: Optional caching to improve performance for repeated requests.
  • Lightweight: A small footprint with a powerful feature set.

📦 Installation

Install api-flex via npm:

npm install api-flex

🎯 Why Choose api-flex?

api-flex abstracts the complexity of API integrations, providing you with a robust and intuitive API to handle HTTP requests. No need to manually set up retries, caching, or token management—everything is handled out-of-the-box.

💻 Usage

🧩 Simplified API Example

Making a simple GET request is as easy as:

import apiFlex from "api-flex";

async function fetchUser() {
  try {
    const data = await apiFlex.get("https://api.example.com/user/12345");
    console.log("User data:", data);
  } catch (error) {
    console.error("Error fetching user:", error);
  }
}

fetchUser();

📚 Real-World Use Cases

  • User Profiles: Manage user data and profiles from your APIs with ease.
  • E-commerce Applications: Fetch and update product listings, inventories, or order details.
  • Analytics Dashboards: Pull in analytical data from multiple sources, with error handling and caching.
  • Dynamic SPAs: Load dynamic content in your Single Page Applications with smooth, cached API calls.

⚙️ Advanced Examples

🟢 GET Request with Caching

import apiFlex from "api-flex";

// GET request with 5-minute cache enabled
apiFlex
  .get("https://jsonplaceholder.typicode.com/posts/1", {}, true)
  .then((data) => console.log("Post data:", data))
  .catch((err) => console.error("Error:", err));

🟢 POST Request with Data

import apiFlex from "api-flex";

// POST request to create a new resource
apiFlex
  .post("https://jsonplaceholder.typicode.com/posts", {
    title: "New Post",
    body: "This is a new post.",
    userId: 1,
  })
  .then((data) => console.log("Created Post:", data))
  .catch((err) => console.error("Error:", err));

🟢 PUT Request to Update Data

import apiFlex from "api-flex";

// PUT request to update a resource
apiFlex
  .put("https://jsonplaceholder.typicode.com/posts/1", {
    id: 1,
    title: "Updated Title",
    body: "Updated body content",
    userId: 1,
  })
  .then((data) => console.log("Updated Post:", data))
  .catch((err) => console.error("Error:", err));

🟢 DELETE Request

import apiFlex from "api-flex";

// DELETE request to remove a resource
apiFlex
  .delete("https://jsonplaceholder.typicode.com/posts/1")
  .then(() => console.log("Post deleted successfully."))
  .catch((err) => console.error("Error:", err));

🟢 GET Request with Custom Headers

import apiFlex from "api-flex";

// GET request with custom headers (e.g., Authorization)
apiFlex
  .get(
    "https://jsonplaceholder.typicode.com/posts",
    { Authorization: "Bearer my-token" },
    false
  )
  .then((data) => console.log("Data with custom headers:", data))
  .catch((err) => console.error("Error:", err));

🟢 Retry Logic Example

import apiFlex from "api-flex";

// GET request with retries
apiFlex
  .get("https://api.somedomain.com/unreliable-endpoint", {}, true)
  .then((data) => console.log("Data after retries:", data))
  .catch((err) => console.error("Error after retries:", err));

🟢 Handling Timeouts

import apiFlex from "api-flex";

// GET request with timeout of 2 seconds
apiFlex
  .get("https://jsonplaceholder.typicode.com/posts", { timeout: 2000 })
  .then((data) => console.log("Data received:", data))
  .catch((err) => console.error("Timeout Error:", err));

🟢 Dynamic Token Management

import apiFlex from "api-flex";

// Set token dynamically
apiFlex.setToken("my-dynamic-token");

// GET request with token
apiFlex
  .get("https://jsonplaceholder.typicode.com/posts")
  .then((data) => console.log("Data with token:", data))
  .catch((err) => console.error("Error:", err));

🌐 Express Integration with api-flex

You can use api-flex in a Node.js/Express environment effortlessly:

import express from "express";
import apiFlex from "api-flex";

const app = express();

app.get("/product", async (req, res) => {
  try {
    const product = await apiFlex.get(
      "https://jsonplaceholder.typicode.com/products/1"
    );
    res.json(product);
  } catch (error) {
    console.error("Error fetching product:", error.message);
    res.status(500).send("Failed to fetch product.");
  }
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

📊 Comparison: fetch, axios, and api-flex

| Feature | fetch | axios | api-flex | | ---------------------- | ----------- | ----------- | -------------- | | Built-in retries | ❌ | ❌ | ✅ | | Token management | ❌ (manual) | ❌ (manual) | ✅ (automatic) | | Centralized errors | ❌ | ❌ | ✅ | | Response caching | ❌ | ❌ | ✅ |

📈 Conclusion

api-flex combines the best of modern API handling libraries, making it your go-to tool for powerful and streamlined API requests. Forget the headaches of retries, caching, token handling, and error management—api-flex does it all for you!


📜 License

MIT License. See LICENSE for details.

🤝 Contributing

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