summaryrefslogtreecommitdiff
path: root/moneyjsx/src/api.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'moneyjsx/src/api.tsx')
-rw-r--r--moneyjsx/src/api.tsx80
1 files changed, 80 insertions, 0 deletions
diff --git a/moneyjsx/src/api.tsx b/moneyjsx/src/api.tsx
new file mode 100644
index 0000000..4ca79c2
--- /dev/null
+++ b/moneyjsx/src/api.tsx
@@ -0,0 +1,80 @@
+// 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[];
+}