threefoldtech / tfgrid-sdk-ts

Apache License 2.0
4 stars 8 forks source link

evaluate openRPC options for typescript #2840

Closed AhmedHanafy725 closed 1 month ago

AhmedHanafy725 commented 4 months ago

Which package/s are you suggesting this feature for?

grid_client

Is your feature request related to a problem? Please describe

-

Describe the solution you'd like

Check if the usability of openPRC is easy or not with typescript

Mahmoud-Emad commented 3 months ago

Update

After some research, I found that the OpenRPC organization offers several packages that can be very useful for our needs. In particular, they provide a generator package that helps with generating clients, servers, and documentation from an OpenRPC JSON file. Here’s a more detailed overview of my findings and experience:

OpenRPC Generator

The OpenRPC generator is a powerful tool that simplifies the process of creating clients, servers, and documentation for JSON-RPC APIs. You can easily generate these services by pointing the generator to your OpenRPC JSON file and selecting the desired services.

Installation

First, you need to install the OpenRPC generator. You can do this globally using npm:

npm install -g @open-rpc/generator

Generating Services

Once installed, generating services is straightforward. Here’s a step-by-step guide on how to use the generator:

  1. Prepare Your OpenRPC JSON File: Ensure your openrpc.json file is ready and correctly describes your JSON-RPC API. Here’s a basic example of what it might look like:

    {
      "openrpc": "1.2.6",
      "info": {
        "title": "Example API",
        "version": "1.0.0",
        "description": "This is an example OpenRPC document for a JSON-RPC API."
      },
      "methods": [
        {
          "name": "sayHello",
          "description": "Returns a greeting.",
          "params": [
            {
              "name": "name",
              "schema": {
                "type": "string"
              },
              "required": true
            }
          ],
          "result": {
            "name": "greeting",
            "schema": {
              "type": "string"
            }
          }
        },
        {
          "name": "add",
          "description": "Adds two numbers.",
          "params": [
            {
              "name": "a",
              "schema": {
                "type": "number"
              },
              "required": true
            },
            {
              "name": "b",
              "schema": {
                "type": "number"
              },
              "required": true
            }
          ],
          "result": {
            "name": "sum",
            "schema": {
              "type": "number"
            }
          }
        }
      ],
      "components": {
        "schemas": {}
      }
    }
  2. Run the Generator: Use the generator to create a client, server, or documentation. For example, to generate a TypeScript client, you can run:

    open-rpc-generator generate \
  -t client \
  -l typescript \
  -n petstoreClientTs \
  -d /home/thunder/work/projects/typescript/tsOpenRPC/openrpc.json \
  -o ./generated-client