import {z} from 'zod' export type GetApiApiGifsTrendingGifsArgs = {media_format?: string | undefined, locale?: string | undefined, limit?: number | undefined, provider?: string | undefined}; export type GetApiApiGifsTrendingGifsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const gifResponse = z.object({id: z.string(), title: z.string(), url: z.string(), src: z.string(), gif_src: z.string(), width: z.number().int(), height: z.number().int(), preview: z.string()}); export const gifsResponse = z.array(gifResponse); 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 }; /** * GET /api/gifs/trending-gifs/ */ export const getApiApiGifsTrendingGifs = async (args: GetApiApiGifsTrendingGifsArgs, options?: GetApiApiGifsTrendingGifsOptions) => { const {baseUrl = "https://api.rory.server.spacebar.chat", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.media_format != null) query.append("media_format", String(args.media_format)) if (args.locale != null) query.append("locale", String(args.locale)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.provider != null) query.append("provider", String(args.provider)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/gifs/trending-gifs/${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(gifsResponse, await res.json()) };