From 2687a5d292ba143fdfa7fc61adb34ae64a179367 Mon Sep 17 00:00:00 2001 From: dragongoose Date: Sun, 18 Jun 2023 23:42:40 -0400 Subject: [PATCH] Add mixins --- src/mixins.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/mixins.ts diff --git a/src/mixins.ts b/src/mixins.ts new file mode 100644 index 0000000..d9d57cb --- /dev/null +++ b/src/mixins.ts @@ -0,0 +1,40 @@ +export function truncate(value: string, length: number) { + if (value.length > length) { + return value.substring(0, length) + '...' + } else { + return value + } +} + +export function abbreviate(text: number) { + return Intl.NumberFormat('en-US', { + //@ts-ignore + notation: 'compact', + maximumFractionDigits: 1 + }).format(text) +} + +const https = (import.meta.env.SAFETWITCH_HTTPS.slice() === "true") +const protocol = https ? 'https://' : 'http://' +const rootBackendUrl = `${protocol}${import.meta.env.SAFETWITCH_BACKEND_DOMAIN}` + +export async function getEndpoint(endpoint: string) { + let data + + try { + const res = await fetch(rootBackendUrl + endpoint) + const rawData = await res.json() + + if (rawData.status === 'ok') { + data = rawData.data + } else { + data = { status: 'error' } + } + + } catch (error) { + console.error(error) + data = { status: 'error' } + } + + return data +} \ No newline at end of file