import {z} from 'zod' export type GetApiApiAppGamesSearchArgs = {page?: number | undefined, limit?: number | undefined, q?: string | undefined, category?: string | undefined, tags?: string | undefined, developer?: string | undefined, year?: number | undefined, is_online?: boolean | undefined, sort?: string | undefined}; export type GetApiApiAppGamesSearchOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const gameList = z.object({id: z.number().int().optional(), title: z.string().min(1).max(255), slug: z.string().min(1).max(255), header: z.string().nullable().optional(), categories: z.array(z.string()).optional(), views: z.number().int().gte(0).lte(2147483647).optional(), likes: z.number().int().gte(0).lte(2147483647).optional(), is_liked: z.string().optional(), is_favorited: z.string().optional(), versions_count: z.string().optional(), latest_version: z.string().optional(), is_online: z.boolean().optional(), published_at: z.string().optional()}); export const getApiApiAppGamesSearchResponse = z.array(gameList); 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 }; /** * Recherche avancée avec fuzzy matching et filtres multiples * * GET /api/app/games/search/ */ export const getApiApiAppGamesSearch = async (args: GetApiApiAppGamesSearchArgs, options?: GetApiApiAppGamesSearchOptions) => { const {baseUrl = "https://api.jeuxcracks.fr", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.q != null) query.append("q", String(args.q)) if (args.category != null) query.append("category", String(args.category)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.developer != null) query.append("developer", String(args.developer)) if (args.year != null) query.append("year", String(args.year)) if (args.is_online != null) query.append("is_online", String(args.is_online)) if (args.sort != null) query.append("sort", String(args.sort)) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/app/games/search/${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(getApiApiAppGamesSearchResponse, await res.json()) };