env thing ts
All checks were successful
Rebuild signaller for deprived.dev to rebuild site / test_service (push) Successful in 20s

This commit is contained in:
BOTAlex 2025-10-09 00:43:57 +02:00
parent fd1ef8612f
commit 07f0d798a2
2 changed files with 46 additions and 32 deletions

View file

@ -1,42 +1,54 @@
// used to deal with .env things, but with this instead because nix
// Drafted manually, then fed through chatgpt
// Idk what is going on anymore, but it isn't really an important aspect of the code, since this is just for changing from productiong to dev environment
type Env = { [K in keyof typeof env]: (typeof env)[K] } & Record<
string,
unknown
>;
export let env = {
POCKETBASE_URL: "https://pocket.deprived.dev/",
DEV_POCKETBASE_URL: "https://dev.pocket.deprived.dev/",
};
POCKETBASE_URL: "https://pocket.deprived.dev",
} as const;
export default env;
// Check if ?debug=1 or debug in localstorage is 1 or true
export function checkIfDev(): boolean {
if (typeof window === "undefined") return false;
let initialized = false;
try {
const params = new URL(window.location.href).searchParams;
const urlDebug = params.get("debug");
// Load overrides from localstorage or url params
const isOverride = (k: string) => k.startsWith("-E");
const norm = (k: string) => k.slice(2); // strip "-E"
const ls = window.localStorage?.getItem("debug") ?? "";
const lsDebug = ls.toLowerCase();
const isUrlDebug = urlDebug === "1";
const isLocalDebug = lsDebug === "1" || lsDebug === "true";
return isUrlDebug || isLocalDebug;
} catch {
return false;
function apply(from: Record<string, string>, tag: string) {
for (const [rawK, rawV] of Object.entries(from)) {
env[rawK] = rawV;
}
}
// Load overrides from localstorage
function loadOverrides() {
for (const [name, val] of Object.entries(env)) {
const envOverride = localStorage.getItem(name);
if (envOverride) {
env[name] = envOverride;
console.log("Env var +: " + name + "=" + val);
} else {
console.log("Env var loaded: " + name + "=" + val);
}
export function initEnv(): void {
if (initialized) return;
initialized = true;
window.env = env!;
// localStorage overrides: only keys starting with "-E"
const ls: Record<string, string> = {};
for (const [k, v] of Object.entries(env)) {
const val = localStorage.getItem(k);
if (val != null) ls[k] = val;
}
apply(ls, "localStorage");
// URL param overrides: ?-EKEY=value
const params = new URLSearchParams(location.search);
const url: Record<string, string> = {};
for (const [k, v] of params.entries()) url[k] = v;
apply(url, "url");
// (optional) persist URL overrides in localStorage with the "-E" prefix
for (const [k, v] of Object.entries(url)) {
if (isOverride(k)) localStorage.setItem(norm(k), String(v));
}
loadOverrides();
// Object.assign(env as any, current);
// for (const [k, v] of Object.entries(env))
// console.log(`[env final] ${k}=${String(v)}`);
}

View file

@ -1,5 +1,6 @@
import { parseProfile, type Profile } from "./Profile";
import env from "@ts/EnvHandler";
import { initEnv } from "@ts/EnvHandler";
class Redactor {
public unredactedProfile: Profile | undefined = undefined;
@ -24,7 +25,8 @@ class Redactor {
const unredactHash = hashResJson.response;
console.log('Trying unredact hash: "' + unredactHash + '"');
const url = `https://${env.POCKETBASE_URL}/api/files/redacted_content/${unredactHash}/redacted_cv_info_ha08bbn520.json`;
initEnv();
const url = `${env.POCKETBASE_URL}/api/files/redacted_content/${unredactHash}/redacted_cv_info_ha08bbn520.json`;
const res = await fetch(url, {
method: "GET",