344 lines
9.8 KiB
TypeScript
344 lines
9.8 KiB
TypeScript
/* This file is generated by scripts/generate-api-client.mjs. */
|
|
|
|
export interface ApiError {
|
|
error: string;
|
|
}
|
|
|
|
export interface CampaignDetails {
|
|
id: string;
|
|
name: string;
|
|
rulesetId: string;
|
|
gm: UserSummary;
|
|
characters: Array<CharacterSummary>;
|
|
skills: Array<SkillSummary>;
|
|
}
|
|
|
|
export interface CampaignLogEntry {
|
|
rollId: string;
|
|
campaignId: string;
|
|
characterId: string;
|
|
skillId: string;
|
|
rollerUserId: string;
|
|
visibility: string;
|
|
result: number;
|
|
breakdown: string;
|
|
timestampUtc: string;
|
|
}
|
|
|
|
export interface CampaignSummary {
|
|
id: string;
|
|
name: string;
|
|
rulesetId: string;
|
|
gmUserId: string;
|
|
}
|
|
|
|
export interface CharacterSummary {
|
|
id: string;
|
|
name: string;
|
|
ownerUserId: string;
|
|
campaignId: string;
|
|
}
|
|
|
|
export interface CreateCampaignRequest {
|
|
name: string;
|
|
rulesetId: string;
|
|
}
|
|
|
|
export interface CreateCharacterRequest {
|
|
name: string;
|
|
campaignId: string;
|
|
}
|
|
|
|
export interface CreateSkillRequest {
|
|
name: string;
|
|
diceRollDefinition: string;
|
|
}
|
|
|
|
export interface HealthResponse {
|
|
status: string;
|
|
}
|
|
|
|
export interface LoginRequest {
|
|
username: string;
|
|
password: string;
|
|
}
|
|
|
|
export interface MeResponse {
|
|
user: UserSummary;
|
|
activeCharacterId?: string;
|
|
currentCampaignId?: string;
|
|
}
|
|
|
|
export interface RegisterRequest {
|
|
username: string;
|
|
password: string;
|
|
displayName: string;
|
|
}
|
|
|
|
export interface RollResult {
|
|
rollId: string;
|
|
campaignId: string;
|
|
characterId: string;
|
|
skillId: string;
|
|
rollerUserId: string;
|
|
visibility: string;
|
|
result: number;
|
|
breakdown: string;
|
|
timestampUtc: string;
|
|
}
|
|
|
|
export interface RollSkillRequest {
|
|
visibility: string;
|
|
}
|
|
|
|
export interface RulesetDefinition {
|
|
id: string;
|
|
name: string;
|
|
diceSyntax: string;
|
|
}
|
|
|
|
export interface SkillSummary {
|
|
id: string;
|
|
characterId: string;
|
|
name: string;
|
|
diceRollDefinition: string;
|
|
}
|
|
|
|
export interface UpdateCharacterRequest {
|
|
name: string;
|
|
campaignId: string;
|
|
}
|
|
|
|
export interface UpdateSkillRequest {
|
|
name: string;
|
|
diceRollDefinition: string;
|
|
}
|
|
|
|
export interface UserSummary {
|
|
id: string;
|
|
username: string;
|
|
displayName: string;
|
|
}
|
|
|
|
type ApiOperation = {
|
|
method: string;
|
|
path: string;
|
|
expectsJson: boolean;
|
|
};
|
|
|
|
type RequestOptions = {
|
|
pathParams?: Record<string, string | number | boolean>;
|
|
query?: Record<string, string | number | boolean | undefined>;
|
|
body?: unknown;
|
|
};
|
|
|
|
export const apiOperations = {
|
|
activateCharacter: { method: "POST", path: "/api/characters/{characterId}/activate", expectsJson: true },
|
|
createCampaign: { method: "POST", path: "/api/campaigns", expectsJson: true },
|
|
createCharacter: { method: "POST", path: "/api/characters", expectsJson: true },
|
|
createSkill: { method: "POST", path: "/api/characters/{characterId}/skills", expectsJson: true },
|
|
getCampaign: { method: "GET", path: "/api/campaigns/{campaignId}", expectsJson: true },
|
|
getCampaignLog: { method: "GET", path: "/api/campaigns/{campaignId}/log", expectsJson: true },
|
|
getCampaigns: { method: "GET", path: "/api/campaigns", expectsJson: true },
|
|
getCurrentCampaignCharacters: { method: "GET", path: "/api/characters/current-campaign", expectsJson: true },
|
|
getHealth: { method: "GET", path: "/api/health", expectsJson: true },
|
|
getMe: { method: "GET", path: "/api/me", expectsJson: true },
|
|
getRulesets: { method: "GET", path: "/api/rulesets", expectsJson: true },
|
|
loginUser: { method: "POST", path: "/api/auth/login", expectsJson: true },
|
|
logoutUser: { method: "POST", path: "/api/auth/logout", expectsJson: false },
|
|
registerUser: { method: "POST", path: "/api/auth/register", expectsJson: true },
|
|
rollSkill: { method: "POST", path: "/api/skills/{skillId}/roll", expectsJson: true },
|
|
updateCharacter: { method: "PUT", path: "/api/characters/{characterId}", expectsJson: true },
|
|
updateSkill: { method: "PUT", path: "/api/skills/{skillId}", expectsJson: true }
|
|
} as const satisfies Record<string, ApiOperation>;
|
|
|
|
function withPathParams(pathTemplate: string, pathParams: Record<string, string | number | boolean>): string {
|
|
let pathValue = pathTemplate;
|
|
for (const [key, value] of Object.entries(pathParams)) {
|
|
pathValue = pathValue.replace(`{${key}}`, encodeURIComponent(String(value)));
|
|
}
|
|
|
|
return pathValue;
|
|
}
|
|
|
|
function withQuery(pathValue: string, query: Record<string, string | number | boolean | undefined>): string {
|
|
const entries = Object.entries(query).filter(([, value]) => value !== undefined);
|
|
if (entries.length === 0) {
|
|
return pathValue;
|
|
}
|
|
|
|
const queryString = new URLSearchParams(entries.map(([key, value]) => [key, String(value)])).toString();
|
|
return queryString.length === 0 ? pathValue : `${pathValue}?${queryString}`;
|
|
}
|
|
|
|
async function send<TResult>(operation: ApiOperation, options: RequestOptions): Promise<TResult> {
|
|
const resolvedPath = withQuery(withPathParams(operation.path, options.pathParams ?? {}), options.query ?? {});
|
|
|
|
const headers: Record<string, string> = {
|
|
"Accept": "application/json"
|
|
};
|
|
|
|
let body: string | undefined;
|
|
if (options.body !== undefined) {
|
|
headers["Content-Type"] = "application/json";
|
|
body = JSON.stringify(options.body);
|
|
}
|
|
|
|
const response = await fetch(resolvedPath, {
|
|
method: operation.method,
|
|
headers,
|
|
body
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorPayload: unknown = await response.json().catch(() => ({ error: "Unknown API error." }));
|
|
if (errorPayload && typeof errorPayload === "object" && "error" in errorPayload && typeof errorPayload.error === "string") {
|
|
throw new Error(errorPayload.error);
|
|
}
|
|
|
|
throw new Error(`Request failed with status ${response.status}`);
|
|
}
|
|
|
|
if (!operation.expectsJson) {
|
|
return undefined as TResult;
|
|
}
|
|
|
|
return response.json() as Promise<TResult>;
|
|
}
|
|
|
|
export async function activateCharacter(characterId: string): Promise<boolean> {
|
|
return send<boolean>(apiOperations.activateCharacter, {
|
|
pathParams: { characterId: characterId },
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function createCampaign(body: CreateCampaignRequest): Promise<CampaignSummary> {
|
|
return send<CampaignSummary>(apiOperations.createCampaign, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: body
|
|
});
|
|
}
|
|
|
|
export async function createCharacter(body: CreateCharacterRequest): Promise<CharacterSummary> {
|
|
return send<CharacterSummary>(apiOperations.createCharacter, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: body
|
|
});
|
|
}
|
|
|
|
export async function createSkill(characterId: string, body: CreateSkillRequest): Promise<SkillSummary> {
|
|
return send<SkillSummary>(apiOperations.createSkill, {
|
|
pathParams: { characterId: characterId },
|
|
query: undefined,
|
|
body: body
|
|
});
|
|
}
|
|
|
|
export async function getCampaign(campaignId: string): Promise<CampaignDetails> {
|
|
return send<CampaignDetails>(apiOperations.getCampaign, {
|
|
pathParams: { campaignId: campaignId },
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function getCampaignLog(campaignId: string): Promise<Array<CampaignLogEntry>> {
|
|
return send<Array<CampaignLogEntry>>(apiOperations.getCampaignLog, {
|
|
pathParams: { campaignId: campaignId },
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function getCampaigns(): Promise<Array<CampaignSummary>> {
|
|
return send<Array<CampaignSummary>>(apiOperations.getCampaigns, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function getCurrentCampaignCharacters(): Promise<Array<CharacterSummary>> {
|
|
return send<Array<CharacterSummary>>(apiOperations.getCurrentCampaignCharacters, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function getHealth(): Promise<HealthResponse> {
|
|
return send<HealthResponse>(apiOperations.getHealth, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function getMe(): Promise<MeResponse> {
|
|
return send<MeResponse>(apiOperations.getMe, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function getRulesets(): Promise<Array<RulesetDefinition>> {
|
|
return send<Array<RulesetDefinition>>(apiOperations.getRulesets, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function loginUser(body: LoginRequest): Promise<UserSummary> {
|
|
return send<UserSummary>(apiOperations.loginUser, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: body
|
|
});
|
|
}
|
|
|
|
export async function logoutUser(): Promise<void> {
|
|
return send<void>(apiOperations.logoutUser, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: undefined
|
|
});
|
|
}
|
|
|
|
export async function registerUser(body: RegisterRequest): Promise<UserSummary> {
|
|
return send<UserSummary>(apiOperations.registerUser, {
|
|
pathParams: {},
|
|
query: undefined,
|
|
body: body
|
|
});
|
|
}
|
|
|
|
export async function rollSkill(skillId: string, body: RollSkillRequest): Promise<RollResult> {
|
|
return send<RollResult>(apiOperations.rollSkill, {
|
|
pathParams: { skillId: skillId },
|
|
query: undefined,
|
|
body: body
|
|
});
|
|
}
|
|
|
|
export async function updateCharacter(characterId: string, body: UpdateCharacterRequest): Promise<CharacterSummary> {
|
|
return send<CharacterSummary>(apiOperations.updateCharacter, {
|
|
pathParams: { characterId: characterId },
|
|
query: undefined,
|
|
body: body
|
|
});
|
|
}
|
|
|
|
export async function updateSkill(skillId: string, body: UpdateSkillRequest): Promise<SkillSummary> {
|
|
return send<SkillSummary>(apiOperations.updateSkill, {
|
|
pathParams: { skillId: skillId },
|
|
query: undefined,
|
|
body: body
|
|
});
|
|
}
|