|
1 | | -A TypeScript/JavaScript API client for [Diskuto]. |
| 1 | +[![JSR Version]][JSR Link] |
2 | 2 |
|
3 | | -[Diskuto]: https://github.com/diskuto/ |
| 3 | +Diskuto API Client |
| 4 | +================== |
| 5 | + |
| 6 | +A TypeScript/JavaScript API client for [Diskuto REST API]. |
| 7 | + |
| 8 | +Example |
| 9 | +------- |
| 10 | + |
| 11 | +Here's an example from [this blog post][1] of how to fetch and validate an `Item` |
| 12 | +using this API client: |
| 13 | + |
| 14 | +```typescript |
| 15 | +#!/usr/bin/env -S deno run --check -EN |
| 16 | + |
| 17 | +const baseUrl = `https://blog.nfnitloop.com` |
| 18 | +const userID = `A719rvsCkuN2SC5W2vz5hypDE2SpevNTUsEXrVFe9XQ7` |
| 19 | +const signature = `4sVxU7pVvUenEdG41BYJDZJfDBZBjBkLSF7dcGzpGMgtVLbZjTh6w5LzC4Rwjkk5SNyn57o3cfsvEbsZJkFELaW3` |
| 20 | + |
| 21 | +// Given these three things, we can validate that the content on the page |
| 22 | +// was in fact created by that user, and has not been modified. |
| 23 | + |
| 24 | +import {Client, Signature, UserID} from "jsr:@diskuto/client@0.10.0" |
| 25 | +const client = new Client({baseUrl}) |
| 26 | + |
| 27 | +const bytes = await client.getItemBytes(userID, signature) |
| 28 | +if (bytes == null) { |
| 29 | + throw new Error(`No such item on this server.`) |
| 30 | +} |
| 31 | + |
| 32 | +// Some helper types for working with cryptography: |
| 33 | +const uid = UserID.fromString(userID) |
| 34 | +const sig = Signature.fromString(signature) |
| 35 | + |
| 36 | +const valid = await sig.isValid(uid, bytes) |
| 37 | +if (!valid) { |
| 38 | + throw new Error(`Invalid signature!`) |
| 39 | +} |
| 40 | + |
| 41 | +// OK, we have a valid signature for ... some bytes. But is that what's on the page? |
| 42 | +// We'll need to inspect the contents of those bytes to verify that the server isn't |
| 43 | +// misrepresenting them: |
| 44 | +import { fromBinary, ItemSchema } from "jsr:@diskuto/client/types"; |
| 45 | +const item = fromBinary(ItemSchema, bytes) |
| 46 | +console.log(item) |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +[Diskuto REST API]: https://github.com/diskuto/diskuto-api/tree/main/docs/rest_api |
| 51 | +[1]: https://blog.nfnitloop.com/u/A719rvsCkuN2SC5W2vz5hypDE2SpevNTUsEXrVFe9XQ7/i/33QiHdeD3sSiB5mWwSBJhpkjQEystr4nx9W8FFWWM1oWfCFMfDHpYuKMGJ6v8kmf2pHnnhL67Reg14jGRVoTyJ7Z/ |
| 52 | +[JSR Version]: https://jsr.io/badges/@diskuto/client |
| 53 | +[JSR Link]: https://jsr.io/@diskuto/client |
0 commit comments