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

@feutopia/http

v0.0.8

Published

HTTP Request Library, base on Axios

Downloads

361

Readme

@feutopia/http

中文 | English

一个基于 Axios 的强大 HTTP 请求库,提供增强功能和更便捷的 API。

特性

  • 🚀 基于 Axios 构建,具有增强功能
  • 🎯 内置 TypeScript 支持
  • 🔄 请求/响应拦截器
  • ⚡ 基于 Promise 的 API
  • 📁 支持文件上传及进度跟踪
  • ⬇️ 支持文件下载
  • ❌ 请求取消功能
  • 🎨 可自定义请求/响应处理器
  • 🔍 全面的错误处理

安装

pnpm install @feutopia/http

基础用法

import { HttpClient } from "@feutopia/http";

class MyHttpClient extends HttpClient {
  constructor() {
    super({
      config: {
        baseURL: "https://api.example.com",
        timeout: 5000,
      },
    });
  }
}

const http = new MyHttpClient();

// GET 请求
const getData = async () => {
  const response = await http.get({ 
    url: "/users",
    showError: true // 可选:控制错误显示
  });
  return response.data;
};

// POST 请求(带类型安全)
const createUser = async (userData: UserData) => {
  const response = await http.post<UserResponse>({
    url: "/users",
    data: userData,
  });
  return response.data;
};

高级功能

自定义请求/响应拦截器

const http = new MyHttpClient();

// 添加请求拦截器
http.interceptors.request.use(
  (config) => {
    // 添加自定义请求头
    config.headers["X-Custom-Header"] = "value";
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 添加响应拦截器
http.interceptors.response.use(
  (response) => {
    // 转换响应数据
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);

文件上传

const uploadFile = async (file: File) => {
  await http.upload({
    url: "/upload",
    data: file, // 可以是 File、FormData 或其他数据
    onProgress: (percentage) => {
      console.log(`上传进度:${percentage}%`);
    }
  });
};

文件下载

const downloadFile = async () => {
	await http.download({
		url: "/files/document.pdf",
	});
};

请求取消

// 方式一:使用 cancel 方法
const request = http.get({ url: "/data" });
request.cancel();

// 方式二:取消所有待处理的请求
http.cancelAllRequests();

错误处理

该库提供全面的错误处理机制,包含预定义的错误代码:

import { ErrorCode } from "@feutopia/http";
try {
	await http.get({ url: "/data" });
} catch (error) {
	switch (error.code) {
		case ErrorCode.NETWORK_ERROR:
			console.error("发生网络错误");
			break;
		case ErrorCode.TIMEOUT:
			console.error("请求超时");
			break;
		case ErrorCode.CANCELED:
			console.error("请求已取消");
			break;
		// ... 处理其他错误类型
	}
}

许可证

MIT