import {z} from 'zod' export type TournamentCreateRequest = {game_code: string, title: string, starts_at: string, duration_seconds: number, max_score_attempts?: number | undefined}; export type CreateApiV2TournamentsArgs = {'Idempotency-Key': string, body: TournamentCreateRequest}; export type CreateApiV2TournamentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tournamentGame = z.object({code: z.string().optional(), name: z.string().optional(), url: z.string().optional()}); export const tournament = z.object({tournament_id: z.string().optional(), game: tournamentGame.optional(), title: z.string().optional(), status: z.enum(["future", "live", "past"]).optional(), starts_at: z.string().optional(), ends_at: z.string().optional(), duration_seconds: z.number().int().optional(), created_at: z.string().optional()}); export const createApiV2TournamentsResponse = z.object({success: z.boolean().optional(), data: z.object({tournament: tournament.optional()}).optional()}); 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 }; /** * Create Tournament API * * POST /v2/tournaments */ export const createApiV2Tournaments = async (args: CreateApiV2TournamentsArgs, options?: CreateApiV2TournamentsOptions) => { const {baseUrl = "https://api.astrozop.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/tournaments${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2TournamentsResponse, await res.json()) };