Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import type { MatchCommandEnvelope, MatchCommandResult, MatchEvent, PlayerMatchView, PublicMatchView,} from "@instlang/economancy-protocol";
export interface CreateMatchResponse { readonly view: PublicMatchView; readonly player: string; readonly inviteToken: string; readonly hostToken: string; readonly seatToken: string;}
export interface JoinMatchResponse { readonly matchId: string; readonly player: string; readonly seatToken: string; readonly view: PlayerMatchView;}
export class EconomancyApi { private seatToken = ""; private hostToken = "";
public constructor(private readonly baseUrl: string) {}
public setCredentials(seatToken: string, hostToken: string): void { this.seatToken = seatToken; this.hostToken = hostToken; }
public async createMatch(matchId: string, players: readonly string[], player: string): Promise<CreateMatchResponse> { return this.request<CreateMatchResponse>("/matches", { method: "POST", body: { matchId, players, player }, }); }
public async joinMatch(matchId: string, player: string, inviteToken: string): Promise<JoinMatchResponse> { return this.request<JoinMatchResponse>(`/matches/${encodeURIComponent(matchId)}/join`, { method: "POST", body: { player, inviteToken }, }); }
public async getPlayerView(matchId: string): Promise<PlayerMatchView> { const response = await this.request<{ readonly view: PlayerMatchView }>( `/matches/${encodeURIComponent(matchId)}`, {}, this.seatToken, ); return response.view; }
public async dispatch(matchId: string, envelope: MatchCommandEnvelope): Promise<MatchCommandResult> { const token = envelope.command.kind === "advance_income" ? this.hostToken : this.seatToken; return this.request<MatchCommandResult>(`/matches/${encodeURIComponent(matchId)}/commands`, { method: "POST", body: envelope, }, token); }
public async getEvents(matchId: string, sequenceExclusive = -1): Promise<readonly MatchEvent[]> { const response = await this.request<{ readonly events: readonly MatchEvent[] }>(`/matches/${encodeURIComponent(matchId)}/events?since=${sequenceExclusive}`); return response.events; }
private async request<T>(path: string, options: { readonly method?: string; readonly body?: unknown } = {}, token?: string): Promise<T> { const init: RequestInit = { method: options.method ?? "GET", }; const headers: Record<string, string> = {}; if (options.body !== undefined) { headers["content-type"] = "application/json"; init.body = JSON.stringify(options.body); } if (token !== undefined && token !== "") headers.authorization = `Bearer ${token}`; if (Object.keys(headers).length > 0) init.headers = headers; const response = await fetch(`${this.baseUrl.replace(/\/$/, "")}${path}`, init); const body = await response.json() as unknown; if (!response.ok) { const error = isRecord(body) && typeof body.error === "string" ? body.error : `http_${response.status}`; throw new Error(error); } return body as T; }}
function isRecord(value: unknown): value is Readonly<Record<string, unknown>> { return value !== null && typeof value === "object" && !Array.isArray(value);}