0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch.git synced 2025-01-09 14:20:09 -05:00
safetwitch/src/mixins.ts
2023-08-18 14:23:59 -04:00

42 lines
No EOL
962 B
TypeScript

export function truncate(value: string, length: number) {
if (value.length > length) {
return value.substring(0, length) + '...'
} else {
return value
}
}
const language = localStorage.getItem('language') || 'en-us'
export function abbreviate(text: number) {
return Intl.NumberFormat(language, {
//@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) {
const res = await fetch(rootBackendUrl + endpoint, {
method: 'GET',
headers: {
'Accept-Language': language
}
})
const rawData = await res.json()
if (!res.ok) {
throw res
}
if (rawData.status !== 'ok') {
throw rawData
}
const data = rawData.data
return data
}