filebrowser/frontend/src/api/share.ts

46 lines
1023 B
TypeScript
Raw Normal View History

2022-05-02 13:47:22 +00:00
import { fetchURL, fetchJSON, removePrefix, createURL } from "./utils";
export async function list() {
return fetchJSON<Share[]>("/api/shares");
}
export async function get(url: string) {
2021-03-21 11:51:58 +00:00
url = removePrefix(url);
return fetchJSON<Share>(`/api/share${url}`);
}
export async function remove(hash: string) {
await fetchURL(`/api/share/${hash}`, {
2021-03-21 11:51:58 +00:00
method: "DELETE",
});
}
export async function create(
url: string,
password = "",
expires = "",
unit = "hours"
) {
2021-03-21 11:51:58 +00:00
url = removePrefix(url);
url = `/api/share${url}`;
if (expires !== "") {
url += `?expires=${expires}&unit=${unit}`;
}
2021-03-21 11:51:58 +00:00
let body = "{}";
if (password != "" || expires !== "" || unit !== "hours") {
body = JSON.stringify({
password: password,
expires: expires.toString(), // backend expects string not number
unit: unit,
});
}
return fetchJSON(url, {
2021-03-21 11:51:58 +00:00
method: "POST",
body: body,
2021-03-21 11:51:58 +00:00
});
}
2022-05-02 13:47:22 +00:00
export function getShareURL(share: Share) {
2022-05-02 13:47:22 +00:00
return createURL("share/" + share.hash, {}, false);
}