diff --git a/deltachat-jsonrpc/README.md b/deltachat-jsonrpc/README.md index e5f12caa9..f1969d8db 100644 --- a/deltachat-jsonrpc/README.md +++ b/deltachat-jsonrpc/README.md @@ -27,16 +27,29 @@ If you are targetting other architectures (like KaiOS or Android), the webserver cross build --features=webserver --target armv7-linux-androideabi --release ``` +### Using the typescript client + +The package includes TypeScript bindings which are partially auto-generated through the JSON-RPC library used by this crate ([yerpc](https://github.com/Frando/yerpc/)). Find the source in the [`typescript`](typescript) folder. The client is also published on npm as `@deltachat/jsonrpc-client`. + +To use it locally, first install the dependencies and compile the TypeScript code to JavaScript: +```sh +cd typescript +npm install +npm run build +``` + +You can also build autogenerated documentation, including all RPC methods: +```sh +cd typescript +npm run docs +``` +Now, open the [`typescript/docs`](typescript/docs) folder in a web browser. + + ### Running the example app We include a small demo web application that talks to the WebSocket server. To run it, follow these steps: -* The package includes TypeScript bindings which are partially auto-generated through the JSON-RPC library used by this crate ([yerpc](https://github.com/Frando/yerpc/)). - ```sh - cd typescript - npm install - npm run build - ``` * Then, build and run the example application: ```sh diff --git a/deltachat-jsonrpc/typescript/.gitignore b/deltachat-jsonrpc/typescript/.gitignore index 98d74b443..ca2372bc1 100644 --- a/deltachat-jsonrpc/typescript/.gitignore +++ b/deltachat-jsonrpc/typescript/.gitignore @@ -4,3 +4,5 @@ test_dist coverage yarn.lock package-lock.json +docs +accounts diff --git a/deltachat-jsonrpc/typescript/example/example.ts b/deltachat-jsonrpc/typescript/example/example.ts index f13d3a717..f543a51cf 100644 --- a/deltachat-jsonrpc/typescript/example/example.ts +++ b/deltachat-jsonrpc/typescript/example/example.ts @@ -1,7 +1,5 @@ -import { RawClient } from "../src/lib"; -import { WebsocketTransport, Request } from "yerpc"; +import { Deltachat, DeltachatEvent } from "../deltachat.js"; -type DeltaEvent = { id: string; contextId: number; field1: any; field2: any }; var SELECTED_ACCOUNT = 0; window.addEventListener("DOMContentLoaded", (_event) => { @@ -9,6 +7,7 @@ window.addEventListener("DOMContentLoaded", (_event) => { SELECTED_ACCOUNT = Number(id); window.dispatchEvent(new Event("account-changed")); }; + console.log('launch run script...') run().catch((err) => console.error("run failed", err)); }); @@ -17,31 +16,24 @@ async function run() { const $side = document.getElementById("side")!; const $head = document.getElementById("header")!; - const transport = new WebsocketTransport("ws://localhost:20808/ws"); - const client = new RawClient(transport); - transport.on("error", (err) => { - console.error("WebSocket transport error", err) - }); + const client = new Deltachat('ws://localhost:20808/ws') - ;(window as any).client = client; + ;(window as any).client = client.rpc; - transport.on("request", (request: Request) => { - const method = request.method; - if (method === "event") { - const params = request.params! as DeltaEvent; - onIncomingEvent(params, params.id); - } - }); + client.on("ALL", event => { + onIncomingEvent(event) + }) window.addEventListener("account-changed", async (_event: Event) => { - await client.selectAccount(SELECTED_ACCOUNT); listChatsForSelectedAccount(); }); await Promise.all([loadAccountsInHeader(), listChatsForSelectedAccount()]); async function loadAccountsInHeader() { - const accounts = await client.getAllAccounts(); + console.log('load accounts') + const accounts = await client.rpc.getAllAccounts(); + console.log('accounts loaded', accounts) for (const account of accounts) { if (account.type === "Configured") { write( @@ -63,31 +55,30 @@ async function run() { async function listChatsForSelectedAccount() { clear($main); - const selectedAccount = await client.getSelectedAccountId(); - if (!selectedAccount) return write($main, "No account selected"); - const info = await client.getAccountInfo(selectedAccount); + const selectedAccount = SELECTED_ACCOUNT + const info = await client.rpc.getAccountInfo(selectedAccount); if (info.type !== "Configured") { return write($main, "Account is not configured"); } write($main, `