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 🙏

© 2025 – Pkg Stats / Ryan Hefner

api-basekit

v1.0.3

Published

Abstract base class for Axios-powered HTTP services with pagination helpers.

Downloads

363

Readme

base-api

An abstract base class for building Axios-powered HTTP services in TypeScript. It cannot be instantiated directly; you must extend and implement the required hooks.

Install

# pnpm
pnpm add base-api axios

# npm
npm install base-api axios

# yarn
yarn add base-api axios

Quick start

  1. Extend BaseAPI.
  2. Implement the abstract methods:
    getAxiosInstanceWithToken, getRawAxiosInstance, updateAxiosInstanceParamsSerializer, getNormalizedListType, getNormalizedIndexFilter, getNormalizedItem.
  3. Configure endpoints (e.g., base and byId).
// api/UserApi.ts
import axios from 'axios'
import { BaseAPI, type ListType } from 'base-api'

type User = { id: number; name: string }

export class UserApi extends BaseAPI<User> {
  constructor () {
    super('/api/users')
    this.defaultObject = { id: 0, name: '' }
  }

  getAxiosInstanceWithToken () {
    const instance = axios.create({
      // example: add auth header
      headers: { Authorization: `Bearer ${localStorage.getItem('token') ?? ''}` }
    })
    this.updateAxiosInstanceParamsSerializer(instance)
    return instance
  }

  getRawAxiosInstance () {
    const instance = axios.create()
    this.updateAxiosInstanceParamsSerializer(instance)
    return instance
  }

  updateAxiosInstanceParamsSerializer (instance: ReturnType<typeof axios.create>) {
    // apply params serializer or other axios-level config here
  }

  getNormalizedIndexFilter (filters: Record<string, any>) {
    // example: convert page to offset
    if (filters.page != null && filters.length != null) {
      return { ...filters, offset: (filters.page - 1) * filters.length }
    }
    return filters
  }

  getNormalizedListType (response: { data: ListType<User> }): ListType<User> {
    // map/normalize response fields if needed
    return response.data
  }

  getNormalizedItem<G> (item: G): G {
    // normalize individual item if needed
    return item
  }
}

Using the methods

const userApi = new UserApi()

// list
const list = await userApi.index({ page: 1, length: 20 }) // or offset/length

// fetch one
const user = await userApi.get('123')

// create
const newId = await userApi.create({ name: 'Alice' })

// update
await userApi.update('123', { name: 'Bob' })

// delete
await userApi.delete('123')

Fetch all pages helpers

// using built-in list method
const allUsers = await userApi.getAllPagesBaseList({ length: 100 })

// or pass a custom list function
const allUsers2 = await userApi.getAllPages(userApi.index.bind(userApi), { length: 100 })

Scripts

pnpm run build   # build dist with ESM/CJS and d.ts
pnpm run clean   # remove dist

Notes

  • ListType is open-ended; add your own keys and map them inside getNormalizedListType.
  • axios is a peer dependency; install a compatible version in the consumer project.
  • If you rely on custom axios extensions (e.g., getWithCache), extend axios types or wrap the instance in your project.