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 🙏

© 2024 – Pkg Stats / Ryan Hefner

axios-http-client

v1.0.19

Published

axios of http-client

Downloads

28

Readme

使用示例

import HttpClient, { HttpClientConfig, HttpClientRequestConfig } from 'http-client'

// 初始化请求实例
const config:HttpClientConfig = {
  baseURL: '',
  headers: {
    Authorization: 'xx--x-x-x-x-x-x--xxxxx'
  },
  showMessage: ({ code, message }) => alert(message)
}

const http = new HttpClient(config)

// 配置单次请求信息
const options:HttpClientRequestConfig = { url: '/api/auth' }
http.request(options).then(res => {
  console.log(res);
})

初始化参数

import type { Method, AxiosRequestHeaders, AxiosRequestConfig, AxiosResponseHeaders } from "axios";

/**
 * 响应结果自定义 key 
 * 不要忘记设置 successCode 的值,避免出现不符合预期的情况
 */
type ProxyConfig = {
  code?: string;
  data?: string;
  message?: string;
  successCode?: number | number[] // 请求成功 code 码, 默认是 0
};

/**
 * 初始化配置信息
 */
type HttpClientConfig = {
  baseURL?: string; // 公共请求地址 
  timeout?: number; // 公共超时时间 默认为 60 * 1000
  method?: Method; // 公共默认请求方法 默认为 GET
  headers?: AxiosRequestHeaders; // 公共默认请求头
  requestBeforeHook?: RequestBeforeHook; // 公共请求拦截钩子
  responseAfterHook?: ResponseAfterHook; // 公共响应拦截钩子 !!!如果设置了此方法,则公共响应处理则会失效,需要自行处理公共响应拦截
  showMessage?: ShowMessage; // 展示提示信息钩子
  errorCallback?: ErrorCallback; // 响应错误钩子
};

type RequestBeforeHook = (config: HttpClientRequestConfig) => void;

type ResponseAfterHook = (option: HttpClientResponse) => Promise<any>;

type ShowMessage = (options: { code: number; message: string }) => void;

type ErrorCallback = (data: any) => void;

interface HttpClientRequestConfig extends AxiosRequestConfig {
  useFormData?: Boolean; // 使用 formData 方式提交
  showSuccessMessage?: Boolean; // 触发响应成功提示信息 默认为 false
  showErrorMessage?: Boolean; // 触发响应错误提示信息 默认为 true
  repeatReq?: Boolean; //  是否允许在pending状态中重复请求
}

interface HttpClientResponse<T = any> {
  data: T;
  status: number;
  statusText: string;
  headers: AxiosResponseHeaders;
  config: HttpClientRequestConfig;
  request?: any;
}

tips

请求结果如果是文件流,需要是ArrayBuffer的格式,响应拦截会返回完整的响应体,否则会造成报错以及不符合预期的情况