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

origin-request

v1.0.0

Published

A front-end request enhancement tool.

Downloads

6

Readme

Origin Request

一个轻量级的请求缓存和归源请求工具,用于确保同一时间内相同的请求只执行一次,并提供灵活的缓存管理功能。

特性

  • 🚀 归源请求:确保同一时间内相同的请求只执行一次
  • 💾 灵活的缓存管理:
    • 支持设置全局默认缓存时间
    • 支持为单个请求设置特定缓存时间
    • 支持设置永久缓存
  • 🧹 智能的缓存清理:
    • 自动清理过期缓存
    • 最小化资源占用
  • 🔒 类型安全:完整的 TypeScript 类型支持
  • 🎯 轻量级:零依赖,体积小巧

安装

npm install origin-request
# 或
yarn add origin-request

使用示例

基本使用

import { oReq } from 'origin-request';

// 模拟API请求
async function fetchUserData(userId: string) {
  return oReq(
    async () => {
      const response = await fetch(`/api/users/${userId}`);
      return response.json();
    },
    {
      key: `user-${userId}`, // 可选:指定唯一键
      expireTime: 5 * 60 * 1000 // 可选:设置缓存时间(5分钟)
    }
  );
}

// 多次调用同一请求,实际只会执行一次
const user1 = await fetchUserData('123');
const user2 = await fetchUserData('123'); // 从缓存中获取,不会重复请求

按需使用

async function fetchUserData(userId: string) {
  // 请求...
}

// 多次调用同一请求,实际只会执行一次
const user1 = await oReq(fetchUserData)
const user2 = await oReq(fetchUserData) // 从缓存中获取,不会重复请求

设置全局缓存时间

import { OriginRequest } from 'origin-request';

// 设置全局默认缓存时间为10分钟
OriginRequest.setDefaultCacheExpireTime(10 * 60 * 1000);

// 设置永久缓存
OriginRequest.setDefaultCacheExpireTime(null);

手动管理缓存

import { OriginRequest } from 'origin-request';

// 清除特定请求的缓存
OriginRequest.clearRequestCache('user-123');

// 清除所有请求缓存
OriginRequest.clearAllRequestCache();

API 文档

originRequest

归源请求函数,确保同一时间内相同的请求只执行一次。

function originRequest<T>(
  requestFn: () => Promise<T>,
  options?: OriginRequestOptions
): Promise<T>

参数:

  • requestFn: 要执行的请求函数
  • options: 配置选项
    • key: 请求的唯一键,用于标识相同请求
    • expireTime: 缓存有效期(毫秒),null 表示永不过期

setDefaultCacheExpireTime

设置全局默认缓存有效期。

function setDefaultCacheExpireTime(expireTime: number | null): void

参数:

  • expireTime: 缓存有效期(毫秒),null 表示永不过期

clearRequestCache

清除特定请求的缓存。

function clearRequestCache(key: string | number | symbol): boolean

参数:

  • key: 要清除的缓存键

clearAllRequestCache

清除所有请求缓存。

function clearAllRequestCache(): void

缓存清理策略

该库使用智能的缓存清理机制,具有以下特点:

  1. 按需清理

    • 在访问缓存项时检查过期
    • 设置新缓存项时检查是否已过期
    • 避免不必要的清理操作
  2. 智能调度

    • 记录最近的过期时间
    • 只在必要时设置清理定时器
    • 避免频繁的定时器操作
  3. 资源优化

    • 最小化定时器使用
    • 避免内存泄漏
    • 自动清理过期资源

注意事项

  1. 如果不提供 key,将使用请求函数本身或函数签名作为键
  2. 对于匿名函数,建议提供 key 以确保正确识别相同请求
  3. 缓存时间从请求执行完成时开始计算
  4. 清理操作是异步的,不会阻塞主线程

贡献

欢迎提交 Issue 和 Pull Request!

许可证

MIT