Add test-http-api crate to make app work in browser

A new `invoke` function calls either the tauri backend or the
`test-http-api` function if ran in a browser.
This commit is contained in:
Sebastian Martinez
2024-10-28 13:56:23 +01:00
parent e0323e34bc
commit d90dc0735c
45 changed files with 4617 additions and 814 deletions
+9 -7
View File
@@ -3,7 +3,7 @@
import { onDestroy, onMount } from "svelte";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
import { listen } from "@tauri-apps/api/event";
import * as router from "@app/lib/router";
@@ -25,13 +25,15 @@
let unlistenNodeEvents: UnlistenFn | undefined = undefined;
onMount(async () => {
unlistenEvents = await listen("event", event => {
console.log(event.payload);
});
if (window.__TAURI_INTERNALS__) {
unlistenEvents = await listen("event", event => {
console.log(event.payload);
});
unlistenNodeEvents = await listen<boolean>("node_running", event => {
nodeRunning.set(event.payload);
});
unlistenNodeEvents = await listen<boolean>("node_running", event => {
nodeRunning.set(event.payload);
});
}
try {
await invoke("authenticate");
+1 -1
View File
@@ -7,7 +7,7 @@
import { Renderer, markdownWithExtensions } from "@app/lib/markdown";
import { highlight } from "@app/lib/syntax";
import { twemoji, scrollIntoView, isCommit } from "@app/lib/utils";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
export let rid: string;
export let content: string;
+1 -1
View File
@@ -8,7 +8,7 @@
patchStatusBackgroundColor,
patchStatusColor,
} from "@app/lib/utils";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
import { onMount } from "svelte";
import { push } from "@app/lib/router";
+21
View File
@@ -0,0 +1,21 @@
import * as tauri from "@tauri-apps/api/core";
export async function invoke<T = null>(
cmd: string,
args?: tauri.InvokeArgs,
options?: tauri.InvokeOptions,
): Promise<T> {
if (window.__TAURI_INTERNALS__) {
return tauri.invoke(cmd, args, options);
} else {
return fetch(`http://127.0.0.1:8080/${cmd}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(args),
})
.then(data => data.json())
.catch(() => {
throw Error(`Issue with HTTP Route: ${JSON.stringify({ cmd, args })}`);
});
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ import type { Config } from "@bindings/config/Config";
import type { RepoInfo } from "@bindings/repo/RepoInfo";
import type { LoadedRepoRoute, RepoRoute } from "@app/views/repo/router";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
import {
loadCreateIssue,
+1 -1
View File
@@ -4,7 +4,7 @@
import type { Issue } from "@bindings/cob/issue/Issue";
import type { RepoInfo } from "@bindings/repo/RepoInfo";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
import { issueStatusColor } from "@app/lib/utils";
import * as router from "@app/lib/router";
+1 -1
View File
@@ -14,7 +14,7 @@
publicKeyFromDid,
scrollIntoView,
} from "@app/lib/utils";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
import { announce } from "@app/components/AnnounceSwitch.svelte";
+1 -1
View File
@@ -9,7 +9,7 @@
formatTimestamp,
patchStatusColor,
} from "@app/lib/utils";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
import Border from "@app/components/Border.svelte";
import CopyableId from "@app/components/CopyableId.svelte";
+1 -1
View File
@@ -5,7 +5,7 @@
import type { PatchStatus } from "./router";
import type { RepoInfo } from "@bindings/repo/RepoInfo";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
import Layout from "./Layout.svelte";
import Border from "@app/components/Border.svelte";
+1 -1
View File
@@ -5,7 +5,7 @@ import type { Patch } from "@bindings/cob/patch/Patch";
import type { RepoInfo } from "@bindings/repo/RepoInfo";
import type { Revision } from "@bindings/cob/patch/Revision";
import { invoke } from "@tauri-apps/api/core";
import { invoke } from "@app/lib/invoke";
import { unreachable } from "@app/lib/utils";
export type IssueStatus = "all" | Issue["state"]["status"];