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

rpc-client-fetch

v1.1.0

Published

A universal, zero-dependency RPC client based on Fetch API. 基于 Fetch API 的通用 RPC 客户端,适用于 Web、Node.js 和 React Native。

Downloads

41

Readme

rpc-client-fetch

NPM version NPM downloads

一个基于标准 Fetch API 的通用 RPC 客户端。

它是 RPC 生态的重要组成部分,专门为 Web 前端 (Vue/React)Node.jsReact Native 环境设计。

它让你能像调用本地函数一样,远程调用部署在 腾讯云 SCF阿里云 FCNode.js 服务器 上的 RPC 服务。配合 rpc-server-scf 使用,体验更佳。

✨ 特性

  • 🌐 全平台支持: 完美运行于所有现代浏览器、Node.js (18+) 和 React Native 环境。
  • 🔐 智能鉴权: 支持函数式 Headers 配置,完美解决 Token 动态获取和过期刷新问题。
  • 🪄 极致体验: 以 rpc.module.action(params) 的方式调用远程 API,完全屏蔽 HTTP 细节。
  • 📦 零依赖: 基于原生 Fetch 实现,极致轻量,无需引入 Axios 等庞大的第三方库。
  • 🛡️ 统一错误处理: 无论是网络 404/500 还是后端业务抛出的 Error,均通过标准 try...catch 捕获。

📦 安装

npm install rpc-client-fetch

🚀 快速开始

1. 初始化客户端

在你的项目中创建一个 API 实例(建议放在 src/api/rpc.js)。

import { create } from 'rpc-client-fetch';

const rpc = create({
  // 1. 设置 RPC 服务地址 (例如腾讯云 API 网关地址)
  url: 'https://service-xxxx.tencentapigw.com/release/rpc',

  // 2. (可选) 配置请求头
  // 强烈建议使用函数形式,这样每次请求都会重新执行,获取最新的 Token
  headers: () => {
    const token = localStorage.getItem('auth_token');
    return {
      Authorization: token ? `Bearer ${token}` : '',
    };
  },
});

export default rpc;

2. 在组件中调用

现在,你可以在任何页面或组件中直接使用它。

import rpc from '@/api/rpc';

async function loadUserProfile() {
  try {
    // ✨ 魔法发生的地方:就像调用本地函数一样!
    // 实际发送 POST 请求: { rpcModule: 'user', rpcAction: 'getProfile', rpcParams: [123] }
    const user = await rpc.user.getProfile(123);

    console.log('用户信息:', user);
  } catch (error) {
    // 统一捕获错误
    console.error('调用失败:', error.message);

    if (error.code === 'UNAUTHORIZED') {
      // 处理未登录逻辑,例如跳转登录页
      location.href = '/login';
    }
  }
}

📖 指南

动态 Token 鉴权

在 Web 开发中,Token 通常存储在 localStorage 中,且可能会在用户重新登录后发生变化。

如果将 headers 配置为静态对象,Token 将无法自动更新。rpc-client-fetch 支持将 headers 配置为一个函数(甚至是异步函数),每次发起请求前都会执行该函数。

const rpc = create({
  url: '...',
  // 推荐:函数形式
  headers: async () => {
    // 你甚至可以在这里处理 Token 刷新逻辑
    let token = await getTokenFromStorage();
    return { Authorization: token };
  },
});

Node.js 环境兼容 (Polyfill)

Node.js 18+ 原生支持 fetch。如果你在旧版本 Node.js 环境中使用,可以通过 fetch 选项注入 polyfill(如 node-fetch)。

import nodeFetch from 'node-fetch';
import { create } from 'rpc-client-fetch';

const rpc = create({
  url: 'http://localhost:3000/rpc',
  fetch: nodeFetch, // 注入自定义 fetch
});

⚙️ API 参考

create(options)

| 属性 | 类型 | 必填 | 描述 | | :-------- | :------------------------- | :--- | :------------------------------------------- | | url | string | ✅ | RPC 服务的完整 URL 地址。 | | headers | object | () => object | ❌ | 请求头。推荐使用函数形式,支持返回 Promise。 | | fetch | Function | ❌ | 自定义 fetch 实现。用于兼容性处理。 |

🤝 服务端配合

此客户端需要配合遵循标准 RPC 协议的服务端使用:

| 环境 | 服务端包 | | :------------------------ | :--------------------------------------------------------------- | | 腾讯云云函数 (SCF) | rpc-server-scf | | Node.js (Express/Koa) | rpc-server-node |

📄 开源协议

MIT