improve docs, fix example

This commit is contained in:
Franz Heinzmann (Frando)
2022-06-29 17:53:45 +02:00
committed by Simon Laux
parent 035e208e4f
commit 329f498651
5 changed files with 72 additions and 38 deletions

View File

@@ -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 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 ### Running the example app
We include a small demo web application that talks to the WebSocket server. To run it, follow these steps: 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: * Then, build and run the example application:
```sh ```sh

View File

@@ -4,3 +4,5 @@ test_dist
coverage coverage
yarn.lock yarn.lock
package-lock.json package-lock.json
docs
accounts

View File

@@ -1,7 +1,5 @@
import { RawClient } from "../src/lib"; import { Deltachat, DeltachatEvent } from "../deltachat.js";
import { WebsocketTransport, Request } from "yerpc";
type DeltaEvent = { id: string; contextId: number; field1: any; field2: any };
var SELECTED_ACCOUNT = 0; var SELECTED_ACCOUNT = 0;
window.addEventListener("DOMContentLoaded", (_event) => { window.addEventListener("DOMContentLoaded", (_event) => {
@@ -9,6 +7,7 @@ window.addEventListener("DOMContentLoaded", (_event) => {
SELECTED_ACCOUNT = Number(id); SELECTED_ACCOUNT = Number(id);
window.dispatchEvent(new Event("account-changed")); window.dispatchEvent(new Event("account-changed"));
}; };
console.log('launch run script...')
run().catch((err) => console.error("run failed", err)); run().catch((err) => console.error("run failed", err));
}); });
@@ -17,31 +16,24 @@ async function run() {
const $side = document.getElementById("side")!; const $side = document.getElementById("side")!;
const $head = document.getElementById("header")!; const $head = document.getElementById("header")!;
const transport = new WebsocketTransport("ws://localhost:20808/ws"); const client = new Deltachat('ws://localhost:20808/ws')
const client = new RawClient(transport);
transport.on("error", (err) => {
console.error("WebSocket transport error", err)
});
;(window as any).client = client; ;(window as any).client = client.rpc;
transport.on("request", (request: Request) => { client.on("ALL", event => {
const method = request.method; onIncomingEvent(event)
if (method === "event") { })
const params = request.params! as DeltaEvent;
onIncomingEvent(params, params.id);
}
});
window.addEventListener("account-changed", async (_event: Event) => { window.addEventListener("account-changed", async (_event: Event) => {
await client.selectAccount(SELECTED_ACCOUNT);
listChatsForSelectedAccount(); listChatsForSelectedAccount();
}); });
await Promise.all([loadAccountsInHeader(), listChatsForSelectedAccount()]); await Promise.all([loadAccountsInHeader(), listChatsForSelectedAccount()]);
async function loadAccountsInHeader() { 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) { for (const account of accounts) {
if (account.type === "Configured") { if (account.type === "Configured") {
write( write(
@@ -63,31 +55,30 @@ async function run() {
async function listChatsForSelectedAccount() { async function listChatsForSelectedAccount() {
clear($main); clear($main);
const selectedAccount = await client.getSelectedAccountId(); const selectedAccount = SELECTED_ACCOUNT
if (!selectedAccount) return write($main, "No account selected"); const info = await client.rpc.getAccountInfo(selectedAccount);
const info = await client.getAccountInfo(selectedAccount);
if (info.type !== "Configured") { if (info.type !== "Configured") {
return write($main, "Account is not configured"); return write($main, "Account is not configured");
} }
write($main, `<h2>${info.addr!}</h2>`); write($main, `<h2>${info.addr!}</h2>`);
const chats = await client.getChatlistEntries( const chats = await client.rpc.getChatlistEntries(
selectedAccount, selectedAccount,
0, 0,
null, null,
null null
); );
for (const [chatId, _messageId] of chats) { for (const [chatId, _messageId] of chats) {
const chat = await client.chatlistGetFullChatById( const chat = await client.rpc.chatlistGetFullChatById(
selectedAccount, selectedAccount,
chatId chatId
); );
write($main, `<h3>${chat.name}</h3>`); write($main, `<h3>${chat.name}</h3>`);
const messageIds = await client.messageListGetMessageIds( const messageIds = await client.rpc.messageListGetMessageIds(
selectedAccount, selectedAccount,
chatId, chatId,
0 0
); );
const messages = await client.messageGetMessages( const messages = await client.rpc.messageGetMessages(
selectedAccount, selectedAccount,
messageIds messageIds
); );
@@ -97,12 +88,12 @@ async function run() {
} }
} }
function onIncomingEvent(event: DeltaEvent, name: string) { function onIncomingEvent(event: DeltachatEvent) {
write( write(
$side, $side,
` `
<p class="message"> <p class="message">
[<strong>${name}</strong> on account ${event.contextId}]<br> [<strong>${event.id}</strong> on account ${event.contextId}]<br>
<em>f1:</em> ${JSON.stringify(event.field1)}<br> <em>f1:</em> ${JSON.stringify(event.field1)}<br>
<em>f2:</em> ${JSON.stringify(event.field2)} <em>f2:</em> ${JSON.stringify(event.field2)}
</p>` </p>`

View File

@@ -0,0 +1,26 @@
import { Deltachat } from "../dist/deltachat.js";
run().catch(console.error);
async function run() {
const delta = new Deltachat('ws://localhost:20808/ws');
delta.on("event", (event) => {
console.log("event", event.data);
});
const email = process.argv[2]
const password = process.argv[3]
if (!email || !password) throw new Error('USAGE: node node-add-account.js <EMAILADDRESS> <PASSWORD>')
console.log(`creating acccount for ${email}`)
const id = await delta.rpc.addAccount()
console.log(`created account id ${id}`)
await delta.rpc.setConfig(id, "addr", email);
await delta.rpc.setConfig(id, "mail_pw", password);
console.log('configuration updated')
await delta.rpc.configure(id)
console.log('account configured!')
const accounts = await delta.rpc.getAllAccounts();
console.log("accounts", accounts);
console.log("waiting for events...")
}

View File

@@ -12,11 +12,12 @@
"build": "npm run generate-bindings && tsc", "build": "npm run generate-bindings && tsc",
"bundle": "npm run build && esbuild --bundle dist/deltachat.js --outfile=dist/deltachat.bundle.js", "bundle": "npm run build && esbuild --bundle dist/deltachat.js --outfile=dist/deltachat.bundle.js",
"generate-bindings": "cargo test", "generate-bindings": "cargo test",
"example:build": "tsc && esbuild --bundle dist/example.js --outfile=dist/example.bundle.js && cp example/index.html dist", "example:build": "tsc && esbuild --bundle dist/example/example.js --outfile=dist/example.bundle.js",
"example:start": "http-server dist/", "example:start": "http-server .",
"example:dev": "esbuild example/example.ts --bundle --outfile=dist/example.bundle.js --servedir=.", "example:dev": "esbuild example/example.ts --bundle --outfile=dist/example.bundle.js --servedir=.",
"coverage": "tsc -b test && COVERAGE=1 NODE_OPTIONS=--enable-source-maps c8 --include \"dist/*\" -r text -r html -r json mocha test_dist && node report_api_coverage.mjs", "coverage": "tsc -b test && COVERAGE=1 NODE_OPTIONS=--enable-source-maps c8 --include \"dist/*\" -r text -r html -r json mocha test_dist && node report_api_coverage.mjs",
"test": "rm -rf dist && npm run build && npm run coverage && npm run prettier:check" "test": "rm -rf dist && npm run build && npm run coverage && npm run prettier:check",
"docs": "typedoc --out docs deltachat.ts"
}, },
"dependencies": { "dependencies": {
"isomorphic-ws": "^4.0.1", "isomorphic-ws": "^4.0.1",
@@ -24,8 +25,6 @@
"yerpc": "^0.3.3" "yerpc": "^0.3.3"
}, },
"devDependencies": { "devDependencies": {
"prettier": "^2.6.2",
"chai-as-promised": "^7.1.1",
"@types/chai": "^4.2.21", "@types/chai": "^4.2.21",
"@types/chai-as-promised": "^7.1.5", "@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^9.0.0", "@types/mocha": "^9.0.0",
@@ -33,9 +32,12 @@
"@types/ws": "^7.2.4", "@types/ws": "^7.2.4",
"c8": "^7.10.0", "c8": "^7.10.0",
"chai": "^4.3.4", "chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"esbuild": "^0.14.11", "esbuild": "^0.14.11",
"mocha": "^9.1.1", "mocha": "^9.1.1",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"prettier": "^2.6.2",
"typedoc": "^0.23.2",
"typescript": "^4.5.5", "typescript": "^4.5.5",
"ws": "^8.5.0" "ws": "^8.5.0"
} }