safetwitch/src/mixins.ts

42 lines
962 B
TypeScript
Raw Normal View History

2023-06-18 23:42:40 -04:00
export function truncate(value: string, length: number) {
2023-07-20 13:57:01 -04:00
if (value.length > length) {
return value.substring(0, length) + '...'
} else {
return value
}
2023-06-18 23:42:40 -04:00
}
2023-07-20 13:57:01 -04:00
const language = localStorage.getItem('language') || 'en-us'
2023-07-04 20:52:49 -04:00
2023-06-18 23:42:40 -04:00
export function abbreviate(text: number) {
2023-07-20 13:57:01 -04:00
return Intl.NumberFormat(language, {
//@ts-ignore
notation: 'compact',
maximumFractionDigits: 1
}).format(text)
2023-06-18 23:42:40 -04:00
}
2023-07-20 13:57:01 -04:00
const https = import.meta.env.SAFETWITCH_HTTPS.slice() === 'true'
2023-06-18 23:42:40 -04:00
const protocol = https ? 'https://' : 'http://'
2023-06-19 23:18:10 -04:00
const rootBackendUrl = `${protocol}${import.meta.env.SAFETWITCH_BACKEND_DOMAIN}/`
2023-06-18 23:42:40 -04:00
export async function getEndpoint(endpoint: string) {
2023-07-20 13:57:01 -04:00
const res = await fetch(rootBackendUrl + endpoint, {
method: 'GET',
headers: {
'Accept-Language': language
}
})
const rawData = await res.json()
2023-06-18 23:42:40 -04:00
2023-07-20 13:57:01 -04:00
if (!res.ok) {
throw res
}
if (rawData.status !== 'ok') {
throw rawData
}
2023-06-18 23:42:40 -04:00
2023-07-20 13:57:01 -04:00
const data = rawData.data
2023-06-18 23:42:40 -04:00
2023-07-20 13:57:01 -04:00
return data
2023-08-18 14:23:59 -04:00
}