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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kgan/zero-api

v1.0.22

Published

Minimal-config HTTP client with token support and environment routing.

Downloads

150

Readme

🌀 KGAN Zero API

KGAN Zero API is a lightweight, minimal-config HTTP client for frontend projects. It simplifies making authenticated API requests with automatic environment detection, token extraction, and unified error handling — all in a clean, fluent API.


✨ Features

  • 🔧 Minimal configuration with smart defaults
  • 🔐 Token-based authentication via Authorization header
  • 🌐 Auto-detects environment using location.origin
  • 🧩 Supports both environment-based and custom API endpoints
  • 📦 Handles both application/json and FormData
  • 🚦 Unified error handling for consistent response structure
  • 🧠 Deep token path resolution from localStorage, sessionStorage, or cookies

📦 Installation

npm install @kgan/zero-api

⚙️ Configuration

import { apiClient } from "@kgan/zero-api";

const req = apiClient()
  .config({
    tokenStorage: "localStorage", // or "cookie", "sessionStorage"
    tokenKey: "auth", // Key used to fetch the token object
    tokenPath: "auth.token", // Dot-path to extract token from the object
  })
  .clientURL([
    { type: "local", url: "http://localhost:3000" },
    { type: "development", url: "https://dev.example.com" },
    { type: "test", url: "https://test.example.com" },
    { type: "production", url: "https://example.com" },
  ])
  .serverURL([
    { type: "local", url: "http://localhost:5000/api" },
    { type: "development", url: "https://dev-api.example.com/api" },
    { type: "test", url: "https://test-api.example.com/api" },
    { type: "production", url: "https://api.example.com/api" },
  ])
  .customURL([
    { key: "admin", url: "https://admin-api.example.com" },
    { key: "upload", url: "https://uploads.example.com" },
  ]);

🚀 Usage

Auto-detected environment usage:

const api = req.api(); // Automatically selects matching environment via location.origin

const users = await api.get("/users");
const newUser = await api.post("/users", { name: "Jane Doe" });
const updatedUser = await api.put("/users/123", { active: true });
const patchUser = await api.patch("/users/123", { role: "editor" });
await api.delete("/users/123");

Use a specific environment:

const testAPI = req.get("test");
const stats = await testAPI.get("/stats");

Use a custom endpoint:

const uploadAPI = req.get("upload");

const formData = new FormData();
formData.append("file", selectedFile);

const result = await uploadAPI.post("/media", formData);

🔐 Token Extraction Logic

The token is extracted based on your configuration:

| Option | Description | | -------------- | -------------------------------------------------------------------- | | tokenStorage | Source of token: "localStorage", "sessionStorage", or "cookie" | | tokenKey | Key under which the token object is stored | | tokenPath | Path inside the object to extract the token |

Example stored value in localStorage:

{
  "auth": {
    "token": "abc123token"
  }
}

❗ Error Handling

Errors return a structured object:

try {
  await api.get("/restricted");
} catch (err) {
  console.error(err.status); // HTTP status code
  console.error(err.message); // Error message from server
}

Non-JSON responses are returned as { message: string }.


🧪 Types

export type EnvType = "local" | "development" | "test" | "production";

export type ConfigOptions = {
  tokenStorage: "cookie" | "localStorage" | "sessionStorage";
  tokenKey: string;
  tokenPath: string;
};

export type URLMapping = {
  type: EnvType;
  url: string;
};

export type CustomURLMapping = {
  key: string;
  url: string;
};

🛠️ License

MIT License © 2025 KGAN