import {z} from 'zod' export type PlacementXpTier = {place: number, xp: number}; export type StreakXpTier = {threshold: number, xp: number}; export type TournamentCategoryCreateRequest = {name: string, difficulties: Array<'Easy' | 'Medium' | 'Hard' | 'Very Hard' | 'Extreme' | 'Hell'>, participation_xp?: number | undefined, placement_xp?: Array | undefined, streak_xp?: Array | undefined, champion_role_id?: (number | null) | undefined}; export type CreateApiApiV3TournamentsCategoriesArgs = {body: TournamentCategoryCreateRequest}; export type CreateApiApiV3TournamentsCategoriesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const placementXpTier = z.object({place: z.number().int(), xp: z.number().int()}); export const streakXpTier = z.object({threshold: z.number().int(), xp: z.number().int()}); export const tournamentCategoryResponse = z.object({id: z.number().int(), name: z.string(), difficulties: z.array(z.enum(["Easy", "Medium", "Hard", "Very Hard", "Extreme", "Hell"])), participation_xp: z.number().int(), placement_xp: z.array(placementXpTier), streak_xp: z.array(streakXpTier), champion_role_id: z.number().int().nullable().optional(), is_active: z.boolean(), created_at: z.string(), updated_at: 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 }; /** * Create Tournament Category * * POST /api/v3/tournaments/categories */ export const createApiApiV3TournamentsCategories = async (args: CreateApiApiV3TournamentsCategoriesArgs, options?: CreateApiApiV3TournamentsCategoriesOptions) => { const {baseUrl = "https://dev-api.genji.pk", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/tournaments/categories${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(tournamentCategoryResponse, await res.json()) };