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

@tanzerfe/http

v1.0.1

Published

A request lib

Downloads

59

Readme

开箱即用的请求库 @tanzerfe/http


一个用于处理 HTTP 请求和协议的库,提供了丰富的配置选项和拦截器机制,方便开发者进行网络请求管理。

模块导出

$Config

  • 类型: Config
  • 描述: 请求库的配置类,包含了请求成功的 code 值、token 获取方法、toast 实例及其默认主题、请求错误处理函数和响应处理函数。
  • 详细信息: 请参见 请求库配置

$http

  • 类型: AxiosInstance
  • 描述: 带有拦截器的 Axios 实例,用于发送 HTTP 请求。
  • 使用说明: 请参见 使用示例

$httpNano

  • 类型: AxiosInstance
  • 描述: 不带拦截器的 Axios 实例,用于发送直连 HTTP 请求。

$protocol

  • 类型: any
  • 描述: 协议相关的工具或配置。
  • 使用说明: 该导出的具体使用方法根据项目中的 protocol 实现而定。

请求库配置

import {$Config} from "./index";

// $Config.getCustomHeaders = { zh_id: '' };
$Config.getToken = () => {
  return auth.userInfo?.token;
};
$Config.$toastLight = useToastCtx({theme: 'light'});
$Config.$toastDark = useToastCtx({theme: 'dark'});
// ...

Config类用于配置请求库的各种参数和处理函数。

属性

success_codes
  • 类型: (string | number)[]
  • 默认值: ['success', '200', '201', '202', 200]
  • 描述: 定义正常响应的 code 值。
getCustomHeaders
  • 类型: Record<string, any> | (() => Record<string, any>)
  • 默认值: undefined
  • 描述: 获取 header 的对象或函数。
  • 版本:v1.0.1+
getToken
  • 类型: () => string | undefined
  • 默认值: () => void 0
  • 描述: 获取 token 的函数。
defaultToastTheme
  • 类型: 'light' | 'dark'
  • 默认值: 'dark'
  • 描述: 默认的 toast 主题。
$toastLight
  • 类型: IToast
  • 描述: light 主题的 toast 实例。
$toastDark
  • 类型: IToast
  • 描述: dark 主题的 toast 实例。

方法

requestErrorHandler
  • 类型: (error: any, reqData: any) => void
  • 描述: 请求错误处理函数。
responseHandler
  • 类型: (resData: any, reqData: any) => void
  • 描述: 响应处理函数。
_cfg
  • 类型 Object
  • 描述:参数附加配置
    • noAuth 为true则不传token
    • showTip 错误提示
    • showOkTip 成功提示
    • okTipContent 成功提示文案
    • theme toast皮肤 'dark'、'light'

使用示例

import { $http, $Config } from '@tanzerfe/http';

(function getData() {
  const url = 'https://httpbin.org/post';
  const params = { t1: 'test' };

  $http.post(url, {
    data: {
      ...params,
      // 单个请求配置成功提示、错误提示、皮肤
      _cfg: {
        showTip: true,
        showOkTip: true,
        theme: 'dark', // 'dark' or 'light'
      },
    },
  });

  // $http.get(url, {
  //   data: {
  //     _cfg: {...},
  //   },
  // });
})();