# pb-to-ts-api

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

Latest version **1.0.5** (published 2023-03-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install pb-to-ts-api
pnpm add pb-to-ts-api
yarn add pb-to-ts-api
bun add pb-to-ts-api
```

Provides the commands `pb2TSApi`, `pb-to-ts-api`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2023-03-16 |
| First published | 2023-03-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 77.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | xingbofeng |
| Maintainers | counterxing |
| Keywords | pb, protobuf, ts, mock |

## Links

- npm: https://www.npmjs.com/package/pb-to-ts-api
- npm.io page: https://npm.io/package/pb-to-ts-api

## Dependencies (10)

- [glob](https://npm.io/package/glob.md) ^9.2.1
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [mkdirp](https://npm.io/package/mkdirp.md) ^2.1.5
- [express](https://npm.io/package/express.md) ^4.18.2
- [minimist](https://npm.io/package/minimist.md) ^1.2.8
- [ts-morph](https://npm.io/package/ts-morph.md) ^17.0.1
- [protobufjs](https://npm.io/package/protobufjs.md) ^7.2.2
- [protobufjs-cli](https://npm.io/package/protobufjs-cli.md) ^1.1.1
- [json-schema-faker](https://npm.io/package/json-schema-faker.md) ^0.5.0-rcv.46
- [typescript-json-schema](https://npm.io/package/typescript-json-schema.md) ^0.55.0

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 1.0.5 (latest) — 2023-03-16
- 1.0.4 — 2023-03-15
- 1.0.3 — 2023-03-15
- 1.0.2 — 2023-03-11
- 1.0.1 — 2023-03-11
- 1.0.0 — 2023-03-11

## README

# pb-to-ts-api

前后台联调过程中，前端同学拿到协议后，还需要对protobuf协议转化为ts代码，并还需要手动书写大量的http请求代码，然而代码大部分都是相同的，完全可以通过自动生成的方式来完成，[pb-to-ts-api](https://github.com/xingbofeng/protobuf-to-ts-api)就较好地解决了这个问题，[pb-to-ts-api](https://github.com/xingbofeng/protobuf-to-ts-api)基于protobufjs-cli，将cli处理后的代码做简单转换，并提供将标准protobuf文件转化为d.ts文件和生成自定义api请求方法文件的方法。后续还将提供mock方法来改善我们前后台的联调体验。

## 开始

```bash
npm install -g pb-to-ts-api
pb2TSApi ./*/**.proto
```

## 使用方法
创建你自定义的protobuf文件，按照指定的规则定义协议的请求字段和返回字段

```proto
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文件举例如下：

```typescript
/* 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文件如下：

```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功能：

![](https://km.woa.com/asset/c77a6edfd3f34c9fbfbb4b8fdb84d354?height=1356&origin_url=https%3A%2F%2Fstatic.wecity.qq.com%2Fh5%2F2023-3%2F1-1e1bfcf5c5eb716484e01a9762aa919c.png&width=1726)


### 参数

|  参数   | 说明  |
|  ----  | ----  |
| --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` |

---
_Source: https://npm.io/package/pb-to-ts-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
