Socket
Socket
Sign inDemoInstall

ts-brpc

Package Overview
Dependencies
204
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ts-brpc

基于TS的RPC库


Version published
Weekly downloads
110
increased by150%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

ts-brpc

ts-brpc支持以 RPC 风格调用 TypeScript 编写的服务端接口,让开发者专注业务实现。

特性

  • ts-brpc让客户端以 RPC 风格调用接口,函数调用是程序最自然的通信方式,免去手动构造 HTTP 请求。
  • ts-brpc可以扫描服务端 TS 代码中的类型信息,可在编码时为客户端提供类型校验、代码补全、接口注释;代码即 API 文档。
  • ts-brpc借由扫描获得的类型信息,零成本支持自动生成接口 Mock 数据的能力。
  • ts-brpc由 CLI + SDK 组成,运行时(SDK)非常轻量,可与 Koa、Midway.js 快速集成。

示例展示

Server

import { RPCMethod, RPCService } from 'ts-brpc/server'

interface UserInfo {
  id: string
  name: string
  age: number
}

@RPCService()
export class User {

  @RPCMethod()
  getInfoById (id: string): UserInfo {
    return { id, name: '22', age: 18 }
  }

  @RPCMethod()
  getUnreadMsg (id: string): Promise<string[]> {
    // 模拟异步调用
    return Promise.resolve(['msg1', 'msg2'])
  }
}

Client

import { createRemoteService } from 'ts-brpc/client'
// ts-brpc 扫描服务端代码生成的 rpc-definition.ts
import { App } from './rpc-definition'

const rpc = createRemoteService<App>({
  baseUrl: '<web app base url>'
})

await rpc.User.getInfoById('22') // => { id: '22', name: '22', age: 18 }

运行 demo

  1. git clone git@github.com:hughfenghen/ts-rpc.git
  2. yarn && yarn build
  3. cd demo && yarn
  4. yarn server 然后新开 shell 窗口 yarn client

文档

  • 接入指南
  • API
  • 工作原理

FAQs

Last updated on 10 Aug 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc