Skip to content

Commit

Permalink
feat: generate typescript bindings (#825)
Browse files Browse the repository at this point in the history
* feat: generate typescript bindigns

* remove _pb

* add index.d.ts

* package.json

* make typescript

---------

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
fadeev and lumtis authored Oct 31, 2023
1 parent e5bffa9 commit c324935
Show file tree
Hide file tree
Showing 51 changed files with 8,420 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ proto:
@buf format -w
.PHONY: proto

typescript:
@echo "--> Generating TypeScript bindings"
@bash ./scripts/protoc-gen-typescript.sh
.PHONY: typescript

proto-format:
@bash ./scripts/proto-format.sh

Expand All @@ -179,14 +184,14 @@ docs-zetacored:
@bash ./scripts/gen-docs-zetacored.sh
.PHONY: docs-zetacored

generate: proto openapi specs docs-zetacored
.PHONY: generate

mocks:
@echo "--> Generating mocks"
@bash ./scripts/mocks-generate.sh
.PHONY: mocks

generate: proto openapi specs typescript docs-zetacored
.PHONY: generate

###############################################################################
### Docker Images ###
###############################################################################
Expand Down
7 changes: 7 additions & 0 deletions proto/buf.ts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: v1
managed:
enabled: true
plugins:
- plugin: buf.build/bufbuild/es:v1.3.0
out: ../typescript
opt: target=dts
38 changes: 38 additions & 0 deletions scripts/protoc-gen-typescript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

DIR="typescript"

rm -rf $DIR

(cd proto && buf generate --template buf.ts.yaml)

cat <<EOL > $DIR/package.json
{
"name": "@zetachain/blockchain-types",
"version": "0.0.0-set-on-publish",
"description": "",
"main": "",
"keywords": [],
"author": "ZetaChain",
"license": "MIT"
}
EOL

# Loop through all directories recursively
find "$DIR" -type d | while read -r dir; do
# Check if there are any .d.ts files in the directory
if ls "$dir"/*.d.ts &> /dev/null; then
# Create or clear index.d.ts in the directory
> "$dir/index.d.ts"

# Loop through all .d.ts files in the directory
for file in "$dir"/*.d.ts; do
# Extract the base filename without the .d.ts extension
base_name=$(basename "$file" .d.ts)
# If the base name is not 'index', append the export line to index.d.ts
if [ "$base_name" != "index" ]; then
echo "export * from \"./$base_name\";" >> "$dir/index.d.ts"
fi
done
fi
done
42 changes: 42 additions & 0 deletions typescript/common/bitcoin/bitcoin_pb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @generated by protoc-gen-es v1.3.0 with parameter "target=dts"
// @generated from file common/bitcoin/bitcoin.proto (package bitcoin, syntax proto3)
/* eslint-disable */
// @ts-nocheck

import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf";
import { Message, proto3 } from "@bufbuild/protobuf";

/**
* @generated from message bitcoin.Proof
*/
export declare class Proof extends Message<Proof> {
/**
* @generated from field: bytes tx_bytes = 1;
*/
txBytes: Uint8Array;

/**
* @generated from field: bytes path = 2;
*/
path: Uint8Array;

/**
* @generated from field: uint32 index = 3;
*/
index: number;

constructor(data?: PartialMessage<Proof>);

static readonly runtime: typeof proto3;
static readonly typeName = "bitcoin.Proof";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): Proof;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): Proof;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): Proof;

static equals(a: Proof | PlainMessage<Proof> | undefined, b: Proof | PlainMessage<Proof> | undefined): boolean;
}

1 change: 1 addition & 0 deletions typescript/common/bitcoin/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./bitcoin_pb";
Loading

0 comments on commit c324935

Please sign in to comment.