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