Installation

Installation Guide for hmm-api v2.0.0.

1. Installation

Using npm:

npm install hmm-api@latest

Using yarn:

yarn add hmm-api@latest

2. Basic Usage

Simple setup:

import ApiClient from "hmm-api";

// Create an instance of the ApiClient
const apiClient = new ApiClient();

// Make API requests with automatic error handling
const response = await apiClient.get("https://api.example.com/data");

if (response.success) {
  // Handle successful response
  console.log(response.data);
} else {
  // Handle error response (automatically logged via onError callback)
  console.error(response.error);
}
import ApiClient, { parseResponseMessage } from "hmm-api";
import { toast } from "sonner"; // or your preferred toast library

// Configure once, handle all errors globally
const apiClient = new ApiClient({
  baseUrl: "https://api.example.com",
  returnParsedError: true, // Get clean error messages
  onError: (response) => {
    // Handle ALL API errors in one place
    toast.error(response.error);

    // Handle auth errors globally
    if (response.status === 401) {
      redirectToLogin();
    }
  },
  onSuccess: (response) => {
    // Optional: Handle all successes globally
    console.log("✅ Request successful");
  },
});

// Now all your API calls are automatically handled
const response = await apiClient.get("/users");
const createResponse = await apiClient.post("/users", userData);
// No need to handle errors in every request!

4. Using parseResponseMessage Utility

import { parseResponseMessage } from "hmm-api";

// Use the built-in parser in your own code
const customErrorHandler = (error) => {
  const parsed = parseResponseMessage(error);
  console.log(parsed.message); // Clean error message

  // Use in your own error handling
  showCustomErrorDialog(parsed.message);
};

5. Key Benefits of Global Configuration

  • No repetitive error handling - Configure once, works everywhere
  • Consistent user experience - All errors handled the same way
  • Centralized auth handling - Handle 401 errors globally
  • Clean code - Focus on business logic, not error handling
// Before: Repetitive error handling
const response1 = await api.get("/users");
if (!response1.success) toast.error(response1.error);

const response2 = await api.post("/users", data);
if (!response2.success) toast.error(response2.error);

// After: Global handling - no repetition needed!
const response1 = await api.get("/users"); // Errors handled automatically
const response2 = await api.post("/users", data); // Errors handled automatically