import {z} from 'zod' export type GetApiApiAppGamesArgs = {page?: number | undefined, limit?: number | undefined, q?: string | undefined, category?: string | undefined, tags?: string | undefined, sort?: string | undefined}; export type GetApiApiAppGamesOptions = {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 getApiApiAppGamesResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: 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 }; /** * API principale du catalogue de jeux * * GET /api/app/games/ */ export const getApiApiAppGames = async (args: GetApiApiAppGamesArgs, options?: GetApiApiAppGamesOptions) => { 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.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/${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(getApiApiAppGamesResponse, await res.json()) };