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 | undefined): Promise { try { const response = await fetch(this.serverAddress, { signal: abort }); return await response.json(); } catch { return null; } } async submitCard(cardId: string, abort?: AbortSignal | undefined): 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(), serverAddress, createdAt: new Date(), serverName: "New Server", lastUsed: null, apiVersion: 0 } } static schema: Realm.ObjectSchema = { name: "server", primaryKey: "_id", properties: { _id: "objectId", serverAddress: "string", serverName: {type: "string", default: "New Server"}, lastUsed: {type: "date", optional: true}, createdAt: {type: "date", default: new Date()}, apiVersion: {type: "int", default: 0} } }; }