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

pb-to-ts-api

v1.0.5

Published

help you transfer protobuf file to your custom d.ts and api request method

Readme

pb-to-ts-api

前后台联调过程中,前端同学拿到协议后,还需要对protobuf协议转化为ts代码,并还需要手动书写大量的http请求代码,然而代码大部分都是相同的,完全可以通过自动生成的方式来完成,pb-to-ts-api就较好地解决了这个问题,pb-to-ts-api基于protobufjs-cli,将cli处理后的代码做简单转换,并提供将标准protobuf文件转化为d.ts文件和生成自定义api请求方法文件的方法。后续还将提供mock方法来改善我们前后台的联调体验。

开始

npm install -g pb-to-ts-api
pb2TSApi ./*/**.proto

使用方法

创建你自定义的protobuf文件,按照指定的规则定义协议的请求字段和返回字段

syntax = "proto3";
package test;

enum Status {
  SUCCESS = 0;
  FAIL = 1;
  UNKNOWN = 2;
}

message GetExampleDataReq {
  int32 id = 1;
  string name = 2;
}

message GetExampleDataRsp {
  repeated Status status = 1;
  string msg = 2;
  string city = 3;
  int32 code = 4;
}

message PostExampleDataReq {
  int32 id = 1;
  string name = 2;
}

message PostExampleDataRsp {
  repeated Status status = 1;
  optional string msg = 2;
  int32 code = 3;
}

service API {
  rpc getExampleData (GetExampleDataReq) returns (GetExampleDataRsp);
  rpc postExampleData (PostExampleDataReq) returns (PostExampleDataRsp);
}

通过pb2TSApi转换工具,上述协议文件会生成d.ts文件、请求文件、schema文件、mock文件,请求ts文件举例如下:

/* eslint-disable */
import request from axios;
import api from './test.d';

export function GetExampleData(req: api.test.IGetExampleDataReq): Promise<api.test.IGetExampleDataRsp> {
  return request.get('/GetExampleData', { params: req });
};

export function PostExampleData(req: api.test.IPostExampleDataReq): Promise<api.test.IPostExampleDataRsp> {
  return request.post('/PostExampleData', req);
};

mock.json文件如下:

{
  "GetExampleData": {
    "city": "commodo esse enim magna nulla",
    "code": 60013672.10478094,
    "msg": "tempor cupidatat",
    "status": [
      1,
      2,
      2
    ]
  },
  "PostExampleData": {
    "code": 87936671.0172576,
    "msg": "adipisicing",
    "status": [
      1,
      0,
      2,
      2
    ]
  }
}

使用postman发送请求,可以正常使用mock功能:

参数

| 参数 | 说明 | | ---- | ---- | | --requestModule/-r | 请求方法,默认为'axios',用户可以替换为项目中自定义的请求方法,如'@/request' | | --baseUrl/-b | 请求的baseUrl,默认为'/',用户可以替换为项目中后台服务部署的路径,如'/api' | | --folder/-f | 生成目录的路径,默认为'./api',表示d.ts和ts文件存放位置,用户可以自定义存放到项目中的任意地方 | | --root/-r | 转化proto文件的根路径,默认为命令执行路径,即process.cwd() | | --optional/-o | 因为 protobuf 3.0 版本的协议默认将会转换所有字段为可选字段,设置-o为false之后,所有字段的可选值将会去除,因为遵循 protobuf 3.0 版本的协议,默认这个配置为true | | --mock/-m | 是否生成mock文件和开启mock server,默认为false,即不开启 | | --port/-p | 开启mock server 的端口号,默认为3000 |