import { Option, Some, None, none } from "optionals"; export function catcher(f: () => T): Option { try { return Some(f()); } catch { // There is no need to define what is already undefined. return None(); } } export async function catcher_async( f: () => Promise ): Promise> { try { return Some(await f()); } catch { return None(); } } export function squish_option(opt: Option>): Option { return opt.unwrapOr(None()); } export function json_response(obj: any, options?: ResponseInit): Response { return new Response(JSON.stringify(obj), { ...options, headers: { "Content-Type": "application/json" }, }); } export function fetch_json( input: RequestInfo | URL, init?: RequestInit ): Promise> { return catcher_async(() => fetch(input, init).then(j => j.json())); } export function class_list(...classes: Array): string { return classes.filter(Boolean).join(" "); } export function remove_undefined_keys(obj: any): any { return Object.keys(obj).reduce((acc, key) => { if (obj[key] !== undefined && obj[key] !== "") { acc[key] = obj[key]; } return acc; }, {}); } export const URL = "http://fl.pyrope.net";