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

etherreq

v1.2.9

Published

A lightweight custom HTTP request library with TypeScript support.

Readme

EtherReq

一个轻量级、无感知的 HTTP 请求库,基于 Fetch 封装,支持自动 Token 注入、拦截器、TypeScript 类型定义和智能缓存功能。

特性

  • 🚀 轻量级: 基于原生 Fetch API 构建
  • 🔐 自动认证: login 方法自动管理 token
  • 🔄 智能缓存: 支持内存和 localStorage 双重缓存
  • 🎯 TypeScript: 完整的类型支持
  • 拦截器: 请求/响应拦截器支持
  • 📦 零依赖: 无需额外依赖包

安装

npm install etherreq
# 或者
yarn add etherreq

快速开始

import { etherreq } from 'etherreq';

// GET 请求(自动缓存)
const users = await etherreq.get('http://localhost:8081/api/users');
console.log(users);

// POST 请求
const result = await etherreq.post('/login', { 
  username: 'test', 
  password: '123456' 
});
console.log('登录成功:', result);

// 带数据的POST请求
const user = {
  id: 1,
  name: '张三',
  sex: '男',
  age: 18,
};
const newUser = await etherreq.post('/add', user);
console.log(newUser);

登录认证

// 登录方法(自动保存 token)
const login = async () => {
  const userData = {
    id: 1,
    username: "zhy",
    password: "123456"
  };
  
  const loginResult = await etherreq.login('users/login', userData);
  console.log(loginResult);
  // 后续请求会自动携带 token
};

缓存功能

自动缓存

GET 请求默认启用智能缓存:

  • 内存缓存(快速访问)
  • localStorage 持久化(页面刷新后仍有效)
  • 自动过期清理(默认5分钟)

缓存控制选项

// 禁用缓存
const data = await etherreq.get('/api/data', {
  cache: false
});

// 自定义缓存时间
const data = await etherreq.get('/api/data', {
  cache: {
    enabled: true,
    ttl: 10 * 60 * 1000 // 10分钟
  }
});

// 指定存储策略
const data = await etherreq.get('/api/data', {
  cache: {
    storage: 'memory+local', // 'memory' | 'local' | 'memory+local'
    ttl: 300000
  }
});

带参数的请求缓存

// 不同参数生成不同缓存键
const page1 = await etherreq.get('/users', {
  params: { page: 1, limit: 10 }
});

const page2 = await etherreq.get('/users', {
  params: { page: 2, limit: 10 }
});
// 这两个请求会有独立的缓存

API 接口

请求方法

etherreq(url, options?)           // 默认 GET
etherreq.get(url, options?)
etherreq.post(url, data?, options?)
etherreq.put(url, data?, options?)
etherreq.delete(url, options?)
etherreq.del(url, options?)       // delete 别名
etherreq.head(url, options?)
etherreq.options(url, options?)
etherreq.patch(url, data?, options?)
etherreq.login(url, data?, options?)

配置选项

interface EtherRequestOptions {
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
  headers?: Record<string, string>;
  body?: any;
  params?: Record<string, string | number | boolean>;
  baseURL?: string;
  cache?: CacheOptions | false;
}

缓存选项

interface CacheOptions {
  enabled?: boolean;     // 是否启用缓存
  storage?: 'memory' | 'local' | 'memory+local'; // 存储策略
  ttl?: number;          // 过期时间(毫秒)
}

高级用法

设置基础URL

import { setBaseURL } from 'etherreq';

setBaseURL('https://api.example.com');
// 后续请求将使用此基础URL

并发请求

const [users, posts] = await Promise.all([
  etherreq.get('/users'),
  etherreq.get('/posts')
]);

错误处理

try {
  const data = await etherreq.get('/api/data');
} catch (error) {
  console.error('请求失败:', error.message);
}

TypeScript 支持

完整的类型定义支持:

interface User {
  id: number;
  name: string;
  email: string;
}

// 类型安全的请求
const user: User = await etherreq.get<User>('/users/1');
const users: User[] = await etherreq.get<User[]>('/users');

浏览器兼容性

  • Chrome 67+
  • Firefox 61+
  • Safari 12+
  • Edge 79+

需要 Promise 和 fetch API 支持。

License

MIT