@bufferapp/micro-rpc-client

中文文档
1.5.0发布于 2024/09/23MITCommonJS近 30 天下载 4.8万

Universal JS client for micro-rpc

原始使用文档
文档目录11 节
  1. Quickstart
  2. Usage
  3. API
  4. constructor
  5. listMethods
  6. call
  7. Error Handling
  8. Success
  9. Fail (Handled)
  10. Fail (Unhandled)
  11. Fail (Unexpected)

deprecated

As of the 23rd of September 2024, micro-rpc-client will be unmaintained and deprecated.

micro-rpc-client

Build Status

Universal JS RPC Client https://github.com/bufferapp/buffer-rpc/

Quickstart

Create an RPC Client and call an add function

const RPCClient = require('micro-rpc-client');

const client = new RPCClient({
  url: 'https://localhost:3000/rpc',
});

const main = async () => {
  const result = await client.call('add', [1, 2]);
  console.log(result); // 3
};
main();

Usage

A few examples of how to call client methods

const RPCClient = require('micro-rpc-client');

const client = new RPCClient({
  url: 'https://localhost:3000/rpc',
  sendCredentials: 'same-origin', // send cookies on same origin requests
});

const main = async () => {
  const methods = await client.listMethods();
  console.log(method);
  /*
  [
    {
      "docs": "add two numbers"
      "name": "add"
    },
    {
      "docs": "list all available methods",
      "name": "methods"
    }
  ]
  */
  const result = await client.call('add', [1, 2]);
  console.log(result); // 3
};
main();

API

constructor

Create an instance of RPCClient

const client = new RPCClient({
  url,
  sendCredentials,
});

url - string - full url to the RPC Endpoint Server
sendCredentials - string - when to send things like cookies with a request, passes arguments to the credentials init argument in the fetch API https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters

Returns an instance of the RPC client

listMethods

List all remote methods on a server

const methods = await client.listMethods()

Returns a promise that resolves with a list of the remote methods

call

const result = await client.call(name, args)

name - string - name of the remote RPC endpoint
args - array or object - arguments to pass to the remote RPC endpoint

Returns a promise that resolves with the result and rejects with an error.

Error Handling

When calling a remote function with call there are different categories of responses:

Success

This is the typical success case

statusCode = 200
result = {} // JSON

Fail (Handled)

An error triggered from createError:

statusCode = 400
result = {
  error: 'string',
  code: 1000, // or some custom code
  handled: true,
}

Fail (Unhandled)

An error triggered from errorMiddleware:

statusCode = 500
result = {
  error: 'string',
  code: 1000, // or some custom code
  handled: true,
}

Fail (Unexpected)

If the errorMiddleware is not hooked up, or something unexpected happened the original error will be thrown.

版本与发布记录

@bufferapp/micro-rpc-client 共发布 9 个版本,首次发布于 2020/03/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.0latest2024/09/232 年前
1.4.32021/04/305 年前
1.4.2lastest2021/04/305 年前
1.4.1beta2021/04/305 年前
1.4.02021/04/305 年前
1.3.02021/04/305 年前

关于 @bufferapp/micro-rpc-client 的常见问题

@bufferapp/micro-rpc-client 是什么?

Universal JS client for micro-rpc

如何安装 @bufferapp/micro-rpc-client?

在项目目录运行 npm install @bufferapp/micro-rpc-client,或使用 pnpm add / yarn add / bun add。

@bufferapp/micro-rpc-client 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bufferapp__micro-rpc-client 是否存在。

@bufferapp/micro-rpc-client 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bufferapp/micro-rpc-client 可以商用吗?

@bufferapp/micro-rpc-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bufferapp/micro-rpc-client 的下载量有多少?

根据 npm 官方下载统计,@bufferapp/micro-rpc-client 在 2026/10/09 – 2026/11/07 这 30 天内被下载 47,946 次,日均约 1,598 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bufferapp/micro-rpc-client 还在维护吗?

最新版本 1.5.0 发布于 2024/09/23(2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。