Skip to content

Commit

Permalink
Feature automatically generate Typescript client based on openRPC doc…
Browse files Browse the repository at this point in the history
…umentation (#545)

* feat: add code auto gen client

* feat: remove petstore-openrpc.json

* feat: ok gen rooch client types

* feat: auto gen client

* feat: auto gen rooch typescript client ok

* feat: remove deps

* feat: update utils comment
  • Loading branch information
yubing744 authored Jul 30, 2023
1 parent f5306c1 commit 7e654b4
Show file tree
Hide file tree
Showing 13 changed files with 4,337 additions and 9 deletions.
5 changes: 4 additions & 1 deletion sdk/typescript/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ dist/
build/

# Doc generation output
docs/
docs/

# client generation output
generated/
20 changes: 12 additions & 8 deletions sdk/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"node": ">=18.0.0"
},
"packageManager": "[email protected]",
"type": "module",
"scripts": {
"clean": "rm -rf tsconfig.tsbuildinfo ./dist",
"dev": "vite",
Expand All @@ -25,7 +26,8 @@
"eslint:check": "eslint --max-warnings=0 .",
"eslint:fix": "pnpm run eslint:check --fix",
"lint": "pnpm run eslint:check && pnpm run prettier:check",
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix"
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix",
"gen:client": "node ./tools/generator/main.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -57,21 +59,22 @@
"@iarna/toml": "^2.2.5",
"@size-limit/preset-small-lib": "^8.2.4",
"@types/node": "^18.15.11",
"eslint": "8.23.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-config-airbnb-typescript": "17.0.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0",
"json-schema-ref-parser": "6.1.0",
"msw": "^1.2.1",
"prettier": "^3.0.0",
"ts-retry-promise": "^0.7.0",
"tsx": "^3.12.7",
"typedoc": "^0.24.1",
"typescript": "^5.0.4",
"vite": "^4.2.3",
"vite-plugin-dts": "^3.3.1",
"vitest": "^0.32.0",
"wait-on": "^7.0.1",
"eslint": "8.23.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-config-airbnb-typescript": "17.0.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0",
"prettier": "2.6.2"
"wait-on": "^7.0.1"
},
"dependencies": {
"@noble/curves": "^1.0.0",
Expand All @@ -80,6 +83,7 @@
"@scure/bip32": "^1.3.0",
"@scure/bip39": "^1.2.0",
"@suchipi/femver": "^1.0.0",
"ejs": "^3.1.9",
"events": "^3.3.0",
"superstruct": "^1.0.3",
"tweetnacl": "^1.0.3"
Expand Down
161 changes: 161 additions & 0 deletions sdk/typescript/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// This file was generated by `yarn gen:client`. Please, do not modify it.
import { RequestManager, Client } from "@open-rpc/client-js";
import {
ExecuteTransactionResponseView,
FunctionCallView,
AnnotatedFunctionReturnValueView,
AccessPath,
AnnotatedStateView,
EventFilterView,
AnnotatedEventView,
StructTag,
PageView_for_Nullable_AnnotatedEventView_and_uint64,
StateView,
Hex,
TransactionView,
TransactionExecutionInfoView,
PageView_for_Nullable_TransactionExecutionInfoView_and_uint128,
} from "./types";

export class RoochClient extends Client {
constructor(requestManager: RequestManager) {
super(requestManager);
}

async getRpcApiVersion(): Promise<string | undefined> {
const resp = await this.request({
method: "rpc.discover",
params: [],
});

return resp.info.version as string;
}

// Send the signed transaction in bcs hex format This method blocks waiting for the transaction to be executed.
async rooch_executeRawTransaction(
tx_bcs_hex: Uint8Array,
): Promise<ExecuteTransactionResponseView> {
const resp = await this.request({
method: "rooch_executeRawTransaction",
params: [tx_bcs_hex],
});

return resp as ExecuteTransactionResponseView;
}

// Execute a read-only function call The function do not change the state of Application
async rooch_executeViewFunction(
function_call: FunctionCallView,
): Promise<AnnotatedFunctionReturnValueView[]> {
const resp = await this.request({
method: "rooch_executeViewFunction",
params: [function_call],
});

return resp as AnnotatedFunctionReturnValueView[];
}

// Get the annotated states by access_path The annotated states include the decoded move value of the state
async rooch_getAnnotatedStates(
access_path: AccessPath,
): Promise<AnnotatedStateView | null[]> {
const resp = await this.request({
method: "rooch_getAnnotatedStates",
params: [access_path],
});

return resp as AnnotatedStateView | null[];
}

// Get the events by event filter
async rooch_getEvents(
filter: EventFilterView,
): Promise<AnnotatedEventView | null[]> {
const resp = await this.request({
method: "rooch_getEvents",
params: [filter],
});

return resp as AnnotatedEventView | null[];
}

// Get the events by event handle id
async rooch_getEventsByEventHandle(
event_handle_type: StructTag,
cursor: number,
limit: number,
): Promise<PageView_for_Nullable_AnnotatedEventView_and_uint64> {
const resp = await this.request({
method: "rooch_getEventsByEventHandle",
params: [event_handle_type, cursor, limit],
});

return resp as PageView_for_Nullable_AnnotatedEventView_and_uint64;
}

// Get the states by access_path
async rooch_getStates(access_path: AccessPath): Promise<StateView | null[]> {
const resp = await this.request({
method: "rooch_getStates",
params: [access_path],
});

return resp as StateView | null[];
}

async rooch_getTransactionByHash(hash: Hex): Promise<TransactionView> {
const resp = await this.request({
method: "rooch_getTransactionByHash",
params: [hash],
});

return resp as TransactionView;
}

async rooch_getTransactionByIndex(
start: number,
limit: number,
): Promise<TransactionView[]> {
const resp = await this.request({
method: "rooch_getTransactionByIndex",
params: [start, limit],
});

return resp as TransactionView[];
}

async rooch_getTransactionInfosByTxHash(
tx_hashes: Hex[],
): Promise<TransactionExecutionInfoView | null[]> {
const resp = await this.request({
method: "rooch_getTransactionInfosByTxHash",
params: [tx_hashes],
});

return resp as TransactionExecutionInfoView | null[];
}

async rooch_getTransactionInfosByTxOrder(
cursor: number,
limit: number,
): Promise<PageView_for_Nullable_TransactionExecutionInfoView_and_uint128> {
const resp = await this.request({
method: "rooch_getTransactionInfosByTxOrder",
params: [cursor, limit],
});

return resp as PageView_for_Nullable_TransactionExecutionInfoView_and_uint128;
}

// Send the signed transaction in bcs hex format This method does not block waiting for the transaction to be executed.
async rooch_sendRawTransaction(tx_bcs_hex: Uint8Array): Promise<Hex> {
const resp = await this.request({
method: "rooch_sendRawTransaction",
params: [tx_bcs_hex],
});

return resp as Hex;
}
}

// This file was generated by `yarn gen:client`. Please, do not modify it.
5 changes: 5 additions & 0 deletions sdk/typescript/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file was generated by `yarn gen:client`. Please, do not modify it.
export * from "./types";
export * from "./client";

// This file was generated by `yarn gen:client`. Please, do not modify it.
Loading

0 comments on commit 7e654b4

Please sign in to comment.