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
+2 -2
View File
@@ -1,7 +1,7 @@
import {Realm} from 'realm';
import {AMServerInfoResponse} from "amnet-shared";
import {AMServerConnectionInfo, AMServerMetadata} from "amnet-shared";
export class AMServer extends Realm.Object<AMServer> implements AMServerInfoResponse {
export class AMServer extends Realm.Object<AMServer> implements AMServerConnectionInfo, AMServerMetadata {
_id!: Realm.BSON.ObjectId;
apiVersion: number;
+20 -5
View File
@@ -2,23 +2,38 @@
* The latest version of the AMNet API this library supports
*/
declare const AMNET_API_VERSION = 1;
/**
* The minimum version of the AMNet API this library supports
*/
declare const MIN_AMNET_API_VERSION = 1;
interface AMCardInRequest {
cardId: string;
}
/**
* Information needed to perform a request against an AMNet server
* Server connection info (api version, address, etc.)
*/
interface AMServerConnectionInfo {
apiVersion: number;
serverAddress: string;
}
/**
* Received metadata from an AMNet server
* User-definable server metadata
*/
interface AMServerInfoResponse extends AMServerConnectionInfo {
interface AMServerMetadata {
serverName: string;
gameId?: string | null | undefined;
}
/**
* Metrics about the current game session
*/
interface AMServerInstanceMetrics {
timeSinceLastPoll?: number;
sessionUptime?: number;
}
/**
* The response from an AMNet server's info endpoint
*/
type AMServerInfo = AMServerMetadata & AMServerConnectionInfo & AMServerInstanceMetrics;
/**
* Performs the card-in request to the specified server
* @param server The server to perform the request against
@@ -31,7 +46,7 @@ declare function performCardIn(server: AMServerConnectionInfo, request: AMCardIn
* @param serverAddress The origin address of the server to connect to
* @param abort The abort signal to use for the request (for cancellation)
*/
declare function fetchServerInfo(serverAddress: string, abort: AbortSignal): Promise<AMServerInfoResponse | null>;
declare function fetchServerInfo(serverAddress: string, abort: AbortSignal): Promise<AMServerInfo | null>;
declare const CARD_ID_REGEX: RegExp;
declare function generateCardNumber(): string;
@@ -47,4 +62,4 @@ declare function validateCardId(id: string): boolean;
*/
declare function getGameName(id: string): string | null;
export { type AMCardInRequest, AMNET_API_VERSION, type AMServerConnectionInfo, type AMServerInfoResponse, CARD_ID_REGEX, fetchServerInfo, generateCardNumber, getGameName, performCardIn, validateCardId };
export { type AMCardInRequest, AMNET_API_VERSION, type AMServerConnectionInfo, type AMServerInfo, type AMServerInstanceMetrics, type AMServerMetadata, CARD_ID_REGEX, MIN_AMNET_API_VERSION, fetchServerInfo, generateCardNumber, getGameName, performCardIn, validateCardId };
+20 -5
View File
@@ -2,23 +2,38 @@
* The latest version of the AMNet API this library supports
*/
declare const AMNET_API_VERSION = 1;
/**
* The minimum version of the AMNet API this library supports
*/
declare const MIN_AMNET_API_VERSION = 1;
interface AMCardInRequest {
cardId: string;
}
/**
* Information needed to perform a request against an AMNet server
* Server connection info (api version, address, etc.)
*/
interface AMServerConnectionInfo {
apiVersion: number;
serverAddress: string;
}
/**
* Received metadata from an AMNet server
* User-definable server metadata
*/
interface AMServerInfoResponse extends AMServerConnectionInfo {
interface AMServerMetadata {
serverName: string;
gameId?: string | null | undefined;
}
/**
* Metrics about the current game session
*/
interface AMServerInstanceMetrics {
timeSinceLastPoll?: number;
sessionUptime?: number;
}
/**
* The response from an AMNet server's info endpoint
*/
type AMServerInfo = AMServerMetadata & AMServerConnectionInfo & AMServerInstanceMetrics;
/**
* Performs the card-in request to the specified server
* @param server The server to perform the request against
@@ -31,7 +46,7 @@ declare function performCardIn(server: AMServerConnectionInfo, request: AMCardIn
* @param serverAddress The origin address of the server to connect to
* @param abort The abort signal to use for the request (for cancellation)
*/
declare function fetchServerInfo(serverAddress: string, abort: AbortSignal): Promise<AMServerInfoResponse | null>;
declare function fetchServerInfo(serverAddress: string, abort: AbortSignal): Promise<AMServerInfo | null>;
declare const CARD_ID_REGEX: RegExp;
declare function generateCardNumber(): string;
@@ -47,4 +62,4 @@ declare function validateCardId(id: string): boolean;
*/
declare function getGameName(id: string): string | null;
export { type AMCardInRequest, AMNET_API_VERSION, type AMServerConnectionInfo, type AMServerInfoResponse, CARD_ID_REGEX, fetchServerInfo, generateCardNumber, getGameName, performCardIn, validateCardId };
export { type AMCardInRequest, AMNET_API_VERSION, type AMServerConnectionInfo, type AMServerInfo, type AMServerInstanceMetrics, type AMServerMetadata, CARD_ID_REGEX, MIN_AMNET_API_VERSION, fetchServerInfo, generateCardNumber, getGameName, performCardIn, validateCardId };
+9
View File
@@ -59,6 +59,7 @@ var src_exports = {};
__export(src_exports, {
AMNET_API_VERSION: () => AMNET_API_VERSION,
CARD_ID_REGEX: () => CARD_ID_REGEX,
MIN_AMNET_API_VERSION: () => MIN_AMNET_API_VERSION,
fetchServerInfo: () => fetchServerInfo,
generateCardNumber: () => generateCardNumber,
getGameName: () => getGameName,
@@ -69,8 +70,12 @@ module.exports = __toCommonJS(src_exports);
// src/api.ts
var AMNET_API_VERSION = 1;
var MIN_AMNET_API_VERSION = 1;
function performCardIn(server, request, signal) {
return __async(this, null, function* () {
if (server.apiVersion < MIN_AMNET_API_VERSION || server.apiVersion > AMNET_API_VERSION) {
throw new Error("Unsupported server API version");
}
try {
const response = yield fetch(`${new URL(server.serverAddress).origin}/amnet/signin`, {
method: "POST",
@@ -92,6 +97,9 @@ function fetchServerInfo(serverAddress, abort) {
const response = yield fetch(`${new URL(serverAddress).origin}/amnet/info`, {
signal: abort
});
if (!response.ok) {
return null;
}
return __spreadProps(__spreadValues({}, yield response.json()), {
serverAddress
});
@@ -143,6 +151,7 @@ function getGameName(id) {
0 && (module.exports = {
AMNET_API_VERSION,
CARD_ID_REGEX,
MIN_AMNET_API_VERSION,
fetchServerInfo,
generateCardNumber,
getGameName,
+1 -1
View File
File diff suppressed because one or more lines are too long
+8
View File
@@ -40,8 +40,12 @@ var __async = (__this, __arguments, generator) => {
// src/api.ts
var AMNET_API_VERSION = 1;
var MIN_AMNET_API_VERSION = 1;
function performCardIn(server, request, signal) {
return __async(this, null, function* () {
if (server.apiVersion < MIN_AMNET_API_VERSION || server.apiVersion > AMNET_API_VERSION) {
throw new Error("Unsupported server API version");
}
try {
const response = yield fetch(`${new URL(server.serverAddress).origin}/amnet/signin`, {
method: "POST",
@@ -63,6 +67,9 @@ function fetchServerInfo(serverAddress, abort) {
const response = yield fetch(`${new URL(serverAddress).origin}/amnet/info`, {
signal: abort
});
if (!response.ok) {
return null;
}
return __spreadProps(__spreadValues({}, yield response.json()), {
serverAddress
});
@@ -113,6 +120,7 @@ function getGameName(id) {
export {
AMNET_API_VERSION,
CARD_ID_REGEX,
MIN_AMNET_API_VERSION,
fetchServerInfo,
generateCardNumber,
getGameName,
+1 -1
View File
File diff suppressed because one or more lines are too long
+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;
}
+2 -2
View File
@@ -1,9 +1,9 @@
import Dexie, { type EntityTable } from 'dexie';
import {AMServerInfoResponse} from "amnet-shared";
import {AMServerConnectionInfo, AMServerMetadata} from "amnet-shared";
export const CARD_LIMIT = 15;
interface AimeServer extends AMServerInfoResponse {
interface AimeServer extends AMServerMetadata, AMServerConnectionInfo {
id: number;
lastConnected: number;
}