add the visitor API endpoint

This commit is contained in:
ngn
2025-01-08 00:20:11 +03:00
parent fc11748e57
commit dee3ef4d85
26 changed files with 444 additions and 163 deletions

View File

@ -1,5 +1,6 @@
import { join } from "$lib/api.js";
import { browser } from "$app/environment";
const default_lang = "en";
const colors = [
"yellow",
"cyan",
@ -8,8 +9,26 @@ const colors = [
"red",
// "blue" (looks kinda ass)
];
let colors_pos = -1;
let api_url = join;
function urljoin(url, path = null, query = {}) {
let url_len = url.length;
if (url[url_len - 1] != "/") url += "/";
if (null === path || "" === path) url = new URL(url);
else if (path[0] === "/") url = new URL(path.slice(1), url);
else url = new URL(path, url);
for (let k in query) url.searchParams.append(query[k]);
return url.href;
}
function frontend_url(path = null, query = {}) {
return urljoin(import.meta.env.VITE_FRONTEND_URL, path, query);
}
function color() {
if (colors_pos < 0) colors_pos = Math.floor(Math.random() * colors.length);
@ -23,13 +42,13 @@ function click() {
audio.play();
}
function frontend_url(path) {
if (null !== path && path !== "") return new URL(path, import.meta.env.VITE_FRONTEND_URL).href;
else return new URL(import.meta.env.VITE_FRONTEND_URL).href;
function browser_lang() {
if (browser) return window.navigator.language.slice(0, 2).toLowerCase();
return default_lang;
}
function time_from_ts(ts) {
return new Date(ts * 1000).toLocaleTimeString();
}
export { api_url, frontend_url, click, color, time_from_ts };
export { urljoin, frontend_url, browser_lang, click, color, time_from_ts };