import {z} from 'zod' export type GetApiTournamentsIdentifierMeArgs = {identifier: string}; export type GetApiTournamentsIdentifierMeOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const myTournamentActableTeam = z.object({teamId: z.string(), name: z.string(), avatarUrl: z.string().optional()}); export const tournamentParticipantStatus = z.enum(["PENDING", "APPROVED", "DENIED", "WITHDRAWN", "REMOVED", "DISQUALIFIED"]); export const myTournamentParticipant = z.object({participantId: z.string(), teamId: z.string(), teamName: z.string(), teamAvatarUrl: z.string().optional(), status: tournamentParticipantStatus, seed: z.number().int().gte(-9007199254740991).lte(9007199254740991).optional(), checkedInAt: z.number().optional(), finalPlacement: z.number().int().gte(-9007199254740991).lte(9007199254740991).optional(), registeredAt: z.number(), canManage: z.boolean()}); export const tournamentInviteStatus = z.enum(["PENDING", "ACCEPTED", "DECLINED", "REVOKED"]); export const myTournamentInvite = z.object({inviteId: z.string(), teamId: z.string(), teamName: z.string(), status: tournamentInviteStatus, invitedByUserId: z.string(), createdAt: z.number(), respondedAt: z.number().optional()}); export const tournamentFreeAgentOfferStatus = z.enum(["PENDING", "ACCEPTED", "DECLINED", "EXPIRED", "REVOKED"]); export const myTournamentOffer = z.object({offerId: z.string(), teamId: z.string(), teamName: z.string(), teamAvatarUrl: z.string().optional(), participantId: z.string(), offeringParticipantStatus: tournamentParticipantStatus.nullable(), status: tournamentFreeAgentOfferStatus, message: z.string().optional(), createdAt: z.number()}); export const tournamentRosterSource = z.enum(["TEAM_MEMBER", "FREE_AGENT"]); export const myTournamentRosterEntry = z.object({rosterEntryId: z.string(), participantId: z.string(), source: tournamentRosterSource, addedAt: z.number()}); export const tournamentFreeAgentStatus = z.enum(["AVAILABLE", "PICKED_UP", "WITHDRAWN"]); export const myTournamentFreeAgent = z.object({freeAgentId: z.string(), status: tournamentFreeAgentStatus, note: z.string().optional(), pickedUpByParticipantId: z.string().optional(), createdAt: z.number()}); export const myTournamentEntry = z.object({tournamentId: z.string(), slug: z.string(), requiredConnectionMissing: z.boolean(), actableTeams: z.array(myTournamentActableTeam), participants: z.array(myTournamentParticipant), invites: z.array(myTournamentInvite), offers: z.array(myTournamentOffer), rosterEntries: z.array(myTournamentRosterEntry), freeAgent: myTournamentFreeAgent.nullable()}); export const myTournamentEntryResponse = z.object({entry: myTournamentEntry, timestamp: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get your own state in a tournament * * GET /tournaments/{identifier}/me */ export const getApiTournamentsIdentifierMe = async (args: GetApiTournamentsIdentifierMeArgs, options?: GetApiTournamentsIdentifierMeOptions) => { const {baseUrl = "https://teambattles.gg/api/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/tournaments/${encodeURIComponent(String(args.identifier))}/me${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(myTournamentEntryResponse, await res.json()) };