mirror of
https://gitea.tendokyu.moe/ppc/amnet.git
synced 2026-09-30 10:48:19 +03:00
split and rename server response interfaces
This commit is contained in:
+32
-6
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user