Self-hosted, federated location sharing app and server that prioritizes user privacy and security
end-to-end-encryption location-sharing privacy self-hosted federated
at main 1.7 kB view raw
1import { Store } from "./store.ts"; 2 3// TODO: test if this is still needed: 4// Don't mind this piece of code, it's a polyfill until chromium decides to merge it (it's been so long) 5// @ts-ignore 6// Uint8Array.prototype.toBase64 = function () { 7// let binary = ""; 8// for (let i = 0; i < this.length; i++) { 9// const byte = this[i]; 10// if (byte !== undefined) { 11// binary += String.fromCharCode(byte); 12// } 13// } 14// return btoa(binary); 15// }; 16 17export async function createAccount(signup_key: string): Promise<{ user_id: string; is_admin: boolean }> { 18 try { 19 const server_url = await Store.get("server_url"); 20 21 const response = await fetch(server_url + "/create-account", { 22 method: "POST", 23 body: signup_key, 24 }); 25 26 if (!response.ok) { 27 throw new Error(`${await response.text()}`); 28 } 29 return await response.json(); 30 } catch (err) { 31 alert(`${err}`); 32 throw err; 33 } 34} 35 36export async function post(endpoint: string, data: Object | string | undefined): Promise<any> { 37 try { 38 const user_id = await Store.get("user_id"); 39 const server_url = await Store.get("server_url"); 40 41 const headers: Record<string, string> = { 42 "x-auth": JSON.stringify({ user_id }), 43 }; 44 45 let stringified_data: string | undefined; 46 47 if (typeof data === "object") { 48 stringified_data = JSON.stringify(data); 49 headers["Content-Type"] = "application/json"; 50 } else { 51 stringified_data = data; 52 } 53 54 const res = await fetch(`${server_url}/${endpoint}`, { 55 method: "POST", 56 headers, 57 body: stringified_data, 58 }); 59 60 if (!res.ok) { 61 throw new Error(`${await res.text()}`); 62 } 63 64 return await res.text(); 65 } catch (err) { 66 alert(`${err}`); 67 throw err; 68 } 69}