2020-08-27 13:46:34 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-04-20 09:39:47 +02:00
|
|
|
export async function postRequest(
|
|
|
|
path: string,
|
|
|
|
body: string | Uint8Array
|
|
|
|
): Promise<Uint8Array> {
|
|
|
|
const headers = {};
|
|
|
|
if (body instanceof Uint8Array) {
|
|
|
|
headers["Content-type"] = "application/octet-stream";
|
|
|
|
}
|
2020-08-27 13:46:34 +02:00
|
|
|
const resp = await fetch(path, {
|
|
|
|
method: "POST",
|
2021-04-20 09:39:47 +02:00
|
|
|
headers,
|
2020-08-27 13:46:34 +02:00
|
|
|
body,
|
|
|
|
});
|
|
|
|
if (!resp.ok) {
|
2021-01-29 05:37:29 +01:00
|
|
|
const body = await resp.text();
|
|
|
|
throw Error(`${resp.status}: ${body}`);
|
2020-08-27 13:46:34 +02:00
|
|
|
}
|
|
|
|
// get returned bytes
|
|
|
|
const respBlob = await resp.blob();
|
|
|
|
const respBuf = await new Response(respBlob).arrayBuffer();
|
|
|
|
const bytes = new Uint8Array(respBuf);
|
|
|
|
return bytes;
|
|
|
|
}
|