split and rename server response interfaces

This commit is contained in:
ppc
2024-09-04 10:15:29 +01:00
parent 7b5c1b9672
commit 20577f76f0
9 changed files with 95 additions and 22 deletions
+32 -6
View File
@@ -3,12 +3,17 @@
*/
export const AMNET_API_VERSION = 1;
/**
* The minimum version of the AMNet API this library supports
*/
export const MIN_AMNET_API_VERSION = 1;
export interface AMCardInRequest {
cardId: string;
}
/**
* Information needed to perform a request against an AMNet server
* Server connection info (api version, address, etc.)
*/
export interface AMServerConnectionInfo {
apiVersion: number;
@@ -16,20 +21,37 @@ export interface AMServerConnectionInfo {
}
/**
* Received metadata from an AMNet server
* User-definable server metadata
*/
export interface AMServerInfoResponse extends AMServerConnectionInfo {
export interface AMServerMetadata {
serverName: string;
gameId?: string | null | undefined;
}
/**
* Metrics about the current game session
*/
export interface AMServerInstanceMetrics {
timeSinceLastPoll?: number;
sessionUptime?: number;
}
/**
* The response from an AMNet server's info endpoint
*/
export type AMServerInfo = AMServerMetadata & AMServerConnectionInfo & AMServerInstanceMetrics;
/**
* Performs the card-in request to the specified server
* @param server The server to perform the request against
* @param request The card-in request to send
* @param signal The abort signal to use for the request
*/
export async function performCardIn(server: AMServerConnectionInfo, request: AMCardInRequest, signal: AbortSignal) {
export async function performCardIn(server: AMServerConnectionInfo, request: AMCardInRequest, signal: AbortSignal): Promise<boolean> {
if (server.apiVersion < MIN_AMNET_API_VERSION || server.apiVersion > AMNET_API_VERSION) {
throw new Error("Unsupported server API version");
}
try {
const response = await fetch(`${new URL(server.serverAddress).origin}/amnet/signin`, {
method: "POST",
@@ -51,16 +73,20 @@ export async function performCardIn(server: AMServerConnectionInfo, request: AMC
* @param serverAddress The origin address of the server to connect to
* @param abort The abort signal to use for the request (for cancellation)
*/
export async function fetchServerInfo(serverAddress: string, abort: AbortSignal): Promise<AMServerInfoResponse | null> {
export async function fetchServerInfo(serverAddress: string, abort: AbortSignal): Promise<AMServerInfo | null> {
try {
const response = await fetch(`${new URL(serverAddress).origin}/amnet/info`, {
signal: abort
});
if (!response.ok) {
return null;
}
return {
...await response.json(),
serverAddress: serverAddress
} satisfies AMServerInfoResponse;
} satisfies AMServerMetadata;
} catch {
return null;
}