forked from timonson/gentle_rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
733 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,25 +10,25 @@ JSON-RPC 2.0 library (server and client) with HTTP and WebSockets support for | |
Takes `Methods`, `ServerRequest` and `Options`. | ||
|
||
```typescript | ||
import { serve } from "https://deno.land/[email protected]/http/server.ts" | ||
import { respond } from "https://deno.land/x/gentle_rpc/mod.ts" | ||
import { serve } from "https://deno.land/[email protected]/http/server.ts"; | ||
import { respond } from "https://deno.land/x/gentle_rpc/mod.ts"; | ||
|
||
const server = serve("0.0.0.0:8000") | ||
const server = serve("0.0.0.0:8000"); | ||
const rpcMethods = { | ||
sayHello: ([w]: [string]) => `Hello ${w}`, | ||
callNamedParameters: ({ a, b, c }: { a: number; b: number; c: string }) => | ||
`${c} ${a * b}`, | ||
animalsMakeNoise: (noise: string[]) => | ||
noise.map((el) => el.toUpperCase()).join(" "), | ||
} | ||
}; | ||
|
||
console.log("listening on 0.0.0.0:8000") | ||
console.log("listening on 0.0.0.0:8000"); | ||
|
||
for await (const req of server) { | ||
// HTTP: | ||
respond(rpcMethods, req) | ||
respond(rpcMethods, req); | ||
// WebSockets: | ||
respond(rpcMethods, req, { proto: "ws" }) | ||
respond(rpcMethods, req, { proto: "ws" }); | ||
} | ||
``` | ||
|
||
|
@@ -37,7 +37,7 @@ for await (const req of server) { | |
Throw a `CustomError` to send a server-defined error response. | ||
|
||
```typescript | ||
import { CustomError, respond } from "https://deno.land/x/gentle_rpc/mod.ts" | ||
import { CustomError, respond } from "https://deno.land/x/gentle_rpc/mod.ts"; | ||
|
||
//.. | ||
await respond( | ||
|
@@ -46,12 +46,12 @@ await respond( | |
throw new CustomError( | ||
-32040, // the JSON-RPC error code. Note, must be -32040 to -32099 | ||
"An error occurred", // the error message, a short sentence | ||
{ details: "..." } // optional additional details, can be any `JsonValue` | ||
) | ||
{ details: "..." }, // optional additional details, can be any `JsonValue` | ||
); | ||
}, | ||
}, | ||
req | ||
) | ||
req, | ||
); | ||
//.. | ||
``` | ||
|
||
|
@@ -63,15 +63,15 @@ Takes a `Resource` for HTTP or a `WebSocket` for WebSockets and returns | |
`Remote`. | ||
|
||
```typescript | ||
import { createRemote } from "https://deno.land/x/gentle_rpc/mod.ts" | ||
import { createRemote } from "https://deno.land/x/gentle_rpc/mod.ts"; | ||
// Or import directly into the browser with: | ||
import { createRemote } from "https://cdn.jsdelivr.net/gh/timonson/[email protected]/client/dist/remote.js" | ||
import { createRemote } from "https://cdn.jsdelivr.net/gh/timonson/[email protected]/client/dist/remote.js"; | ||
|
||
// HTTP: | ||
const remote = createRemote("http://0.0.0.0:8000") | ||
const remote = createRemote("http://0.0.0.0:8000"); | ||
|
||
// WebSocket: | ||
const remote = await createRemote(new WebSocket("ws://0.0.0.0:8000")) | ||
const remote = await createRemote(new WebSocket("ws://0.0.0.0:8000")); | ||
``` | ||
|
||
### HTTP | ||
|
@@ -82,14 +82,14 @@ Takes a string and an `Array<JsonValue>` or `Record<string, JsonValue>` object | |
and returns `Promise<JsonValue>`. | ||
|
||
```typescript | ||
const greeting = await remote.call("sayHello", ["World"]) | ||
const greeting = await remote.call("sayHello", ["World"]); | ||
// Hello World | ||
|
||
const namedParams = await remote.call("callNamedParameters", { | ||
a: 5, | ||
b: 10, | ||
c: "result:", | ||
}) | ||
}); | ||
// result: 50 | ||
``` | ||
|
||
|
@@ -100,7 +100,7 @@ Using the option `{ isNotification: true }` will retun `Promise<undefined>`. | |
```typescript | ||
const notification = await remote.call("sayHello", ["World"], { | ||
isNotification: true, | ||
}) | ||
}); | ||
// undefined | ||
``` | ||
|
||
|
@@ -110,7 +110,7 @@ Adding the option `{jwt: string}` will set the `Authorization` header to | |
`` `Bearer ${jwt}` ``. | ||
|
||
```typescript | ||
const user = await remote.call("login", undefined, { jwt }) | ||
const user = await remote.call("login", undefined, { jwt }); | ||
// Bob | ||
``` | ||
|
||
|
@@ -127,7 +127,7 @@ const noise1 = await remote.batch([ | |
], | ||
sayHello: [["World"], undefined, ["World"]], | ||
}, | ||
]) | ||
]); | ||
// [ "MIAAOW", "WUUUUFU WUUUUFU", "IAAAIAIA IAAAIAIA IAAAIAIA", "FIIIIIRE", "Hello World", "Hello ", "Hello World" ] | ||
``` | ||
|
||
|
@@ -141,7 +141,7 @@ let noise2 = await remote.batch({ | |
dog: ["animalsMakeNoise", ["wuuuufu"]], | ||
donkey: ["sayHello"], | ||
dragon: ["animalsMakeNoise", ["fiiiiire", "fiiiiire"]], | ||
}) | ||
}); | ||
// { cat: "Hello miaaow", dog: "WUUUUFU", donkey: "Hello ", dragon: "FIIIIIRE FIIIIIRE" } | ||
``` | ||
|
||
|
@@ -160,16 +160,16 @@ const noise = await remote.call("callNamedParameters", { | |
a: 10, | ||
b: 20, | ||
c: "The result is:", | ||
}) | ||
}); | ||
// The result is: 200 | ||
|
||
remote.socket.close() | ||
remote.socket.close(); | ||
``` | ||
|
||
##### notification | ||
|
||
```typescript | ||
const notification = await remote.call("animalsMakeNoise", ["wuufff"], true) | ||
const notification = await remote.call("animalsMakeNoise", ["wuufff"], true); | ||
// undefined | ||
``` | ||
|
||
|
@@ -184,26 +184,26 @@ Other clients can _listen to_ and _emit_ messages by _subscribing_ to the same | |
method. | ||
|
||
```typescript | ||
const firstClient = await createRemote(new WebSocket("ws://0.0.0.0:8000")) | ||
const secondClient = await createRemote(new WebSocket("ws://0.0.0.0:8000")) | ||
const firstClient = await createRemote(new WebSocket("ws://0.0.0.0:8000")); | ||
const secondClient = await createRemote(new WebSocket("ws://0.0.0.0:8000")); | ||
|
||
async function run(iter: AsyncGenerator<unknown>) { | ||
try { | ||
for await (let x of iter) { | ||
console.log(x) | ||
console.log(x); | ||
} | ||
} catch (err) { | ||
console.log(err.message, err.code, err.data) | ||
console.log(err.message, err.code, err.data); | ||
} | ||
} | ||
|
||
const greeting = firstClient.subscribe("sayHello") | ||
const second = secondClient.subscribe("sayHello") | ||
const greeting = firstClient.subscribe("sayHello"); | ||
const second = secondClient.subscribe("sayHello"); | ||
|
||
run(greeting.generator) | ||
run(second.generator) | ||
greeting.emit({ w: "first" }) | ||
second.emitBatch([{ w: "second" }, { w: "third" }]) | ||
run(greeting.generator); | ||
run(second.generator); | ||
greeting.emit({ w: "first" }); | ||
second.emitBatch([{ w: "second" }, { w: "third" }]); | ||
// Hello first | ||
// Hello first | ||
// Hello second | ||
|
@@ -218,21 +218,25 @@ Optionally, you can import _syntactical sugar_ and use a more friendly API | |
supported by `Proxy` objects. | ||
|
||
```typescript | ||
import { createRemote, HttpProxy, httpProxyHandler } from "../../mod.ts" | ||
import { | ||
createRemote, | ||
HttpProxy, | ||
httpProxyHandler, | ||
} from "https://deno.land/x/gentle_rpc/mod.ts"; | ||
|
||
const remote = new Proxy<HttpProxy>( | ||
createRemote("http://0.0.0.0:8000"), | ||
httpProxyHandler | ||
) | ||
httpProxyHandler, | ||
); | ||
|
||
let greeting = await remote.sayHello(["World"]) | ||
let greeting = await remote.sayHello(["World"]); | ||
// Hello World | ||
|
||
const namedParams = await remote.callNamedParameters({ | ||
a: 5, | ||
b: 10, | ||
c: "result:", | ||
}) | ||
}); | ||
// result: 50 | ||
``` | ||
|
||
|
Oops, something went wrong.