import {Realm} from 'realm'; import {AMServerInfoResponse} from "./AM-API"; export class AMServer extends Realm.Object implements AMServerInfoResponse { _id!: Realm.BSON.ObjectId; server_name: string; server_address: string; last_used: Date; created_at: Date; api_version: number; async connect(abort?: AbortSignal | undefined): Promise { try { const response = await fetch(this.server_address, { signal: abort }); const data: AMServerInfoResponse = await response.json(); this.last_used = new Date(); Object.apply(this, data); return true; } catch { return false; } } async submitCard(cardId: string, abort?: AbortSignal | undefined): Promise { try { const response = await fetch(`${this.server_address}/card`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ card_id: cardId }), signal: abort }); return response.ok; } catch { return false; } } static schema: Realm.ObjectSchema = { name: "server", primaryKey: "_id", properties: { _id: "objectId", server_address: "string", server_name: {type: "string", default: "New Server"}, last_used: {type: "date", optional: true}, created_at: {type: "date", default: new Date()}, api_version: {type: "int", default: 0} } }; }