JSON-RPC 2.0 TypeScript library for deno and browser

JSON-RPC 2.0 TypeScript library for deno and browser

gentle_rpc .JSON-RPC 2.0 TypeScript library for deno and browser

gentle_rpc

JSON-RPC 2.0 TypeScript library for deno and the browser.

This library is accessible through the https://deno.land/x/ service or through https://nest.land/package/gentle_rpc.

Features

  • Complies with the JSON-RPC 2.0 specification
  • Sends data with the fetch API
  • Uses JavaScript/TypeScript native proxies for a simple API on the client side

API

respond

Takes a req, methods and options. You can set options for an additional server argument or public error stacks.

import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { respond } from "https://deno.land/x/gentle_rpc/respond.ts"

const s = serve("0.0.0.0:8000")
console.log("listening on 0.0.0.0:8000")

const rpcMethods = {
  sayHello: ([w]: [string]) => `Hello ${w}`,
  animalsMakeNoise: (noise: [string]) =>
    noise.map((el) => el.toUpperCase()).join(" "),
  namedParameters: ({ a, b, c }: { a: number; b: number; c: string }) =>
    `${c} ${a * b}`,
}

for await (const req of s) {
  await respond(req, rpcMethods)
}

createRemote

Takes a resource and options and returns a javascript proxy which we will call remote from now on.

remote

All remote methods take an Array<JsonValue> or Record<string, JsonValue> object and return Promise<JsonValue | undefined>.

import { createRemote } from "https://deno.land/x/gentle_rpc/request.ts"

const remote = createRemote("http://0.0.0.0:8000")
const greeting = await remote.sayHello(["World"])
const namedParameters = await remote.namedParameters({ a: 5, b: 10, c: "result:" })

console.log(greeting) // Hello World
console.log(namedParameters) // result: 50
const remote = createRemote("http://0.0.0.0:8000", { isNotification: true })
await remote.sayHello(["World"]) // undefined

remote.batch

Takes either a batchObject or a batchArray as argument and returns a promise.

await remote.batch({
  cat: ["sayHello", ["miaaow"]],
  dog: ["animalsMakeNoise", ["wuuuufu"]],
  donkey: ["sayHello"],
  dragon: ["animalsMakeNoise", ["fiiiiire", "fiiiiire"]],
})
// { cat: "Hello miaaow", dog: "WUUUUFU", donkey: "Hello ", dragon: "FIIIIIRE FIIIIIRE" }

The example above uses the object keys cat, dog, donkey, dragon as RPC request object ids under the hood. The returned RPC result values will be assigned to these keys.

For other use cases you might prefer the following example:

await remote.batch([
  "animalsMakeNoise",
  ["miaaow"],
  ["wuuuufu", "wuuuufu"],
  ["iaaaiaia", "iaaaiaia", "iaaaiaia"],
  ["fiiiiire"],
])
// [ "MIAAOW", "WUUUUFU WUUUUFU", "IAAAIAIA IAAAIAIA IAAAIAIA", "FIIIIIRE" ]

Examples and Tests

Checkout the examples and tests folders for more detailed examples.

Contribution

Every kind of contribution to this project is highly appreciated. Please run deno fmt on the changed files before making a pull request.

Download Details:

Author: timonson

Source Code: https://github.com/timonson/gentle_rpc

deno node nodejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings