Coder Social home page Coder Social logo

proto2ts's Introduction

Introduction ๐Ÿ“š

pro2ts is a lightweight, out-of-the-box tool that enables seamless conversion of Proto files to TypeScript. It caters to basic daily usage scenarios and offers convenience for expansion.

Key Features โญ๏ธ

  • Ready for Immediate Use: pro2ts is built to be readily usable, requiring minimal setup and configuration.based on protobufjs
  • Support for Common Use Cases: pro2ts support the vast majority of conversion scenarios, whether it is fields, arrays, enumerations, methods or nested messages
  • Easily Extensible: pro2ts provides a flexible foundation for future expansion, allowing users to incorporate additional functionality and customize the conversion process according to their specific requirements.

Installation and Usage ๐Ÿš€

  1. Install pro2ts using the package manager of your choice:
pnpm install pro2ts
  1. Import parseProtoRoot method
import { parseProtoRoot } from 'pro2ts'

const ts = parseProtoRoot(readFileSync(join(process.cwd(), './__proto__/bff.proto'), 'utf-8'))

related typescript declaration

export interface IParseOptions {

    /** Keeps field casing instead of converting to camel case */
    keepCase?: boolean;

    /** Recognize double-slash comments in addition to doc-block comments. */
    alternateCommentMode?: boolean;

    /** Use trailing comment when both leading comment and trailing comment exist. */
    preferTrailingComment?: boolean;
}

export declare function parseProtoRoot(proto: string, options?: IParseOptions): string;

This way you will get a converted typescript string u can choose to write into a .d.ts file like me:

writeFileSync(join(process.cwd(), './__proto__/', `./result.type.d.ts`), ts)

For example if proto file is like this

syntax = "proto2";
package proto.bff.test;

enum ErrorCode {
  ERROR_1 = 1001;
  ERROR_2 = 1002;
}

message ResponseMeta {
  optional int32 code = 1;
  required string type = 2;
}

message RequestMeta{
  message InnerItem {
    optional string comment = 1;
    repeated string photos = 2;
    repeated string videos = 3;
  }

  optional string comment = 1;
  repeated ResponseMeta photos = 2;
  repeated string videos = 3;
  map<string, ResponseMeta> map_meta = 4;
  map<string, string> map_string = 5;
  optional InnerItem item = 6;
}

service GRPCDemo {
  rpc SimpleMethod (RequestMeta) returns (ResponseMeta);
}

After conversion it will look like this

declare namespace proto.bff.test {

  declare namespace RequestMeta {
    export interface InnerItem {
        comment?: string;
        photos: string[];
        videos: string[];
    }

  }

  export enum ErrorCode {
    ERROR_1 = 1001,
    ERROR_2 = 1002,
  }

  export interface ResponseMeta {
    code?: number;
    type: string;
  }

  export interface RequestMeta {
    comment?: string;
    photos: proto.bff.test.ResponseMeta[];
    videos: string[];
    map_meta: Record<string, proto.bff.test.ResponseMeta>;
    map_string: Record<string, string>;
    item?: RequestMeta.InnerItem;
  }

  export interface SimpleMethod {
    (params: RequestMeta): Promise<ResponseMeta>;
  }

}

Related Projects and References ๐ŸŒ

  • prtobufjs Official Protocol Buffers documentation

proto2ts's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.