-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path+page.svelte
More file actions
64 lines (55 loc) · 1.66 KB
/
+page.svelte
File metadata and controls
64 lines (55 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<script lang="ts">
import init, { Hello } from "rust-idl";
import { onMount } from "svelte";
const WEBSERVER_URL = "http://localhost:8000";
let loaded = false;
let payload = new Uint8Array();
let accept = "application/json";
let name = "";
let hello: Hello | null = null;
const greet = async (name: string): Promise<Hello> => {
const headers = new Headers();
headers.set("Accept", accept);
const result = await fetch(`${WEBSERVER_URL}/hello/${name}`, { headers });
console.log(`Response status: ${result.status}`);
payload = new Uint8Array(await result.arrayBuffer());
console.log("using accept header: ", accept);
switch (accept) {
case "application/json":
return Hello.from_json(payload);
case "application/msgpack":
return Hello.from_msgpack(payload);
default:
throw new Error(`Unsupported accept header: ${accept}`);
}
};
onMount(async () => {
await init();
loaded = true;
});
</script>
<h1>Ridl Demo</h1>
{#if !loaded}
<p>Loading...</p>
{:else}
<div>
<select bind:value={accept}>
<option value="application/json">application/json</option>
<option value="application/msgpack">application/msgpack</option>
</select>
</div>
<div>
<input type="text" bind:value={name} placeholder="Enter your name" />
<button on:click={async () => (hello = await greet(name))}>Greet</button>
</div>
<div>
{#if payload.byteLength > 0}
<p>Raw payload: {payload.join(", ")}</p>
{/if}
{#if hello}
<p>Decoded name = {hello.name}</p>
{:else}
<p>Enter your name and click the button above to be greeted</p>
{/if}
</div>
{/if}