@@ -10,25 +10,25 @@ JSON-RPC 2.0 library (server and client) with HTTP and WebSockets support for
1010Takes ` Methods ` , ` ServerRequest ` and ` Options ` .
1111
1212``` typescript
13- import { serve } from " https://deno.land/std@0.105.0/http/server.ts"
14- import { respond } from " https://deno.land/x/gentle_rpc/mod.ts"
13+ import { serve } from " https://deno.land/std@0.105.0/http/server.ts" ;
14+ import { respond } from " https://deno.land/x/gentle_rpc/mod.ts" ;
1515
16- const server = serve (" 0.0.0.0:8000" )
16+ const server = serve (" 0.0.0.0:8000" );
1717const rpcMethods = {
1818 sayHello : ([w ]: [string ]) => ` Hello ${w } ` ,
1919 callNamedParameters : ({ a , b , c }: { a: number ; b: number ; c: string }) =>
2020 ` ${c } ${a * b } ` ,
2121 animalsMakeNoise : (noise : string []) =>
2222 noise .map ((el ) => el .toUpperCase ()).join (" " ),
23- }
23+ };
2424
25- console .log (" listening on 0.0.0.0:8000" )
25+ console .log (" listening on 0.0.0.0:8000" );
2626
2727for await (const req of server ) {
2828 // HTTP:
29- respond (rpcMethods , req )
29+ respond (rpcMethods , req );
3030 // WebSockets:
31- respond (rpcMethods , req , { proto: " ws" })
31+ respond (rpcMethods , req , { proto: " ws" });
3232}
3333```
3434
@@ -37,7 +37,7 @@ for await (const req of server) {
3737Throw a ` CustomError ` to send a server-defined error response.
3838
3939``` typescript
40- import { CustomError , respond } from " https://deno.land/x/gentle_rpc/mod.ts"
40+ import { CustomError , respond } from " https://deno.land/x/gentle_rpc/mod.ts" ;
4141
4242// ..
4343await respond (
@@ -46,12 +46,12 @@ await respond(
4646 throw new CustomError (
4747 - 32040 , // the JSON-RPC error code. Note, must be -32040 to -32099
4848 " An error occurred" , // the error message, a short sentence
49- { details: " ..." } // optional additional details, can be any `JsonValue`
50- )
49+ { details: " ..." }, // optional additional details, can be any `JsonValue`
50+ );
5151 },
5252 },
53- req
54- )
53+ req ,
54+ );
5555// ..
5656```
5757
@@ -63,15 +63,15 @@ Takes a `Resource` for HTTP or a `WebSocket` for WebSockets and returns
6363` Remote ` .
6464
6565``` typescript
66- import { createRemote } from " https://deno.land/x/gentle_rpc/mod.ts"
66+ import { createRemote } from " https://deno.land/x/gentle_rpc/mod.ts" ;
6767// Or import directly into the browser with:
68- import { createRemote } from " https://cdn.jsdelivr.net/gh/timonson/gentle_rpc@v3.0/client/dist/remote.js"
68+ import { createRemote } from " https://cdn.jsdelivr.net/gh/timonson/gentle_rpc@v3.0/client/dist/remote.js" ;
6969
7070// HTTP:
71- const remote = createRemote (" http://0.0.0.0:8000" )
71+ const remote = createRemote (" http://0.0.0.0:8000" );
7272
7373// WebSocket:
74- const remote = await createRemote (new WebSocket (" ws://0.0.0.0:8000" ))
74+ const remote = await createRemote (new WebSocket (" ws://0.0.0.0:8000" ));
7575```
7676
7777### HTTP
@@ -82,14 +82,14 @@ Takes a string and an `Array<JsonValue>` or `Record<string, JsonValue>` object
8282and returns ` Promise<JsonValue> ` .
8383
8484``` typescript
85- const greeting = await remote .call (" sayHello" , [" World" ])
85+ const greeting = await remote .call (" sayHello" , [" World" ]);
8686// Hello World
8787
8888const namedParams = await remote .call (" callNamedParameters" , {
8989 a: 5 ,
9090 b: 10 ,
9191 c: " result:" ,
92- })
92+ });
9393// result: 50
9494```
9595
@@ -100,7 +100,7 @@ Using the option `{ isNotification: true }` will retun `Promise<undefined>`.
100100``` typescript
101101const notification = await remote .call (" sayHello" , [" World" ], {
102102 isNotification: true ,
103- })
103+ });
104104// undefined
105105```
106106
@@ -110,7 +110,7 @@ Adding the option `{jwt: string}` will set the `Authorization` header to
110110`` `Bearer ${jwt}` `` .
111111
112112``` typescript
113- const user = await remote .call (" login" , undefined , { jwt })
113+ const user = await remote .call (" login" , undefined , { jwt });
114114// Bob
115115```
116116
@@ -127,7 +127,7 @@ const noise1 = await remote.batch([
127127 ],
128128 sayHello: [[" World" ], undefined , [" World" ]],
129129 },
130- ])
130+ ]);
131131// [ "MIAAOW", "WUUUUFU WUUUUFU", "IAAAIAIA IAAAIAIA IAAAIAIA", "FIIIIIRE", "Hello World", "Hello ", "Hello World" ]
132132```
133133
@@ -141,7 +141,7 @@ let noise2 = await remote.batch({
141141 dog: [" animalsMakeNoise" , [" wuuuufu" ]],
142142 donkey: [" sayHello" ],
143143 dragon: [" animalsMakeNoise" , [" fiiiiire" , " fiiiiire" ]],
144- })
144+ });
145145// { cat: "Hello miaaow", dog: "WUUUUFU", donkey: "Hello ", dragon: "FIIIIIRE FIIIIIRE" }
146146```
147147
@@ -160,16 +160,16 @@ const noise = await remote.call("callNamedParameters", {
160160 a: 10 ,
161161 b: 20 ,
162162 c: " The result is:" ,
163- })
163+ });
164164// The result is: 200
165165
166- remote .socket .close ()
166+ remote .socket .close ();
167167```
168168
169169##### notification
170170
171171``` typescript
172- const notification = await remote .call (" animalsMakeNoise" , [" wuufff" ], true )
172+ const notification = await remote .call (" animalsMakeNoise" , [" wuufff" ], true );
173173// undefined
174174```
175175
@@ -184,26 +184,26 @@ Other clients can _listen to_ and _emit_ messages by _subscribing_ to the same
184184method.
185185
186186``` typescript
187- const firstClient = await createRemote (new WebSocket (" ws://0.0.0.0:8000" ))
188- const secondClient = await createRemote (new WebSocket (" ws://0.0.0.0:8000" ))
187+ const firstClient = await createRemote (new WebSocket (" ws://0.0.0.0:8000" ));
188+ const secondClient = await createRemote (new WebSocket (" ws://0.0.0.0:8000" ));
189189
190190async function run(iter : AsyncGenerator <unknown >) {
191191 try {
192192 for await (let x of iter ) {
193- console .log (x )
193+ console .log (x );
194194 }
195195 } catch (err ) {
196- console .log (err .message , err .code , err .data )
196+ console .log (err .message , err .code , err .data );
197197 }
198198}
199199
200- const greeting = firstClient .subscribe (" sayHello" )
201- const second = secondClient .subscribe (" sayHello" )
200+ const greeting = firstClient .subscribe (" sayHello" );
201+ const second = secondClient .subscribe (" sayHello" );
202202
203- run (greeting .generator )
204- run (second .generator )
205- greeting .emit ({ w: " first" })
206- second .emitBatch ([{ w: " second" }, { w: " third" }])
203+ run (greeting .generator );
204+ run (second .generator );
205+ greeting .emit ({ w: " first" });
206+ second .emitBatch ([{ w: " second" }, { w: " third" }]);
207207// Hello first
208208// Hello first
209209// Hello second
@@ -218,21 +218,25 @@ Optionally, you can import _syntactical sugar_ and use a more friendly API
218218supported by ` Proxy ` objects.
219219
220220``` typescript
221- import { createRemote , HttpProxy , httpProxyHandler } from " ../../mod.ts"
221+ import {
222+ createRemote ,
223+ HttpProxy ,
224+ httpProxyHandler ,
225+ } from " https://deno.land/x/gentle_rpc/mod.ts" ;
222226
223227const remote = new Proxy <HttpProxy >(
224228 createRemote (" http://0.0.0.0:8000" ),
225- httpProxyHandler
226- )
229+ httpProxyHandler ,
230+ );
227231
228- let greeting = await remote .sayHello ([" World" ])
232+ let greeting = await remote .sayHello ([" World" ]);
229233// Hello World
230234
231235const namedParams = await remote .callNamedParameters ({
232236 a: 5 ,
233237 b: 10 ,
234238 c: " result:" ,
235- })
239+ });
236240// result: 50
237241```
238242
0 commit comments