11# json-rpc-ts
22
33[ ![ deno.land/x] ( https://shield.deno.dev/x/json_rpc_ts )] ( https://deno.land/x/json_rpc_ts )
4+ [ ![ ci] ( https://github.com/yieldray/json-rpc-ts/actions/workflows/ci.yml/badge.svg )] ( https://github.com/yieldray/json-rpc-ts/actions/workflows/ci.yml )
45
5- A strictly typed json-rpc(2.0) implemention , zero dependency, minimal abstraction, with simple api
6+ A strictly typed json-rpc(2.0) implementation , zero dependency, minimal abstraction, with simple api
67
78> Specification < https://www.jsonrpc.org/specification >
89
@@ -14,19 +15,29 @@ const methodSet = {
1415 minus : ([a , b ]: [number , number ]) => a - b ,
1516}
1617
18+ // initialize all methods with the constructor
1719const server = new JSONRPCServer (methodSet )
1820
21+ // or add methods manually
22+ const server = new JSONRPCServer <typeof methodSet >()
23+ server .setMethod (' upper' , methodSet .upper )
24+ server .setMethod (' lower' , methodSet .lower )
25+ server .setMethod (' plus' , methodSet .plus )
26+ server .setMethod (' minus' , methodSet .minus )
27+
28+ // (optional) provide a generic parameter to enable ts check
1929const client = new JSONRPCClient <typeof methodSet >((json ) =>
2030 server .process (json )
2131)
2232
33+ // request, Notification and batch are always async
2334assertEquals (await client .request (' upper' , ' hello' ), ' HELLO' )
2435
2536assertEquals (
2637 await client .batch (
2738 client .createRequest (' upper' , ' nihao' ),
28- // notifaction does not have response, even when response errors
29- client .createNotifaction (' upper' ),
39+ // Notification does not have response, even when response errors
40+ client .createNotification (' upper' ),
3041 client .createRequest (' upper' , ' shijie' ),
3142 client .createRequest (' plus' , [1 , 1 ]),
3243 ),
@@ -85,3 +96,13 @@ const httpServer = Deno.serve(
8596 },
8697)
8798```
99+
100+ # build for JavaScript
101+
102+ To use this library without typescript, you have to build it to javascript.
103+
104+ ``` sh
105+ git clone https://github.com/YieldRay/json-rpc-ts.git
106+ cd json-rpc-ts
107+ esbuild --bundle src/index.ts --outdir=dist --format=esm
108+ ```
0 commit comments