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

@dsanchos/api

v1.0.5

Published

A lightweight TypeScript fetch wrapper with caching, polling, interceptors, and logging.

Downloads

561

Readme

api

A lightweight TypeScript fetch wrapper with caching, polling, interceptors, and logging.

Лёгкая TypeScript обёртка над fetch с кэшированием, поллингом, интерсепторами и логированием.


Installation / Установка

npm i @dsanchos/api

Quick Start / Быстрый старт

import { ApiCore, defineApi } from "./core.js";
import { api } from "./api.js";

// Configure once / Настройте один раз
ApiCore({
  baseUrl: "https://jsonplaceholder.typicode.com",
  baseKeepUnusedDataFor: 60,
  baseHeader: { "Content-Type": "application/json" },
});

// Make a request / Сделайте запрос
const { data, isLoading, isError, error } = await api({
  method: "GET",
  url: "/posts",
});

ApiCore

Global configuration for all requests.

Глобальная конфигурация для всех запросов.

ApiCore({
  baseUrl: "https://api.example.com", // Base URL for all requests
  baseKeepUnusedDataFor: 60, // Cache TTL in seconds (default: 60)
  baseHeader: {
    // Default headers for all requests
    "Content-Type": "application/json",
  },
  logging: true, // Enable request logging
  interceptors: {
    request: (props) => ({
      // Modify request before sending
      ...props,
      headers: { Authorization: `Bearer ${getToken()}` },
    }),
  },
});

api()

const { data, isLoading, isFetching, isError, error } = await api({
  method: "GET", // GET | POST | PUT | PATCH | DELETE
  url: "/posts", // Path (baseUrl is prepended automatically)
  headers: {}, // Override default headers
  body: {}, // Request body (auto JSON.stringify)

  // Cache
  provideCache: "posts", // Save response under this key
  keepUnusedDataFor: 30, // Override TTL for this request (seconds)
  invalidateCache: "posts", // Delete this cache key after request
});

Response / Ответ

| Field | Type | Description | | ------------ | ---------------- | ---------------------- | | data | any | Response data | | isLoading | boolean | True on first load | | isFetching | boolean | True on any request | | isError | boolean | True if request failed | | error | string \| null | Error message |


Caching / Кэширование

// Save to cache / Сохранить в кэш
await api({
  method: "GET",
  url: "/posts",
  provideCache: "posts", // Cache key
  keepUnusedDataFor: 120, // 2 minutes
});

// Invalidate cache after mutation / Инвалидировать после мутации
await api({
  method: "POST",
  url: "/posts",
  body: { title: "New post" },
  invalidateCache: "posts", // Delete "posts" cache
});

Polling

import { polling } from "./polling.js";

const stop = polling(
  {
    method: "GET",
    url: "/notifications",
    pollingInterval: 3000, // Every 3 seconds / Каждые 3 секунды
    stopAfter: 30000, // Stop after 30 seconds / Остановить через 30 секунд
  },
  (res, stop) => {
    console.log(res.data);
    if (res.isError) stop(); // Stop on error / Остановить при ошибке
  },
);

// Stop manually / Остановить вручную
stop();

defineApi

Group related endpoints without writing types manually.

Группируйте связанные запросы без ручного написания типов.

const usersApi = defineApi({
  getUsers: { method: "GET", url: "/users" },
  createUser: { method: "POST", url: "/users" },
  updateUser: { method: "PUT", url: "/users/1" },
  deleteUser: { method: "DELETE", url: "/users/1" },
});

const { data } = await api(usersApi.getUsers);

Logging / Логирование

When logging: true is set in ApiCore, every request is logged:

При logging: true в ApiCore каждый запрос логируется:

✅ [GET]    /posts    → 200 (123ms)
✅ [POST]   /posts    → 201 (89ms)
❌ [GET]    /posts/99 → 404 (45ms)

Interceptors

Modify every request automatically (e.g. add auth token).

Изменяйте каждый запрос автоматически (например, добавить токен).

ApiCore({
  baseUrl: "https://api.example.com",
  interceptors: {
    request: (props) => ({
      ...props,
      headers: {
        ...props.headers,
        Authorization: `Bearer ${localStorage.getItem("token")}`,
      },
    }),
  },
});

Project Structure / Структура проекта

source/
├── api.ts          # Main fetch wrapper
├── core.ts         # ApiCore config + defineApi
├── polling.ts      # Polling utility
├── logger.ts       # Request logger
└── api.types.ts    # TypeScript types

License / Лицензия

MIT