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

axhr

v1.0.0

Published

axhr axios

Readme

axhr

XHR 配置化请求库 基于 axios ⏲ 🚀


AXHR AXHR version npm download build status

Installing

# npm
npm install axhr axios --save
# yarn
yarn add axhr axios

use

xhr.request({
  url: '/add',
  type: 'GET',
  headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
  baseUrl: 'https://some-domain.com/api/',
  data: {},
  config: {
    ...others
  },
  cancelMsg: '',
  success: (res, response) => {},
  error: res => {},
  cancel: err => {},  
});
  • url: url [required] 请求接口服务地址
  • type: type [required] 请求方式, 默认 GET
  • header: header 自定义请求头, 默认 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  • baseUrl: baseURL基础地址, 除非url是绝对地址,否则会加在url前面
  • data: data 请求体参数,会根据配置自动转paramsdata
  • success: 请求成功响应, 使用xhr.success 拦截需要返回 true
  • error: 请求失败响应 或 xhr.success 拦截返回 false
  • cancel: 取消请求响应
  • cancelMsg: 取消请求提示信息
  • config: 请求配置项,详情见https://github.com/axios/axios#request-config
    • isCancelTokenfalse, 默认当前请求不参与取消请求; 也可以设置true,独立token
    • noRepeat: false, 默认不判断重复请求, cancelToken 必须是 true 将会取消还未响应结束的上一个重复请求

API

搭配一些全局定义方法进行全局配置,对所有请求管用

xhr.defaultConfig

全局默认配置, 将会合并进 config

xhr.defaultConfig = {
  timeout: 10000,
  withCredentials: true // cookie
};
// or
xhr.defaultConfig = () => {
  return {
    timeout: 10000,
    withCredentials: true // cookie
  }
}

xhr.baseData

全局基础参数, 将会合并进 config.data

xhr.baseData = {
  t: Date.now() // IE catch
};

xhr.baseUrl

全局定义baseUrl 优先级低于 xhr.getUrl

xhr.getUrl

全局动态设置获取url, baseUrl

const apiBaseUrl = '/oapi';

xhr.getUrl = option => {
  if (option.baseUrl) {
    return {
      baseUrl: option.baseUrl,
      url: option.url
    };
  }
  return {
    baseUrl: apiBaseUrl,
    url: option.url
  };
};

xhr.success

当请求成功时实现动态拦截配置, 也可以在这块做一些接口数据返回初步逻辑拦截

xhr.success = (res, resp) => {
  let isSuccess = true;

  if (typeof res === 'string') {
    res = JSON.parse(res);
  }

  if (typeof res !== 'object') {
    // console.error(apiUrl + ': response data should be JSON');
    isSuccess = false;
  }
  switch (res.code + '') {
    case '200':
      isSuccess = true;
      break;
    default:
      // console.error(res.message || 'unknown error');
      isSuccess = false;
  }

  return isSuccess;
}

xhr.error

当请求失败时实现动态拦截配置,

xhr.error = (err, [isCancel]) => {}

xhr.cancelXhr

手动调用取消请求,并传递取消信息

可以指定urls取消;不传urls则取消所有请求

xhr.cancelXhr(message, urls?: []);

xhr.before

请求前执行

xhr.before = () => {}

xhr.end

请求后执行

xhr.end = (res) => {}

全局配置

简单示例

import xhr from 'axhr';
import {message} from 'antd';
import auth from './auth';

let apiUrl = '';
const apiBaseUrl = '/';

xhr.getUrl = option => {
  if (option.baseUrl) {
    apiUrl = option.baseUrl + option.url;
    return {
      baseUrl: option.baseUrl,
      url: option.url
    };
  }
  apiUrl = apiBaseUrl + option.url;
  return {
    baseUrl: apiBaseUrl,
    url: option.url
  };
};

xhr.baseData = {
  t: Date.now()
};

xhr.defaultConfig = {
  timeout: 10000,
  withCredentials: true
};

xhr.success = (res, response) => {
  let isSuccess = true;

  if (typeof res !== 'object') {
    message.error(apiUrl + ': response data should be JSON');
    isSuccess = false;
  }
  switch (res.code + '') {
    case '000000':
      isSuccess = true;
      break;
    case '100006':
      auth.toOutLogin();
      isSuccess = false;
      break;
    case '100013':
      auth.toOutLogin();
      isSuccess = false;
      break;
    default:
      message.error(res.message || 'unknown error');
      isSuccess = false;
  }

  return isSuccess;
};

xhr.error = (err) => {
  message.error('The server strayed!');
};