// todo: change this export const url = "https://api.axonbox.net"; export let models: Model[] = []; export interface ReqParams { method: string, body?: string, } export interface Model { name: string, capabilities: any, description: { full: string, short: string, }, license: string, free: number } export async function post( endpoint: string, body: Object ) { return await req( endpoint, { method: "POST", body: JSON.stringify( body ), } ); } export async function req( endpoint: string, params: ReqParams ) { const res = await fetch( `${url}/${endpoint}`, { method: params.method, headers: { "Content-Type": "application/json", }, body: params.body, } ); if( !res.ok ) { let json = null; try { json = await res.json(); } catch( e: any ) { throw new Error( "error contacting server" ); } throw new Error( json.msg ); } const json = await res.json(); if( json.status != 'ok' ) throw new Error( json.msg ); return json; } export async function updateModels() { parseModels(); try { const res = await post( 'models', {} ); models = res.models as Model[]; localStorage.setItem( 'models', JSON.stringify( models ) ); } catch( e: any ) { throw new Error( e.message ); } } export function getModelFromName( name: string ) { for( let model of models ) { if( model.name === name ) return model; } return null; } /** * parses existing models from localStorage **/ export function parseModels() { models = JSON.parse( localStorage.getItem( 'models' ) || '[]' ) as Model[]; }