@cats-oss/ts-proto-optimize

中文文档
0.2.0发布于 2022/10/19MIT近 30 天下载 3814

It's CLI tool for optimize TypeScript protobuf type-definition file.

原始使用文档
文档目录5 节
  1. Features
  2. Installation
  3. Usage
  4. Example
  5. License

@cats-oss/ts-proto-optimize

Continuous Integration

[EXPERIMENTAL] It's CLI tool for optimize TypeScript protobuf type-definition file.

Optimize the definition of a protobuf that is only used to get type-safe. Compilation using Babel becomes possible.

Features

  • Extract all enum.
    • Remove from namespace and concat names. (use pascal case)
    • Replaces interface with enum.
  • Remove import * as $protobuf from "protobufjs";.
    • Keep the small bundle size.
  • Remove all Long, Nullable, and Optional.
  • Remove all class.
  • Remove all function declaration type aliases.
  • Add export keyword to interface in namespace.

Installation

$ npm i -D @cats-oss/ts-proto-optimize

Usage

# Output to stdout
$ $(npm bin)/ts-proto-optimize path/to/proto.d.ts

# Output to file
$ $(npm bin)/ts-proto-optimize path/to/proto.d.ts --output dist/to/proto.ts

Example

input:

import * as $protobuf from 'protobufjs';

/** Namespace root. */
export namespace root {
  /** Namespace api. */
  namespace api {
    /** Properties of a Foo */
    interface IFoo {
      /** Foo str */
      str?: string | null;

      /** Foo number */
      num?: number | Long | null;
    }

    /** class Enumerate */
    class Foo implements IFoo {
      /**
       * comment
       */
      constructor(properties?: root.api.IFoo);

      /** comment */
      public str: string;

      /** comment */
      public num: number;
    }

    /** Enum enum */
    enum Enum {
      KEY1_SNAKE_CASE = 0,
      KEY2_SNAKE_CASE = 1,
      KEY3_SNAKE_CASE = 2,
    }

    namespace Nest {
      /**
       * Callback as used by {@link ...}.
       * @param error Error, if any
       * @param [response] GetFooResponse
       */
      type GetFooCallback = (
        error: Error | null,
        response?: root.api.GetFooResponse,
      ) => void;

      /** Enum enum. */
      enum Enum {
        KEY1 = 0,
        KEY2 = 1,
        KEY3 = 2,
      }

      /** Properties Data */
      interface IData {
        name?: string | null;
        value?: number | null;
        field?: root.api.Nest.IData | null;
        enumratee?: root.api.Nest.Enum | null;
      }

      /** Represents Data */
      class Data implements IData {
        constructor(properties?: root.api.Nest.IData);
      }
    }
  }
}

output:

/** Namespace root. */
export declare namespace root {
  /** Namespace api. */
  export namespace api {
    /** Properties of a Foo */
    export interface IFoo {
      /** Foo str */
      str: string;
      /** Foo number */
      num: number;
    }
    export namespace Nest {
      /** Properties Data */
      export interface IData {
        name: string;
        value: number;
        field: root.api.Nest.IData;
        enumratee: RootApiNestEnum;
      }
    }
  }
}
export enum RootApiEnum {
  KEY1_SNAKE_CASE = 0,
  KEY2_SNAKE_CASE = 1,
  KEY3_SNAKE_CASE = 2,
}
export enum RootApiNestEnum {
  KEY1 = 0,
  KEY2 = 1,
  KEY3 = 2,
}

License

MIT © Cyberagent, Inc

版本与发布记录

@cats-oss/ts-proto-optimize 共发布 6 个版本,首次发布于 2019/08/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.0latest2022/10/194 年前
0.1.22021/08/025 年前
0.1.12021/07/285 年前
0.1.02019/10/247 年前
0.0.22019/08/287 年前
0.0.12019/08/237 年前

关于 @cats-oss/ts-proto-optimize 的常见问题

@cats-oss/ts-proto-optimize 是什么?

It's CLI tool for optimize TypeScript protobuf type-definition file.

如何安装 @cats-oss/ts-proto-optimize?

在项目目录运行 npm install @cats-oss/ts-proto-optimize,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 ts-proto-optimize,可以用 npx ts-proto-optimize 直接运行。

@cats-oss/ts-proto-optimize 支持 TypeScript 吗?

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

@cats-oss/ts-proto-optimize 可以商用吗?

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

@cats-oss/ts-proto-optimize 的下载量有多少?

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

@cats-oss/ts-proto-optimize 还在维护吗?

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