npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rpc-gen

v1.0.28

Published

Solution for generating RPC code from TypeScript code

Downloads

47

Readme

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