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

axios-custom

v1.0.3

Published

A configurable axios wrapper with UI-agnostic 401 handling

Downloads

39

Readme

axios-custom

A small axios wrapper for frontend projects. It keeps axios configurable, adds token injection, supports request-level token overrides, and provides UI-agnostic 401 handling.

Install

npm install axios-custom axios

axios is a peer dependency, so the application should install it.

Basic Usage

import { createAxiosClient } from "axios-custom";

export const request = createAxiosClient({
  baseURL: import.meta.env.VITE_API_URL,
  getToken: () => localStorage.getItem("token"),
});

Then use it like a normal axios instance:

const res = await request({
  url: `/goods/cake/brand/list/dgss/20/${page.value}`,
  method: "get",
});

By default, the client adds this header when a token exists:

Authorization: Bearer <token>

Request-Level Token

Use token when one project needs different tokens for different APIs:

const res = await request({
  baseURL: import.meta.env.VITE_API_URL,
  url: `/goods/cake/brand/list/dgss/20/${page.value}`,
  method: "get",
  token: goodsToken,
});

If token already starts with Bearer , it will be used as-is. Otherwise, the wrapper will add the Bearer prefix.

You can also pass the header yourself:

await request({
  url: "/profile",
  method: "get",
  headers: {
    Authorization: `Bearer ${userToken}`,
  },
});

An explicit Authorization header has priority over automatic token injection.

Skip Auth

Set noAuth: true for public APIs:

await request({
  url: "/login",
  method: "post",
  noAuth: true,
  data: {
    username,
    password,
  },
});

Multiple Clients

For separate business domains, create separate clients:

export const userRequest = createAxiosClient({
  baseURL: import.meta.env.VITE_USER_API_URL,
  getToken: () => localStorage.getItem("userToken"),
});

export const adminRequest = createAxiosClient({
  baseURL: import.meta.env.VITE_ADMIN_API_URL,
  getToken: () => localStorage.getItem("adminToken"),
});

Options

createAxiosClient({
  baseURL: "",
  timeout: 10000,
  headers: {},
  getToken: () => localStorage.getItem("token"),
  attachToken: true,
  beforeRequest: (config) => config,
  transformResponse: (response) => response.data,
  onAuthFail: (error, ctx) => ctx.defaultRedirect("/login"),
  routerPush: (path) => router.push(path),
  showError: (msg) => message.error(msg),
  showInfo: (msg) => message.info(msg),
});

Token Lookup Order

When attachToken is enabled and noAuth is not set, the client uses the first available value:

  1. config.headers.Authorization
  2. config.token
  3. getToken()
  4. localStorage.getItem("token")
  5. sessionStorage.getItem("token")
  6. document.cookie value named token

Response Handling

The default response interceptor supports common response shapes:

{ code: 0, data: ... }
{ success: true, result: ... }

For other backend formats, use transformResponse:

const request = createAxiosClient({
  transformResponse(response) {
    const data = response.data;
    if (data.status === "ok") return data.payload;
    throw new Error(data.message || "Request Error");
  },
});

401 Handling

Use onAuthFail to handle authorization failures without coupling this package to a UI framework:

const request = createAxiosClient({
  onAuthFail(error, ctx) {
    localStorage.removeItem("token");
    ctx.defaultRedirect("/login");
  },
});

TypeScript

This package ships with type declarations and augments axios request config with:

interface AxiosRequestConfig {
  noAuth?: boolean;
  token?: string | null;
}