allow postrequest to send a binary body; fix WithGraphData

As far as I can tell, the existing code was transforming the encoded
protobuf data into UTF8, and we're just lucky it wasn't causing problems
with the small message we were sending.
This commit is contained in:
Damien Elmes 2021-04-20 17:39:47 +10:00
parent 10b7ab2c6d
commit 094c272294
2 changed files with 10 additions and 4 deletions

View file

@ -38,9 +38,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
): Promise<void> {
await postRequest(
"/_anki/setGraphPreferences",
new TextDecoder().decode(
pb.BackendProto.GraphPreferences.encode(prefs).finish()
)
pb.BackendProto.GraphPreferences.encode(prefs).finish()
);
}

View file

@ -1,9 +1,17 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export async function postRequest(path: string, body: string): Promise<Uint8Array> {
export async function postRequest(
path: string,
body: string | Uint8Array
): Promise<Uint8Array> {
const headers = {};
if (body instanceof Uint8Array) {
headers["Content-type"] = "application/octet-stream";
}
const resp = await fetch(path, {
method: "POST",
headers,
body,
});
if (!resp.ok) {