import { getWindowHost, checkResponse, getResult, make_path, stripPrefixes, getFetchVars, FetchVariables } from './imports' export function api(endpoint: string) { return ensureAbstractUrl(endpoint) } /** * Strip a leading host (http://host) from any URL-like string. */ export function stripHost(str: string | null): string { // now also removes "abstractendeavors.com" even without protocol str = str || "" const hostPattern = `(?:https?:\\/\\/)?${getWindowHost()}`; return str.replace(new RegExp(`^${hostPattern}`), ''); } export function ensureAbstractUrl( endpoint: string, slices: string[] = [] ): string { slices = slices || ['https//abstractendeavors.com', 'api'] // 1) build a prefix string like "api/v1/" const prefix = slices.map((s) => `${s}/`).join(""); const windowHost = getWindowHost(); const normalized: any = [ '/', ...slices, windowHost, // so "abstractendeavors.com" will be stripped `${windowHost}/api` // etc, if you need it ]; const stripped = stripPrefixes(endpoint, normalized); console.log('BUILD PREFIX:', prefix); console.log('RAW ENDPOINT:', endpoint); console.log('STRIPPED ENDPT:', stripped); return make_path(prefix, stripped); } /** * Given an “endpoint” slug like "api/list", build the full URL * from the BASE_API_URL entry in your JSON config. */ export async function get_app_config_url(endpoint: string): Promise { // 1) normalize + strip prefixes const clean = stripHost(endpoint); // 2) fetch your BASE_API_URL const vars: FetchVariables = getFetchVars(null, 'POST', { key: 'BASE_API_URL', path: '/var/www/abstractendeavors/secure-files/public/config.json' }); const resp = await fetch(ensureAbstractUrl('/secure_env'), vars); checkResponse(resp); // 3) parse, then unwrap { result } const json = await resp.json(); const baseUrl = getResult(json); // 4) now build your final URL return make_path(baseUrl, clean); }