import {z} from 'zod' export type GetApiApiAppGamesIdOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const category = z.object({id: z.number().int().optional(), name: z.string().min(1).max(100), slug: z.string().min(1).max(100), icon: z.string().max(50).optional(), games_count: z.string().optional()}); export const tag = z.object({id: z.number().int().optional(), name: z.string().min(1).max(100)}); export const gameVersionPublic = z.object({id: z.number().int().optional(), source: z.enum(["onlinefix", "steamrip", "dodi", "fitgirl", "other"]), version: z.string().max(100).optional(), size: z.number().int().gte(-9223372036854776000).lte(9223372036854776000).optional(), size_formatted: z.string().min(1).optional(), size_raw: z.string().max(50).optional(), is_online: z.boolean().optional(), created_at: z.string().optional()}); export const gameDetail = z.object({id: z.number().int().optional(), steam_id: z.string().max(20).nullable().optional(), title: z.string().min(1).max(255), slug: z.string().min(1).max(255), description: z.string().optional(), description_short: z.string().max(500).optional(), description_fr: z.string().optional(), header: z.string().nullable().optional(), background: z.string().nullable().optional(), video: z.string().max(200).optional(), screenshots: z.object({}).optional(), categories: z.array(category).optional(), tags: z.array(tag).optional(), developer: z.string().max(255).optional(), publisher: z.string().max(255).optional(), requirements: z.object({}).optional(), source: z.object({}).optional(), views: z.number().int().gte(0).lte(2147483647).optional(), likes: z.number().int().gte(0).lte(2147483647).optional(), favorites_count: z.number().int().gte(0).lte(2147483647).optional(), downloads_count: z.number().int().gte(0).lte(2147483647).optional(), release_date: z.string().nullable().optional(), published_at: z.string().optional(), updated_at: z.string().optional(), is_liked: z.string().optional(), is_favorited: z.string().optional(), versions: z.array(gameVersionPublic).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 }; /** * API principale du catalogue de jeux * * GET /api/app/games/{id}/ */ export const getApiApiAppGamesId = async (options?: GetApiApiAppGamesIdOptions) => { 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 (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/app/games/${encodeURIComponent(String(args.id))}/${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(gameDetail, await res.json()) };