1.0.54 • Published 4 months ago

rpc-sync v1.0.54

Weekly downloads
15
License
ISC
Repository
github
Last release
4 months ago

rpc-sync

Server and client, type defined, Remote Procedure Call.

Client Installation

First install rpc-sync globally

npm install -g rpc-sync

Create rpc.json

First create a rpc.json file in the root of your project:

{
    "url": "http://api.example.com/api",
    "secret: null
    "target": "ReactTypeScript",
    "output": "src/rcpClient.ts",
    "namespace": "rcpClient",
}

url

Replace the url parameter value with the URL of your remote API server.

secret

If your API server requires a secret in order to obtain the schema/documentation supply the secret in this parameter

target

Targets include:

  • ReactTypeScript
  • TypeScript
  • Dart
  • VisualBasic

output

The output source file generated by rpc-sync.

namespace

The namespace/class for the generated source files.

Sync project RPC references

Run the following command from your project root. You can re-run this command to update any API reference changes.

rpc-sync

or

rpc-sync rpc.json

Server Installation

Create a Node Typescript with Express project, then install the following package:

npm install rpc-sync

Now using the example below setup an express API using rpc-sync:

import express from "express";
import path from "path";
import rpcSync from "rpc-sync";
import HelloWorldAPI from "src/api/helloWorldAPI"

export interface IContext {
    req: express.Request;
    res: express.Response;
}

const port = 3000;
const app = express();

app.listen(port, () => {
    console.log("Listening on port:", port);

rpcSync.server.buildAPI({
        srcPath: path.join(process.cwd(), "/src"),
        apiPath: path.join(process.cwd(), "/src/api"),
        entityPath: path.join(process.cwd(), "/src/entity"),
        schemaOutputPath: path.join(process.cwd(), "schema.json"),
        enableDocs: true,
        enableClientSchema: true,
        minClientVersion: "0.1",
        debug: true,
    }).then((build)=>{
            rpcSync.server.exposeExpressAPI(build, app, (request) => {
                // This occurs on every web request.
                // Build a context object.. this can be anything. It will be sent to the API method as the first parameter.
                const context: IContext = {
                    req: request.req,
                    res: request.res
                };

                //Here is a good place to setup the database connection, apply authentication etc.
                //Expose the APIs to the request
                request.expose(context, {
                    "helloWorld": HelloWorldAPI
                });
            }).catch((e) => {
                console.error(e);
            });
    }).catch((e)=>{
            console.error(e)
    });
});
// src/api/helloWorldAPI.ts
interface IHelloWorldParameters {
    myTestValue: string;
}
interface IHelloWorldResult {
    myResult: string;
}
export class HelloWorldAPI {
    static async helloWorld(context: IContext, parameters: IHelloWorldParameters): Promise<IHelloWorldResult> {
        return new Promise<IHelloWorldResult>((accept, reject) => {
            accept({
                myResult: `Hello ${parameters.myTestValue}!`
            });
        });
    }
}
// src/entity

You can leave this directory empty.
1.0.54

4 months ago

1.0.53

1 year ago

1.0.52

1 year ago

1.0.44

1 year ago

1.0.43

1 year ago

1.0.48

1 year ago

1.0.47

1 year ago

1.0.46

1 year ago

1.0.45

1 year ago

1.0.49

1 year ago

1.0.50

1 year ago

1.0.42

2 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.40

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.11

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago