@@ -11,25 +11,25 @@ Takes the arguments `methods`, `req` and `options`. You can set options for an
1111additional server argument or public error stacks.
1212
1313``` typescript
14- import { serve } from " https://deno.land/std@0.97.0/http/server.ts"
15- import { respond } from " https://deno.land/x/gentle_rpc/mod.ts"
14+ import { serve } from " https://deno.land/std@0.97.0/http/server.ts" ;
15+ import { respond } from " https://deno.land/x/gentle_rpc/mod.ts" ;
1616
17- const server = serve (" 0.0.0.0:8000" )
17+ const server = serve (" 0.0.0.0:8000" );
1818const rpcMethods = {
1919 sayHello : (w : [string ]) => ` Hello ${w } ` ,
2020 callNamedParameters : ({ a , b , c }: { a: number ; b: number ; c: string }) =>
2121 ` ${c } ${a * b } ` ,
2222 animalsMakeNoise : (noise : string []) =>
2323 noise .map ((el ) => el .toUpperCase ()).join (" " ),
24- }
24+ };
2525
26- console .log (" listening on 0.0.0.0:8000" )
26+ console .log (" listening on 0.0.0.0:8000" );
2727
2828for await (const req of server ) {
2929 // HTTP:
30- await respond (rpcMethods , req )
30+ await respond (rpcMethods , req );
3131 // WebSockets:
32- await respond (rpcMethods , req , { proto: " ws" })
32+ await respond (rpcMethods , req , { proto: " ws" });
3333}
3434```
3535
@@ -41,13 +41,15 @@ Takes a `resource` for HTTP or a `WebSocket` for WebSockets and returns a
4141TypeScript ` Proxy ` or ` Promise<Proxy> ` which we will call ` remote ` from now on.
4242
4343``` typescript
44- import { createRemote } from " https://deno.land/x/gentle_rpc/mod.ts"
44+ import { createRemote } from " https://deno.land/x/gentle_rpc/mod.ts" ;
45+ // Or import directly into the browser with:
46+ import { createRemote } from " https://cdn.jsdelivr.net/gh/timonson/gentle_rpc@v2.8/client/dist/remote.js" ;
4547
4648// HTTP:
47- const remote = createRemote (" http://0.0.0.0:8000" )
49+ const remote = createRemote (" http://0.0.0.0:8000" );
4850
4951// WebSocket:
50- const remote = await createRemote (new WebSocket (" ws://0.0.0.0:8000" ))
52+ const remote = await createRemote (new WebSocket (" ws://0.0.0.0:8000" ));
5153```
5254
5355### HTTP
@@ -58,21 +60,21 @@ All `remote` methods take an `Array<JsonValue>` or `Record<string, JsonValue>`
5860object and return ` Promise<JsonValue | undefined> ` .
5961
6062``` typescript
61- const greeting = await remote .sayHello ([" World" ])
63+ const greeting = await remote .sayHello ([" World" ]);
6264// Hello World
6365
6466const namedParams = await remote .callNamedParameters ({
6567 a: 5 ,
6668 b: 10 ,
6769 c: " result:" ,
68- })
70+ });
6971// result: 50
7072```
7173
7274##### notification
7375
7476``` typescript
75- const notification = await remote .sayHello .notify ([" World" ])
77+ const notification = await remote .sayHello .notify ([" World" ]);
7678// undefined
7779```
7880
@@ -81,7 +83,7 @@ const notification = await remote.sayHello.notify(["World"])
8183This method will set the ` Authorization ` header to `` `Bearer ${jwt}` `` .
8284
8385``` typescript
84- const greeting = await remote .sayHello .auth (jwt )([" World" ])
86+ const greeting = await remote .sayHello .auth (jwt )([" World" ]);
8587// Hello World
8688```
8789
@@ -93,7 +95,7 @@ const noise1 = await remote.animalsMakeNoise.batch([
9395 [" wuuuufu" , " wuuuufu" ],
9496 [" iaaaiaia" , " iaaaiaia" , " iaaaiaia" ],
9597 [" fiiiiire" ],
96- ])
98+ ]);
9799// [ "MIAAOW", "WUUUUFU WUUUUFU", "IAAAIAIA IAAAIAIA IAAAIAIA", "FIIIIIRE" ]
98100```
99101
@@ -108,7 +110,7 @@ await remote.batch({
108110 dog: [" animalsMakeNoise" , [" wuuuufu" ]],
109111 donkey: [" sayHello" ],
110112 dragon: [" animalsMakeNoise" , [" fiiiiire" , " fiiiiire" ]],
111- })
113+ });
112114// { cat: "Hello miaaow", dog: "WUUUUFU", donkey: "Hello ", dragon: "FIIIIIRE FIIIIIRE" }
113115```
114116
@@ -125,7 +127,7 @@ await remote.batch([
125127 [" wuuuufu" , " wuuuufu" ],
126128 [" iaaaiaia" , " iaaaiaia" , " iaaaiaia" ],
127129 [" fiiiiire" ],
128- ])
130+ ]);
129131// [ "MIAAOW", "WUUUUFU WUUUUFU", "IAAAIAIA IAAAIAIA IAAAIAIA", "FIIIIIRE" ]
130132```
131133
@@ -140,16 +142,16 @@ All `remote` methods take an `Array<JsonValue>` or `Record<string, JsonValue>`
140142object and return ` Promise<JsonValue | undefined> ` .
141143
142144``` typescript
143- const noise = await remote .animalsMakeNoise ([" wuufff" ])
144- console .log (noise )
145+ const noise = await remote .animalsMakeNoise ([" wuufff" ]);
146+ console .log (noise );
145147
146- remote .socket .close ()
148+ remote .socket .close ();
147149```
148150
149151##### notification
150152
151153``` typescript
152- const notification = await remote .animalsMakeNoise .notify ([" wuufff" ])
154+ const notification = await remote .animalsMakeNoise .notify ([" wuufff" ]);
153155```
154156
155157##### messaging between multiple clients
@@ -167,32 +169,32 @@ method.
167169export async function run(iter : AsyncGenerator <unknown >) {
168170 try {
169171 for await (let x of iter ) {
170- console .log (x )
172+ console .log (x );
171173 }
172174 } catch (err ) {
173- console .log (err .message , err .code )
175+ console .log (err .message , err .code );
174176 }
175177}
176178
177- const greeting = remote .sayHello .subscribe ()
178- run (greeting .generator )
179- greeting .emit ([" first" ])
179+ const greeting = remote .sayHello .subscribe ();
180+ run (greeting .generator );
181+ greeting .emit ([" first" ]);
180182// Hello first
181183// Hello second
182184// Hello third
183185```
184186
185187``` typescript
186188// Second client
187- const greeting = remote .sayHello .subscribe ()
188- run (greeting .generator )
189- greeting .emitBatch ([[" second" ], [" third" ]])
189+ const greeting = remote .sayHello .subscribe ();
190+ run (greeting .generator );
191+ greeting .emitBatch ([[" second" ], [" third" ]]);
190192// Hello first
191193// Hello second
192194// Hello third
193195
194196// You can optionally unsubscribe:
195- greeting .unsubscribe ()
197+ greeting .unsubscribe ();
196198```
197199
198200## Examples and Tests
@@ -204,5 +206,5 @@ more detailed examples.
204206
205207## Contribution
206208
207- Every kind of contribution to this project is highly appreciated.
209+ Every kind of contribution to this project is highly appreciated.\
208210Please run ` deno fmt ` on the changed files before making a pull request.
0 commit comments