Skip to main content
Home

Built and signed on GitHub Actions

A strictly typed json-rpc(2.0) implementation, zero dependency, minimal abstraction, with simple api

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
88%
Published
11 months ago (0.2.1)

json-rpc-ts

deno.land/x JSR npm codecov ci

A strictly typed json-rpc(2.0) implementation, zero dependency, minimal abstraction, with simple api

Specification https://www.jsonrpc.org/specification

Installation

For Node.js

npx jsr add @yieldray/json-rpc-ts # recommended
# or
npm install @yieldray/json-rpc-ts

For Deno

deno add @yieldray/json-rpc-ts

Examples

Example to use the client

const requestForResponse = (json: string) =>
    fetch('http://localhost:6800/jsonrpc', {
        method: 'POST',
        body: json,
    }).then((res) => res.text())

const client = new JSONRPCClient<{
    'aria2.addUri': (
        urls: string[],
        options?: object,
        position?: number,
    ) => string
    'aria2.remove': (gid: string) => string
    'system.listMethods': () => string[]
}>(requestForResponse)

const resultGid: string = await client.request('aria2.addUri', [
    ['https://example.net/index.html'],
    {},
    0,
])

Example to use the server

const server = new JSONRPCServer({
    upper: (str: string) => str.toUpperCase(),
    lower: (str: string) => str.toLowerCase(),
    plus: ([a, b]: [number, number]) => a + b,
    minus: ([a, b]: [number, number]) => a - b,
})

// or:
server.setMethod('trim', (str: string) => str.trim())
server.setMethod('trimStart', (str: string) => str.trimStart())
server.setMethod('trimEnd', (str: string) => str.trimEnd())

const httpServer = Deno.serve(
    async (request) => {
        // server.handleRequest() accept string and returns Promise<string>
        const jsonString = await server.handleRequest(await request.text())

        return new Response(jsonString, {
            headers: { 'content-type': 'application/json' },
        })
    },
)
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@yieldray/json-rpc-ts

Import symbol

import * as json_rpc_ts from "@yieldray/json-rpc-ts";
or

Import directly with a jsr specifier

import * as json_rpc_ts from "jsr:@yieldray/json-rpc-ts";

Add Package

pnpm i jsr:@yieldray/json-rpc-ts
or (using pnpm 10.8 or older)
pnpm dlx jsr add @yieldray/json-rpc-ts

Import symbol

import * as json_rpc_ts from "@yieldray/json-rpc-ts";

Add Package

yarn add jsr:@yieldray/json-rpc-ts
or (using Yarn 4.8 or older)
yarn dlx jsr add @yieldray/json-rpc-ts

Import symbol

import * as json_rpc_ts from "@yieldray/json-rpc-ts";

Add Package

vlt install jsr:@yieldray/json-rpc-ts

Import symbol

import * as json_rpc_ts from "@yieldray/json-rpc-ts";

Add Package

npx jsr add @yieldray/json-rpc-ts

Import symbol

import * as json_rpc_ts from "@yieldray/json-rpc-ts";

Add Package

bunx jsr add @yieldray/json-rpc-ts

Import symbol

import * as json_rpc_ts from "@yieldray/json-rpc-ts";