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

axios-class

v1.0.5

Published

new http request with axios

Downloads

4

Readme

Http

To create instances of axios with common config、token and interceptors. we also give out a default status validator and code validator. but a more intuitive ui component to notify errors is strongly recommanded.

Usage

Use Your Own Notification

The default notification while got http error and some other error message from server is console.log, you should customize your own notification first.

import { Notify } from "axios-class";
import { notification } from "ant-design-vue";

// use your own way of notify errors,
// obviously, this is a global setting.
const notifyError = (message: string, duration: number) =>
  notification.error({ message, duration, description: "" });
Notify.init({ error: notifyError });

Create Your Http Instances

Our application usually have multiple axios instances. Rather than multi-configs, multi-instances has a more semantic expression. You should create new axios instances when different base url, configs or inteceptors are needed.

import { Http } from "axios-class";

// create your axios instances
const baseURL = "http://api.xxx.net";
export const baseHttp = new Http(baseURL);

const ssoURL = "http://sso.xxx.net";
export const ssoHttp = new Http(ssoURL);

// we can use ssoHttp to do some login/authorize stuffs,
// then get datas with baseHttp mostly.

Make Http Request

Use 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head' | 'options' functions to make http request, just like you are using axios functions with typescript supported.

CRUD demo with typescript

  • default types declared in http.d.ts
// the default response wrapper
export declare interface ResponseWrapper<T> {
  code: number;
  data?: T;
  msg?: string;
}

// default data declaration for paginated response
export declare interface Paginated<T> {
  current: number;
  pageSize: number;
  total: number;
  items?: T[];
}

// default query params declaration for paginated query
export declare type PaginatedQuery = {
  current: number;
  pageSize: number;
  sorter?: Sorter;
};
export declare type Sorter = {
  [key: string]: "asc" | "desc";
};
  • the demo model
export interface ICategory {
  id: number;
  title: string;
  description?: string;
  parent_id?: number;
  created_at?: number;
}
  • create http instance
// http.ts
import { Http } from "axios-class";

// create http instances
const baseURL = "http://api.xxx.net";
export const http = new Http(baseURL);
  • your fetch api
// category.api.ts
import { http } from "./http";

/**
 * get paginated list of category
 * @param {Partial<ICategory> & PaginatedQuery} params fetch params
 * @returns the paginated list
 */
export const getCategoryList = (params: Partial<ICategory> & PaginatedQuery) =>
  http.get<Paginated<ICategory>>("/api/categories", { params });

/**
 * add new category
 * @param {*} params
 * @returns the newly created entity
 */
export const addCategory = (params: Partial<ICategory>) =>
  http.post("/api/categories", params);

/**
 * update an existed category
 * @param {Partial<ICategory>} params the entity to update
 * @returns the updated entity
 */
export const updateCategory = (params: Partial<ICategory>) =>
  http.put(`/api/categories/${params.id}`, params);

/**
 * delete category with the given id
 * @param {number} id
 * @returns 1 if deleted
 */
export const deleteCategory = (id: number) =>
  http.delete(`/api/categories/${id}`);
  • use the apis
import { getCategoryList, addCategory, updateCategory, deleteCategory } from '@/service/category.api.ts';

const queryParams = { pageSize: 12, current: 1 };
const { code, data, msg } = await getCategoryList(queryParams);
console.log(data instanceof Paginated<ICategory>); // output: true


const toAdd = { title: 'new category', description: 'test add category' };
const { code, data, msg } = await addCategory(toAdd);
console.log('created category : ', JSON.stringify(data));
// output: { id: 1001, title: 'new category', description: 'test add category' }

const toUpdate = { id: 1001, title: 'update category', description: 'test update category' };
const { code, data, msg } = await updateCategory(toUpdate);
console.log('update category : ', JSON.stringify(data);
// output: { id: 1001, title: 'update category', description: 'test update category' }


const { code, data, msg } = await deleteCategory(1001);
console.log('delete category : ', data > 0);
// output: delete category : true