Norvell's Public Machinations

    @mini-rpc/server
    TypeScript icon, indicating that this package has built-in type declarations

    0.0.14 • Public • Published

    Mini-RPC

    simple but effective RPC framework. Both server and client.

    Usage

    Get start

    Install

    npm i @mini-rpc/server
    # or
    pnpm add @mini-rpc/server
    # or
    yarn add @mini-rpc/server

    Config

    • tsconfig.json in server
    {
      "compilerOptions": {
        "target": "ES2018",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
      }
    }
    • install reflect-metadata
    npm i reflect-metadata
    # or
    pnpm add reflect-metadata
    # or
    yarn add reflect-metadata
    • import reflect-metadata on entry (e.g: index.ts)

    Server

    import { Service, Callable } from "@mini-rpc/server";
    
    @Service()
    class MyService {
    
    	@Callable()
    	add(...nums: number[]) {
    		return nums.reduce((acc, cur) => acc + cur);
    	}
    
    }
    
    const server = new RPCServer();
    server.registerServices([MyService]);
    
    server.listen(3000);

    Client

    /*
     * you need to install `@mini-rpc/client` on the client side
     */
    import { RPCClient } from "@mini-rpc/client";
    
    const client = new RPCClient("ws://localhost:3000");
    client.connect();
    
    client
      .call("MyService.add", [1,2,3])
      .then((result) => {
        console.log(result); // 6
      })
      .catch((err) => {
        console.error(err);
      });

    Tips

    • the @Callable method can be asynchronous.
    class x {
    	//...
    
    	@Callable()
    	async add(a:number,b:number) {
    		// asynchronous code
    	}
    
    	// or declare the return type Promise
    
    	@Callable()
    	promiseAdd(a:number,b:number):Promise<number> {
    		return Promise<number>()
    	}
    }

    Install

    npm i @mini-rpc/server

    DownloadsWeekly Downloads

    14

    Version

    0.0.14

    License

    MIT

    Unpacked Size

    18.9 kB

    Total Files

    32

    Last publish

    Collaborators

    • xiaoliu-heng