import createDebug from "debug";
import CONFIG from "./config.json";
const logger = createDebug("config");
const CONFIG_URL = "https://configuration.starkingdoms.io";
export interface Config {
servers: {[id: string]: ConfigServer}
}
export interface ConfigServer {
name: string;
clientHubUrl: string;
apiBaseUrl: string;
isProduction: boolean;
isDevelopment: boolean;
isPrimary: boolean;
}
async function fetchWithTimeout(resource: RequestInfo | URL, options = {}) {
// @ts-ignore
const { timeout = 8000 } = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal
});
clearTimeout(id);
return response;
}
export async function loadConfig(): Promise<Config> {
logger("loading configuration from " + CONFIG_URL);
try {
const response = await fetchWithTimeout(CONFIG_URL, {
timeout: 1000
});
return await response.json();
} catch (e) {
logger(`error loading configuration: ${e}, using fallback`);
// @ts-ignore strong types are unhelpful here
return CONFIG;
}
}