Files
ppc_amnet/amnet-native/models/AMServer.ts
T
2024-08-15 10:10:49 +01:00

69 lines
1.8 KiB
TypeScript

import {Realm} from 'realm';
import {AMServerInfoResponse} from "./AM-API";
export class AMServer extends Realm.Object<AMServer> implements AMServerInfoResponse {
_id!: Realm.BSON.ObjectId;
serverName: string;
serverAddress: string;
lastUsed: Date;
createdAt: Date;
apiVersion: number;
async getInfo(abort?: AbortSignal | undefined): Promise<AMServerInfoResponse> {
try {
const response = await fetch(this.serverAddress, {
signal: abort
});
return await response.json();
} catch {
return null;
}
}
async submitCard(cardId: string, abort?: AbortSignal | undefined): Promise<boolean> {
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}
}
};
}