1.0.28 • Published 1 year ago

rpc-gen v1.0.28

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Rpc-Gen

Rpc-Gen is a tool to auto-generate typescript RPC code for your project.

Define function at backend

a.png

and use it at frontend

b.png

Installation

# Install with npm
npm install rpc-gen
# Or install with yarn
yarn add rpc-gen

Usage

  1. Add RpcContext module augmentation file to your project.

    // rpc-context.d.ts
    declare module "rpc-gen" {
      interface RpcContext {
        // Add your context here
        // For example:
        name: string;
      }
    }
  2. Make api file with .api.ts extension, and implement RPC function. Type of the first parameter must be RpcContext.

    // test.api.ts
    import { RpcContext } from "rpc-gen";
    
    export function testRpc(context: RpcContext, prefix: string) {
      console.log(context.name);
      return {
        name: prefix + ":" + context.name,
      };
    }
  3. Run rpc-gen command in your project root directory.

    # With npx
    npx rpc-gen
    # Or with yarn
    yarn rpc-gen
  4. Then you will frontend.rpc.ts and backend.rpc.ts in your project root directory.

    // frontend.rpc.ts
    const rpc = async (
      module: string,
      func: string,
      hash: number,
      args: any[]
    ) => {
      return fetch("/api/rpc", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          module,
          func,
          hash,
          args,
        }),
      }).then((res) => res.json());
    };
    export const test = {
      testRpc: async (prefix: string): Promise<{ name: string }> =>
        rpc("test", "testRpc", 3662805367, [prefix]),
    };
    // backend.rpc.ts
    import * as test from "./apis/test.api";
    import { RpcContext } from "rpc-gen";
    export const rpc = async (
      context: RpcContext,
      {
        module,
        func,
        hash,
        args,
      }: { module: string; func: string; hash: number; args: any[] }
    ) => {
      const moduleObj = modules[module];
      if (!moduleObj) throw new Error("Module not found");
      const funcObj = moduleObj[func];
      if (!funcObj) throw new Error("Function not found");
      if (funcObj.hash !== hash) throw new Error("Hash mismatch");
      return await funcObj.func(context, ...args);
    };
    const modules: {
      [key: string]: {
        [key: string]: { hash: number; func: (...args: any[]) => any };
      };
    } = {
      test: {
        testRpc: { hash: 3662805367, func: test.testRpc },
      },
    };
  5. Import frontend.rpc.ts in your frontend project, and import backend.rpc.ts in your backend project.

    // frontend.ts
    import { test } from "./frontend.rpc";
    test.testRpc("Hello").then((res) => {
      console.log(res.name); // Hello:World
    });
    // backend.ts
    import { rpc } from "./backend.rpc";
    import { RpcContext } from "rpc-gen";
    import express from "express";
    
    const app = express();
    
    app.post("/api/rpc", async (req, res) => {
      const context: RpcContext = {
        name: req.session.name,
      };
      const result = await rpc(context, req.body);
      res.json(result);
    });

Docs

  Usage: rpc gen [options] [command]

  Commands:
    help     Display help
    version  Display version

  Options:
    -b, --backendFile [value]   Path to backend file (defaults to "./backend.rpc.ts")
    -f, --frontendFile [value]  Path to frontend file (defaults to "./frontend.rpc.ts")
    -h, --help                  Output usage information
    -r, --rpcApiUrl [value]     URL of RPC API (defaults to "/api/rpc")
    -v, --version               Output the version number

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Author

unknownpgr

1.0.19

1 year ago

1.0.18

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago